From a3cb407d8914b98a24997f3ba8771f153b9ef018 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 22 Feb 2025 13:29:42 +0800 Subject: [PATCH] :sparkles: Post channel modes --- pkg/internal/http/api/insight_api.go | 4 +-- pkg/internal/http/api/posts_api.go | 8 +++--- pkg/internal/http/api/what_new_api.dart.go | 2 +- pkg/internal/services/posts.go | 30 +++++++++++++++++++--- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/pkg/internal/http/api/insight_api.go b/pkg/internal/http/api/insight_api.go index 2f2a155..5913f6d 100644 --- a/pkg/internal/http/api/insight_api.go +++ b/pkg/internal/http/api/insight_api.go @@ -26,9 +26,9 @@ func getPostInsight(c *fiber.Ctx) error { tx := services.FilterPostDraft(database.C) if user, authenticated := c.Locals("user").(authm.Account); authenticated { - tx = services.FilterPostWithUserContext(tx, &user, len(c.Query("realm")) > 0) + tx = services.FilterPostWithUserContext(c, tx, &user) } else { - tx = services.FilterPostWithUserContext(tx, nil, len(c.Query("realm")) > 0) + tx = services.FilterPostWithUserContext(c, tx, nil) } if numericId, paramErr := strconv.Atoi(id); paramErr == nil { diff --git a/pkg/internal/http/api/posts_api.go b/pkg/internal/http/api/posts_api.go index d5a5a3f..963934a 100644 --- a/pkg/internal/http/api/posts_api.go +++ b/pkg/internal/http/api/posts_api.go @@ -24,9 +24,9 @@ func UniversalPostFilter(c *fiber.Ctx, tx *gorm.DB) (*gorm.DB, error) { tx = services.FilterPostDraft(tx) if user, authenticated := c.Locals("user").(authm.Account); authenticated { - tx = services.FilterPostWithUserContext(tx, &user, len(c.Query("realm")) > 0) + tx = services.FilterPostWithUserContext(c, tx, &user) } else { - tx = services.FilterPostWithUserContext(tx, nil, len(c.Query("realm")) > 0) + tx = services.FilterPostWithUserContext(c, tx, nil) } if c.QueryBool("noReply", true) { @@ -71,9 +71,9 @@ func getPost(c *fiber.Ctx) error { tx := services.FilterPostDraft(database.C) if user, authenticated := c.Locals("user").(authm.Account); authenticated { - tx = services.FilterPostWithUserContext(tx, &user, len(c.Query("realm")) > 0) + tx = services.FilterPostWithUserContext(c, tx, &user) } else { - tx = services.FilterPostWithUserContext(tx, nil, len(c.Query("realm")) > 0) + tx = services.FilterPostWithUserContext(c, tx, nil) } if numericId, paramErr := strconv.Atoi(id); paramErr == nil { diff --git a/pkg/internal/http/api/what_new_api.dart.go b/pkg/internal/http/api/what_new_api.dart.go index 953f8fe..3431f82 100644 --- a/pkg/internal/http/api/what_new_api.dart.go +++ b/pkg/internal/http/api/what_new_api.dart.go @@ -20,7 +20,7 @@ func getWhatsNew(c *fiber.Ctx) error { } tx := services.FilterPostDraft(database.C) - tx = services.FilterPostWithUserContext(tx, &user, len(c.Query("realm")) > 0) + tx = services.FilterPostWithUserContext(c, tx, &user) tx = tx.Where("id > ?", pivot) diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index f2d26e3..73ea9de 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "git.solsynth.dev/hypernet/nexus/pkg/nex" + "github.com/gofiber/fiber/v2" "regexp" "strconv" "strings" @@ -29,7 +30,7 @@ import ( "gorm.io/gorm" ) -func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account, withRealm bool) *gorm.DB { +func FilterPostWithUserContext(c *fiber.Ctx, tx *gorm.DB, user *authm.Account) *gorm.DB { if user == nil { return tx.Where("visibility = ? AND realm_id IS NULL", models.PostVisibilityAll) } @@ -44,6 +45,7 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account, withRealm bool) type userContextState struct { Allowlist []uint `json:"allow"` InvisibleList []uint `json:"invisible"` + FollowList []uint `json:"follow"` RealmList []uint `json:"realm"` } @@ -51,7 +53,7 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account, withRealm bool) marshal := marshaler.New(cacheManager) ctx := context.Background() - var allowlist, invisibleList, realmList []uint + var allowlist, invisibleList, followList, realmList []uint statusCacheKey := fmt.Sprintf("post-user-context-query#%d", user.ID) statusCache, err := marshal.Get(ctx, statusCacheKey, new(userContextState)) @@ -59,6 +61,7 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account, withRealm bool) state := statusCache.(*userContextState) allowlist = state.Allowlist invisibleList = state.InvisibleList + followList = state.FollowList realmList = state.RealmList } else { // Getting the relationships @@ -106,6 +109,19 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account, withRealm bool) models.PublisherTypePersonal, ).Find(&publishers) + // Getting the follow list + { + var subs []models.Subscription + if err := database.C.Where("follower_id = ? AND account_id IS NOT NULL", user.ID).Find(&subs).Error; err != nil { + log.Error().Err(err).Msg("An error occurred when getting subscriptions...") + } + followList = lo.Map(lo.Filter(subs, func(item models.Subscription, index int) bool { + return item.AccountID != nil + }), func(item models.Subscription, index int) uint { + return *item.AccountID + }) + } + allowlist = lo.Map(lo.Filter(publishers, func(item models.Publisher, index int) bool { if item.AccountID == nil { return false @@ -130,6 +146,7 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account, withRealm bool) Allowlist: allowlist, InvisibleList: invisibleList, RealmList: realmList, + FollowList: followList, }, store.WithExpiration(2*time.Minute), store.WithTags([]string{"post-user-context-query", fmt.Sprintf("user#%d", user.ID)}), @@ -154,7 +171,7 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account, withRealm bool) if len(invisibleList) > 0 { tx = tx.Where("publisher_id NOT IN ?", invisibleList) } - if !withRealm { + if len(c.Query("realm")) == 0 { if len(realmList) > 0 { tx = tx.Where("realm_id IN ? OR realm_id IS NULL", realmList) } else { @@ -162,6 +179,13 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account, withRealm bool) } } + switch c.Query("channel") { + case "friends": + tx = tx.Where("publisher_id IN ?", allowlist) + case "following": + tx = tx.Where("publisher_id IN ?", followList) + } + return tx }