Set ap.ItemTyperFunc to correctly unmarshal JSON
This commit is contained in:
parent
bffb682117
commit
30b431da49
6 changed files with 37 additions and 54 deletions
|
@ -90,7 +90,7 @@ var (
|
||||||
"git-receive-pack": perm.AccessModeWrite,
|
"git-receive-pack": perm.AccessModeWrite,
|
||||||
lfsAuthenticateVerb: perm.AccessModeNone,
|
lfsAuthenticateVerb: perm.AccessModeNone,
|
||||||
}
|
}
|
||||||
alphaDashDotPattern = regexp.MustCompile(`[^\w-\.]`)
|
alphaDashDotPattern = regexp.MustCompile(`[^\w-\.@]`)
|
||||||
)
|
)
|
||||||
|
|
||||||
func fail(userMessage, logMessage string, args ...interface{}) error {
|
func fail(userMessage, logMessage string, args ...interface{}) error {
|
||||||
|
|
|
@ -8,8 +8,8 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
|
"code.gitea.io/gitea/models/forgefed"
|
||||||
"code.gitea.io/gitea/modules/context"
|
"code.gitea.io/gitea/modules/context"
|
||||||
"code.gitea.io/gitea/modules/json"
|
|
||||||
|
|
||||||
ap "github.com/go-ap/activitypub"
|
ap "github.com/go-ap/activitypub"
|
||||||
)
|
)
|
||||||
|
@ -26,37 +26,32 @@ func AuthorizeInteraction(c *context.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var object map[string]interface{}
|
ap.ItemTyperFunc = forgefed.GetItemByType
|
||||||
err = json.Unmarshal(resp, &object)
|
object, err := ap.UnmarshalJSON(resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ServerError("Unmarshal", err)
|
c.ServerError("UnmarshalJSON", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
switch object["type"] {
|
|
||||||
case "Person":
|
switch object.GetType() {
|
||||||
var person ap.Person
|
case ap.PersonType:
|
||||||
err = person.UnmarshalJSON(resp)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ServerError("UnmarshalJSON", err)
|
c.ServerError("UnmarshalJSON", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = FederatedUserNew(c, person)
|
err = FederatedUserNew(c, object.(ap.Person))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ServerError("FederatedUserNew", err)
|
c.ServerError("FederatedUserNew", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
name, err := personIRIToName(person.GetLink())
|
name, err := personIRIToName(object.GetLink())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.ServerError("personIRIToName", err)
|
c.ServerError("personIRIToName", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Redirect(name)
|
c.Redirect(name)
|
||||||
/*case "organization":
|
case forgefed.RepositoryType:
|
||||||
// Do something idk
|
err = FederatedRepoNew(object.(forgefed.Repository))
|
||||||
case "repository":
|
|
||||||
FederatedRepoNew() // TODO
|
|
||||||
case "ticket":
|
|
||||||
// TODO*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Status(http.StatusOK)
|
c.Status(http.StatusOK)
|
||||||
|
|
|
@ -5,17 +5,19 @@
|
||||||
package activitypub
|
package activitypub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.gitea.io/gitea/models"
|
//"code.gitea.io/gitea/models"
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
"code.gitea.io/gitea/models/forgefed"
|
||||||
|
/*repo_model "code.gitea.io/gitea/models/repo"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
"code.gitea.io/gitea/modules/repository"
|
"code.gitea.io/gitea/modules/repository"
|
||||||
|
|
||||||
ap "github.com/go-ap/activitypub"
|
ap "github.com/go-ap/activitypub"*/
|
||||||
)
|
)
|
||||||
|
|
||||||
func FederatedRepoNew(user *user_model.User, name string, iri ap.IRI) (*repo_model.Repository, error) {
|
func FederatedRepoNew(repo forgefed.Repository) error {
|
||||||
// TODO: also handle forks
|
// TODO: also handle forks
|
||||||
return repository.CreateRepository(user, user, models.CreateRepoOptions{
|
/*_, err := repository.CreateRepository(user, user, models.CreateRepoOptions{
|
||||||
Name: name,
|
Name: repo.Name.String(),
|
||||||
})
|
})*/
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ func FederatedUserNew(ctx context.Context, person ap.Person) error {
|
||||||
|
|
||||||
user := &user_model.User{
|
user := &user_model.User{
|
||||||
Name: name,
|
Name: name,
|
||||||
FullName: person.Name.String(),
|
FullName: person.Name.String(), // May not exist!!
|
||||||
Email: email,
|
Email: email,
|
||||||
Avatar: avatar,
|
Avatar: avatar,
|
||||||
LoginType: auth.Federated,
|
LoginType: auth.Federated,
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
"code.gitea.io/gitea/models/forgefed"
|
"code.gitea.io/gitea/models/forgefed"
|
||||||
"code.gitea.io/gitea/modules/activitypub"
|
"code.gitea.io/gitea/modules/activitypub"
|
||||||
"code.gitea.io/gitea/modules/context"
|
"code.gitea.io/gitea/modules/context"
|
||||||
"code.gitea.io/gitea/modules/json"
|
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
|
||||||
|
@ -96,51 +95,40 @@ func RepoInbox(ctx *context.APIContext) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var activity map[string]interface{}
|
ap.ItemTyperFunc = forgefed.GetItemByType
|
||||||
err = json.Unmarshal(body, activity)
|
var activity ap.Activity
|
||||||
|
err = activity.UnmarshalJSON(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.ServerError("Unmarshal", err)
|
ctx.ServerError("UnmarshalJSON", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure keyID matches the user doing the activity
|
// Make sure keyID matches the user doing the activity
|
||||||
_, keyID, _ := getKeyID(ctx.Req)
|
_, keyID, _ := getKeyID(ctx.Req)
|
||||||
actor, ok := activity["actor"]
|
if activity.Actor != nil && !strings.HasPrefix(keyID, activity.Actor.GetID().String()) {
|
||||||
if ok && !strings.HasPrefix(keyID, actor.(string)) {
|
|
||||||
ctx.ServerError("Actor does not match HTTP signature keyID", nil)
|
ctx.ServerError("Actor does not match HTTP signature keyID", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
attributedTo, ok := activity["attributedTo"]
|
if activity.AttributedTo != nil && !strings.HasPrefix(keyID, activity.AttributedTo.GetID().String()) {
|
||||||
if ok && !strings.HasPrefix(keyID, attributedTo.(string)) {
|
|
||||||
ctx.ServerError("AttributedTo does not match HTTP signature keyID", nil)
|
ctx.ServerError("AttributedTo does not match HTTP signature keyID", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process activity
|
// Process activity
|
||||||
switch activity["type"].(ap.ActivityVocabularyType) {
|
switch activity.Type {
|
||||||
case ap.CreateType:
|
case ap.CreateType:
|
||||||
// Create activity, extract the object
|
if activity.Object == nil {
|
||||||
object, ok := activity["object"].(map[string]interface{})
|
ctx.ServerError("Activity does not contain object", err)
|
||||||
if ok {
|
|
||||||
ctx.ServerError("Create activity does not contain object", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
objectBinary, err := json.Marshal(object)
|
|
||||||
if err != nil {
|
|
||||||
ctx.ServerError("Marshal", err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch object["type"].(ap.ActivityVocabularyType) {
|
switch activity.Object.(ap.Object).Type {
|
||||||
case forgefed.RepositoryType:
|
case forgefed.RepositoryType:
|
||||||
// Fork created by remote instance
|
// Fork created by remote instance
|
||||||
var repository forgefed.Repository
|
activitypub.ForkFromCreate(ctx, activity.Object.(forgefed.Repository))
|
||||||
repository.UnmarshalJSON(objectBinary)
|
|
||||||
activitypub.ForkFromCreate(ctx, repository)
|
|
||||||
case forgefed.TicketType:
|
case forgefed.TicketType:
|
||||||
// New issue or pull request
|
// New issue or pull request
|
||||||
var ticket forgefed.Ticket
|
ticket := activity.Object.(forgefed.Ticket)
|
||||||
ticket.UnmarshalJSON(objectBinary)
|
|
||||||
if ticket.Origin != nil {
|
if ticket.Origin != nil {
|
||||||
// New pull request
|
// New pull request
|
||||||
activitypub.PullRequest(ctx, ticket)
|
activitypub.PullRequest(ctx, ticket)
|
||||||
|
@ -150,12 +138,10 @@ func RepoInbox(ctx *context.APIContext) {
|
||||||
}
|
}
|
||||||
case ap.NoteType:
|
case ap.NoteType:
|
||||||
// New comment
|
// New comment
|
||||||
var note ap.Note
|
activitypub.Comment(ctx, activity.Object.(ap.Note))
|
||||||
note.UnmarshalJSON(objectBinary)
|
|
||||||
activitypub.Comment(ctx, note)
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
log.Info("Incoming unsupported ActivityStreams type: %s", activity["type"])
|
log.Info("Incoming unsupported ActivityStreams type: %s", activity.Type)
|
||||||
ctx.PlainText(http.StatusNotImplemented, "ActivityStreams type not supported")
|
ctx.PlainText(http.StatusNotImplemented, "ActivityStreams type not supported")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,7 +39,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
separatorAntiPattern = regexp.MustCompile(`[^\w-\.]`)
|
separatorAntiPattern = regexp.MustCompile(`[^\w-\.@]`)
|
||||||
langCodePattern = regexp.MustCompile(`^[a-z]{2}-[A-Z]{2}$`)
|
langCodePattern = regexp.MustCompile(`^[a-z]{2}-[A-Z]{2}$`)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue