Rig up inbox/outbox to the API

This commit is contained in:
Anthony Wang 2022-04-14 10:04:22 -05:00
parent d2c5df88eb
commit f9b38495fd
Signed by: a
GPG key ID: BC96B00AEC5F2D76
4 changed files with 191 additions and 5 deletions

View file

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

View file

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

View file

@ -636,7 +636,10 @@ func Routes() *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)

View file

@ -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": [