Compare commits

...

2 Commits

3 changed files with 92 additions and 24 deletions

View File

@ -2,6 +2,7 @@ package services
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hypernet/interactive/pkg/internal/database" "git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap" "git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models" "git.solsynth.dev/hypernet/interactive/pkg/internal/models"

View File

@ -4,6 +4,11 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"regexp"
"strconv"
"strings"
"time"
localCache "git.solsynth.dev/hypernet/interactive/pkg/internal/cache" localCache "git.solsynth.dev/hypernet/interactive/pkg/internal/cache"
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap" "git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
"git.solsynth.dev/hypernet/nexus/pkg/proto" "git.solsynth.dev/hypernet/nexus/pkg/proto"
@ -13,10 +18,6 @@ import (
"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" "gorm.io/datatypes"
"regexp"
"strconv"
"strings"
"time"
"git.solsynth.dev/hypernet/interactive/pkg/internal/database" "git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models" "git.solsynth.dev/hypernet/interactive/pkg/internal/models"
@ -39,7 +40,6 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
type userContextState struct { type userContextState struct {
Allowlist []uint Allowlist []uint
Blocklist []uint
InvisibleList []uint InvisibleList []uint
} }
@ -47,14 +47,13 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
marshal := marshaler.New(cacheManager) marshal := marshaler.New(cacheManager)
ctx := context.Background() ctx := context.Background()
var allowlist, blocklist, invisibleList []uint var allowlist, 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))
if err == nil { if err == nil {
state := statusCache.(*userContextState) state := statusCache.(*userContextState)
allowlist = state.Allowlist allowlist = state.Allowlist
blocklist = state.Blocklist
invisibleList = state.InvisibleList 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)
@ -86,14 +85,6 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
}), func(item models.Publisher, index int) uint { }), func(item models.Publisher, index int) uint {
return uint(item.ID) return uint(item.ID)
}) })
blocklist = lo.Map(lo.Filter(publishers, func(item models.Publisher, index int) bool {
if item.AccountID == nil {
return false
}
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 { invisibleList = lo.Map(lo.Filter(publishers, func(item models.Publisher, index int) bool {
if item.AccountID == nil { if item.AccountID == nil {
return false return false
@ -108,7 +99,6 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
statusCacheKey, statusCacheKey,
userContextState{ userContextState{
Allowlist: allowlist, Allowlist: allowlist,
Blocklist: blocklist,
InvisibleList: invisibleList, InvisibleList: invisibleList,
}, },
store.WithExpiration(5*time.Minute), store.WithExpiration(5*time.Minute),
@ -118,14 +108,13 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB {
tx = tx.Where( tx = tx.Where(
"publisher_id = ? OR visibility != ? OR "+ "publisher_id = ? OR visibility != ? OR "+
"(visibility = ? AND publisher_id IN ? AND publisher_id NOT IN ?) OR "+ "(visibility = ? AND publisher_id IN ?) OR "+
"(visibility = ? AND ?) OR "+ "(visibility = ? AND ?) OR "+
"(visibility = ? AND NOT ?)", "(visibility = ? AND NOT ?)",
user.ID, user.ID,
NoneVisibility, NoneVisibility,
FriendsVisibility, FriendsVisibility,
allowlist, allowlist,
blocklist,
SelectedVisibility, SelectedVisibility,
datatypes.JSONQuery("visible_users").HasKey(strconv.Itoa(int(user.ID))), datatypes.JSONQuery("visible_users").HasKey(strconv.Itoa(int(user.ID))),
FilteredVisibility, FilteredVisibility,
@ -440,16 +429,17 @@ func NewPost(user models.Publisher, item models.Post) (models.Post, error) {
var title *string var title *string
title, _ = item.Body["title"].(*string) title, _ = item.Body["title"].(*string)
go func() { go func() {
if err := NotifyUserSubscription(user, content, title); err != nil { item.Publisher = user
if err := NotifyUserSubscription(user, item, content, title); err != nil {
log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by user...") log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by user...")
} }
for _, tag := range item.Tags { for _, tag := range item.Tags {
if err := NotifyTagSubscription(tag, user, content, title); err != nil { if err := NotifyTagSubscription(tag, user, item, content, title); err != nil {
log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by tag...") log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by tag...")
} }
} }
for _, category := range item.Categories { for _, category := range item.Categories {
if err := NotifyCategorySubscription(category, user, content, title); err != nil { if err := NotifyCategorySubscription(category, user, item, content, title); err != nil {
log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by category...") log.Error().Err(err).Msg("An error occurred when notifying subscriptions user by category...")
} }
} }

View File

@ -3,12 +3,15 @@ package services
import ( import (
"errors" "errors"
"fmt" "fmt"
"git.solsynth.dev/hypernet/interactive/pkg/internal/database" "git.solsynth.dev/hypernet/interactive/pkg/internal/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap" "git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models" "git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"git.solsynth.dev/hypernet/passport/pkg/authkit" "git.solsynth.dev/hypernet/passport/pkg/authkit"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/pusher/pkg/pushkit" "git.solsynth.dev/hypernet/pusher/pkg/pushkit"
"github.com/samber/lo"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -135,7 +138,11 @@ func UnsubscribeFromCategory(user authm.Account, target models.Category) error {
return err return err
} }
func NotifyUserSubscription(poster models.Publisher, content string, title *string) error { func NotifyUserSubscription(poster models.Publisher, item models.Post, content string, title *string) error {
if item.Visibility == models.PostVisibilityNone {
return nil
}
var subscriptions []models.Subscription var subscriptions []models.Subscription
if err := database.C.Where("account_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil { if err := database.C.Where("account_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil {
return fmt.Errorf("unable to get subscriptions: %v", err) return fmt.Errorf("unable to get subscriptions: %v", err)
@ -154,6 +161,20 @@ func NotifyUserSubscription(poster models.Publisher, content string, title *stri
userIDs = append(userIDs, uint64(subscription.Follower.ID)) userIDs = append(userIDs, uint64(subscription.Follower.ID))
} }
if item.Visibility == models.PostVisibilitySelected {
userIDs = lo.Filter(userIDs, func(entry uint64, index int) bool {
return lo.Contains(item.VisibleUsers, uint(entry))
})
} else if item.Visibility == models.PostVisibilityFiltered {
userIDs = lo.Filter(userIDs, func(entry uint64, index int) bool {
return !lo.Contains(item.InvisibleUsers, uint(entry))
})
} else if invisibleList := ListPostInvisibleUser(poster, item.Visibility); len(invisibleList) > 0 {
userIDs = lo.Filter(userIDs, func(entry uint64, index int) bool {
return !lo.Contains(invisibleList, uint(entry))
})
}
err := authkit.NotifyUserBatch(gap.Nx, userIDs, pushkit.Notification{ err := authkit.NotifyUserBatch(gap.Nx, userIDs, pushkit.Notification{
Topic: "interactive.subscription", Topic: "interactive.subscription",
Title: nTitle, Title: nTitle,
@ -165,7 +186,11 @@ func NotifyUserSubscription(poster models.Publisher, content string, title *stri
return err return err
} }
func NotifyTagSubscription(poster models.Tag, og models.Publisher, content string, title *string) error { func NotifyTagSubscription(poster models.Tag, og models.Publisher, item models.Post, content string, title *string) error {
if item.Visibility == models.PostVisibilityNone {
return nil
}
var subscriptions []models.Subscription var subscriptions []models.Subscription
if err := database.C.Where("tag_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil { if err := database.C.Where("tag_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil {
return fmt.Errorf("unable to get subscriptions: %v", err) return fmt.Errorf("unable to get subscriptions: %v", err)
@ -184,6 +209,20 @@ func NotifyTagSubscription(poster models.Tag, og models.Publisher, content strin
userIDs = append(userIDs, uint64(subscription.Follower.ID)) userIDs = append(userIDs, uint64(subscription.Follower.ID))
} }
if item.Visibility == models.PostVisibilitySelected {
userIDs = lo.Filter(userIDs, func(entry uint64, index int) bool {
return lo.Contains(item.VisibleUsers, uint(entry))
})
} else if item.Visibility == models.PostVisibilityFiltered {
userIDs = lo.Filter(userIDs, func(entry uint64, index int) bool {
return !lo.Contains(item.InvisibleUsers, uint(entry))
})
} else if invisibleList := ListPostInvisibleUser(item.Publisher, item.Visibility); len(invisibleList) > 0 {
userIDs = lo.Filter(userIDs, func(entry uint64, index int) bool {
return !lo.Contains(invisibleList, uint(entry))
})
}
err := authkit.NotifyUserBatch(gap.Nx, userIDs, pushkit.Notification{ err := authkit.NotifyUserBatch(gap.Nx, userIDs, pushkit.Notification{
Topic: "interactive.subscription", Topic: "interactive.subscription",
Title: nTitle, Title: nTitle,
@ -195,7 +234,11 @@ func NotifyTagSubscription(poster models.Tag, og models.Publisher, content strin
return err return err
} }
func NotifyCategorySubscription(poster models.Category, og models.Publisher, content string, title *string) error { func NotifyCategorySubscription(poster models.Category, og models.Publisher, item models.Post, content string, title *string) error {
if item.Visibility == models.PostVisibilityNone {
return nil
}
var subscriptions []models.Subscription var subscriptions []models.Subscription
if err := database.C.Where("category_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil { if err := database.C.Where("category_id = ?", poster.ID).Preload("Follower").Find(&subscriptions).Error; err != nil {
return fmt.Errorf("unable to get subscriptions: %v", err) return fmt.Errorf("unable to get subscriptions: %v", err)
@ -214,6 +257,20 @@ func NotifyCategorySubscription(poster models.Category, og models.Publisher, con
userIDs = append(userIDs, uint64(subscription.Follower.ID)) userIDs = append(userIDs, uint64(subscription.Follower.ID))
} }
if item.Visibility == models.PostVisibilitySelected {
userIDs = lo.Filter(userIDs, func(entry uint64, index int) bool {
return lo.Contains(item.VisibleUsers, uint(entry))
})
} else if item.Visibility == models.PostVisibilityFiltered {
userIDs = lo.Filter(userIDs, func(entry uint64, index int) bool {
return !lo.Contains(item.InvisibleUsers, uint(entry))
})
} else if invisibleList := ListPostInvisibleUser(item.Publisher, item.Visibility); len(invisibleList) > 0 {
userIDs = lo.Filter(userIDs, func(entry uint64, index int) bool {
return !lo.Contains(invisibleList, uint(entry))
})
}
err := authkit.NotifyUserBatch(gap.Nx, userIDs, pushkit.Notification{ err := authkit.NotifyUserBatch(gap.Nx, userIDs, pushkit.Notification{
Topic: "interactive.subscription", Topic: "interactive.subscription",
Title: nTitle, Title: nTitle,
@ -224,3 +281,23 @@ func NotifyCategorySubscription(poster models.Category, og models.Publisher, con
return err return err
} }
// ListPostInvisibleUser will return a list of users which should not be notified the post.
// NOTICE If the visibility is PostVisibilitySelected, PostVisibilityFiltered or PostVisibilityNone, you need do extra steps to filter users
// WARNING This function won't use cache, be careful of the queries
func ListPostInvisibleUser(og models.Publisher, visibility models.PostVisibilityLevel) []uint {
switch visibility {
case models.PostVisibilityAll:
return []uint{}
case models.PostVisibilityFriends:
if og.AccountID == nil {
return []uint{}
}
userFriends, _ := authkit.ListRelative(gap.Nx, *og.AccountID, int32(authm.RelationshipFriend), true)
return lo.Map(userFriends, func(item *proto.UserInfo, index int) uint {
return uint(item.GetId())
})
default:
return nil
}
}