From ec0a2c3ac69b91cdf9de1cb814fe04429a38c5ea Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 30 Jul 2024 00:06:58 +0800 Subject: [PATCH] :bug: Fix posts query --- pkg/internal/server/api/recommendation_api.go | 14 +++++++++++++ pkg/internal/services/posts.go | 20 ++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/pkg/internal/server/api/recommendation_api.go b/pkg/internal/server/api/recommendation_api.go index e58fbc8..48f6e35 100644 --- a/pkg/internal/server/api/recommendation_api.go +++ b/pkg/internal/server/api/recommendation_api.go @@ -7,6 +7,7 @@ import ( "git.solsynth.dev/hydrogen/interactive/pkg/internal/models" "git.solsynth.dev/hydrogen/interactive/pkg/internal/services" "github.com/gofiber/fiber/v2" + "gorm.io/gorm" ) func listRecommendationFeatured(c *fiber.Ctx) error { @@ -68,6 +69,19 @@ func listRecommendationNews(c *fiber.Ctx) error { } } + 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 { diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 7e73884..eb7b530 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -3,6 +3,7 @@ package services import ( "errors" "fmt" + "strconv" "time" "git.solsynth.dev/hydrogen/interactive/pkg/internal/database" @@ -10,6 +11,7 @@ import ( "github.com/rs/zerolog/log" "github.com/samber/lo" "github.com/spf13/viper" + "gorm.io/datatypes" "gorm.io/gorm" ) @@ -18,17 +20,17 @@ func FilterPostWithUserContext(tx *gorm.DB, user *models.Account) *gorm.DB { return tx.Where("visibility = ?", models.PostVisibilityAll) } - // TODO Blocked by dealer, need support get friend list - tx = tx.Where( - "visibility != ? OR (visibility = ? AND visible_users @> '[?]'::jsonb) OR (visibility = ? AND NOT invisible_users @> '[?]'::jsonb) OR visibility != ?", - models.PostVisibilityFriends, - models.PostVisibilitySelected, - user.ID, - models.PostVisibilitySelected, - user.ID, - models.PostVisibilityNone, + const ( + FriendsVisibility = models.PostVisibilityFriends + SelectedVisibility = models.PostVisibilitySelected + NoneVisibility = models.PostVisibilityNone ) + // TODO Blocked by dealer, need support get friend list + tx = tx.Where("visibility != ? OR visibility != ?", FriendsVisibility, NoneVisibility). + Or("visibility = ? AND ?", SelectedVisibility, datatypes.JSONQuery("visible_users").HasKey(strconv.Itoa(int(user.ID)))). + Or("visibility = ? AND NOT ?", SelectedVisibility, datatypes.JSONQuery("invisible_users").HasKey(strconv.Itoa(int(user.ID)))) + return tx }