✨ Eager to load realms
🐛 Trying to fix empty reactions list
This commit is contained in:
parent
ce9d663bc3
commit
6f7a2de41e
@ -428,7 +428,7 @@ func ListPost(tx *gorm.DB, take int, offset int, order any, user *uint, noReact
|
|||||||
itemMap := make(map[uint]*models.Post, len(posts))
|
itemMap := make(map[uint]*models.Post, len(posts))
|
||||||
for i, item := range posts {
|
for i, item := range posts {
|
||||||
idx[i] = item.ID
|
idx[i] = item.ID
|
||||||
itemMap[item.ID] = &item
|
itemMap[item.ID] = &posts[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Batch load reactions
|
// Batch load reactions
|
||||||
|
@ -26,7 +26,7 @@ func CompletePostMeta(in ...models.Post) ([]models.Post, error) {
|
|||||||
itemMap := make(map[uint]*models.Post, len(in))
|
itemMap := make(map[uint]*models.Post, len(in))
|
||||||
for i, item := range in {
|
for i, item := range in {
|
||||||
idx[i] = item.ID
|
idx[i] = item.ID
|
||||||
itemMap[item.ID] = &item
|
itemMap[item.ID] = &in[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Batch load reactions
|
// Batch load reactions
|
||||||
@ -59,8 +59,10 @@ func CompletePostMeta(in ...models.Post) ([]models.Post, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Batch load some metadata
|
// Batch load some metadata
|
||||||
|
var err error
|
||||||
var attachmentsRid []string
|
var attachmentsRid []string
|
||||||
var usersId []uint
|
var usersId []uint
|
||||||
|
var realmsId []uint
|
||||||
|
|
||||||
// Scan records that can be load eagerly
|
// Scan records that can be load eagerly
|
||||||
var bodies []models.PostStoryBody
|
var bodies []models.PostStoryBody
|
||||||
@ -74,6 +76,9 @@ func CompletePostMeta(in ...models.Post) ([]models.Post, error) {
|
|||||||
if info.Publisher.AccountID != nil {
|
if info.Publisher.AccountID != nil {
|
||||||
usersId = append(usersId, *info.Publisher.AccountID)
|
usersId = append(usersId, *info.Publisher.AccountID)
|
||||||
}
|
}
|
||||||
|
if info.RealmID != nil {
|
||||||
|
realmsId = append(realmsId, *info.RealmID)
|
||||||
|
}
|
||||||
attachmentsRid = append(attachmentsRid, bodies[idx].Attachments...)
|
attachmentsRid = append(attachmentsRid, bodies[idx].Attachments...)
|
||||||
for _, field := range singularAttachmentFields {
|
for _, field := range singularAttachmentFields {
|
||||||
if raw, ok := info.Body[field]; ok {
|
if raw, ok := info.Body[field]; ok {
|
||||||
@ -87,16 +92,32 @@ func CompletePostMeta(in ...models.Post) ([]models.Post, error) {
|
|||||||
|
|
||||||
// Batch load attachments
|
// Batch load attachments
|
||||||
attachmentsRid = lo.Uniq(attachmentsRid)
|
attachmentsRid = lo.Uniq(attachmentsRid)
|
||||||
attachments, err := filekit.ListAttachment(gap.Nx, attachmentsRid)
|
var attachments []fmodels.Attachment
|
||||||
if err != nil {
|
if len(attachmentsRid) > 0 {
|
||||||
return in, fmt.Errorf("failed to load attachments: %v", err)
|
attachments, err = filekit.ListAttachment(gap.Nx, attachmentsRid)
|
||||||
|
if err != nil {
|
||||||
|
return in, fmt.Errorf("failed to load attachments: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Batch load publisher users
|
// Batch load publisher users
|
||||||
usersId = lo.Uniq(usersId)
|
usersId = lo.Uniq(usersId)
|
||||||
users, err := authkit.ListUser(gap.Nx, usersId)
|
var users []amodels.Account
|
||||||
if err != nil {
|
if len(users) > 0 {
|
||||||
return in, fmt.Errorf("failed to load users: %v", err)
|
users, err = authkit.ListUser(gap.Nx, usersId)
|
||||||
|
if err != nil {
|
||||||
|
return in, fmt.Errorf("failed to load users: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Batch load posts realm
|
||||||
|
realmsId = lo.Uniq(realmsId)
|
||||||
|
var realms []amodels.Realm
|
||||||
|
if len(realmsId) > 0 {
|
||||||
|
realms, err = authkit.ListRealm(gap.Nx, realmsId)
|
||||||
|
if err != nil {
|
||||||
|
return in, fmt.Errorf("failed to load realms: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Putting information back to data
|
// Putting information back to data
|
||||||
@ -118,12 +139,16 @@ func CompletePostMeta(in ...models.Post) ([]models.Post, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
item.Body["attachments"] = this
|
item.Body["attachments"] = this
|
||||||
item.Publisher.Account = lo.FindOrElse(users, amodels.Account{}, func(acc amodels.Account) bool {
|
if item.Publisher.AccountID != nil {
|
||||||
if item.Publisher.AccountID == nil {
|
item.Publisher.Account = lo.FindOrElse(users, amodels.Account{}, func(acc amodels.Account) bool {
|
||||||
return false
|
return acc.ID == *item.Publisher.AccountID
|
||||||
}
|
})
|
||||||
return acc.ID == *item.Publisher.AccountID
|
}
|
||||||
})
|
if item.RealmID != nil {
|
||||||
|
item.Realm = lo.ToPtr(lo.FindOrElse(realms, amodels.Realm{}, func(realm amodels.Realm) bool {
|
||||||
|
return realm.ID == *item.RealmID
|
||||||
|
}))
|
||||||
|
}
|
||||||
in[idx] = item
|
in[idx] = item
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user