♻️ Refactor all ListPost to versionable

This commit is contained in:
LittleSheep 2025-03-30 14:30:44 +08:00
parent 79b12624d8
commit 37dc92dc43
8 changed files with 34 additions and 14 deletions

View File

@ -65,7 +65,7 @@ func apUserOutbox(c *fiber.Ctx) error {
}
var activities []activitypub.Item
if posts, err := services.ListPostV1(tx, limit, (page-1)*limit, "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())
} else {
for _, post := range posts {

View File

@ -113,7 +113,7 @@ func searchPost(c *fiber.Ctx) error {
if c.Get("X-API-Version", "1") == "2" {
items, err = queries.ListPost(tx, take, offset, "published_at DESC", userId)
} else {
items, err = services.ListPostV1(tx, take, offset, "published_at DESC", userId)
items, err = services.ListPost(tx, take, offset, "published_at DESC", userId)
}
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
@ -159,7 +159,7 @@ func listPost(c *fiber.Ctx) error {
if c.Get("X-API-Version", "1") == "2" {
items, err = queries.ListPost(tx, take, offset, "published_at DESC", userId)
} else {
items, err = services.ListPostV1(tx, take, offset, "published_at DESC", userId)
items, err = services.ListPost(tx, take, offset, "published_at DESC", userId)
}
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
@ -242,7 +242,7 @@ func listDraftPost(c *fiber.Ctx) error {
if c.Get("X-API-Version", "1") == "2" {
items, err = queries.ListPost(tx, take, offset, "published_at DESC", userId)
} else {
items, err = services.ListPostV1(tx, take, offset, "published_at DESC", userId)
items, err = services.ListPost(tx, take, offset, "published_at DESC", userId)
}
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())

View File

@ -33,7 +33,7 @@ func listPinnedPost(c *fiber.Ctx) error {
userId = &user.ID
}
items, err := services.ListPostV1(tx, 100, 0, "published_at DESC", userId)
items, err := services.ListPost(tx, 100, 0, "published_at DESC", userId)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

View File

@ -6,6 +6,7 @@ import (
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
"git.solsynth.dev/hypernet/interactive/pkg/internal/services/queries"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
@ -29,7 +30,13 @@ func listRecommendation(c *fiber.Ctx) error {
}
tx := database.C.Where("id IN ?", postIdx)
newPosts, err := services.ListPostV1(tx, featuredMax, 0, "id ASC", userId)
var newPosts []models.Post
var err error
if c.Get("X-API-Version", "1") == "2" {
newPosts, err = queries.ListPost(tx, featuredMax, 0, "id ASC", userId)
} else {
newPosts, err = services.ListPost(tx, featuredMax, 0, "id ASC", userId)
}
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
@ -67,7 +74,13 @@ func listRecommendationShuffle(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
items, err := services.ListPostV1(tx, take, offset, "RANDOM()", userId)
var items []models.Post
var err error
if c.Get("X-API-Version", "1") == "2" {
items, err = queries.ListPost(tx, take, offset, "RANDOM()", userId)
} else {
items, err = services.ListPost(tx, take, offset, "RANDOM()", userId)
}
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

View File

@ -47,7 +47,7 @@ func listPostReplies(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
items, err := services.ListPostV1(tx, take, offset, "published_at DESC", userId)
items, err := services.ListPost(tx, take, offset, "published_at DESC", userId)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
@ -90,7 +90,7 @@ func listPostFeaturedReply(c *fiber.Ctx) error {
tx = services.FilterPostWithTag(tx, c.Query("tag"))
}
items, err := services.ListPostV1(tx, take, 0, "(COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC, published_at DESC", userId)
items, err := services.ListPost(tx, take, 0, "(COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC, published_at DESC", userId)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

View File

@ -40,7 +40,7 @@ func getWhatsNew(c *fiber.Ctx) error {
order = "published_at DESC, (COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC"
}
items, err := services.ListPostV1(tx, 10, 0, order, userId)
items, err := services.ListPost(tx, 10, 0, order, userId)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

View File

@ -10,6 +10,7 @@ import (
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/interactive/pkg/internal/services/queries"
"git.solsynth.dev/hypernet/interactive/pkg/proto"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"github.com/gofiber/fiber/v2"
@ -44,7 +45,7 @@ func GetFeed(c *fiber.Ctx, limit int, user *uint, cursor *time.Time) ([]FeedEntr
if cursor != nil {
interTx = interTx.Where("published_at < ?", *cursor)
}
interPosts, err := ListPostForFeed(interTx, interCount, user)
interPosts, err := ListPostForFeed(interTx, interCount, user, c.Get("X-API-Version", "1"))
if err != nil {
return nil, fmt.Errorf("failed to load interactive posts: %v", err)
}
@ -78,8 +79,14 @@ func GetFeed(c *fiber.Ctx, limit int, user *uint, cursor *time.Time) ([]FeedEntr
// We assume the database context already handled the filtering and pagination
// Only manage to pulling the content only
func ListPostForFeed(tx *gorm.DB, limit int, user *uint) ([]FeedEntry, error) {
posts, err := ListPostV1(tx, limit, -1, "published_at DESC", user)
func ListPostForFeed(tx *gorm.DB, limit int, user *uint, api string) ([]FeedEntry, error) {
var posts []models.Post
var err error
if api == "2" {
posts, err = queries.ListPost(tx, limit, -1, "published_at DESC", user)
} else {
posts, err = ListPost(tx, limit, -1, "published_at DESC", user)
}
if err != nil {
return nil, err
}

View File

@ -396,7 +396,7 @@ func CountPostReactions(id uint) int64 {
return count
}
func ListPostV1(tx *gorm.DB, take int, offset int, order any, user *uint, noReact ...bool) ([]models.Post, error) {
func ListPost(tx *gorm.DB, take int, offset int, order any, user *uint, noReact ...bool) ([]models.Post, error) {
if take > 100 {
take = 100
}