Interactive/pkg/internal/server/api/feed_api.go

132 lines
3.2 KiB
Go
Raw Normal View History

package api
import (
"fmt"
2024-07-06 13:40:50 +00:00
"time"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
2024-07-09 13:07:46 +00:00
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
"github.com/gofiber/fiber/v2"
2024-07-06 13:40:50 +00:00
jsoniter "github.com/json-iterator/go"
)
2024-07-06 13:40:50 +00:00
type FeedRecord struct {
Type string `json:"type"`
Data map[string]any `json:"data"`
CreatedAt time.Time `json:"created_at"`
}
func listFeed(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
2024-05-15 11:45:49 +00:00
realmId := c.QueryInt("realmId", 0)
2024-07-06 13:40:50 +00:00
postTx := services.FilterPostDraft(database.C)
2024-05-15 11:45:49 +00:00
if realmId > 0 {
if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
} else {
2024-07-06 13:40:50 +00:00
postTx = services.FilterPostWithRealm(postTx, realm.ID)
}
}
if len(c.Query("authorId")) > 0 {
2024-05-15 11:45:49 +00:00
var author models.Account
if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
2024-07-06 13:40:50 +00:00
postTx = postTx.Where("author_id = ?", author.ID)
}
2024-05-15 11:45:49 +00:00
if len(c.Query("category")) > 0 {
2024-07-06 13:40:50 +00:00
postTx = services.FilterPostWithCategory(postTx, c.Query("category"))
}
2024-05-15 11:45:49 +00:00
if len(c.Query("tag")) > 0 {
2024-07-06 13:40:50 +00:00
postTx = services.FilterPostWithTag(postTx, c.Query("tag"))
2024-03-03 14:57:17 +00:00
}
2024-07-06 13:40:50 +00:00
postCountTx := postTx
2024-07-09 13:07:46 +00:00
postCount, err := services.CountPost(postCountTx)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
postItems, err := services.ListPost(postTx, take, offset)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
var feed []FeedRecord
encodeToFeed := func(t string, in any, createdAt time.Time) FeedRecord {
var result map[string]any
raw, _ := jsoniter.Marshal(in)
2024-07-21 06:47:51 +00:00
_ = jsoniter.Unmarshal(raw, &result)
2024-07-09 13:07:46 +00:00
return FeedRecord{
Type: t,
Data: result,
CreatedAt: createdAt,
}
}
for _, post := range postItems {
feed = append(feed, encodeToFeed("post", post, post.CreatedAt))
}
return c.JSON(fiber.Map{
2024-07-21 06:47:51 +00:00
"count": postCount,
2024-07-09 13:07:46 +00:00
"data": feed,
})
}
func listDraftMixed(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
postTx := services.FilterPostWithAuthorDraft(database.C, user.ID)
postCountTx := postTx
2024-07-06 13:40:50 +00:00
postCount, err := services.CountPost(postCountTx)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
2024-03-10 10:38:42 +00:00
2024-07-06 13:40:50 +00:00
postItems, err := services.ListPost(postTx, take, offset)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
2024-03-03 13:24:08 +00:00
2024-07-06 13:40:50 +00:00
var feed []FeedRecord
encodeToFeed := func(t string, in any, createdAt time.Time) FeedRecord {
var result map[string]any
raw, _ := jsoniter.Marshal(in)
2024-07-21 06:47:51 +00:00
_ = jsoniter.Unmarshal(raw, &result)
2024-07-06 13:40:50 +00:00
return FeedRecord{
Type: t,
Data: result,
CreatedAt: createdAt,
}
}
for _, post := range postItems {
feed = append(feed, encodeToFeed("post", post, post.CreatedAt))
}
2024-03-03 13:24:08 +00:00
return c.JSON(fiber.Map{
2024-07-21 06:47:51 +00:00
"count": postCount,
2024-07-07 08:26:50 +00:00
"data": feed,
2024-03-03 13:24:08 +00:00
})
}