2022-03-19 13:55:58 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2021-10-27 01:59:14 +00: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"
|
2022-06-14 17:30:36 +00:00
|
|
|
"strings"
|
2021-10-27 01:59:14 +00:00
|
|
|
|
2021-10-29 03:13:24 +00:00
|
|
|
"code.gitea.io/gitea/modules/activitypub"
|
2021-10-27 01:59:14 +00:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2022-06-09 22:59:43 +00:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2022-06-14 12:26:41 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-10-27 01:59:14 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-03-19 14:14:08 +00:00
|
|
|
|
2022-05-23 16:43:59 +00:00
|
|
|
ap "github.com/go-ap/activitypub"
|
2021-10-27 01:59:14 +00:00
|
|
|
)
|
|
|
|
|
2021-11-10 12:35:02 +00:00
|
|
|
// Person function
|
2021-10-27 01:59:14 +00:00
|
|
|
func Person(ctx *context.APIContext) {
|
2021-11-10 12:35:02 +00:00
|
|
|
// swagger:operation GET /activitypub/user/{username} activitypub activitypubPerson
|
2021-10-27 01:59:14 +00: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"
|
|
|
|
|
2022-06-14 17:30:36 +00:00
|
|
|
link := strings.TrimSuffix(setting.AppURL, "/") + "/api/v1/activitypub/user/" + ctx.ContextUser.Name
|
2022-05-23 16:43:59 +00:00
|
|
|
person := ap.PersonNew(ap.IRI(link))
|
2021-10-27 01:59:14 +00:00
|
|
|
|
2022-06-13 22:14:41 +00:00
|
|
|
person.Name = ap.NaturalLanguageValuesNew()
|
2022-06-14 17:01:41 +00:00
|
|
|
err := person.Name.Set("en", ap.Content(ctx.ContextUser.FullName))
|
2022-06-09 22:59:43 +00:00
|
|
|
if err != nil {
|
2022-06-13 22:14:41 +00:00
|
|
|
ctx.Error(http.StatusInternalServerError, "Set Name", err)
|
2022-06-09 22:59:43 +00:00
|
|
|
return
|
|
|
|
}
|
2022-06-13 22:14:41 +00:00
|
|
|
|
|
|
|
person.PreferredUsername = ap.NaturalLanguageValuesNew()
|
2022-06-14 17:01:41 +00:00
|
|
|
err = person.PreferredUsername.Set("en", ap.Content(ctx.ContextUser.Name))
|
2022-06-13 22:14:41 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "Set PreferredUsername", err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-14 12:12:00 +00:00
|
|
|
|
2022-06-14 17:01:41 +00:00
|
|
|
person.URL = ap.IRI(ctx.ContextUser.HTMLURL())
|
2022-06-13 22:14:41 +00:00
|
|
|
|
|
|
|
person.Icon = ap.Image{
|
2022-06-14 12:12:00 +00:00
|
|
|
Type: ap.ImageType,
|
2022-06-13 22:14:41 +00:00
|
|
|
MediaType: "image/png",
|
2022-06-14 17:01:41 +00:00
|
|
|
URL: ap.IRI(ctx.ContextUser.AvatarLink()),
|
2022-06-13 22:14:41 +00:00
|
|
|
}
|
2021-10-27 01:59:14 +00:00
|
|
|
|
2022-06-11 17:40:10 +00:00
|
|
|
person.Inbox, _ = ap.Inbox.AddTo(person)
|
|
|
|
person.Outbox, _ = ap.Outbox.AddTo(person)
|
2021-10-29 03:13:24 +00:00
|
|
|
|
2022-05-23 16:43:59 +00:00
|
|
|
person.PublicKey.ID = ap.IRI(link + "#main-key")
|
|
|
|
person.PublicKey.Owner = ap.IRI(link)
|
2022-06-09 22:43:54 +00:00
|
|
|
|
2022-06-14 17:01:41 +00:00
|
|
|
publicKeyPem, err := activitypub.GetPublicKey(ctx.ContextUser)
|
2022-05-10 00:10:45 +00:00
|
|
|
if err != nil {
|
2021-10-29 03:13:24 +00:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetPublicKey", err)
|
2022-03-30 20:52:52 +00:00
|
|
|
return
|
2021-10-29 03:13:24 +00:00
|
|
|
}
|
2022-05-23 16:43:59 +00:00
|
|
|
person.PublicKey.PublicKeyPem = publicKeyPem
|
2021-10-29 03:13:24 +00:00
|
|
|
|
2022-05-23 16:43:59 +00:00
|
|
|
binary, err := person.MarshalJSON()
|
2022-03-30 20:52:52 +00:00
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "Serialize", err)
|
2022-06-14 17:01:41 +00:00
|
|
|
return
|
2022-03-30 20:52:52 +00:00
|
|
|
}
|
2022-06-09 22:59:43 +00:00
|
|
|
|
|
|
|
var jsonmap map[string]interface{}
|
2022-06-10 15:37:55 +00:00
|
|
|
err = json.Unmarshal(binary, &jsonmap)
|
2022-06-09 22:59:43 +00:00
|
|
|
if err != nil {
|
2022-06-14 17:01:41 +00:00
|
|
|
ctx.Error(http.StatusInternalServerError, "Unmarshal", err)
|
|
|
|
return
|
2022-06-09 22:59:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 12:12:00 +00:00
|
|
|
jsonmap["@context"] = []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"}
|
2022-06-10 16:04:07 +00:00
|
|
|
|
2022-06-13 22:14:41 +00:00
|
|
|
ctx.Resp.Header().Add("Content-Type", "application/activity+json")
|
|
|
|
ctx.Resp.WriteHeader(http.StatusOK)
|
2022-06-14 12:26:41 +00:00
|
|
|
binary, err = json.Marshal(jsonmap)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "Marshal", err)
|
2022-06-14 17:26:06 +00:00
|
|
|
return
|
2022-06-14 12:26:41 +00:00
|
|
|
}
|
|
|
|
if _, err = ctx.Resp.Write(binary); err != nil {
|
|
|
|
log.Error("write to resp err: %v", err)
|
|
|
|
}
|
2021-10-27 01:59:14 +00:00
|
|
|
}
|
2021-11-10 12:35:02 +00: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)
|
|
|
|
}
|