🐛 Fix realm-related fetch

This commit is contained in:
LittleSheep 2025-02-22 12:54:27 +08:00
parent 4b89474534
commit a15a0d1c11
4 changed files with 20 additions and 14 deletions

View File

@ -26,9 +26,9 @@ func getPostInsight(c *fiber.Ctx) error {
tx := services.FilterPostDraft(database.C) tx := services.FilterPostDraft(database.C)
if user, authenticated := c.Locals("user").(authm.Account); authenticated { 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 { } else {
tx = services.FilterPostWithUserContext(tx, nil) tx = services.FilterPostWithUserContext(tx, nil, len(c.Query("realm")) > 0)
} }
if numericId, paramErr := strconv.Atoi(id); paramErr == nil { if numericId, paramErr := strconv.Atoi(id); paramErr == nil {

View File

@ -24,9 +24,9 @@ func UniversalPostFilter(c *fiber.Ctx, tx *gorm.DB) (*gorm.DB, error) {
tx = services.FilterPostDraft(tx) tx = services.FilterPostDraft(tx)
if user, authenticated := c.Locals("user").(authm.Account); authenticated { 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 { } else {
tx = services.FilterPostWithUserContext(tx, nil) tx = services.FilterPostWithUserContext(tx, nil, len(c.Query("realm")) > 0)
} }
if c.QueryBool("noReply", true) { if c.QueryBool("noReply", true) {
@ -71,9 +71,9 @@ func getPost(c *fiber.Ctx) error {
tx := services.FilterPostDraft(database.C) tx := services.FilterPostDraft(database.C)
if user, authenticated := c.Locals("user").(authm.Account); authenticated { 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 { } else {
tx = services.FilterPostWithUserContext(tx, nil) tx = services.FilterPostWithUserContext(tx, nil, len(c.Query("realm")) > 0)
} }
if numericId, paramErr := strconv.Atoi(id); paramErr == nil { if numericId, paramErr := strconv.Atoi(id); paramErr == nil {

View File

@ -20,7 +20,7 @@ func getWhatsNew(c *fiber.Ctx) error {
} }
tx := services.FilterPostDraft(database.C) 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) tx = tx.Where("id > ?", pivot)

View File

@ -29,7 +29,7 @@ import (
"gorm.io/gorm" "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 { if user == nil {
return tx.Where("visibility = ? AND realm_id IS NULL", models.PostVisibilityAll) 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 // Getting the realm list
{ {
conn, err := gap.Nx.GetClientGrpcConn(nex.ServiceTypeAuth) conn, err := gap.Nx.GetClientGrpcConn(nex.ServiceTypeAuth)
if err != nil { if err == nil {
ac := aproto.NewRealmServiceClient(conn) ac := aproto.NewRealmServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel() 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 { realmList = lo.Map(resp.GetData(), func(item *aproto.RealmInfo, index int) uint {
return uint(item.GetId()) 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, InvisibleList: invisibleList,
RealmList: realmList, 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)}), 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 { if len(invisibleList) > 0 {
tx = tx.Where("publisher_id NOT IN ?", invisibleList) tx = tx.Where("publisher_id NOT IN ?", invisibleList)
} }
if len(realmList) > 0 { if !withRealm {
tx = tx.Where("realm_id IN ?", realmList) if len(realmList) > 0 {
} else { tx = tx.Where("realm_id IN ?", realmList)
tx = tx.Where("realm_id IS NULL") } else {
tx = tx.Where("realm_id IS NULL")
}
} }
return tx return tx