[bugfix] Read Bookwyrm Articles more thoroughly (#1410)

This commit is contained in:
tobi 2023-02-02 16:41:02 +01:00 committed by GitHub
commit 271da016b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 123 additions and 118 deletions

View file

@ -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 := &gtsmodel.MediaAttachment{
File: gtsmodel.File{},
}
attachment := &gtsmodel.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 := &gtsmodel.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