From a8cb4a80bf24d86fdb51e209b449cd0dd9b38b6b Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Thu, 9 Jun 2022 17:59:43 -0500 Subject: [PATCH] Convert remaining code to go-ap --- integrations/api_activitypub_person_test.go | 56 ++++++--------------- routers/api/v1/activitypub/person.go | 18 +++++-- 2 files changed, 30 insertions(+), 44 deletions(-) diff --git a/integrations/api_activitypub_person_test.go b/integrations/api_activitypub_person_test.go index f3479e65c8..d6aff2079f 100644 --- a/integrations/api_activitypub_person_test.go +++ b/integrations/api_activitypub_person_test.go @@ -16,7 +16,7 @@ import ( "code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/setting" - "github.com/go-ap/activitypub" + ap "github.com/go-ap/activitypub" "github.com/stretchr/testify/assert" ) @@ -34,49 +34,24 @@ func TestActivityPubPerson(t *testing.T) { var m map[string]interface{} DecodeJSON(t, resp, &m) - var person vocab.ActivityStreamsPerson - resolver, _ := streams.NewJSONResolver(func(c context.Context, p vocab.ActivityStreamsPerson) error { - person = p - return nil - }) - ctx := context.Background() - err := resolver.Resolve(ctx, m) + var person ap.Person + err := person.UnmarshalJSON(resp.Body.Bytes()) assert.NoError(t, err) - assert.Equal(t, "Person", person.GetTypeName()) - assert.Equal(t, username, person.GetActivityStreamsName().Begin().GetXMLSchemaString()) - keyID := person.GetJSONLDId().GetIRI().String() + + assert.Equal(t, "Person", person.Type) + assert.Equal(t, username, person.Name) + keyID := person.ID.String() assert.Regexp(t, fmt.Sprintf("activitypub/user/%s$", username), keyID) - assert.Regexp(t, fmt.Sprintf("activitypub/user/%s/outbox$", username), person.GetActivityStreamsOutbox().GetIRI().String()) - assert.Regexp(t, fmt.Sprintf("activitypub/user/%s/inbox$", username), person.GetActivityStreamsInbox().GetIRI().String()) + assert.Regexp(t, fmt.Sprintf("activitypub/user/%s/outbox$", username), person.Outbox.GetID().String()) + assert.Regexp(t, fmt.Sprintf("activitypub/user/%s/inbox$", username), person.Inbox.GetID().String()) - pkp := person.GetW3IDSecurityV1PublicKey() - assert.NotNil(t, pkp) + pkey := person.PublicKey + assert.NotNil(t, pkey) publicKeyID := keyID + "#main-key" - var pkpFound vocab.W3IDSecurityV1PublicKey - for pkpIter := pkp.Begin(); pkpIter != pkp.End(); pkpIter = pkpIter.Next() { - if !pkpIter.IsW3IDSecurityV1PublicKey() { - continue - } - pkValue := pkpIter.Get() - var pkID *url.URL - pkID, err = pub.GetId(pkValue) - if err != nil { - return - } - assert.Equal(t, pkID.String(), publicKeyID) - if pkID.String() != publicKeyID { - continue - } - pkpFound = pkValue - break - } - assert.NotNil(t, pkpFound) + assert.Equal(t, pkey.ID.String(), publicKeyID) - pkPemProp := pkpFound.GetW3IDSecurityV1PublicKeyPem() - assert.NotNil(t, pkPemProp) - assert.True(t, pkPemProp.IsXMLSchemaString()) - - pubKeyPem := pkPemProp.Get() + pubKeyPem := pkey.PublicKeyPem + assert.NotNil(t, pubKeyPem) assert.Regexp(t, "^-----BEGIN PUBLIC KEY-----", pubKeyPem) }) } @@ -109,7 +84,8 @@ func TestActivityPubPersonInbox(t *testing.T) { setting.AppURL = appURL }() username1 := "user1" - user1, err := user_model.GetUserByName(username1) + var ctx context.Context + user1, err := user_model.GetUserByName(ctx, username1) assert.NoError(t, err) user1url := fmt.Sprintf("%s/api/v1/activitypub/user/%s#main-key", srv.URL, username1) c, err := activitypub.NewClient(user1, user1url) diff --git a/routers/api/v1/activitypub/person.go b/routers/api/v1/activitypub/person.go index 361889ec47..4a29bca29f 100644 --- a/routers/api/v1/activitypub/person.go +++ b/routers/api/v1/activitypub/person.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/modules/activitypub" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/routers/api/v1/user" @@ -43,7 +44,11 @@ func Person(ctx *context.APIContext) { person := ap.PersonNew(ap.IRI(link)) name := ap.NaturalLanguageValuesNew() - name.Set("en", ap.Content(username)) + err := name.Set("en", ap.Content(username)) + if err != nil { + ctx.Error(http.StatusInternalServerError, "Set name", err) + return + } person.Name = name person.Inbox = ap.Item(ap.IRI(link + "/inbox")) @@ -63,9 +68,14 @@ func Person(ctx *context.APIContext) { if err != nil { ctx.Error(http.StatusInternalServerError, "Serialize", err) } - ctx.Resp.Header().Set("Content-Type", "application/json;charset=utf-8") - ctx.Write(binary) - ctx.Status(http.StatusOK) + + var jsonmap map[string]interface{} + err = json.Unmarshal(binary, jsonmap) + if err != nil { + ctx.Error(http.StatusInternalServerError, "Unmarshall", err) + } + + ctx.JSON(http.StatusOK, jsonmap) } // PersonInbox function