From ae269dfb3b2f4e37677e2f4a1f341f0335af8979 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 19 Aug 2024 00:04:49 +0800 Subject: [PATCH] :boom: Use realm alias instead of external id --- pkg/internal/server/api/index.go | 2 +- pkg/internal/server/api/posts_api.go | 12 ++-- pkg/internal/server/api/recommendation_api.go | 70 ++++++++++--------- 3 files changed, 44 insertions(+), 40 deletions(-) diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index 31fac61..e25481e 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -14,7 +14,7 @@ func MapAPIs(app *fiber.App, baseURL string) { recommendations := api.Group("/recommendations").Name("Recommendations API") { recommendations.Get("/", listRecommendationNews) - recommendations.Get("/featured", listRecommendationFeatured) + recommendations.Get("/friends", listRecommendationFriends) recommendations.Get("/shuffle", listRecommendationShuffle) } diff --git a/pkg/internal/server/api/posts_api.go b/pkg/internal/server/api/posts_api.go index 6995822..9cc08d1 100644 --- a/pkg/internal/server/api/posts_api.go +++ b/pkg/internal/server/api/posts_api.go @@ -52,7 +52,7 @@ func getPost(c *fiber.Ctx) error { func listPost(c *fiber.Ctx) error { take := c.QueryInt("take", 0) offset := c.QueryInt("offset", 0) - realmId := c.QueryInt("realmId", 0) + realm := c.Query("realm") tx := services.FilterPostDraft(database.C) @@ -62,8 +62,8 @@ func listPost(c *fiber.Ctx) error { tx = services.FilterPostWithUserContext(tx, nil) } - if realmId > 0 { - if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil { + if len(realm) > 0 { + if realm, err := services.GetRealmWithAlias(realm); err != nil { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err)) } else { tx = services.FilterPostWithRealm(tx, realm.ID) @@ -105,7 +105,7 @@ func listPost(c *fiber.Ctx) error { func listPostMinimal(c *fiber.Ctx) error { take := c.QueryInt("take", 0) offset := c.QueryInt("offset", 0) - realmId := c.QueryInt("realmId", 0) + realm := c.Query("realm") tx := services.FilterPostDraft(database.C) @@ -115,8 +115,8 @@ func listPostMinimal(c *fiber.Ctx) error { tx = services.FilterPostWithUserContext(tx, nil) } - if realmId > 0 { - if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil { + if len(realm) > 0 { + if realm, err := services.GetRealmWithAlias(realm); err != nil { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err)) } else { tx = services.FilterPostWithRealm(tx, realm.ID) diff --git a/pkg/internal/server/api/recommendation_api.go b/pkg/internal/server/api/recommendation_api.go index 48f6e35..8c2143a 100644 --- a/pkg/internal/server/api/recommendation_api.go +++ b/pkg/internal/server/api/recommendation_api.go @@ -2,18 +2,18 @@ package api import ( "fmt" - "git.solsynth.dev/hydrogen/interactive/pkg/internal/database" + "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" - "gorm.io/gorm" + "github.com/samber/lo" ) -func listRecommendationFeatured(c *fiber.Ctx) error { +func listRecommendationNews(c *fiber.Ctx) error { take := c.QueryInt("take", 0) offset := c.QueryInt("offset", 0) - realmId := c.QueryInt("realmId", 0) + realm := c.Query("realm") tx := services.FilterPostDraft(database.C) @@ -23,8 +23,8 @@ func listRecommendationFeatured(c *fiber.Ctx) error { tx = services.FilterPostWithUserContext(tx, nil) } - if realmId > 0 { - if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil { + if len(realm) > 0 { + if realm, err := services.GetRealmWithAlias(realm); err != nil { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err)) } else { tx = services.FilterPostWithRealm(tx, realm.ID) @@ -37,7 +37,12 @@ func listRecommendationFeatured(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } - items, err := services.ListPost(tx, take, offset, "published_at DESC, (COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC") + 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) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } @@ -48,47 +53,46 @@ func listRecommendationFeatured(c *fiber.Ctx) error { }) } -func listRecommendationNews(c *fiber.Ctx) error { +func listRecommendationFriends(c *fiber.Ctx) error { + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } + user := c.Locals("user").(models.Account) + take := c.QueryInt("take", 0) offset := c.QueryInt("offset", 0) - realmId := c.QueryInt("realmId", 0) + realm := c.Query("realm") tx := services.FilterPostDraft(database.C) + tx = services.FilterPostWithUserContext(tx, &user) - if user, authenticated := c.Locals("user").(models.Account); authenticated { - tx = services.FilterPostWithUserContext(tx, &user) - } else { - tx = services.FilterPostWithUserContext(tx, nil) - } + friends, _ := services.ListAccountFriends(user) + friendList := lo.Map(friends, func(item models.Account, index int) uint { + return item.ID + }) - if realmId > 0 { - if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil { + tx = tx.Where("author_id IN ?", friendList) + + if len(realm) > 0 { + if realm, err := services.GetRealmWithAlias(realm); err != nil { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err)) } else { tx = services.FilterPostWithRealm(tx, realm.ID) } } - fmt.Println(database.C.ToSQL(func(tx *gorm.DB) *gorm.DB { - tx = services.FilterPostDraft(tx) - - if user, authenticated := c.Locals("user").(models.Account); authenticated { - tx = services.FilterPostWithUserContext(tx, &user) - } else { - tx = services.FilterPostWithUserContext(tx, nil) - } - - services.CountPost(tx) - return tx - })) - countTx := tx count, err := services.CountPost(countTx) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } - items, err := services.ListPost(tx, take, offset, "published_at DESC") + 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) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } @@ -102,7 +106,7 @@ func listRecommendationNews(c *fiber.Ctx) error { func listRecommendationShuffle(c *fiber.Ctx) error { take := c.QueryInt("take", 0) offset := c.QueryInt("offset", 0) - realmId := c.QueryInt("realmId", 0) + realm := c.Query("realm") tx := services.FilterPostDraft(database.C) @@ -112,8 +116,8 @@ func listRecommendationShuffle(c *fiber.Ctx) error { tx = services.FilterPostWithUserContext(tx, nil) } - if realmId > 0 { - if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil { + if len(realm) > 0 { + if realm, err := services.GetRealmWithAlias(realm); err != nil { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err)) } else { tx = services.FilterPostWithRealm(tx, realm.ID)