2022-03-19 14:55:58 +01:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2021-10-27 03:59:14 +02:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package activitypub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
2021-10-29 05:13:24 +02:00
|
|
|
"code.gitea.io/gitea/modules/activitypub"
|
2021-10-27 03:59:14 +02:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/routers/api/v1/user"
|
2022-03-19 15:14:08 +01:00
|
|
|
|
2021-10-27 03:59:14 +02:00
|
|
|
"github.com/go-fed/activity/streams"
|
|
|
|
)
|
|
|
|
|
2021-11-10 13:35:02 +01:00
|
|
|
// Person function
|
2021-10-27 03:59:14 +02:00
|
|
|
func Person(ctx *context.APIContext) {
|
2021-11-10 13:35:02 +01:00
|
|
|
// swagger:operation GET /activitypub/user/{username} activitypub activitypubPerson
|
2021-10-27 03:59:14 +02:00
|
|
|
// ---
|
|
|
|
// 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"
|
|
|
|
|
2021-10-29 05:13:24 +02:00
|
|
|
user := user.GetUserByParamsName(ctx, "username")
|
2021-10-27 03:59:14 +02:00
|
|
|
username := ctx.Params("username")
|
|
|
|
|
|
|
|
person := streams.NewActivityStreamsPerson()
|
|
|
|
|
|
|
|
id := streams.NewJSONLDIdProperty()
|
|
|
|
link := strings.TrimSuffix(setting.AppURL, "/") + strings.TrimSuffix(ctx.Req.URL.EscapedPath(), "/")
|
2021-10-29 05:13:24 +02:00
|
|
|
idIRI, _ := url.Parse(link)
|
|
|
|
id.SetIRI(idIRI)
|
2021-10-27 03:59:14 +02:00
|
|
|
person.SetJSONLDId(id)
|
|
|
|
|
|
|
|
name := streams.NewActivityStreamsNameProperty()
|
|
|
|
name.AppendXMLSchemaString(username)
|
|
|
|
person.SetActivityStreamsName(name)
|
|
|
|
|
|
|
|
ibox := streams.NewActivityStreamsInboxProperty()
|
2021-11-10 13:35:02 +01:00
|
|
|
urlObject, _ := url.Parse(link + "/inbox")
|
|
|
|
ibox.SetIRI(urlObject)
|
2021-10-27 03:59:14 +02:00
|
|
|
person.SetActivityStreamsInbox(ibox)
|
|
|
|
|
|
|
|
obox := streams.NewActivityStreamsOutboxProperty()
|
2021-11-10 13:35:02 +01:00
|
|
|
urlObject, _ = url.Parse(link + "/outbox")
|
|
|
|
obox.SetIRI(urlObject)
|
2021-10-27 03:59:14 +02:00
|
|
|
person.SetActivityStreamsOutbox(obox)
|
|
|
|
|
2021-10-29 05:13:24 +02:00
|
|
|
publicKeyProp := streams.NewW3IDSecurityV1PublicKeyProperty()
|
|
|
|
|
|
|
|
publicKeyType := streams.NewW3IDSecurityV1PublicKey()
|
|
|
|
|
2021-11-10 13:35:02 +01:00
|
|
|
pubKeyIDProp := streams.NewJSONLDIdProperty()
|
2021-10-29 05:13:24 +02:00
|
|
|
pubKeyIRI, _ := url.Parse(link + "/#main-key")
|
2021-11-10 13:35:02 +01:00
|
|
|
pubKeyIDProp.SetIRI(pubKeyIRI)
|
|
|
|
publicKeyType.SetJSONLDId(pubKeyIDProp)
|
2021-10-29 05:13:24 +02:00
|
|
|
|
|
|
|
ownerProp := streams.NewW3IDSecurityV1OwnerProperty()
|
|
|
|
ownerProp.SetIRI(idIRI)
|
|
|
|
publicKeyType.SetW3IDSecurityV1Owner(ownerProp)
|
|
|
|
|
|
|
|
publicKeyPemProp := streams.NewW3IDSecurityV1PublicKeyPemProperty()
|
2021-11-10 13:35:02 +01:00
|
|
|
if publicKeyPem, err := activitypub.GetPublicKey(user); err != nil {
|
2021-10-29 05:13:24 +02:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetPublicKey", err)
|
|
|
|
} else {
|
|
|
|
publicKeyPemProp.Set(publicKeyPem)
|
|
|
|
}
|
|
|
|
publicKeyType.SetW3IDSecurityV1PublicKeyPem(publicKeyPemProp)
|
|
|
|
|
|
|
|
publicKeyProp.AppendW3IDSecurityV1PublicKey(publicKeyType)
|
|
|
|
person.SetW3IDSecurityV1PublicKey(publicKeyProp)
|
|
|
|
|
2021-10-27 03:59:14 +02:00
|
|
|
var jsonmap map[string]interface{}
|
|
|
|
jsonmap, _ = streams.Serialize(person)
|
|
|
|
ctx.JSON(http.StatusOK, jsonmap)
|
|
|
|
}
|
2021-11-10 13:35:02 +01:00
|
|
|
|
|
|
|
// PersonInbox function
|
|
|
|
func PersonInbox(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:
|
|
|
|
// responses:
|
|
|
|
// "204":
|
|
|
|
// "$ref": "#/responses/empty"
|
|
|
|
|
|
|
|
ctx.Status(http.StatusNoContent)
|
|
|
|
}
|