🐛 Fix attachments in body issue
This commit is contained in:
parent
fbe8e9b270
commit
3c2a14800e
@ -12,6 +12,7 @@ import (
|
|||||||
|
|
||||||
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit"
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit"
|
||||||
|
"github.com/goccy/go-json"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
|
||||||
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
|
||||||
@ -678,16 +679,21 @@ func UpdatePostAttachmentMeta(item models.Post, old ...models.Post) error {
|
|||||||
sameAsOld = reflect.DeepEqual(old[0].Body, item.Body)
|
sameAsOld = reflect.DeepEqual(old[0].Body, item.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var oldBody, newBody models.PostStoryBody
|
||||||
|
if len(old) > 0 {
|
||||||
|
raw, _ := json.Marshal(old[0].Body)
|
||||||
|
json.Unmarshal(raw, &oldBody)
|
||||||
|
}
|
||||||
|
{
|
||||||
|
raw, _ := json.Marshal(item.Body)
|
||||||
|
json.Unmarshal(raw, &newBody)
|
||||||
|
}
|
||||||
var minusAttachments, plusAttachments []string
|
var minusAttachments, plusAttachments []string
|
||||||
if len(old) > 0 && !sameAsOld {
|
if len(old) > 0 && !sameAsOld {
|
||||||
if val, ok := old[0].Body["attachments"].([]string); ok {
|
minusAttachments = append(minusAttachments, oldBody.Attachments...)
|
||||||
minusAttachments = append(minusAttachments, val...)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if len(old) == 0 || !sameAsOld {
|
if len(old) == 0 || !sameAsOld {
|
||||||
if val, ok := item.Body["attachments"].([]string); ok {
|
plusAttachments = append(plusAttachments, newBody.Attachments...)
|
||||||
plusAttachments = append(plusAttachments, val...)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if dat, ok := item.Body["thumbnail"].(string); ok {
|
if dat, ok := item.Body["thumbnail"].(string); ok {
|
||||||
plusAttachments = append(plusAttachments, dat)
|
plusAttachments = append(plusAttachments, dat)
|
||||||
@ -751,13 +757,18 @@ func DeletePost(item models.Post) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Cleaning up related attachments
|
// Cleaning up related attachments
|
||||||
if val, ok := item.Body["attachments"].([]string); ok && len(val) > 0 {
|
var body models.PostStoryBody
|
||||||
|
{
|
||||||
|
raw, _ := json.Marshal(item.Body)
|
||||||
|
json.Unmarshal(raw, &body)
|
||||||
|
}
|
||||||
|
if len(body.Attachments) > 0 {
|
||||||
if item.Publisher.AccountID == nil {
|
if item.Publisher.AccountID == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err := filekit.CountAttachmentUsage(gap.Nx, &pproto.UpdateUsageRequest{
|
err := filekit.CountAttachmentUsage(gap.Nx, &pproto.UpdateUsageRequest{
|
||||||
Rid: lo.Uniq(val),
|
Rid: lo.Uniq(body.Attachments),
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("An error occurred when deleting post attachment...")
|
log.Error().Err(err).Msg("An error occurred when deleting post attachment...")
|
||||||
@ -772,10 +783,16 @@ func DeletePostInBatch(items []models.Post) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var bodies []models.PostStoryBody
|
||||||
|
{
|
||||||
|
raw, _ := json.Marshal(items)
|
||||||
|
json.Unmarshal(raw, &bodies)
|
||||||
|
}
|
||||||
|
|
||||||
var attachments []string
|
var attachments []string
|
||||||
for _, item := range items {
|
for idx := range items {
|
||||||
if val, ok := item.Body["attachments"].([]string); ok && len(val) > 0 {
|
if len(bodies[idx].Attachments) > 0 {
|
||||||
attachments = append(attachments, val...)
|
attachments = append(attachments, bodies[idx].Attachments...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,7 +121,15 @@ 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 val, ok := item.Body["attachments"].([]string); ok && len(val) > 0 {
|
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
|
||||||
|
}
|
||||||
|
if len(val) > 0 {
|
||||||
this = lo.Filter(attachments, func(item fmodels.Attachment, _ int) bool {
|
this = lo.Filter(attachments, func(item fmodels.Attachment, _ int) bool {
|
||||||
return lo.Contains(val, item.Rid)
|
return lo.Contains(val, item.Rid)
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user