owl-blogs/web/activity_pub_handler.go

304 lines
8.0 KiB
Go
Raw Normal View History

2023-07-22 18:34:17 +00:00
package web
import (
2024-05-13 17:03:21 +00:00
"errors"
"log/slog"
"net/http"
2023-07-26 19:28:57 +00:00
"net/url"
"owl-blogs/app"
2024-05-17 18:23:38 +00:00
"strings"
2023-07-22 18:34:17 +00:00
2023-07-26 19:28:57 +00:00
vocab "github.com/go-ap/activitypub"
2024-02-26 19:17:53 +00:00
"github.com/go-ap/jsonld"
2023-07-26 19:28:57 +00:00
2023-07-22 18:34:17 +00:00
"github.com/gofiber/fiber/v2"
2024-05-13 17:03:21 +00:00
"github.com/gofiber/fiber/v2/middleware/adaptor"
2023-07-22 18:34:17 +00:00
)
type ActivityPubServer struct {
2024-05-14 19:23:37 +00:00
siteConfigService *app.SiteConfigService
apService *app.ActivityPubService
entryService *app.EntryService
2023-07-22 18:34:17 +00:00
}
type WebfingerResponse struct {
2023-07-26 19:28:57 +00:00
Subject string `json:"subject"`
2024-02-26 19:17:53 +00:00
Aliases []string `json:"aliases"`
2023-07-26 19:28:57 +00:00
Links []WebfingerLink `json:"links"`
2023-07-22 18:34:17 +00:00
}
2023-07-26 19:28:57 +00:00
type WebfingerLink struct {
2023-07-22 18:34:17 +00:00
Rel string `json:"rel"`
Type string `json:"type"`
Href string `json:"href"`
}
2024-05-14 19:23:37 +00:00
func NewActivityPubServer(siteConfigService *app.SiteConfigService, entryService *app.EntryService, apService *app.ActivityPubService) *ActivityPubServer {
2023-07-22 18:34:17 +00:00
return &ActivityPubServer{
2024-05-14 19:23:37 +00:00
siteConfigService: siteConfigService,
entryService: entryService,
apService: apService,
2023-07-22 18:34:17 +00:00
}
}
func (s *ActivityPubServer) HandleWebfinger(ctx *fiber.Ctx) error {
2024-05-14 19:23:37 +00:00
siteConfig, _ := s.siteConfigService.GetSiteConfig()
apConfig, _ := s.apService.GetApConfig()
2023-07-22 18:34:17 +00:00
2024-02-26 19:17:53 +00:00
domain, err := url.Parse(siteConfig.FullUrl)
if err != nil {
return err
}
subject := ctx.Query("resource", "")
2024-05-14 19:23:37 +00:00
blogSubject := "acct:" + apConfig.PreferredUsername + "@" + domain.Host
slog.Info("webfinger request", "for", subject, "required", blogSubject)
if subject != blogSubject {
2024-02-26 19:17:53 +00:00
return ctx.Status(404).JSON(nil)
}
2023-07-22 18:34:17 +00:00
webfinger := WebfingerResponse{
2024-02-26 19:17:53 +00:00
Subject: subject,
2023-07-22 18:34:17 +00:00
2023-07-26 19:28:57 +00:00
Links: []WebfingerLink{
2023-07-22 18:34:17 +00:00
{
Rel: "self",
Type: "application/activity+json",
2024-05-17 18:23:38 +00:00
Href: s.apService.ActorUrl(),
2023-07-22 18:34:17 +00:00
},
},
}
return ctx.JSON(webfinger)
}
func (s *ActivityPubServer) Router(router fiber.Router) {
2023-07-26 19:28:57 +00:00
router.Get("/outbox", s.HandleOutbox)
2024-05-16 18:27:06 +00:00
router.Post("/inbox", s.HandleInbox)
2024-05-13 17:03:21 +00:00
router.Get("/followers", s.HandleFollowers)
2023-07-22 18:34:17 +00:00
}
func (s *ActivityPubServer) HandleActor(ctx *fiber.Ctx) error {
2024-05-18 11:10:14 +00:00
accepts := (strings.Contains(string(ctx.Request().Header.Peek("Accept")), "application/activity+json") ||
strings.Contains(string(ctx.Request().Header.Peek("Accept")), "application/ld+json"))
req_content := (strings.Contains(string(ctx.Request().Header.Peek("Content-Type")), "application/activity+json") ||
strings.Contains(string(ctx.Request().Header.Peek("Content-Type")), "application/ld+json"))
2024-05-17 18:23:38 +00:00
if !accepts && !req_content {
return ctx.Next()
}
2024-05-14 19:23:37 +00:00
apConfig, _ := s.apService.GetApConfig()
2023-07-22 18:34:17 +00:00
2024-05-17 18:23:38 +00:00
actor := vocab.PersonNew(vocab.IRI(s.apService.ActorUrl()))
2023-07-26 19:28:57 +00:00
actor.PreferredUsername = vocab.NaturalLanguageValues{{Value: vocab.Content(apConfig.PreferredUsername)}}
2024-05-17 18:23:38 +00:00
actor.Inbox = vocab.IRI(s.apService.InboxUrl())
actor.Outbox = vocab.IRI(s.apService.OutboxUrl())
actor.Followers = vocab.IRI(s.apService.FollowersUrl())
2023-07-26 19:28:57 +00:00
actor.PublicKey = vocab.PublicKey{
2024-05-17 18:23:38 +00:00
ID: vocab.IRI(s.apService.MainKeyUri()),
Owner: vocab.IRI(s.apService.ActorUrl()),
2023-07-26 19:28:57 +00:00
PublicKeyPem: apConfig.PublicKeyPem,
}
2024-05-18 11:10:14 +00:00
actor.Name = vocab.NaturalLanguageValues{{Value: vocab.Content(s.apService.ActorName())}}
actor.Icon = s.apService.ActorIcon()
actor.Summary = vocab.NaturalLanguageValues{{Value: vocab.Content(s.apService.ActorSummary())}}
2024-02-26 19:17:53 +00:00
data, err := jsonld.WithContext(
jsonld.IRI(vocab.ActivityBaseURI),
jsonld.IRI(vocab.SecurityContextURI),
).Marshal(actor)
2023-07-26 19:28:57 +00:00
if err != nil {
return err
}
ctx.Set("Content-Type", "application/activity+json")
return ctx.Send(data)
}
func (s *ActivityPubServer) HandleOutbox(ctx *fiber.Ctx) error {
2024-05-14 19:23:37 +00:00
siteConfig, _ := s.siteConfigService.GetSiteConfig()
// apConfig, _ := s.apService.GetApConfig()
2023-07-26 19:28:57 +00:00
entries, err := s.entryService.FindAllByType(nil, true, false)
if err != nil {
return err
}
items := make([]vocab.Item, len(entries))
for i, entry := range entries {
url, _ := url.JoinPath(siteConfig.FullUrl, "/posts/"+entry.ID()+"/")
items[i] = *vocab.ActivityNew(vocab.IRI(url), vocab.CreateType, vocab.Object{
ID: vocab.ID(url),
Type: vocab.ArticleType,
Content: vocab.NaturalLanguageValues{
{Value: vocab.Content(entry.Content())},
},
})
}
2024-05-17 18:23:38 +00:00
outbox := vocab.OrderedCollectionNew(vocab.IRI(s.apService.OutboxUrl()))
2023-07-26 19:28:57 +00:00
outbox.TotalItems = uint(len(items))
outbox.OrderedItems = items
data, err := outbox.MarshalJSON()
if err != nil {
return err
2023-07-22 18:34:17 +00:00
}
2023-07-26 19:28:57 +00:00
ctx.Set("Content-Type", "application/activity+json")
return ctx.Send(data)
2024-05-13 17:03:21 +00:00
}
func (s *ActivityPubServer) processFollow(r *http.Request, act *vocab.Activity) error {
follower := act.Actor.GetID().String()
err := s.apService.VerifySignature(r, follower)
if err != nil {
2024-05-16 18:27:06 +00:00
slog.Error("wrong signature", "err", err)
2024-05-13 17:03:21 +00:00
return err
}
err = s.apService.AddFollower(follower)
if err != nil {
return err
}
2024-05-16 19:11:02 +00:00
go s.apService.Accept(act)
2024-05-13 17:03:21 +00:00
return nil
}
2024-05-16 18:27:06 +00:00
func (s *ActivityPubServer) processUndo(r *http.Request, act *vocab.Activity) error {
2024-05-17 20:37:18 +00:00
sender := act.Actor.GetID().String()
err := s.apService.VerifySignature(r, sender)
return vocab.OnObject(act.Object, func(o *vocab.Object) error {
if o.Type == vocab.FollowType {
if err != nil {
slog.Error("wrong signature", "err", err)
return err
}
err = s.apService.RemoveFollower(sender)
if err != nil {
return err
}
go s.apService.Accept(act)
return nil
}
if o.Type == vocab.LikeType {
return s.apService.RemoveLike(o.ID.String())
}
if o.Type == vocab.AnnounceType {
return s.apService.RemoveRepost(o.ID.String())
}
2024-05-17 20:37:18 +00:00
slog.Warn("unsupporeted object type for undo", "object", o)
return errors.New("unsupporeted object type")
})
}
func (s *ActivityPubServer) processLike(r *http.Request, act *vocab.Activity) error {
sender := act.Actor.GetID().String()
liked := act.Object.GetID().String()
err := s.apService.VerifySignature(r, sender)
2024-05-16 18:27:06 +00:00
if err != nil {
slog.Error("wrong signature", "err", err)
return err
}
2024-05-17 20:37:18 +00:00
err = s.apService.AddLike(sender, liked, act.ID.String())
2024-05-16 18:27:06 +00:00
if err != nil {
2024-05-17 20:37:18 +00:00
slog.Error("error saving like", "err", err)
2024-05-16 18:27:06 +00:00
return err
}
2024-05-17 19:05:13 +00:00
go s.apService.Accept(act)
2024-05-13 17:03:21 +00:00
return nil
}
func (s *ActivityPubServer) processAnnounce(r *http.Request, act *vocab.Activity) error {
sender := act.Actor.GetID().String()
liked := act.Object.GetID().String()
err := s.apService.VerifySignature(r, sender)
if err != nil {
slog.Error("wrong signature", "err", err)
return err
}
err = s.apService.AddRepost(sender, liked, act.ID.String())
if err != nil {
slog.Error("error saving like", "err", err)
return err
}
go s.apService.Accept(act)
return nil
}
func (s *ActivityPubServer) processDelete(r *http.Request, act *vocab.Activity) error {
return vocab.OnObject(act.Object, func(o *vocab.Object) error {
slog.Warn("Not processing delete", "action", act, "object", o)
return nil
})
}
2024-05-13 17:03:21 +00:00
func (s *ActivityPubServer) HandleInbox(ctx *fiber.Ctx) error {
body := ctx.Request().Body()
data, err := vocab.UnmarshalJSON(body)
if err != nil {
2024-05-16 18:27:06 +00:00
slog.Error("failed to parse request body", "body", body, "err", err)
2024-05-13 17:03:21 +00:00
return err
}
err = vocab.OnActivity(data, func(act *vocab.Activity) error {
slog.Info("activity retrieved", "activity", act, "type", act.Type)
r, err := adaptor.ConvertRequest(ctx, true)
if err != nil {
return err
}
if act.Type == vocab.FollowType {
return s.processFollow(r, act)
}
if act.Type == vocab.UndoType {
2024-05-16 18:27:06 +00:00
return s.processUndo(r, act)
2024-05-13 17:03:21 +00:00
}
if act.Type == vocab.DeleteType {
return s.processDelete(r, act)
}
2024-05-17 20:37:18 +00:00
if act.Type == vocab.LikeType {
return s.processLike(r, act)
}
if act.Type == vocab.AnnounceType {
return s.processAnnounce(r, act)
}
2024-05-17 20:37:18 +00:00
slog.Warn("Unsupported action", "body", body)
2024-05-13 17:03:21 +00:00
return errors.New("only follow and undo actions supported")
})
return err
}
func (s *ActivityPubServer) HandleFollowers(ctx *fiber.Ctx) error {
fs, err := s.apService.AllFollowers()
if err != nil {
return err
}
followers := vocab.Collection{}
for _, f := range fs {
followers.Append(vocab.IRI(f))
}
followers.TotalItems = uint(len(fs))
2024-05-17 18:23:38 +00:00
followers.ID = vocab.IRI(s.apService.FollowersUrl())
2024-05-13 17:03:21 +00:00
data, err := jsonld.WithContext(
jsonld.IRI(vocab.ActivityBaseURI),
).Marshal(followers)
if err != nil {
return err
}
ctx.Set("Content-Type", "application/activity+json")
return ctx.Send(data)
2023-07-22 18:34:17 +00:00
}