⚗️ Play with activitypub
This commit is contained in:
parent
60fa7a5bff
commit
db59826253
2
go.mod
2
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
|
||||
|
2
go.sum
2
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=
|
||||
|
92
pkg/internal/http/api/activitypub_api.go
Normal file
92
pkg/internal/http/api/activitypub_api.go
Normal file
@ -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)
|
||||
}
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user