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

180 lines
4.4 KiB
Go
Raw Normal View History

2024-07-23 08:12:19 +00:00
package api
import (
"fmt"
2024-07-23 08:12:19 +00:00
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
2024-07-27 17:49:16 +00:00
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
2024-07-23 08:12:19 +00:00
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
2024-07-23 08:12:19 +00:00
)
func listRecommendationNews(c *fiber.Ctx) error {
2024-07-23 08:12:19 +00:00
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
realm := c.Query("realm")
2024-07-23 08:12:19 +00:00
tx := services.FilterPostDraft(database.C)
2024-07-27 17:49:16 +00:00
if user, authenticated := c.Locals("user").(models.Account); authenticated {
tx = services.FilterPostWithUserContext(tx, &user)
} else {
tx = services.FilterPostWithUserContext(tx, nil)
}
2024-07-27 17:49:16 +00:00
if len(realm) > 0 {
if realm, err := services.GetRealmWithAlias(realm); err != nil {
2024-07-27 17:49:16 +00:00
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
} else {
tx = services.FilterPostWithRealm(tx, realm.ID)
}
}
if c.QueryBool("noReply", true) {
tx = services.FilterPostReply(tx)
}
2024-07-27 17:49:16 +00:00
countTx := tx
count, err := services.CountPost(countTx)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
order := "published_at DESC"
if c.QueryBool("featured", false) {
order = "published_at DESC, (COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC"
}
items, err := services.ListPost(tx, take, offset, order)
2024-07-27 17:49:16 +00:00
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))
}
}
}
2024-07-27 17:49:16 +00:00
return c.JSON(fiber.Map{
"count": count,
"data": items,
})
}
func listRecommendationFriends(c *fiber.Ctx) error {
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
2024-07-27 17:49:16 +00:00
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
realm := c.Query("realm")
2024-07-27 17:49:16 +00:00
tx := services.FilterPostDraft(database.C)
tx = services.FilterPostWithUserContext(tx, &user)
2024-07-27 17:49:16 +00:00
friends, _ := services.ListAccountFriends(user)
friendList := lo.Map(friends, func(item models.Account, index int) uint {
return item.ID
})
tx = tx.Where("author_id IN ?", friendList)
2024-07-27 17:49:16 +00:00
if len(realm) > 0 {
if realm, err := services.GetRealmWithAlias(realm); err != nil {
2024-07-23 08:12:19 +00:00
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
} else {
tx = services.FilterPostWithRealm(tx, realm.ID)
}
}
if c.QueryBool("noReply", true) {
tx = services.FilterPostReply(tx)
}
2024-07-23 08:12:19 +00:00
countTx := tx
count, err := services.CountPost(countTx)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
order := "published_at DESC"
if c.QueryBool("featured", false) {
order = "published_at DESC, (COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC"
}
items, err := services.ListPost(tx, take, offset, order)
2024-07-23 08:12:19 +00:00
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))
}
}
}
2024-07-23 08:12:19 +00:00
return c.JSON(fiber.Map{
"count": count,
"data": items,
})
}
func listRecommendationShuffle(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
realm := c.Query("realm")
2024-07-23 08:12:19 +00:00
tx := services.FilterPostDraft(database.C)
2024-07-27 17:49:16 +00:00
if user, authenticated := c.Locals("user").(models.Account); authenticated {
tx = services.FilterPostWithUserContext(tx, &user)
} else {
tx = services.FilterPostWithUserContext(tx, nil)
}
2024-07-27 17:49:16 +00:00
if len(realm) > 0 {
if realm, err := services.GetRealmWithAlias(realm); err != nil {
2024-07-23 08:12:19 +00:00
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
} else {
tx = services.FilterPostWithRealm(tx, realm.ID)
}
}
if c.QueryBool("noReply", true) {
tx = services.FilterPostReply(tx)
}
2024-07-23 08:12:19 +00:00
countTx := tx
count, err := services.CountPost(countTx)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
2024-07-24 06:37:21 +00:00
items, err := services.ListPost(tx, take, offset, "RANDOM()")
2024-07-23 08:12:19 +00:00
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))
}
}
}
2024-07-23 08:12:19 +00:00
return c.JSON(fiber.Map{
"count": count,
"data": items,
})
}