Minor refactoring of internal/reader/atom/atom_10_adapter.go

- Move the population of the feed's entries into a new function, to make
  `BuildFeed` easier to understand/separate concerns/implementation details
- Use `sort+compact` instead of `compact+sort` to remove duplicates
- Change `if !a { a = } if !a {a = }` constructs into `if !a { a = ; if !a {a = }}`.
  This reduce the number of comparisons, but also improves a tad the
  control-flow readability.
This commit is contained in:
jvoisin 2024-03-19 18:03:04 +01:00 committed by Frédéric Guillot
parent 9df12177eb
commit 4be993e055

View file

@ -65,6 +65,12 @@ func (a *Atom10Adapter) BuildFeed(baseURL string) *model.Feed {
feed.IconURL = absoluteLogoURL feed.IconURL = absoluteLogoURL
} }
} }
feed.Entries = a.populateEntries(feed.SiteURL)
return feed
}
func (a *Atom10Adapter) populateEntries(siteURL string) model.Entries {
entries := make(model.Entries, 0, len(a.atomFeed.Entries))
for _, atomEntry := range a.atomFeed.Entries { for _, atomEntry := range a.atomFeed.Entries {
entry := model.NewEntry() entry := model.NewEntry()
@ -72,7 +78,7 @@ func (a *Atom10Adapter) BuildFeed(baseURL string) *model.Feed {
// Populate the entry URL. // Populate the entry URL.
entry.URL = atomEntry.Links.OriginalLink() entry.URL = atomEntry.Links.OriginalLink()
if entry.URL != "" { if entry.URL != "" {
if absoluteEntryURL, err := urllib.AbsoluteURL(feed.SiteURL, entry.URL); err == nil { if absoluteEntryURL, err := urllib.AbsoluteURL(siteURL, entry.URL); err == nil {
entry.URL = absoluteEntryURL entry.URL = absoluteEntryURL
} }
} }
@ -81,27 +87,27 @@ func (a *Atom10Adapter) BuildFeed(baseURL string) *model.Feed {
entry.Content = atomEntry.Content.Body() entry.Content = atomEntry.Content.Body()
if entry.Content == "" { if entry.Content == "" {
entry.Content = atomEntry.Summary.Body() entry.Content = atomEntry.Summary.Body()
}
if entry.Content == "" { if entry.Content == "" {
entry.Content = atomEntry.FirstMediaDescription() entry.Content = atomEntry.FirstMediaDescription()
} }
}
// Populate the entry title. // Populate the entry title.
entry.Title = atomEntry.Title.Title() entry.Title = atomEntry.Title.Title()
if entry.Title == "" { if entry.Title == "" {
entry.Title = sanitizer.TruncateHTML(entry.Content, 100) entry.Title = sanitizer.TruncateHTML(entry.Content, 100)
}
if entry.Title == "" { if entry.Title == "" {
entry.Title = entry.URL entry.Title = entry.URL
} }
}
// Populate the entry author. // Populate the entry author.
authors := atomEntry.Authors.PersonNames() authors := atomEntry.Authors.PersonNames()
if len(authors) == 0 { if len(authors) == 0 {
authors = append(authors, a.atomFeed.Authors.PersonNames()...) authors = a.atomFeed.Authors.PersonNames()
} }
authors = slices.Compact(authors)
sort.Strings(authors) sort.Strings(authors)
authors = slices.Compact(authors)
entry.Author = strings.Join(authors, ", ") entry.Author = strings.Join(authors, ", ")
// Populate the entry date. // Populate the entry date.
@ -126,13 +132,10 @@ func (a *Atom10Adapter) BuildFeed(baseURL string) *model.Feed {
// Populate categories. // Populate categories.
categories := atomEntry.Categories.CategoryNames() categories := atomEntry.Categories.CategoryNames()
if len(categories) == 0 { if len(categories) == 0 {
categories = append(categories, a.atomFeed.Categories.CategoryNames()...) categories = a.atomFeed.Categories.CategoryNames()
} }
if len(categories) > 0 {
categories = slices.Compact(categories)
sort.Strings(categories) sort.Strings(categories)
entry.Tags = categories entry.Tags = slices.Compact(categories)
}
// Populate the commentsURL if defined. // Populate the commentsURL if defined.
// See https://tools.ietf.org/html/rfc4685#section-4 // See https://tools.ietf.org/html/rfc4685#section-4
@ -166,8 +169,7 @@ func (a *Atom10Adapter) BuildFeed(baseURL string) *model.Feed {
} }
for _, link := range atomEntry.Links { for _, link := range atomEntry.Links {
if strings.EqualFold(link.Rel, "enclosure") { if !strings.EqualFold(link.Rel, "enclosure") || link.Href == "" {
if link.Href == "" {
continue continue
} }
@ -181,7 +183,6 @@ func (a *Atom10Adapter) BuildFeed(baseURL string) *model.Feed {
}) })
} }
} }
}
for _, mediaContent := range atomEntry.AllMediaContents() { for _, mediaContent := range atomEntry.AllMediaContents() {
if _, found := uniqueEnclosuresMap[mediaContent.URL]; !found { if _, found := uniqueEnclosuresMap[mediaContent.URL]; !found {
@ -205,8 +206,8 @@ func (a *Atom10Adapter) BuildFeed(baseURL string) *model.Feed {
} }
} }
feed.Entries = append(feed.Entries, entry) entries = append(entries, entry)
} }
return feed return entries
} }