From 14f4da6c9122e8d7e9b80c3b4587e44d7c70bfc3 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 22 Feb 2025 23:03:40 +0800 Subject: [PATCH] :bug: Fixed post visibility --- pkg/internal/services/posts.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 54f0bc7..6cea249 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -36,13 +36,14 @@ func FilterPostWithUserContext(c *fiber.Ctx, tx *gorm.DB, user *authm.Account) * } const ( + AllVisibility = models.PostVisibilityAll FriendsVisibility = models.PostVisibilityFriends SelectedVisibility = models.PostVisibilitySelected FilteredVisibility = models.PostVisibilityFiltered ) type userContextState struct { - Self uint `json:"self"` + Self []uint `json:"self"` Allowlist []uint `json:"allow"` InvisibleList []uint `json:"invisible"` FollowList []uint `json:"follow"` @@ -53,8 +54,7 @@ func FilterPostWithUserContext(c *fiber.Ctx, tx *gorm.DB, user *authm.Account) * marshal := marshaler.New(cacheManager) ctx := context.Background() - var allowlist, invisibleList, followList, realmList []uint - var self uint + var self, allowlist, invisibleList, followList, realmList []uint statusCacheKey := fmt.Sprintf("post-user-context-query#%d", user.ID) statusCache, err := marshal.Get(ctx, statusCacheKey, new(userContextState)) @@ -67,12 +67,16 @@ func FilterPostWithUserContext(c *fiber.Ctx, tx *gorm.DB, user *authm.Account) * self = state.Self } else { // Get itself - var publisher models.Publisher - if err := database.C.Where("account_id = ?", user.ID).First(&publisher).Error; err != nil { - return tx + { + var publishers []models.Publisher + if err := database.C.Where("account_id = ?", user.ID).Find(&publishers).Error; err != nil { + return tx + } + self = lo.Map(publishers, func(item models.Publisher, index int) uint { + return item.ID + }) + allowlist = append(allowlist, self...) } - allowlist = append(allowlist, publisher.ID) - self = publisher.ID // Getting the relationships userFriends, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipFriend), true) @@ -165,10 +169,11 @@ func FilterPostWithUserContext(c *fiber.Ctx, tx *gorm.DB, user *authm.Account) * } tx = tx.Where( - "publisher_id = ? OR "+ + "(publisher_id IN ? OR visibility = ? "+ "(visibility = ? AND publisher_id IN ?) OR "+ "(visibility = ? AND ?) OR "+ - "(visibility = ? AND NOT ?)", + "(visibility = ? AND NOT ?))", + AllVisibility, self, FriendsVisibility, allowlist,