From cfb6ddfcea3e387a7141d4c65099d2f08bc1732b Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 22 Dec 2019 17:47:56 -0800 Subject: Add support for Atom 'replies' link relation Show comments URL for Atom feeds as per RFC 4685. See https://tools.ietf.org/html/rfc4685#section-4 Note that only the first link with type "text/html" is taken into consideration. --- reader/atom/atom.go | 11 +++++++++++ reader/atom/parser_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/reader/atom/atom.go b/reader/atom/atom.go index 517b43d..677f69a 100644 --- a/reader/atom/atom.go +++ b/reader/atom/atom.go @@ -99,6 +99,7 @@ func (a *atomEntry) Transform() *model.Entry { entry.Content = getContent(a) entry.Title = getTitle(a) entry.Enclosures = getEnclosures(a) + entry.CommentsURL = getRelationURLWithType(a.Links, "replies", "text/html") return entry } @@ -126,6 +127,16 @@ func getRelationURL(links []atomLink, relation string) string { return "" } +func getRelationURLWithType(links []atomLink, relation, contentType string) string { + for _, link := range links { + if strings.ToLower(link.Rel) == relation && strings.ToLower(link.Type) == contentType { + return strings.TrimSpace(link.URL) + } + } + + return "" +} + func getDate(a *atomEntry) time.Time { // Note: The published date represents the original creation date for YouTube feeds. // Example: diff --git a/reader/atom/parser_test.go b/reader/atom/parser_test.go index 37ab32e..8fcf7d0 100644 --- a/reader/atom/parser_test.go +++ b/reader/atom/parser_test.go @@ -65,6 +65,10 @@ func TestParseAtomSample(t *testing.T) { t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL) } + if feed.Entries[0].CommentsURL != "" { + t.Errorf("Incorrect entry Comments URL, got: %s", feed.Entries[0].CommentsURL) + } + if feed.Entries[0].Title != "Atom-Powered Robots Run Amok" { t.Errorf("Incorrect entry title, got: %s", feed.Entries[0].Title) } @@ -729,3 +733,47 @@ A website: http://example.org/ } } } + +func TestParseRepliesLinkRelation(t *testing.T) { + data := ` + + http://www.example.org/myfeed + My Example Feed + 2005-07-28T12:00:00Z + + James + + tag:entries.com,2005:1 + My original entry + 2006-03-01T12:12:12Z + + + + This is my original entry + + ` + + feed, err := Parse(bytes.NewBufferString(data)) + if err != nil { + t.Fatal(err) + } + + if len(feed.Entries) != 1 { + t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries)) + } + + if feed.Entries[0].URL != "http://www.example.org/entries/1" { + t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL) + } + + if feed.Entries[0].CommentsURL != "http://www.example.org/comments.html" { + t.Errorf("Incorrect entry comments URL, got: %s", feed.Entries[0].CommentsURL) + } +} -- cgit v1.2.3