This repository has been archived on 2022-11-28. You can view files and clone it, but cannot push or open issues or pull requests.
gitea/routers/api/v1/activitypub/person.go

361 lines
9.9 KiB
Go

// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package activitypub
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
user_model "code.gitea.io/gitea/models/user"
"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"
"code.gitea.io/gitea/routers/api/v1/utils"
"github.com/go-fed/activity/streams"
"github.com/go-fed/activity/streams/vocab"
)
// Person function
func Person(ctx *context.APIContext) {
// swagger:operation GET /activitypub/user/{username} activitypub activitypubPerson
// ---
// summary: Returns the person
// 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")
if user == nil {
return
}
username := ctx.Params("username")
person := streams.NewActivityStreamsPerson()
id := streams.NewJSONLDIdProperty()
link := strings.TrimSuffix(setting.AppURL, "/") + strings.TrimSuffix(ctx.Req.URL.EscapedPath(), "/")
idIRI, _ := url.Parse(link)
id.SetIRI(idIRI)
person.SetJSONLDId(id)
name := streams.NewActivityStreamsNameProperty()
name.AppendXMLSchemaString(username)
person.SetActivityStreamsName(name)
ibox := streams.NewActivityStreamsInboxProperty()
urlObject, _ := url.Parse(link + "/inbox")
ibox.SetIRI(urlObject)
person.SetActivityStreamsInbox(ibox)
obox := streams.NewActivityStreamsOutboxProperty()
urlObject, _ = url.Parse(link + "/outbox")
obox.SetIRI(urlObject)
person.SetActivityStreamsOutbox(obox)
following := streams.NewActivityStreamsFollowingProperty()
urlObject, _ = url.Parse(link + "/following")
following.SetIRI(urlObject)
person.SetActivityStreamsFollowing(following)
followers := streams.NewActivityStreamsFollowersProperty()
urlObject, _ = url.Parse(link + "/followers")
followers.SetIRI(urlObject)
person.SetActivityStreamsFollowers(followers)
publicKeyProp := streams.NewW3IDSecurityV1PublicKeyProperty()
publicKeyType := streams.NewW3IDSecurityV1PublicKey()
pubKeyIDProp := streams.NewJSONLDIdProperty()
pubKeyIRI, _ := url.Parse(link + "#main-key")
pubKeyIDProp.SetIRI(pubKeyIRI)
publicKeyType.SetJSONLDId(pubKeyIDProp)
ownerProp := streams.NewW3IDSecurityV1OwnerProperty()
ownerProp.SetIRI(idIRI)
publicKeyType.SetW3IDSecurityV1Owner(ownerProp)
publicKeyPemProp := streams.NewW3IDSecurityV1PublicKeyPemProperty()
if publicKeyPem, err := activitypub.GetPublicKey(user); err != nil {
ctx.Error(http.StatusInternalServerError, "GetPublicKey", err)
return
} else {
publicKeyPemProp.Set(publicKeyPem)
}
publicKeyType.SetW3IDSecurityV1PublicKeyPem(publicKeyPemProp)
publicKeyProp.AppendW3IDSecurityV1PublicKey(publicKeyType)
person.SetW3IDSecurityV1PublicKey(publicKeyProp)
jsonmap, err := streams.Serialize(person)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Serialize", err)
}
ctx.JSON(http.StatusOK, jsonmap)
}
// PersonInboxGet function
func PersonInboxGet(ctx *context.APIContext) {
// swagger:operation GET /activitypub/user/{username}/inbox activitypub activitypubPersonInbox
// ---
// summary: Returns the inbox
// produces:
// - application/activity+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
// produces:
// - application/json
// parameters:
// - name: username
// in: path
// description: username of the user
// type: string
// required: true
// responses:
// "204":
// "$ref": "#/responses/empty"
r := ctx.Req
body, err := io.ReadAll(r.Body)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Error reading request body", err)
}
var m map[string]interface{}
json.Unmarshal(body, &m)
var t vocab.Type
t, err = streams.ToType(ctx, m)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not serialize payload", err)
}
fmt.Println(m) // Debugging
activitypub.AddToInbox(t)
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/activity+json
// parameters:
// - name: username
// in: path
// description: username of the user
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/ActivityPub"
// Alright so this function is kinda useless right now so let's misuse it for testing following
follow := streams.NewActivityStreamsFollow()
username := ctx.Params("username")
actorIRI, _ := url.Parse("https://git.exozy.me/api/v1/activitypub/user/" + username)
objectIRI, _ := url.Parse("https://git.exozy.me/api/v1/activitypub/user/guest")
id, _ := url.Parse("https://git.exozy.me/" + username)
idProperty := streams.NewJSONLDIdProperty()
idProperty.Set(id)
follow.SetJSONLDId(idProperty)
toProperty := streams.NewActivityStreamsToProperty()
toProperty.AppendIRI(objectIRI)
follow.SetActivityStreamsTo(toProperty)
summary := streams.NewActivityStreamsSummaryProperty()
summary.AppendXMLSchemaString("This is a test")
follow.SetActivityStreamsSummary(summary)
actor := streams.NewActivityStreamsActorProperty()
actor.AppendIRI(actorIRI)
follow.SetActivityStreamsActor(actor)
object := streams.NewActivityStreamsObjectProperty()
object.AppendIRI(objectIRI)
follow.SetActivityStreamsObject(object)
activitypub.AddToOutbox(follow, user.GetUserByParamsName(ctx, "username"), objectIRI)
ctx.Status(http.StatusNoContent)
}
// 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"
/*
r := ctx.Req
body, err := io.ReadAll(r.Body)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Error reading request body", err)
}
var m map[string]interface{}
json.Unmarshal(body, &m)
var t vocab.Type
t, err = streams.ToType(ctx, m)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Could not serialize payload", err)
}
fmt.Println(m) // Debugging
activitypub.AddToOutbox(t)
*/
ctx.Status(http.StatusNoContent)
}
// PersonFollowing function
func PersonFollowing(ctx *context.APIContext) {
// swagger:operation GET /activitypub/user/{username}/following activitypub activitypubPersonFollowing
// ---
// summary: Returns the following collection
// produces:
// - application/activity+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")
if user == nil {
return
}
users, err := user_model.GetUserFollowing(user, utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetUserFollowing", err)
return
}
following := streams.NewActivityStreamsOrderedCollection()
totalItems := streams.NewActivityStreamsTotalItemsProperty()
totalItems.Set(len(users))
following.SetActivityStreamsTotalItems(totalItems)
items := streams.NewActivityStreamsOrderedItemsProperty()
for i := 0; i < len(users); i++ {
user, _ := url.Parse(setting.AppURL+"api/v1/activitypub/user/"+users[i].Name)
items.AppendIRI(user)
}
following.SetActivityStreamsOrderedItems(items)
jsonmap, err := streams.Serialize(following)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Serialize", err)
}
ctx.JSON(http.StatusOK, jsonmap)
}
// PersonFollowers function
func PersonFollowers(ctx *context.APIContext) {
// swagger:operation GET /activitypub/user/{username}/followers activitypub activitypubPersonFollowers
// ---
// summary: Returns the followers collection
// produces:
// - application/activity+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")
if user == nil {
return
}
users, err := user_model.GetUserFollowers(user, utils.GetListOptions(ctx))
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetUserFollowers", err)
return
}
followers := streams.NewActivityStreamsOrderedCollection()
totalItems := streams.NewActivityStreamsTotalItemsProperty()
totalItems.Set(len(users))
followers.SetActivityStreamsTotalItems(totalItems)
items := streams.NewActivityStreamsOrderedItemsProperty()
for i := 0; i < len(users); i++ {
user, _ := url.Parse(setting.AppURL+"api/v1/activitypub/user/"+users[i].Name)
items.AppendIRI(user)
}
followers.SetActivityStreamsOrderedItems(items)
jsonmap, err := streams.Serialize(followers)
fmt.Println(jsonmap)
if err != nil {
ctx.Error(http.StatusInternalServerError, "Serialize", err)
}
ctx.JSON(http.StatusOK, jsonmap)
}