Convert remaining code to go-ap
This commit is contained in:
parent
57e6b67095
commit
a8cb4a80bf
|
@ -16,7 +16,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/activitypub"
|
"code.gitea.io/gitea/modules/activitypub"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
|
|
||||||
"github.com/go-ap/activitypub"
|
ap "github.com/go-ap/activitypub"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -34,49 +34,24 @@ func TestActivityPubPerson(t *testing.T) {
|
||||||
var m map[string]interface{}
|
var m map[string]interface{}
|
||||||
DecodeJSON(t, resp, &m)
|
DecodeJSON(t, resp, &m)
|
||||||
|
|
||||||
var person vocab.ActivityStreamsPerson
|
var person ap.Person
|
||||||
resolver, _ := streams.NewJSONResolver(func(c context.Context, p vocab.ActivityStreamsPerson) error {
|
err := person.UnmarshalJSON(resp.Body.Bytes())
|
||||||
person = p
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
ctx := context.Background()
|
|
||||||
err := resolver.Resolve(ctx, m)
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "Person", person.GetTypeName())
|
|
||||||
assert.Equal(t, username, person.GetActivityStreamsName().Begin().GetXMLSchemaString())
|
assert.Equal(t, "Person", person.Type)
|
||||||
keyID := person.GetJSONLDId().GetIRI().String()
|
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$", username), keyID)
|
||||||
assert.Regexp(t, fmt.Sprintf("activitypub/user/%s/outbox$", username), person.GetActivityStreamsOutbox().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.GetActivityStreamsInbox().GetIRI().String())
|
assert.Regexp(t, fmt.Sprintf("activitypub/user/%s/inbox$", username), person.Inbox.GetID().String())
|
||||||
|
|
||||||
pkp := person.GetW3IDSecurityV1PublicKey()
|
pkey := person.PublicKey
|
||||||
assert.NotNil(t, pkp)
|
assert.NotNil(t, pkey)
|
||||||
publicKeyID := keyID + "#main-key"
|
publicKeyID := keyID + "#main-key"
|
||||||
var pkpFound vocab.W3IDSecurityV1PublicKey
|
assert.Equal(t, pkey.ID.String(), publicKeyID)
|
||||||
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)
|
|
||||||
|
|
||||||
pkPemProp := pkpFound.GetW3IDSecurityV1PublicKeyPem()
|
pubKeyPem := pkey.PublicKeyPem
|
||||||
assert.NotNil(t, pkPemProp)
|
assert.NotNil(t, pubKeyPem)
|
||||||
assert.True(t, pkPemProp.IsXMLSchemaString())
|
|
||||||
|
|
||||||
pubKeyPem := pkPemProp.Get()
|
|
||||||
assert.Regexp(t, "^-----BEGIN PUBLIC KEY-----", pubKeyPem)
|
assert.Regexp(t, "^-----BEGIN PUBLIC KEY-----", pubKeyPem)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -109,7 +84,8 @@ func TestActivityPubPersonInbox(t *testing.T) {
|
||||||
setting.AppURL = appURL
|
setting.AppURL = appURL
|
||||||
}()
|
}()
|
||||||
username1 := "user1"
|
username1 := "user1"
|
||||||
user1, err := user_model.GetUserByName(username1)
|
var ctx context.Context
|
||||||
|
user1, err := user_model.GetUserByName(ctx, username1)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
user1url := fmt.Sprintf("%s/api/v1/activitypub/user/%s#main-key", srv.URL, username1)
|
user1url := fmt.Sprintf("%s/api/v1/activitypub/user/%s#main-key", srv.URL, username1)
|
||||||
c, err := activitypub.NewClient(user1, user1url)
|
c, err := activitypub.NewClient(user1, user1url)
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
|
|
||||||
"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/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/routers/api/v1/user"
|
"code.gitea.io/gitea/routers/api/v1/user"
|
||||||
|
|
||||||
|
@ -43,7 +44,11 @@ func Person(ctx *context.APIContext) {
|
||||||
person := ap.PersonNew(ap.IRI(link))
|
person := ap.PersonNew(ap.IRI(link))
|
||||||
|
|
||||||
name := ap.NaturalLanguageValuesNew()
|
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.Name = name
|
||||||
|
|
||||||
person.Inbox = ap.Item(ap.IRI(link + "/inbox"))
|
person.Inbox = ap.Item(ap.IRI(link + "/inbox"))
|
||||||
|
@ -63,9 +68,14 @@ func Person(ctx *context.APIContext) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Error(http.StatusInternalServerError, "Serialize", err)
|
ctx.Error(http.StatusInternalServerError, "Serialize", err)
|
||||||
}
|
}
|
||||||
ctx.Resp.Header().Set("Content-Type", "application/json;charset=utf-8")
|
|
||||||
ctx.Write(binary)
|
var jsonmap map[string]interface{}
|
||||||
ctx.Status(http.StatusOK)
|
err = json.Unmarshal(binary, jsonmap)
|
||||||
|
if err != nil {
|
||||||
|
ctx.Error(http.StatusInternalServerError, "Unmarshall", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.JSON(http.StatusOK, jsonmap)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PersonInbox function
|
// PersonInbox function
|
||||||
|
|
Loading…
Reference in New Issue