Send a remote follow in PersonInboxGet

This commit is contained in:
Anthony Wang 2022-04-11 22:02:27 -05:00
parent cb0a9de330
commit 046d363305
Signed by: a
GPG key ID: BC96B00AEC5F2D76
2 changed files with 57 additions and 8 deletions

View file

@ -9,8 +9,10 @@ import (
"net/url" "net/url"
"strings" "strings"
user_model "code.gitea.io/gitea/models/user"
"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"
@ -48,10 +50,18 @@ func Person(ctx *context.APIContext) {
id.SetIRI(idIRI) id.SetIRI(idIRI)
person.SetJSONLDId(id) person.SetJSONLDId(id)
tp := streams.NewJSONLDTypeProperty()
tp.AppendXMLSchemaString("Person")
person.SetJSONLDType(tp)
name := streams.NewActivityStreamsNameProperty() name := streams.NewActivityStreamsNameProperty()
name.AppendXMLSchemaString(username) name.AppendXMLSchemaString(username)
person.SetActivityStreamsName(name) person.SetActivityStreamsName(name)
preferredUsername := streams.NewActivityStreamsPreferredUsernameProperty()
preferredUsername.SetXMLSchemaString(username)
person.SetActivityStreamsPreferredUsername(preferredUsername)
ibox := streams.NewActivityStreamsInboxProperty() ibox := streams.NewActivityStreamsInboxProperty()
urlObject, _ := url.Parse(link + "/inbox") urlObject, _ := url.Parse(link + "/inbox")
ibox.SetIRI(urlObject) ibox.SetIRI(urlObject)
@ -67,7 +77,7 @@ func Person(ctx *context.APIContext) {
publicKeyType := streams.NewW3IDSecurityV1PublicKey() publicKeyType := streams.NewW3IDSecurityV1PublicKey()
pubKeyIDProp := streams.NewJSONLDIdProperty() pubKeyIDProp := streams.NewJSONLDIdProperty()
pubKeyIRI, _ := url.Parse(link + "/#main-key") pubKeyIRI, _ := url.Parse(link + "#main-key")
pubKeyIDProp.SetIRI(pubKeyIRI) pubKeyIDProp.SetIRI(pubKeyIRI)
publicKeyType.SetJSONLDId(pubKeyIDProp) publicKeyType.SetJSONLDId(pubKeyIDProp)
@ -96,7 +106,7 @@ func Person(ctx *context.APIContext) {
// PersonInboxGet function // PersonInboxGet function
func PersonInboxGet(ctx *context.APIContext) { func PersonInboxGet(ctx *context.APIContext) {
// swagger:operation GET /activitypub/user/{username}/outbox activitypub activitypubPersonInbox // swagger:operation GET /activitypub/user/{username}/inbox activitypub activitypubPersonInbox
// --- // ---
// summary: Returns the inbox // summary: Returns the inbox
// produces: // produces:
@ -112,8 +122,47 @@ func PersonInboxGet(ctx *context.APIContext) {
// "200": // "200":
// "$ref": "#/responses/ActivityPub" // "$ref": "#/responses/ActivityPub"
follow := streams.NewActivityStreamsFollow()
myIRI, _ := url.Parse("https://git.exozy.me/api/v1/activitypub/user/ta180m/inbox")
followIRI, _ := url.Parse("https://social.exozy.me/users/ta180m")
id, _ := url.Parse("https://git.exozy.me/Ta180m")
idProperty := streams.NewJSONLDIdProperty()
idProperty.Set(id)
me := streams.NewActivityStreamsActorProperty()
me.AppendIRI(myIRI)
follow.SetActivityStreamsActor(me)
op := streams.NewActivityStreamsObjectProperty()
op.AppendIRI(followIRI)
follow.SetActivityStreamsObject(op)
user, err := user_model.GetUserByName("ta180m")
if err != nil {
ctx.Error(http.StatusInternalServerError, "debug", err)
}
c, err := activitypub.NewClient(user, "https://git.exozy.me/api/v1/activitypub/user/ta180m#main-key")
if err != nil {
ctx.Error(http.StatusInternalServerError, "debug", err)
}
jsonmap, err := streams.Serialize(follow)
if err != nil {
ctx.Error(http.StatusInternalServerError, "debug", err)
}
body, err := json.Marshal(jsonmap)
if err != nil {
ctx.Error(http.StatusInternalServerError, "debug", err)
}
resp, err := c.Post(body, "https://social.exozy.me/inbox")
if err != nil {
ctx.Error(http.StatusInternalServerError, "debug", err)
}
ctx.Error(http.StatusInternalServerError, "debug", resp)
ctx.Status(http.StatusOK) ctx.Status(http.StatusOK)
activitypub.GetUserActor().GetInbox(ctx, ctx.Resp, ctx.Req)
} }
// PersonInboxPost function // PersonInboxPost function
@ -134,8 +183,8 @@ func PersonInboxPost(ctx *context.APIContext) {
// "200": // "200":
// "$ref": "#/responses/ActivityPub" // "$ref": "#/responses/ActivityPub"
ctx.Error(http.StatusInternalServerError, "inboxpost", ctx.Req.Body)
ctx.Status(http.StatusOK) ctx.Status(http.StatusOK)
activitypub.GetUserActor().PostInbox(ctx, ctx.Resp, ctx.Req)
} }
// PersonOutboxGet function // PersonOutboxGet function
@ -156,8 +205,8 @@ func PersonOutboxGet(ctx *context.APIContext) {
// "200": // "200":
// "$ref": "#/responses/ActivityPub" // "$ref": "#/responses/ActivityPub"
ctx.Error(http.StatusInternalServerError, "outboxget", ctx.Req.Body)
ctx.Status(http.StatusOK) ctx.Status(http.StatusOK)
activitypub.GetUserActor().GetOutbox(ctx, ctx.Resp, ctx.Req)
} }
// PersonOutboxPost function // PersonOutboxPost function
@ -178,6 +227,6 @@ func PersonOutboxPost(ctx *context.APIContext) {
// "200": // "200":
// "$ref": "#/responses/ActivityPub" // "$ref": "#/responses/ActivityPub"
ctx.Error(http.StatusInternalServerError, "outboxpost", ctx.Req.Body)
ctx.Status(http.StatusOK) ctx.Status(http.StatusOK)
activitypub.GetUserActor().PostOutbox(ctx, ctx.Resp, ctx.Req)
} }

View file

@ -600,9 +600,9 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
m.Get("/nodeinfo", misc.NodeInfo) m.Get("/nodeinfo", misc.NodeInfo)
m.Group("/activitypub", func() { m.Group("/activitypub", func() {
m.Get("/user/{username}", activitypub.Person) m.Get("/user/{username}", activitypub.Person)
m.Get("/user/{username}/inbox", activitypub.ReqSignature(), activitypub.PersonInboxGet) m.Get("/user/{username}/inbox", activitypub.PersonInboxGet)
m.Post("/user/{username}/inbox", activitypub.ReqSignature(), activitypub.PersonInboxPost) m.Post("/user/{username}/inbox", activitypub.ReqSignature(), activitypub.PersonInboxPost)
m.Get("/user/{username}/outbox", activitypub.ReqSignature(), activitypub.PersonOutboxGet) m.Get("/user/{username}/outbox", activitypub.PersonOutboxGet)
m.Post("/user/{username}/outbox", activitypub.ReqSignature(), activitypub.PersonOutboxPost) m.Post("/user/{username}/outbox", activitypub.ReqSignature(), activitypub.PersonOutboxPost)
}) })
} }