Hide user blocked user's publisher's post

This commit is contained in:
LittleSheep 2024-12-11 22:46:04 +08:00
parent 2543e1d125
commit 050d3e3f89

View File

@ -12,6 +12,7 @@ import (
"github.com/eko/gocache/lib/v4/cache" "github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler" "github.com/eko/gocache/lib/v4/marshaler"
"github.com/eko/gocache/lib/v4/store" "github.com/eko/gocache/lib/v4/store"
"gorm.io/datatypes"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -21,7 +22,6 @@ import (
"git.solsynth.dev/hypernet/interactive/pkg/internal/models" "git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/samber/lo" "github.com/samber/lo"
"gorm.io/datatypes"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -38,15 +38,16 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
) )
type userContextState struct { type userContextState struct {
Allowlist []uint Allowlist []uint
Blocklist []uint Blocklist []uint
InvisibleList []uint
} }
cacheManager := cache.New[any](localCache.S) cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager) marshal := marshaler.New(cacheManager)
ctx := context.Background() ctx := context.Background()
var allowlist, blocklist []uint var allowlist, blocklist, invisibleList []uint
statusCacheKey := fmt.Sprintf("post-user-context-query#%d", user.ID) statusCacheKey := fmt.Sprintf("post-user-context-query#%d", user.ID)
statusCache, err := marshal.Get(ctx, statusCacheKey, new(userContextState)) statusCache, err := marshal.Get(ctx, statusCacheKey, new(userContextState))
@ -54,13 +55,18 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
state := statusCache.(*userContextState) state := statusCache.(*userContextState)
allowlist = state.Allowlist allowlist = state.Allowlist
blocklist = state.Blocklist blocklist = state.Blocklist
invisibleList = state.InvisibleList
} else { } else {
userFriends, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipFriend), true) userFriends, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipFriend), true)
userBlocked, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipBlocked), true) userGotBlocked, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipBlocked), true)
userBlocked, _ := authkit.ListRelative(gap.Nx, user.ID, int32(authm.RelationshipBlocked), false)
userFriendList := lo.Map(userFriends, func(item *proto.UserInfo, index int) uint { userFriendList := lo.Map(userFriends, func(item *proto.UserInfo, index int) uint {
return uint(item.GetId()) return uint(item.GetId())
}) })
userBlockList := lo.Map(userBlocked, func(item *proto.UserInfo, index int) uint { userGotBlockList := lo.Map(userGotBlocked, func(item *proto.UserInfo, index int) uint {
return uint(item.GetId())
})
userBlocklist := lo.Map(userBlocked, func(item *proto.UserInfo, index int) uint {
return uint(item.GetId()) return uint(item.GetId())
}) })
@ -68,7 +74,7 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
var publishers []models.Publisher var publishers []models.Publisher
database.C.Where( database.C.Where(
"id IN ? AND type = ?", "id IN ? AND type = ?",
append(userFriendList, userBlockList...), lo.Uniq(append(append(userFriendList, userGotBlockList...), userBlocklist...)),
models.PublisherTypePersonal, models.PublisherTypePersonal,
).Find(&publishers) ).Find(&publishers)
@ -84,7 +90,15 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
if item.AccountID == nil { if item.AccountID == nil {
return false return false
} }
return lo.Contains(userBlockList, *item.AccountID) return lo.Contains(userGotBlockList, *item.AccountID)
}), func(item models.Publisher, index int) uint {
return uint(item.ID)
})
invisibleList = lo.Map(lo.Filter(publishers, func(item models.Publisher, index int) bool {
if item.AccountID == nil {
return false
}
return lo.Contains(userBlocklist, *item.AccountID)
}), func(item models.Publisher, index int) uint { }), func(item models.Publisher, index int) uint {
return uint(item.ID) return uint(item.ID)
}) })
@ -93,8 +107,9 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
ctx, ctx,
statusCacheKey, statusCacheKey,
userContextState{ userContextState{
Allowlist: allowlist, Allowlist: allowlist,
Blocklist: blocklist, Blocklist: blocklist,
InvisibleList: invisibleList,
}, },
store.WithExpiration(5*time.Minute), store.WithExpiration(5*time.Minute),
store.WithTags([]string{"post-user-context-query", fmt.Sprintf("user#%d", user.ID)}), store.WithTags([]string{"post-user-context-query", fmt.Sprintf("user#%d", user.ID)}),
@ -102,7 +117,11 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
} }
tx = tx.Where( tx = tx.Where(
"(visibility != ? OR (visibility = ? AND publisher_id IN ? AND publisher_id NOT IN ?) OR (visibility = ? AND ?) OR (visibility = ? AND NOT ?) OR publisher_id = ?)", "publisher_id = ? OR visibility != ? OR "+
"(visibility = ? AND publisher_id IN ? AND publisher_id NOT IN ?) OR "+
"(visibility = ? AND ?) OR "+
"(visibility = ? AND NOT ?)",
user.ID,
NoneVisibility, NoneVisibility,
FriendsVisibility, FriendsVisibility,
allowlist, allowlist,
@ -111,9 +130,10 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
datatypes.JSONQuery("visible_users").HasKey(strconv.Itoa(int(user.ID))), datatypes.JSONQuery("visible_users").HasKey(strconv.Itoa(int(user.ID))),
FilteredVisibility, FilteredVisibility,
datatypes.JSONQuery("invisible_users").HasKey(strconv.Itoa(int(user.ID))), datatypes.JSONQuery("invisible_users").HasKey(strconv.Itoa(int(user.ID))),
user.ID,
) )
tx = tx.Where("publisher_id NOT IN ?", invisibleList)
return tx return tx
} }