diff --git a/go.mod b/go.mod index 9fd92b1..d7d9c27 100644 --- a/go.mod +++ b/go.mod @@ -36,7 +36,7 @@ require ( github.com/dustin/go-humanize v1.0.1 // indirect github.com/fsnotify/fsnotify v1.8.0 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect - github.com/go-ap/activitypub v0.0.0-20250124194921-d52b4c694e14 // indirect + github.com/go-ap/activitypub v0.0.0-20250212090640-aeb6499ba581 // indirect github.com/go-ap/errors v0.0.0-20250124135319-3da8adefd4a9 // indirect github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73 // indirect github.com/go-playground/locales v0.14.1 // indirect diff --git a/go.sum b/go.sum index b694f92..ccd26e4 100644 --- a/go.sum +++ b/go.sum @@ -99,6 +99,8 @@ github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uq github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk= github.com/go-ap/activitypub v0.0.0-20250124194921-d52b4c694e14 h1:4VkepceDBxPt9BwsHncwtwIZCCgCuxctFHfosz8aWQA= github.com/go-ap/activitypub v0.0.0-20250124194921-d52b4c694e14/go.mod h1:IO2PtAsxfGXN5IHrPuOslENFbq7MprYLNOyiiOELoRQ= +github.com/go-ap/activitypub v0.0.0-20250212090640-aeb6499ba581 h1:73sFEdBsWBTBut0aDMPgt8HRuMO+ML0fd8AA/zjO8BQ= +github.com/go-ap/activitypub v0.0.0-20250212090640-aeb6499ba581/go.mod h1:IO2PtAsxfGXN5IHrPuOslENFbq7MprYLNOyiiOELoRQ= github.com/go-ap/errors v0.0.0-20250124135319-3da8adefd4a9 h1:AJBGzuJVgfkKF3LoXCNQfH9yWmsVDV/oPDJE/zeXOjE= github.com/go-ap/errors v0.0.0-20250124135319-3da8adefd4a9/go.mod h1:Vkh+Z3f24K8nMsJKXo1FHn5ebPsXvB/WDH5JRtYqdNo= github.com/go-ap/jsonld v0.0.0-20221030091449-f2a191312c73 h1:GMKIYXyXPGIp+hYiWOhfqK4A023HdgisDT4YGgf99mw= diff --git a/pkg/internal/http/api/activitypub_api.go b/pkg/internal/http/api/activitypub_api.go new file mode 100644 index 0000000..9a829db --- /dev/null +++ b/pkg/internal/http/api/activitypub_api.go @@ -0,0 +1,92 @@ +package api + +import ( + "fmt" + "git.solsynth.dev/hypernet/interactive/pkg/internal/database" + "git.solsynth.dev/hypernet/interactive/pkg/internal/models" + "git.solsynth.dev/hypernet/interactive/pkg/internal/services" + vocab "github.com/go-ap/activitypub" + "github.com/gofiber/fiber/v2" + "github.com/samber/lo" + "time" +) + +func apGetPublisher(c *fiber.Ctx) error { + name := c.Params("name") + + var publisher models.Publisher + if err := database.C.Where("name = ?", name).First(&publisher).Error; err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + + url := vocab.ID("https://solsynth.dev/publishers/" + publisher.Name) + actor := vocab.Actor{ + ID: url, + Type: vocab.PersonType, + Name: vocab.DefaultNaturalLanguageValue(publisher.Nick), + URL: url, + Icon: vocab.Image{}, + } + + return c.JSON(actor) +} + +func apGetPost(c *fiber.Ctx) error { + take := c.QueryInt("take", 0) + offset := c.QueryInt("offset", 0) + + tx := database.C + + var err error + if tx, err = universalPostFilter(c, tx); err != nil { + return err + } + + items, err := services.ListPost(tx, take, offset, "published_at DESC") + if err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } + + if c.QueryBool("truncate", true) { + for _, item := range items { + if item != nil { + item = lo.ToPtr(services.TruncatePostContent(*item)) + } + } + } + + var acts []vocab.Activity + for _, item := range items { + pubUrl := vocab.ID("https://solsynth.dev/publishers/" + item.Publisher.Name) + url := fmt.Sprintf("https://solsynth.dev/posts/%d", item.ID) + content, ok := item.Body["content"].(string) + if !ok { + content = "Posted a post" + } + acts = append(acts, vocab.Activity{ + ID: vocab.ID(url), + Type: vocab.CreateType, + Actor: vocab.Actor{ + ID: pubUrl, + Type: vocab.PersonType, + Name: vocab.DefaultNaturalLanguageValue(item.Publisher.Nick), + URL: pubUrl, + Icon: vocab.Image{}, + }, + Object: vocab.Object{ + ID: vocab.ID(url), + Type: vocab.NoteType, + Name: vocab.DefaultNaturalLanguageValue(content), + URL: vocab.ID(url), + Icon: vocab.Image{}, + }, + Published: lo.TernaryF(item.PublishedAt != nil, func() time.Time { + return *item.PublishedAt + }, func() time.Time { + return item.CreatedAt + }), + }) + } + + return c.JSON(acts) +} diff --git a/pkg/internal/http/api/index.go b/pkg/internal/http/api/index.go index 5268925..f6e80ba 100644 --- a/pkg/internal/http/api/index.go +++ b/pkg/internal/http/api/index.go @@ -7,6 +7,12 @@ import ( func MapAPIs(app *fiber.App, baseURL string) { api := app.Group(baseURL).Name("API") { + activitypub := api.Group("/activitypub").Name("ActivityPub API") + { + activitypub.Get("/publishers/:name", apGetPublisher) + activitypub.Get("/posts", apGetPost) + } + publishers := api.Group("/publishers").Name("Publisher API") { publishers.Get("/", listRelatedPublisher)