Convert remaining code to go-ap

This commit is contained in:
Anthony Wang 2022-06-09 17:59:43 -05:00
parent 57e6b67095
commit a8cb4a80bf
No known key found for this signature in database
GPG Key ID: BC96B00AEC5F2D76
2 changed files with 30 additions and 44 deletions

View File

@ -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)

View File

@ -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