Skip to content

Commit

Permalink
reader/rss: don't add empty tags to RSS items
Browse files Browse the repository at this point in the history
This commit adds a bunch of checks to prevent reader/rss from adding empty tags
to rss items, as well as some minor refactors like nested conditions and loops
unrolling.
  • Loading branch information
jvoisin authored and fguillot committed Mar 25, 2024
1 parent b54fe66 commit f109e32
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions internal/reader/rss/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ func (r *RSSAdapter) BuildFeed(baseURL string) *model.Feed {
entry.Title = findEntryTitle(&item)
if entry.Title == "" {
entry.Title = sanitizer.TruncateHTML(entry.Content, 100)
}
if entry.Title == "" {
entry.Title = entry.URL
if entry.Title == "" {
entry.Title = entry.URL
}
}

entry.Author = findEntryAuthor(&item)
Expand All @@ -101,11 +101,10 @@ func (r *RSSAdapter) BuildFeed(baseURL string) *model.Feed {
}

// Generate the entry hash.
for _, value := range []string{item.GUID.Data, entryURL} {
if value != "" {
entry.Hash = crypto.Hash(value)
break
}
if item.GUID.Data != "" {
entry.Hash = crypto.Hash(item.GUID.Data)
} else if entryURL != "" {
entry.Hash = crypto.Hash(entryURL)
}

// Find CommentsURL if defined.
Expand All @@ -121,12 +120,27 @@ func (r *RSSAdapter) BuildFeed(baseURL string) *model.Feed {
}

// Populate entry categories.
entry.Tags = append(entry.Tags, item.Categories...)
entry.Tags = append(entry.Tags, item.MediaCategories.Labels()...)
for _, tag := range item.Categories {
if tag != "" {
entry.Tags = append(entry.Tags, tag)
}
}
for _, tag := range item.MediaCategories.Labels() {
if tag != "" {
entry.Tags = append(entry.Tags, tag)
}
}
if len(entry.Tags) == 0 {
entry.Tags = append(entry.Tags, r.rss.Channel.Categories...)
entry.Tags = append(entry.Tags, r.rss.Channel.GetItunesCategories()...)

for _, tag := range r.rss.Channel.Categories {
if tag != "" {
entry.Tags = append(entry.Tags, tag)
}
}
for _, tag := range r.rss.Channel.GetItunesCategories() {
if tag != "" {
entry.Tags = append(entry.Tags, tag)
}
}
if r.rss.Channel.GooglePlayCategory.Text != "" {
entry.Tags = append(entry.Tags, r.rss.Channel.GooglePlayCategory.Text)
}
Expand Down

0 comments on commit f109e32

Please sign in to comment.