mirror of
https://github.com/go-gitea/gitea
synced 2024-11-14 06:04:25 +00:00
91 lines
2.3 KiB
Go
91 lines
2.3 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"
|
|
"strings"
|
|
|
|
"code.gitea.io/gitea/modules/activitypub"
|
|
"code.gitea.io/gitea/modules/context"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
"code.gitea.io/gitea/routers/api/v1/user"
|
|
|
|
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"
|
|
|
|
user := user.GetUserByParamsName(ctx, "username")
|
|
if user == nil {
|
|
return
|
|
}
|
|
username := ctx.Params("username")
|
|
|
|
link := strings.TrimSuffix(setting.AppURL, "/") + strings.TrimSuffix(ctx.Req.URL.EscapedPath(), "/")
|
|
person := ap.PersonNew(ap.IRI(link))
|
|
|
|
name := ap.NaturalLanguageValuesNew()
|
|
name.Set("en", ap.Content(username))
|
|
person.Name = name
|
|
|
|
person.Inbox = ap.Item(ap.IRI(link + "/inbox"))
|
|
person.Outbox = ap.Item(ap.IRI(link + "/outbox"))
|
|
|
|
person.PublicKey.ID = ap.IRI(link + "#main-key")
|
|
person.PublicKey.Owner = ap.IRI(link)
|
|
|
|
publicKeyPem, err := activitypub.GetPublicKey(user)
|
|
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)
|
|
}
|
|
ctx.Resp.Header().Set("Content-Type", "application/json;charset=utf-8")
|
|
ctx.Write(binary)
|
|
ctx.Status(http.StatusOK)
|
|
}
|
|
|
|
// 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)
|
|
}
|