Support webfinger

This commit is contained in:
LittleSheep 2025-03-11 21:51:14 +08:00
parent abb0295858
commit 15e38a9baf
2 changed files with 60 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
func MapAPIs(app *fiber.App, baseURL string) { func MapAPIs(app *fiber.App, baseURL string) {
api := app.Group(baseURL).Name("API") api := app.Group(baseURL).Name("API")
{ {
api.Get("/webfinger", getWebfinger)
activitypub := api.Group("/activitypub").Name("ActivityPub API") activitypub := api.Group("/activitypub").Name("ActivityPub API")
{ {
activitypub.Post("/users/:name/inbox", apUserInbox) activitypub.Post("/users/:name/inbox", apUserInbox)

View File

@ -0,0 +1,59 @@
package api
import (
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
"github.com/gofiber/fiber/v2"
)
type WebFingerResponse struct {
Subject string `json:"subject"`
Aliases []string `json:"aliases"`
Links []struct {
Rel string `json:"rel"`
Type string `json:"type,omitempty"`
Href string `json:"href"`
} `json:"links"`
}
// Although this webfinger is desgined for users
// But in this case we will provide publisher for them
func getWebfinger(c *fiber.Ctx) error {
resource := c.Query("resource")
if len(resource) < 6 || resource[:5] != "acct:" {
return c.Status(400).JSON(fiber.Map{"error": "Invalid resource format"})
}
username := resource[5:]
if username == "" {
return c.Status(400).JSON(fiber.Map{"error": "Invalid username"})
}
var publisher models.Publisher
if err := database.C.Where("name = ?", username).First(&publisher).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
response := WebFingerResponse{
Subject: "acct:" + username,
Aliases: []string{
services.GetActivityID("/users/" + publisher.Name).String(),
},
Links: []struct {
Rel string `json:"rel"`
Type string `json:"type,omitempty"`
Href string `json:"href"`
}{
{
Rel: "self",
Type: "application/activity+json",
Href: services.GetActivityID("/users/" + publisher.Name).String(),
},
// TODO Add avatar here
},
}
return c.JSON(response)
}