Compare commits

..

No commits in common. "d6f0daca616cfdf98ea9cfee8fd055d075f667bd" and "3c2a14800e875494865c1f0f313419809cd02a2b" have entirely different histories.

View File

@ -1,7 +1,6 @@
package queries package queries
import ( import (
"encoding/json"
"fmt" "fmt"
"git.solsynth.dev/hypernet/interactive/pkg/internal/database" "git.solsynth.dev/hypernet/interactive/pkg/internal/database"
@ -17,8 +16,6 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
const singularAttachmentFields = []string{"video", "thumbnail"}
// This api still is experimental and finally with replace the old one // This api still is experimental and finally with replace the old one
// Some changes between ListPost and ListPostV2: // Some changes between ListPost and ListPostV2:
// - Post reply to and repost to are not included // - 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 var usersId []uint
// Scan records that can be load egearly // Scan records that can be load egearly
var bodies []models.PostStoryBody for _, info := range posts {
{
raw, _ := json.Marshal(posts)
json.Unmarshal(raw, &bodies)
}
for idx, info := range posts {
if info.Publisher.AccountID != nil { if info.Publisher.AccountID != nil {
usersId = append(usersId, *info.Publisher.AccountID) usersId = append(usersId, *info.Publisher.AccountID)
} }
attachmentsRid = append(attachmentsRid, bodies[idx].Attachments...) if raw, ok := info.Body["attachments"].([]any); ok && len(raw) > 0 {
for _, field := range singularAttachmentFields { attachmentsRid := make([]string, 0, len(raw))
if raw, ok := info.Body[field]; ok { for _, v := range raw {
if str, ok := raw.(string); ok { if str, ok := v.(string); ok {
attachmentsRid = append(attachmentsRid, str) 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...") log.Info().Int("attachments", len(attachments)).Int("users", len(users)).Msg("Batch loaded metadata for listing post...")
for idx, item := range posts { for idx, item := range posts {
var this []fmodels.Attachment var this []fmodels.Attachment
if len(bodies[idx].Attachments) > 0 { var val []string
this = lo.Filter(attachments, func(item fmodels.Attachment, _ int) bool { if raw, ok := item.Body["attachments"].([]any); ok && len(raw) > 0 {
return lo.Contains(bodies[idx].Attachments, item.Rid) 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 len(val) > 0 {
if raw, ok := item.Body[field]; ok { this = lo.Filter(attachments, func(item fmodels.Attachment, _ int) bool {
if str, ok := raw.(string); ok { return lo.Contains(val, item.Rid)
item.Body[field] = lo.FindOrElse(this, fmodels.Attachment{}, func(item fmodels.Attachment) bool { })
return item.Rid == str
})
}
}
} }
item.Body["attachments"] = this item.Body["attachments"] = this
item.Publisher.Account = lo.FindOrElse(users, amodels.Account{}, func(acc amodels.Account) bool { item.Publisher.Account = lo.FindOrElse(users, amodels.Account{}, func(acc amodels.Account) bool {