From 37dc92dc43712be0b23f9d0b7ed24fbaa6305eda Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 30 Mar 2025 14:30:44 +0800 Subject: [PATCH] :recycle: Refactor all ListPost to versionable --- pkg/internal/http/api/activitypub_api.go | 2 +- pkg/internal/http/api/posts_api.go | 6 +++--- pkg/internal/http/api/publishers_api.go | 2 +- pkg/internal/http/api/recommendation_api.go | 17 +++++++++++++++-- pkg/internal/http/api/replies_api.go | 4 ++-- pkg/internal/http/api/what_new_api.dart.go | 2 +- pkg/internal/services/feed.go | 13 ++++++++++--- pkg/internal/services/posts.go | 2 +- 8 files changed, 34 insertions(+), 14 deletions(-) diff --git a/pkg/internal/http/api/activitypub_api.go b/pkg/internal/http/api/activitypub_api.go index f902288..6a267f5 100644 --- a/pkg/internal/http/api/activitypub_api.go +++ b/pkg/internal/http/api/activitypub_api.go @@ -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 { diff --git a/pkg/internal/http/api/posts_api.go b/pkg/internal/http/api/posts_api.go index 956cb0f..005b2a1 100644 --- a/pkg/internal/http/api/posts_api.go +++ b/pkg/internal/http/api/posts_api.go @@ -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()) diff --git a/pkg/internal/http/api/publishers_api.go b/pkg/internal/http/api/publishers_api.go index a2c8d1d..0ac4d1f 100644 --- a/pkg/internal/http/api/publishers_api.go +++ b/pkg/internal/http/api/publishers_api.go @@ -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()) } diff --git a/pkg/internal/http/api/recommendation_api.go b/pkg/internal/http/api/recommendation_api.go index a73b711..67a3817 100644 --- a/pkg/internal/http/api/recommendation_api.go +++ b/pkg/internal/http/api/recommendation_api.go @@ -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()) } diff --git a/pkg/internal/http/api/replies_api.go b/pkg/internal/http/api/replies_api.go index 25d13e9..c0b73da 100644 --- a/pkg/internal/http/api/replies_api.go +++ b/pkg/internal/http/api/replies_api.go @@ -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()) } diff --git a/pkg/internal/http/api/what_new_api.dart.go b/pkg/internal/http/api/what_new_api.dart.go index e7b9e05..3431f82 100644 --- a/pkg/internal/http/api/what_new_api.dart.go +++ b/pkg/internal/http/api/what_new_api.dart.go @@ -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()) } diff --git a/pkg/internal/services/feed.go b/pkg/internal/services/feed.go index 1ff8b80..3805fbb 100644 --- a/pkg/internal/services/feed.go +++ b/pkg/internal/services/feed.go @@ -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 } diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index a0bcd09..fe64628 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -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 }