From 271da016b91d8d575e13be03b440f970cd333ebe Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Thu, 2 Feb 2023 16:41:02 +0100 Subject: [bugfix] Read Bookwyrm Articles more thoroughly (#1410) --- internal/ap/extract.go | 84 ++++++++++++++++---------------------------------- 1 file changed, 27 insertions(+), 57 deletions(-) (limited to 'internal/ap/extract.go') diff --git a/internal/ap/extract.go b/internal/ap/extract.go index ab51b0858..f3ff6d3b2 100644 --- a/internal/ap/extract.go +++ b/internal/ap/extract.go @@ -49,21 +49,22 @@ func ExtractPreferredUsername(i WithPreferredUsername) (string, error) { return u.GetXMLSchemaString(), nil } -// ExtractName returns a string representation of an interface's name property. -func ExtractName(i WithName) (string, error) { +// ExtractName returns a string representation of an interface's name property, +// or an empty string if this is not found. +func ExtractName(i WithName) string { nameProp := i.GetActivityStreamsName() if nameProp == nil { - return "", errors.New("activityStreamsName not found") + return "" } // take the first name string we can find for iter := nameProp.Begin(); iter != nameProp.End(); iter = iter.Next() { if iter.IsXMLSchemaString() && iter.GetXMLSchemaString() != "" { - return iter.GetXMLSchemaString(), nil + return iter.GetXMLSchemaString() } } - return "", errors.New("activityStreamsName not found") + return "" } // ExtractInReplyToURI extracts the inReplyToURI property (if present) from an interface. @@ -243,23 +244,24 @@ func ExtractImageURL(i WithImage) (*url.URL, error) { } // ExtractSummary extracts the summary/content warning of an interface. -func ExtractSummary(i WithSummary) (string, error) { +// Will return an empty string if no summary was present. +func ExtractSummary(i WithSummary) string { summaryProp := i.GetActivityStreamsSummary() if summaryProp == nil || summaryProp.Len() == 0 { // no summary to speak of - return "", nil + return "" } for iter := summaryProp.Begin(); iter != summaryProp.End(); iter = iter.Next() { switch { case iter.IsIRI(): - return iter.GetIRI().String(), nil + return iter.GetIRI().String() case iter.IsXMLSchemaString(): - return iter.GetXMLSchemaString(), nil + return iter.GetXMLSchemaString() } } - return "", nil + return "" } // ExtractDiscoverable extracts the Discoverable boolean of an interface. @@ -364,36 +366,9 @@ func ExtractContent(i WithContent) string { return "" } -// ExtractAttachments returns a slice of attachments on the interface. -func ExtractAttachments(i WithAttachment) ([]*gtsmodel.MediaAttachment, error) { - attachments := []*gtsmodel.MediaAttachment{} - attachmentProp := i.GetActivityStreamsAttachment() - if attachmentProp == nil { - return attachments, nil - } - for iter := attachmentProp.Begin(); iter != attachmentProp.End(); iter = iter.Next() { - t := iter.GetType() - if t == nil { - continue - } - attachmentable, ok := t.(Attachmentable) - if !ok { - continue - } - attachment, err := ExtractAttachment(attachmentable) - if err != nil { - continue - } - attachments = append(attachments, attachment) - } - return attachments, nil -} - // ExtractAttachment returns a gts model of an attachment from an attachmentable interface. func ExtractAttachment(i Attachmentable) (*gtsmodel.MediaAttachment, error) { - attachment := >smodel.MediaAttachment{ - File: gtsmodel.File{}, - } + attachment := >smodel.MediaAttachment{} attachmentURL, err := ExtractURL(i) if err != nil { @@ -402,17 +377,12 @@ func ExtractAttachment(i Attachmentable) (*gtsmodel.MediaAttachment, error) { attachment.RemoteURL = attachmentURL.String() mediaType := i.GetActivityStreamsMediaType() - if mediaType == nil || mediaType.Get() == "" { - return nil, errors.New("no media type") + if mediaType != nil { + attachment.File.ContentType = mediaType.Get() } - attachment.File.ContentType = mediaType.Get() attachment.Type = gtsmodel.FileTypeImage - name, err := ExtractName(i) - if err == nil { - attachment.Description = name - } - + attachment.Description = ExtractName(i) attachment.Blurhash = ExtractBlurhash(i) attachment.Processing = gtsmodel.ProcessingStatusReceived @@ -470,10 +440,11 @@ func ExtractHashtag(i Hashtaggable) (*gtsmodel.Tag, error) { } tag.URL = hrefProp.GetIRI().String() - name, err := ExtractName(i) - if err != nil { - return nil, err + name := ExtractName(i) + if name == "" { + return nil, errors.New("name prop empty") } + tag.Name = strings.TrimPrefix(name, "#") return tag, nil @@ -523,9 +494,9 @@ func ExtractEmoji(i Emojiable) (*gtsmodel.Emoji, error) { emoji.URI = uri.String() emoji.Domain = uri.Host - name, err := ExtractName(i) - if err != nil { - return nil, err + name := ExtractName(i) + if name == "" { + return nil, errors.New("name prop empty") } emoji.Shortcode = strings.Trim(name, ":") @@ -586,14 +557,13 @@ func ExtractMentions(i WithTag) ([]*gtsmodel.Mention, error) { func ExtractMention(i Mentionable) (*gtsmodel.Mention, error) { mention := >smodel.Mention{} - mentionString, err := ExtractName(i) - if err != nil { - return nil, err + mentionString := ExtractName(i) + if mentionString == "" { + return nil, errors.New("name prop empty") } // just make sure the mention string is valid so we can handle it properly later on... - _, _, err = util.ExtractNamestringParts(mentionString) - if err != nil { + if _, _, err := util.ExtractNamestringParts(mentionString); err != nil { return nil, err } mention.NameString = mentionString -- cgit v1.2.3