From a15a0d1c11717f80b4de045dbf5a82f6a6c3346a Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 22 Feb 2025 12:54:27 +0800 Subject: [PATCH] :bug: Fix realm-related fetch --- 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 | 20 +++++++++++++------- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/pkg/internal/http/api/insight_api.go b/pkg/internal/http/api/insight_api.go index 23a92d8..2f2a155 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) + tx = services.FilterPostWithUserContext(tx, &user, len(c.Query("realm")) > 0) } else { - tx = services.FilterPostWithUserContext(tx, nil) + tx = services.FilterPostWithUserContext(tx, nil, len(c.Query("realm")) > 0) } 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 37f2e5e..d5a5a3f 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) + tx = services.FilterPostWithUserContext(tx, &user, len(c.Query("realm")) > 0) } else { - tx = services.FilterPostWithUserContext(tx, nil) + tx = services.FilterPostWithUserContext(tx, nil, len(c.Query("realm")) > 0) } 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) + tx = services.FilterPostWithUserContext(tx, &user, len(c.Query("realm")) > 0) } else { - tx = services.FilterPostWithUserContext(tx, nil) + tx = services.FilterPostWithUserContext(tx, nil, len(c.Query("realm")) > 0) } 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 25b950b..953f8fe 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) + tx = services.FilterPostWithUserContext(tx, &user, len(c.Query("realm")) > 0) tx = tx.Where("id > ?", pivot) diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index a08645d..475f29e 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -29,7 +29,7 @@ import ( "gorm.io/gorm" ) -func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB { +func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account, withRealm bool) *gorm.DB { if user == nil { return tx.Where("visibility = ? AND realm_id IS NULL", models.PostVisibilityAll) } @@ -69,7 +69,7 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB { // Getting the realm list { conn, err := gap.Nx.GetClientGrpcConn(nex.ServiceTypeAuth) - if err != nil { + if err == nil { ac := aproto.NewRealmServiceClient(conn) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() @@ -80,7 +80,11 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB { realmList = lo.Map(resp.GetData(), func(item *aproto.RealmInfo, index int) uint { return uint(item.GetId()) }) + } else { + log.Warn().Err(err).Uint("user", user.ID).Msg("An error occurred when getting realm list from grpc...") } + } else { + log.Warn().Err(err).Uint("user", user.ID).Msg("An error occurred when getting grpc connection to Auth...") } } @@ -127,7 +131,7 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB { InvisibleList: invisibleList, RealmList: realmList, }, - store.WithExpiration(5*time.Minute), + store.WithExpiration(2*time.Minute), store.WithTags([]string{"post-user-context-query", fmt.Sprintf("user#%d", user.ID)}), ) } @@ -150,10 +154,12 @@ func FilterPostWithUserContext(tx *gorm.DB, user *authm.Account) *gorm.DB { if len(invisibleList) > 0 { tx = tx.Where("publisher_id NOT IN ?", invisibleList) } - if len(realmList) > 0 { - tx = tx.Where("realm_id IN ?", realmList) - } else { - tx = tx.Where("realm_id IS NULL") + if !withRealm { + if len(realmList) > 0 { + tx = tx.Where("realm_id IN ?", realmList) + } else { + tx = tx.Where("realm_id IS NULL") + } } return tx