diff options
author | Anthony Wang | 2022-04-14 10:04:22 -0500 |
---|---|---|
committer | Anthony Wang | 2022-04-14 10:04:22 -0500 |
commit | 39919e5eee634741c4722988c39573a731d9aace (patch) | |
tree | 1166373cf841fe6b76a4984d30eb7ed1d88dade9 | |
parent | 443bfac04e8d852aead7762ec9795c25c07bd0eb (diff) |
Rig up inbox/outbox to the API
-rw-r--r-- | modules/activitypub/database.go | 7 | ||||
-rw-r--r-- | routers/api/v1/activitypub/person.go | 110 | ||||
-rw-r--r-- | routers/api/v1/api.go | 5 | ||||
-rw-r--r-- | templates/swagger/v1_json.tmpl | 74 |
4 files changed, 191 insertions, 5 deletions
diff --git a/modules/activitypub/database.go b/modules/activitypub/database.go index 383186ced..05d65d1ac 100644 --- a/modules/activitypub/database.go +++ b/modules/activitypub/database.go @@ -7,6 +7,7 @@ package activitypub import ( user_model "code.gitea.io/gitea/models/user" + "github.com/go-fed/activity/streams" "github.com/go-fed/activity/streams/vocab" ) @@ -16,8 +17,10 @@ func databaseAddToInbox(activity vocab.ActivityStreamsActivity) { func databaseAddToOutbox(activity vocab.ActivityStreamsActivity) { } -func GetInbox(user user_model.User) { +func GetInbox(user *user_model.User) vocab.ActivityStreamsOrderedCollection { + return streams.NewActivityStreamsOrderedCollection() } -func GetOutbox(user user_model.User) { +func GetOutbox(user *user_model.User) vocab.ActivityStreamsOrderedCollection { + return streams.NewActivityStreamsOrderedCollection() } diff --git a/routers/api/v1/activitypub/person.go b/routers/api/v1/activitypub/person.go index 9df834dd4..eff2a036d 100644 --- a/routers/api/v1/activitypub/person.go +++ b/routers/api/v1/activitypub/person.go @@ -5,16 +5,20 @@ package activitypub import ( + "fmt" + "io" "net/http" "net/url" "strings" "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" "github.com/go-fed/activity/streams" + "github.com/go-fed/activity/streams/vocab" ) // Person function @@ -94,8 +98,34 @@ func Person(ctx *context.APIContext) { ctx.JSON(http.StatusOK, jsonmap) } -// PersonInbox function -func PersonInbox(ctx *context.APIContext) { +// PersonInboxGet function +func PersonInboxGet(ctx *context.APIContext) { + // swagger:operation GET /activitypub/user/{username}/inbox activitypub activitypubPersonInbox + // --- + // summary: Returns the inbox + // produces: + // - application/json + // parameters: + // - name: username + // in: path + // description: username of the user + // type: string + // required: true + // responses: + // "200": + // "$ref": "#/responses/ActivityPub" + + user := user.GetUserByParamsName(ctx, "username") + inbox := activitypub.GetInbox(user) + jsonmap, err := streams.Serialize(inbox) + if err != nil { + ctx.Error(http.StatusInternalServerError, "Serialize", err) + } + ctx.JSON(http.StatusOK, jsonmap) +} + +// PersonInboxPost function +func PersonInboxPost(ctx *context.APIContext) { // swagger:operation POST /activitypub/user/{username}/inbox activitypub activitypubPersonInbox // --- // summary: Send to the inbox @@ -108,9 +138,85 @@ func PersonInbox(ctx *context.APIContext) { // type: string // required: true // responses: + // "204": + // "$ref": "#/responses/empty" + + fmt.Println(ctx) + + r := ctx.Req + body, _ := io.ReadAll(r.Body) + var m map[string]interface{} + json.Unmarshal(body, &m) + + var activity vocab.ActivityStreamsActivity + resolver, _ := streams.NewJSONResolver(func(c context.Context, a vocab.ActivityStreamsActivity) error { + activity = a + return nil + }) + _ = resolver.Resolve(ctx, m) + + activitypub.AddToInbox(activity) + ctx.Status(http.StatusNoContent) +} + +// PersonOutboxGet function +func PersonOutboxGet(ctx *context.APIContext) { + // swagger:operation GET /activitypub/user/{username}/outbox activitypub activitypubPersonOutbox + // --- + // summary: Returns the outbox + // produces: + // - application/json + // parameters: + // - name: username + // in: path + // description: username of the user + // type: string + // required: true + // responses: + // "200": + // "$ref": "#/responses/ActivityPub" + + user := user.GetUserByParamsName(ctx, "username") + inbox := activitypub.GetOutbox(user) + jsonmap, err := streams.Serialize(inbox) + if err != nil { + ctx.Error(http.StatusInternalServerError, "Serialize", err) + } + ctx.JSON(http.StatusOK, jsonmap) +} + +// PersonOutboxPost function +func PersonOutboxPost(ctx *context.APIContext) { + // swagger:operation POST /activitypub/user/{username}/outbox activitypub activitypubPersonOutbox + // --- + // summary: Send to the outbox + // produces: + // - application/json + // parameters: + // - name: username + // in: path + // description: username of the user + // type: string + // required: true + // responses: // responses: // "204": // "$ref": "#/responses/empty" + fmt.Println(ctx) + + r := ctx.Req + body, _ := io.ReadAll(r.Body) + var m map[string]interface{} + json.Unmarshal(body, &m) + + var activity vocab.ActivityStreamsActivity + resolver, _ := streams.NewJSONResolver(func(c context.Context, a vocab.ActivityStreamsActivity) error { + activity = a + return nil + }) + _ = resolver.Resolve(ctx, m) + + activitypub.AddToOutbox(activity) ctx.Status(http.StatusNoContent) } diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index fb1af75ec..19b0d80ca 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -600,7 +600,10 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route { m.Get("/nodeinfo", misc.NodeInfo) m.Group("/activitypub", func() { m.Get("/user/{username}", activitypub.Person) - m.Post("/user/{username}/inbox", activitypub.ReqSignature(), activitypub.PersonInbox) + m.Get("/user/{username}/inbox", activitypub.PersonInboxGet) + m.Post("/user/{username}/inbox", activitypub.ReqSignature(), activitypub.PersonInboxPost) + m.Get("/user/{username}/outbox", activitypub.PersonOutboxGet) + m.Post("/user/{username}/outbox", activitypub.ReqSignature(), activitypub.PersonOutboxPost) }) } m.Get("/signing-key.gpg", misc.SigningKey) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index c068612f5..646479379 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -50,6 +50,30 @@ } }, "/activitypub/user/{username}/inbox": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "activitypub" + ], + "summary": "Returns the inbox", + "operationId": "activitypubPersonInbox", + "parameters": [ + { + "type": "string", + "description": "username of the user", + "name": "username", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "$ref": "#/responses/ActivityPub" + } + } + }, "post": { "produces": [ "application/json" @@ -75,6 +99,56 @@ } } }, + "/activitypub/user/{username}/outbox": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "activitypub" + ], + "summary": "Returns the outbox", + "operationId": "activitypubPersonOutbox", + "parameters": [ + { + "type": "string", + "description": "username of the user", + "name": "username", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "$ref": "#/responses/ActivityPub" + } + } + }, + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "activitypub" + ], + "summary": "Send to the outbox", + "operationId": "activitypubPersonOutbox", + "parameters": [ + { + "type": "string", + "description": "username of the user", + "name": "username", + "in": "path", + "required": true + } + ], + "responses": { + "204": { + "$ref": "#/responses/empty" + } + } + } + }, "/admin/cron": { "get": { "produces": [ |