2024-07-23 08:12:19 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
2024-08-18 16:04:49 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
2024-07-23 08:12:19 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
2024-10-31 14:41:32 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit"
|
|
|
|
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-07-23 08:12:19 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-08-18 16:04:49 +00:00
|
|
|
"github.com/samber/lo"
|
2024-07-23 08:12:19 +00:00
|
|
|
)
|
|
|
|
|
2024-08-18 16:04:49 +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)
|
2024-07-27 17:49:16 +00:00
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
tx := database.C
|
2024-07-27 17:49:16 +00:00
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
var err error
|
|
|
|
if tx, err = universalPostFilter(c, tx); err != nil {
|
|
|
|
return err
|
2024-09-16 13:24:48 +00:00
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2024-08-18 16:04:49 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2024-10-13 12:12:23 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-18 16:04:49 +00:00
|
|
|
func listRecommendationFriends(c *fiber.Ctx) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if err := sec.EnsureAuthenticated(c); err != nil {
|
2024-08-18 16:04:49 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
user := c.Locals("user").(authm.Account)
|
2024-08-18 16:04:49 +00:00
|
|
|
|
2024-07-27 17:49:16 +00:00
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
tx := database.C
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if tx, err = universalPostFilter(c, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-27 17:49:16 +00:00
|
|
|
|
2024-10-31 14:41:32 +00:00
|
|
|
friends, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipFriend), true)
|
|
|
|
friendList := lo.Map(friends, func(item *proto.UserInfo, index int) uint {
|
|
|
|
return uint(item.GetId())
|
2024-08-18 16:04:49 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
tx = tx.Where("author_id IN ?", friendList)
|
2024-07-27 17:49:16 +00:00
|
|
|
|
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-08-18 16:04:49 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2024-10-13 12:12:23 +00:00
|
|
|
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)
|
2024-07-27 17:49:16 +00:00
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
tx := database.C
|
2024-07-27 17:49:16 +00:00
|
|
|
|
2024-10-13 13:25:15 +00:00
|
|
|
var err error
|
|
|
|
if tx, err = universalPostFilter(c, tx); err != nil {
|
|
|
|
return err
|
2024-09-16 13:24:48 +00:00
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2024-10-13 12:12:23 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|