✨ Activitypub outbox pagination
This commit is contained in:
parent
e127a72850
commit
abb0295858
@ -1,7 +1,9 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
@ -39,20 +41,30 @@ func apUserInbox(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
func apUserOutbox(c *fiber.Ctx) error {
|
func apUserOutbox(c *fiber.Ctx) error {
|
||||||
name := c.Params("name")
|
name := c.Params("name")
|
||||||
|
page := c.QueryInt("page", 1)
|
||||||
|
limit := c.QueryInt("limit", 10)
|
||||||
|
|
||||||
|
if limit > 100 {
|
||||||
|
limit = 100
|
||||||
|
}
|
||||||
|
|
||||||
var publisher models.Publisher
|
var publisher models.Publisher
|
||||||
if err := database.C.Where("name = ?", name).First(&publisher).Error; err != nil {
|
if err := database.C.Where("name = ?", name).First(&publisher).Error; err != nil {
|
||||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
take := 50
|
|
||||||
tx, err := UniversalPostFilter(c, database.C)
|
tx, err := UniversalPostFilter(c, database.C)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
count, err := services.CountPost(tx)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
var activities []activitypub.Item
|
var activities []activitypub.Item
|
||||||
if posts, err := services.ListPost(tx, take, 0, "published_at DESC", nil); err != nil {
|
if posts, err := services.ListPost(tx, limit, (page-1)*limit, "published_at DESC", nil); err != nil {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
} else {
|
} else {
|
||||||
for _, post := range posts {
|
for _, post := range posts {
|
||||||
@ -93,11 +105,22 @@ func apUserOutbox(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
outbox := activitypub.OrderedCollection{
|
totalPages := int(math.Ceil(float64(count) / float64(limit)))
|
||||||
|
|
||||||
|
outbox := activitypub.OrderedCollectionPage{
|
||||||
ID: services.GetActivityID("/users/" + publisher.Name + "/outbox"),
|
ID: services.GetActivityID("/users/" + publisher.Name + "/outbox"),
|
||||||
Type: activitypub.OrderedCollectionType,
|
Type: activitypub.OrderedCollectionType,
|
||||||
TotalItems: uint(min(take, len(activities))),
|
TotalItems: uint(count),
|
||||||
OrderedItems: activitypub.ItemCollection(activities),
|
OrderedItems: activitypub.ItemCollection(activities),
|
||||||
|
First: services.GetActivityIRI(fmt.Sprintf("/users/%s/outbox?page=%d", publisher.Name, 1)),
|
||||||
|
Last: services.GetActivityIRI(fmt.Sprintf("/users/%s/outbox?page=%d", publisher.Name, totalPages)),
|
||||||
|
}
|
||||||
|
|
||||||
|
if page > 1 {
|
||||||
|
outbox.Prev = services.GetActivityIRI(fmt.Sprintf("/users/%s/outbox?page=%d&limit=%d", publisher.Name, page-1, limit))
|
||||||
|
}
|
||||||
|
if page < totalPages {
|
||||||
|
outbox.Next = services.GetActivityIRI(fmt.Sprintf("/users/%s/outbox?page=%d&limit=%d", publisher.Name, page+1, limit))
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(outbox)
|
return c.JSON(outbox)
|
||||||
|
@ -5,7 +5,7 @@ grpc_bind = "0.0.0.0:7005"
|
|||||||
|
|
||||||
nexus_addr = "localhost:7001"
|
nexus_addr = "localhost:7001"
|
||||||
|
|
||||||
activitypub_base_url = "https://solsynth.dev/activitypub"
|
activitypub_base_url = "https://api.sn.solsynth.dev/cgi/co/activitypub"
|
||||||
|
|
||||||
[debug]
|
[debug]
|
||||||
database = true
|
database = true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user