restore function signature of ExtractAttachments

This commit is contained in:
tobi 2025-04-26 15:02:12 +02:00
commit b2f244aa96
3 changed files with 23 additions and 9 deletions

View file

@ -32,7 +32,6 @@ import (
"code.superseriousbusiness.org/activity/streams/vocab" "code.superseriousbusiness.org/activity/streams/vocab"
"github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/text" "github.com/superseriousbusiness/gotosocial/internal/text"
"github.com/superseriousbusiness/gotosocial/internal/util" "github.com/superseriousbusiness/gotosocial/internal/util"
) )
@ -637,35 +636,40 @@ func ExtractContent(i WithContent) gtsmodel.Content {
// ExtractAttachments attempts to extract barebones // ExtractAttachments attempts to extract barebones
// MediaAttachment objects from given AS interface type. // MediaAttachment objects from given AS interface type.
func ExtractAttachments(i WithAttachment) []*gtsmodel.MediaAttachment { func ExtractAttachments(i WithAttachment) ([]*gtsmodel.MediaAttachment, error) {
attachmentProp := i.GetActivityStreamsAttachment() attachmentProp := i.GetActivityStreamsAttachment()
if attachmentProp == nil { if attachmentProp == nil {
return nil return nil, nil
} }
attachments := make([]*gtsmodel.MediaAttachment, 0, attachmentProp.Len()) var (
attachments = make([]*gtsmodel.MediaAttachment, 0, attachmentProp.Len())
errs gtserror.MultiError
)
for iter := attachmentProp.Begin(); iter != attachmentProp.End(); iter = iter.Next() { for iter := attachmentProp.Begin(); iter != attachmentProp.End(); iter = iter.Next() {
t := iter.GetType() t := iter.GetType()
if t == nil { if t == nil {
errs.Appendf("nil attachment type")
continue continue
} }
attachmentable, ok := ToAttachmentable(t) attachmentable, ok := ToAttachmentable(t)
if !ok { if !ok {
log.Debugf(nil, "could not cast %T to Attachmentable", t) errs.Appendf("could not cast %T to Attachmentable", t)
continue continue
} }
attachment, err := ExtractAttachment(attachmentable) attachment, err := ExtractAttachment(attachmentable)
if err != nil { if err != nil {
log.Debugf(nil, "error extracting attachment: %v", err) errs.Appendf("error extracting attachment: %w", err)
continue continue
} }
attachments = append(attachments, attachment) attachments = append(attachments, attachment)
} }
return attachments return attachments, errs.Combine()
} }
// ExtractAttachment extracts a minimal gtsmodel.Attachment // ExtractAttachment extracts a minimal gtsmodel.Attachment

View file

@ -142,7 +142,14 @@ func (f *Filter) StatusableOK(
} }
// HEURISTIC 6: Are there any media attachments? // HEURISTIC 6: Are there any media attachments?
attachments := ap.ExtractAttachments(statusable) attachments, err := ap.ExtractAttachments(statusable)
if err != nil {
log.Warnf(ctx,
"error(s) extracting attachments for %s: %v",
ap.GetJSONLDId(statusable), err,
)
}
hasAttachments := len(attachments) != 0 hasAttachments := len(attachments) != 0
if hasAttachments { if hasAttachments {
err := errors.New("status has attachment(s)") err := errors.New("status has attachment(s)")

View file

@ -288,7 +288,10 @@ func (c *Converter) ASStatusToStatus(ctx context.Context, statusable ap.Statusab
// status.Attachments // status.Attachments
// //
// Media attachments for later dereferencing. // Media attachments for later dereferencing.
status.Attachments = ap.ExtractAttachments(statusable) status.Attachments, err = ap.ExtractAttachments(statusable)
if err != nil {
log.Warnf(ctx, "error(s) extracting attachments for %s: %v", uri, err)
}
// status.Poll // status.Poll
// //