mirror of
https://github.com/go-gitea/gitea
synced 2024-11-14 22:24:24 +00:00
e60158c70b
Copied from what I wrote on #19133 discussion: Handling username case is a very tricky issue and I've already encountered a Mastodon <-> Gitea federation bug due to Gitea considering Ta180m and ta180m to be the same user while Mastodon thinks they are two different users. I think the best way forward is for Gitea to only use the original case version of the username for federation so other AP software don't get confused.
122 lines
3.2 KiB
Go
122 lines
3.2 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 (
|
|
"net/http"
|
|
|
|
"code.gitea.io/gitea/modules/activitypub"
|
|
"code.gitea.io/gitea/modules/context"
|
|
"code.gitea.io/gitea/modules/json"
|
|
"code.gitea.io/gitea/modules/log"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
ap "github.com/go-ap/activitypub"
|
|
)
|
|
|
|
// 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"
|
|
|
|
link := setting.AppURL + "api/v1/activitypub/user/" + ctx.ContextUser.Name
|
|
person := ap.PersonNew(ap.IRI(link))
|
|
|
|
person.Name = ap.NaturalLanguageValuesNew()
|
|
err := person.Name.Set("en", ap.Content(ctx.ContextUser.FullName))
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "Set Name", err)
|
|
return
|
|
}
|
|
|
|
person.PreferredUsername = ap.NaturalLanguageValuesNew()
|
|
err = person.PreferredUsername.Set("en", ap.Content(ctx.ContextUser.Name))
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "Set PreferredUsername", err)
|
|
return
|
|
}
|
|
|
|
person.URL = ap.IRI(ctx.ContextUser.HTMLURL())
|
|
|
|
person.Icon = ap.Image{
|
|
Type: ap.ImageType,
|
|
MediaType: "image/png",
|
|
URL: ap.IRI(ctx.ContextUser.AvatarLink()),
|
|
}
|
|
|
|
person.Inbox = nil
|
|
person.Inbox, _ = ap.Inbox.AddTo(person)
|
|
person.Outbox = nil
|
|
person.Outbox, _ = ap.Outbox.AddTo(person)
|
|
|
|
person.PublicKey.ID = ap.IRI(link + "#main-key")
|
|
person.PublicKey.Owner = ap.IRI(link)
|
|
|
|
publicKeyPem, err := activitypub.GetPublicKey(ctx.ContextUser)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "GetPublicKey", err)
|
|
return
|
|
}
|
|
person.PublicKey.PublicKeyPem = publicKeyPem
|
|
|
|
binary, err := person.MarshalJSON()
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "Serialize", err)
|
|
return
|
|
}
|
|
|
|
var jsonmap map[string]interface{}
|
|
err = json.Unmarshal(binary, &jsonmap)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "Unmarshal", err)
|
|
return
|
|
}
|
|
|
|
jsonmap["@context"] = []string{"https://www.w3.org/ns/activitystreams", "https://w3id.org/security/v1"}
|
|
|
|
ctx.Resp.Header().Add("Content-Type", "application/activity+json")
|
|
ctx.Resp.WriteHeader(http.StatusOK)
|
|
binary, err = json.Marshal(jsonmap)
|
|
if err != nil {
|
|
ctx.Error(http.StatusInternalServerError, "Marshal", err)
|
|
}
|
|
if _, err = ctx.Resp.Write(binary); err != nil {
|
|
log.Error("write to resp err: %v", err)
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|