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"
|
|
|
|
"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"
|
2022-06-10 00:59:43 +02:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2021-10-27 03:59:14 +02:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/routers/api/v1/user"
|
2022-03-19 15:14:08 +01:00
|
|
|
|
2022-05-23 18:43:59 +02:00
|
|
|
ap "github.com/go-ap/activitypub"
|
2021-10-27 03:59:14 +02:00
|
|
|
)
|
|
|
|
|
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")
|
2022-03-25 00:44:44 +01:00
|
|
|
if user == nil {
|
2022-03-27 19:25:56 +02:00
|
|
|
return
|
2022-03-25 00:44:44 +01:00
|
|
|
}
|
2021-10-27 03:59:14 +02:00
|
|
|
username := ctx.Params("username")
|
2022-06-10 00:43:54 +02:00
|
|
|
|
2021-10-27 03:59:14 +02:00
|
|
|
link := strings.TrimSuffix(setting.AppURL, "/") + strings.TrimSuffix(ctx.Req.URL.EscapedPath(), "/")
|
2022-05-23 18:43:59 +02:00
|
|
|
person := ap.PersonNew(ap.IRI(link))
|
2021-10-27 03:59:14 +02:00
|
|
|
|
2022-05-23 18:43:59 +02:00
|
|
|
name := ap.NaturalLanguageValuesNew()
|
2022-06-10 00:59:43 +02:00
|
|
|
err := name.Set("en", ap.Content(username))
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "Set name", err)
|
|
|
|
return
|
|
|
|
}
|
2022-05-23 18:43:59 +02:00
|
|
|
person.Name = name
|
2021-10-27 03:59:14 +02:00
|
|
|
|
2022-05-23 18:43:59 +02:00
|
|
|
person.Inbox = ap.Item(ap.IRI(link + "/inbox"))
|
|
|
|
person.Outbox = ap.Item(ap.IRI(link + "/outbox"))
|
2021-10-29 05:13:24 +02:00
|
|
|
|
2022-05-23 18:43:59 +02:00
|
|
|
person.PublicKey.ID = ap.IRI(link + "#main-key")
|
|
|
|
person.PublicKey.Owner = ap.IRI(link)
|
2022-06-10 00:43:54 +02:00
|
|
|
|
2022-05-10 02:10:45 +02:00
|
|
|
publicKeyPem, err := activitypub.GetPublicKey(user)
|
|
|
|
if err != nil {
|
2021-10-29 05:13:24 +02:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetPublicKey", err)
|
2022-03-30 22:52:52 +02:00
|
|
|
return
|
2021-10-29 05:13:24 +02:00
|
|
|
}
|
2022-05-23 18:43:59 +02:00
|
|
|
person.PublicKey.PublicKeyPem = publicKeyPem
|
2021-10-29 05:13:24 +02:00
|
|
|
|
2022-05-23 18:43:59 +02:00
|
|
|
binary, err := person.MarshalJSON()
|
2022-03-30 22:52:52 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "Serialize", err)
|
|
|
|
}
|
2022-06-10 00:59:43 +02:00
|
|
|
|
|
|
|
var jsonmap map[string]interface{}
|
2022-06-10 17:37:55 +02:00
|
|
|
err = json.Unmarshal(binary, &jsonmap)
|
2022-06-10 00:59:43 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "Unmarshall", err)
|
|
|
|
}
|
|
|
|
|
2022-06-10 18:04:07 +02:00
|
|
|
jsonmap["@context"] = "https://www.w3.org/ns/activitystreams"
|
|
|
|
|
2022-06-10 00:59:43 +02:00
|
|
|
ctx.JSON(http.StatusOK, jsonmap)
|
2021-10-27 03:59:14 +02:00
|
|
|
}
|
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)
|
|
|
|
}
|