🐛 Fix unable to list posts with realm id

This commit is contained in:
LittleSheep 2024-06-23 01:18:12 +08:00
parent 608c52ace6
commit c198565ced
3 changed files with 23 additions and 1 deletions

View File

@ -39,7 +39,11 @@ func listPost(c *fiber.Ctx) error {
tx := database.C tx := database.C
if realmId > 0 { if realmId > 0 {
tx = services.FilterWithRealm(tx, uint(realmId)) if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
} else {
tx = services.FilterWithRealm(tx, realm.ID)
}
} }
if len(c.Query("authorId")) > 0 { if len(c.Query("authorId")) > 0 {

View File

@ -55,6 +55,7 @@ func GetPostWithAlias(alias string, ignoreLimitation ...bool) (models.Post, erro
var item models.Post var item models.Post
if err := tx. if err := tx.
Where("alias = ?", alias). Where("alias = ?", alias).
Preload("Realm").
Preload("Author"). Preload("Author").
Preload("ReplyTo"). Preload("ReplyTo").
Preload("ReplyTo.Author"). Preload("ReplyTo.Author").
@ -76,6 +77,7 @@ func GetPost(id uint, ignoreLimitation ...bool) (models.Post, error) {
var item models.Post var item models.Post
if err := tx. if err := tx.
Where("id = ?", id). Where("id = ?", id).
Preload("Realm").
Preload("Author"). Preload("Author").
Preload("ReplyTo"). Preload("ReplyTo").
Preload("ReplyTo.Author"). Preload("ReplyTo.Author").
@ -151,6 +153,7 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]*models.Pos
if err := tx. if err := tx.
Limit(take).Offset(offset). Limit(take).Offset(offset).
Order("created_at DESC"). Order("created_at DESC").
Preload("Realm").
Preload("Author"). Preload("Author").
Preload("ReplyTo"). Preload("ReplyTo").
Preload("ReplyTo.Author"). Preload("ReplyTo.Author").

View File

@ -13,6 +13,21 @@ import (
"reflect" "reflect"
) )
func GetRealmWithExtID(id uint) (models.Realm, error) {
var realm models.Realm
pc, err := gap.H.DiscoverServiceGRPC("Hydrogen.Passport")
if err != nil {
return realm, err
}
response, err := proto.NewRealmsClient(pc).GetRealm(context.Background(), &proto.RealmLookupRequest{
Id: lo.ToPtr(uint64(id)),
})
if err != nil {
return realm, err
}
return LinkRealm(response)
}
func GetRealmWithAlias(alias string) (models.Realm, error) { func GetRealmWithAlias(alias string) (models.Realm, error) {
var realm models.Realm var realm models.Realm
pc, err := gap.H.DiscoverServiceGRPC("Hydrogen.Passport") pc, err := gap.H.DiscoverServiceGRPC("Hydrogen.Passport")