From f18ded6117bc33ff012b1c1257a6ed7b511b1192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Fri, 14 Jan 2022 20:12:22 -0800 Subject: [PATCH] Add support for multiple authors in Atom feeds --- reader/atom/atom_10.go | 22 +++---- reader/atom/atom_10_test.go | 115 ++++++++++++++++++++++++++++++++++++ reader/atom/atom_common.go | 12 ++++ 3 files changed, 138 insertions(+), 11 deletions(-) diff --git a/reader/atom/atom_10.go b/reader/atom/atom_10.go index de2ccc77..cad011ae 100644 --- a/reader/atom/atom_10.go +++ b/reader/atom/atom_10.go @@ -26,7 +26,7 @@ type atom10Feed struct { XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"` ID string `xml:"id"` Title atom10Text `xml:"title"` - Author atomPerson `xml:"author"` + Authors atomAuthors `xml:"author"` Links atomLinks `xml:"link"` Entries []atom10Entry `xml:"entry"` } @@ -61,7 +61,7 @@ func (a *atom10Feed) Transform(baseURL string) *model.Feed { } if item.Author == "" { - item.Author = a.Author.String() + item.Author = a.Authors.String() } if item.Title == "" { @@ -75,14 +75,14 @@ func (a *atom10Feed) Transform(baseURL string) *model.Feed { } type atom10Entry struct { - ID string `xml:"id"` - Title atom10Text `xml:"title"` - Published string `xml:"published"` - Updated string `xml:"updated"` - Links atomLinks `xml:"link"` - Summary atom10Text `xml:"summary"` - Content atom10Text `xml:"http://www.w3.org/2005/Atom content"` - Author atomPerson `xml:"author"` + ID string `xml:"id"` + Title atom10Text `xml:"title"` + Published string `xml:"published"` + Updated string `xml:"updated"` + Links atomLinks `xml:"link"` + Summary atom10Text `xml:"summary"` + Content atom10Text `xml:"http://www.w3.org/2005/Atom content"` + Authors atomAuthors `xml:"author"` media.Element } @@ -90,7 +90,7 @@ func (a *atom10Entry) Transform() *model.Entry { entry := new(model.Entry) entry.URL = a.Links.originalLink() entry.Date = a.entryDate() - entry.Author = a.Author.String() + entry.Author = a.Authors.String() entry.Hash = a.entryHash() entry.Content = a.entryContent() entry.Title = a.entryTitle() diff --git a/reader/atom/atom_10_test.go b/reader/atom/atom_10_test.go index f08b8040..bbf48bb6 100644 --- a/reader/atom/atom_10_test.go +++ b/reader/atom/atom_10_test.go @@ -732,6 +732,121 @@ func TestParseEntryWithoutAuthorName(t *testing.T) { } } +func TestParseEntryWithMultipleAuthors(t *testing.T) { + data := ` + + Example Feed + + + + + urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a + 2003-12-13T18:30:02Z + Some text. + + Alice + + + Bob + + + + ` + + feed, err := Parse("https://example.org/", bytes.NewBufferString(data)) + if err != nil { + t.Fatal(err) + } + + if feed.Entries[0].Author != "Alice, Bob" { + t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author) + } +} + +func TestParseEntryWithoutAuthor(t *testing.T) { + data := ` + + Example Feed + + + John Doe + + + + + urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a + 2003-12-13T18:30:02Z + Some text. + + + ` + + feed, err := Parse("https://example.org/", bytes.NewBufferString(data)) + if err != nil { + t.Fatal(err) + } + + if feed.Entries[0].Author != "John Doe" { + t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author) + } +} + +func TestParseFeedWithMultipleAuthors(t *testing.T) { + data := ` + + Example Feed + + + Alice + + + Bob + + + + + urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a + 2003-12-13T18:30:02Z + Some text. + + + ` + + feed, err := Parse("https://example.org/", bytes.NewBufferString(data)) + if err != nil { + t.Fatal(err) + } + + if feed.Entries[0].Author != "Alice, Bob" { + t.Errorf("Incorrect entry author, got: %s", feed.Entries[0].Author) + } +} + +func TestParseFeedWithoutAuthor(t *testing.T) { + data := ` + + Example Feed + + + + + urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a + 2003-12-13T18:30:02Z + Some text. + + + ` + + feed, err := Parse("https://example.org/", bytes.NewBufferString(data)) + if err != nil { + t.Fatal(err) + } + + if feed.Entries[0].Author != "" { + t.Errorf("Incorrect entry author, got: %q", feed.Entries[0].Author) + } +} + func TestParseEntryWithEnclosures(t *testing.T) { data := ` diff --git a/reader/atom/atom_common.go b/reader/atom/atom_common.go index 511777b6..f6ead67f 100644 --- a/reader/atom/atom_common.go +++ b/reader/atom/atom_common.go @@ -24,6 +24,18 @@ func (a *atomPerson) String() string { return strings.TrimSpace(name) } +type atomAuthors []*atomPerson + +func (a atomAuthors) String() string { + var authors []string + + for _, person := range a { + authors = append(authors, person.String()) + } + + return strings.Join(authors, ", ") +} + type atomLink struct { URL string `xml:"href,attr"` Type string `xml:"type,attr"`