Compare commits
No commits in common. "d6f0daca616cfdf98ea9cfee8fd055d075f667bd" and "3c2a14800e875494865c1f0f313419809cd02a2b" have entirely different histories.
d6f0daca61
...
3c2a14800e
@ -1,7 +1,6 @@
|
||||
package queries
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
|
||||
@ -17,8 +16,6 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const singularAttachmentFields = []string{"video", "thumbnail"}
|
||||
|
||||
// This api still is experimental and finally with replace the old one
|
||||
// Some changes between ListPost and ListPostV2:
|
||||
// - Post reply to and repost to are not included
|
||||
@ -91,19 +88,14 @@ func ListPostV2(tx *gorm.DB, take int, offset int, order any, user *uint) ([]mod
|
||||
var usersId []uint
|
||||
|
||||
// Scan records that can be load egearly
|
||||
var bodies []models.PostStoryBody
|
||||
{
|
||||
raw, _ := json.Marshal(posts)
|
||||
json.Unmarshal(raw, &bodies)
|
||||
}
|
||||
for idx, info := range posts {
|
||||
for _, info := range posts {
|
||||
if info.Publisher.AccountID != nil {
|
||||
usersId = append(usersId, *info.Publisher.AccountID)
|
||||
}
|
||||
attachmentsRid = append(attachmentsRid, bodies[idx].Attachments...)
|
||||
for _, field := range singularAttachmentFields {
|
||||
if raw, ok := info.Body[field]; ok {
|
||||
if str, ok := raw.(string); ok {
|
||||
if raw, ok := info.Body["attachments"].([]any); ok && len(raw) > 0 {
|
||||
attachmentsRid := make([]string, 0, len(raw))
|
||||
for _, v := range raw {
|
||||
if str, ok := v.(string); ok {
|
||||
attachmentsRid = append(attachmentsRid, str)
|
||||
}
|
||||
}
|
||||
@ -129,19 +121,18 @@ func ListPostV2(tx *gorm.DB, take int, offset int, order any, user *uint) ([]mod
|
||||
log.Info().Int("attachments", len(attachments)).Int("users", len(users)).Msg("Batch loaded metadata for listing post...")
|
||||
for idx, item := range posts {
|
||||
var this []fmodels.Attachment
|
||||
if len(bodies[idx].Attachments) > 0 {
|
||||
this = lo.Filter(attachments, func(item fmodels.Attachment, _ int) bool {
|
||||
return lo.Contains(bodies[idx].Attachments, item.Rid)
|
||||
var val []string
|
||||
if raw, ok := item.Body["attachments"].([]any); ok && len(raw) > 0 {
|
||||
val = lo.Map(raw, func(v any, _ int) string {
|
||||
return v.(string) // Safe if you're sure all elements are strings
|
||||
})
|
||||
} else if raw, ok := item.Body["attachments"].([]string); ok {
|
||||
val = raw
|
||||
}
|
||||
for _, field := range singularAttachmentFields {
|
||||
if raw, ok := item.Body[field]; ok {
|
||||
if str, ok := raw.(string); ok {
|
||||
item.Body[field] = lo.FindOrElse(this, fmodels.Attachment{}, func(item fmodels.Attachment) bool {
|
||||
return item.Rid == str
|
||||
})
|
||||
}
|
||||
}
|
||||
if len(val) > 0 {
|
||||
this = lo.Filter(attachments, func(item fmodels.Attachment, _ int) bool {
|
||||
return lo.Contains(val, item.Rid)
|
||||
})
|
||||
}
|
||||
item.Body["attachments"] = this
|
||||
item.Publisher.Account = lo.FindOrElse(users, amodels.Account{}, func(acc amodels.Account) bool {
|
||||
|
Loading…
x
Reference in New Issue
Block a user