2022-03-19 07:55:58 -06:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2021-10-26 19:59:14 -06: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-28 21:13:24 -06:00
|
|
|
"code.gitea.io/gitea/modules/activitypub"
|
2021-10-26 19:59:14 -06:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/routers/api/v1/user"
|
2022-03-19 08:14:08 -06:00
|
|
|
|
2021-10-26 19:59:14 -06:00
|
|
|
"github.com/go-fed/activity/streams"
|
|
|
|
)
|
|
|
|
|
2021-11-10 05:35:02 -07:00
|
|
|
// Person function
|
2021-10-26 19:59:14 -06:00
|
|
|
func Person(ctx *context.APIContext) {
|
2021-11-10 05:35:02 -07:00
|
|
|
// swagger:operation GET /activitypub/user/{username} activitypub activitypubPerson
|
2021-10-26 19:59:14 -06: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-28 21:13:24 -06:00
|
|
|
user := user.GetUserByParamsName(ctx, "username")
|
2022-03-24 17:44:44 -06:00
|
|
|
if user == nil {
|
|
|
|
return
|
|
|
|
}
|
2021-10-26 19:59:14 -06:00
|
|
|
username := ctx.Params("username")
|
|
|
|
|
|
|
|
person := streams.NewActivityStreamsPerson()
|
|
|
|
|
|
|
|
id := streams.NewJSONLDIdProperty()
|
|
|
|
link := strings.TrimSuffix(setting.AppURL, "/") + strings.TrimSuffix(ctx.Req.URL.EscapedPath(), "/")
|
2021-10-28 21:13:24 -06:00
|
|
|
idIRI, _ := url.Parse(link)
|
|
|
|
id.SetIRI(idIRI)
|
2021-10-26 19:59:14 -06:00
|
|
|
person.SetJSONLDId(id)
|
|
|
|
|
|
|
|
name := streams.NewActivityStreamsNameProperty()
|
|
|
|
name.AppendXMLSchemaString(username)
|
|
|
|
person.SetActivityStreamsName(name)
|
|
|
|
|
|
|
|
ibox := streams.NewActivityStreamsInboxProperty()
|
2021-11-10 05:35:02 -07:00
|
|
|
urlObject, _ := url.Parse(link + "/inbox")
|
|
|
|
ibox.SetIRI(urlObject)
|
2021-10-26 19:59:14 -06:00
|
|
|
person.SetActivityStreamsInbox(ibox)
|
|
|
|
|
|
|
|
obox := streams.NewActivityStreamsOutboxProperty()
|
2021-11-10 05:35:02 -07:00
|
|
|
urlObject, _ = url.Parse(link + "/outbox")
|
|
|
|
obox.SetIRI(urlObject)
|
2021-10-26 19:59:14 -06:00
|
|
|
person.SetActivityStreamsOutbox(obox)
|
|
|
|
|
2021-10-28 21:13:24 -06:00
|
|
|
publicKeyProp := streams.NewW3IDSecurityV1PublicKeyProperty()
|
|
|
|
|
|
|
|
publicKeyType := streams.NewW3IDSecurityV1PublicKey()
|
|
|
|
|
2021-11-10 05:35:02 -07:00
|
|
|
pubKeyIDProp := streams.NewJSONLDIdProperty()
|
2021-10-28 21:13:24 -06:00
|
|
|
pubKeyIRI, _ := url.Parse(link + "/#main-key")
|
2021-11-10 05:35:02 -07:00
|
|
|
pubKeyIDProp.SetIRI(pubKeyIRI)
|
|
|
|
publicKeyType.SetJSONLDId(pubKeyIDProp)
|
2021-10-28 21:13:24 -06:00
|
|
|
|
|
|
|
ownerProp := streams.NewW3IDSecurityV1OwnerProperty()
|
|
|
|
ownerProp.SetIRI(idIRI)
|
|
|
|
publicKeyType.SetW3IDSecurityV1Owner(ownerProp)
|
|
|
|
|
|
|
|
publicKeyPemProp := streams.NewW3IDSecurityV1PublicKeyPemProperty()
|
2021-11-10 05:35:02 -07:00
|
|
|
if publicKeyPem, err := activitypub.GetPublicKey(user); err != nil {
|
2021-10-28 21:13:24 -06:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetPublicKey", err)
|
|
|
|
} else {
|
|
|
|
publicKeyPemProp.Set(publicKeyPem)
|
|
|
|
}
|
|
|
|
publicKeyType.SetW3IDSecurityV1PublicKeyPem(publicKeyPemProp)
|
|
|
|
|
|
|
|
publicKeyProp.AppendW3IDSecurityV1PublicKey(publicKeyType)
|
|
|
|
person.SetW3IDSecurityV1PublicKey(publicKeyProp)
|
|
|
|
|
2021-10-26 19:59:14 -06:00
|
|
|
var jsonmap map[string]interface{}
|
|
|
|
jsonmap, _ = streams.Serialize(person)
|
|
|
|
ctx.JSON(http.StatusOK, jsonmap)
|
|
|
|
}
|
2021-11-10 05:35:02 -07: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)
|
|
|
|
}
|