From eb2fb30d8d122821e9f988440c0aa9ecdd65451d Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Mon, 17 Jun 2024 13:40:18 +0300 Subject: [PATCH] Add the `isPermaLink` attribute to `guid` in RSS (#107) Adds the optional attribute from the `guid` RSS spec: https://www.rssboard.org/rss-specification#ltguidgtSubelementOfLtitemgt This is a breaking change to the `RssItem` struct as `Guid` is now a struct, not a string. - Related Issue https://github.com/go-gitea/gitea/issues/28734 - Closes #78 - Closes #109 Signed-off-by: Yarden Shoham --- consume_test.go | 20 ++++++++++---------- feed.go | 1 + rss.go | 17 +++++++++++++---- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/consume_test.go b/consume_test.go index 1ccf475..5abe4d1 100644 --- a/consume_test.go +++ b/consume_test.go @@ -46,7 +46,7 @@ var testRssFeedXML = RssFeedXml{ Category: "", Comments: "", Enclosure: (*RssEnclosure)(nil), - Guid: "http://example.com/test/1540941720", + Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941720", IsPermaLink: "true"}, PubDate: "Tue, 30 Oct 2018 23:22:00 GMT", Source: "", }, @@ -60,7 +60,7 @@ var testRssFeedXML = RssFeedXml{ Category: "", Comments: "", Enclosure: (*RssEnclosure)(nil), - Guid: "http://example.com/test/1540941660", + Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941660", IsPermaLink: "true"}, PubDate: "Tue, 30 Oct 2018 23:21:00 GMT", Source: "", }, @@ -74,7 +74,7 @@ var testRssFeedXML = RssFeedXml{ Category: "", Comments: "", Enclosure: (*RssEnclosure)(nil), - Guid: "http://example.com/test/1540941600", + Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941600", IsPermaLink: "true"}, PubDate: "Tue, 30 Oct 2018 23:20:00 GMT", Source: "", }, @@ -88,7 +88,7 @@ var testRssFeedXML = RssFeedXml{ Category: "", Comments: "", Enclosure: (*RssEnclosure)(nil), - Guid: "http://example.com/test/1540941540", + Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941540", IsPermaLink: "true"}, PubDate: "Tue, 30 Oct 2018 23:19:00 GMT", Source: "", }, @@ -102,7 +102,7 @@ var testRssFeedXML = RssFeedXml{ Category: "", Comments: "", Enclosure: (*RssEnclosure)(nil), - Guid: "http://example.com/test/1540941480", + Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941480", IsPermaLink: "true"}, PubDate: "Tue, 30 Oct 2018 23:18:00 GMT", Source: "", }, @@ -116,7 +116,7 @@ var testRssFeedXML = RssFeedXml{ Category: "", Comments: "", Enclosure: (*RssEnclosure)(nil), - Guid: "http://example.com/test/1540941420", + Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941420", IsPermaLink: "true"}, PubDate: "Tue, 30 Oct 2018 23:17:00 GMT", Source: "", }, @@ -130,7 +130,7 @@ var testRssFeedXML = RssFeedXml{ Category: "", Comments: "", Enclosure: (*RssEnclosure)(nil), - Guid: "http://example.com/test/1540941360", + Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941360", IsPermaLink: "true"}, PubDate: "Tue, 30 Oct 2018 23:16:00 GMT", Source: "", }, @@ -144,7 +144,7 @@ var testRssFeedXML = RssFeedXml{ Category: "", Comments: "", Enclosure: (*RssEnclosure)(nil), - Guid: "http://example.com/test/1540941300", + Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941300", IsPermaLink: "true"}, PubDate: "Tue, 30 Oct 2018 23:15:00 GMT", Source: "", }, @@ -158,7 +158,7 @@ var testRssFeedXML = RssFeedXml{ Category: "", Comments: "", Enclosure: (*RssEnclosure)(nil), - Guid: "http://example.com/test/1540941240", + Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941240", IsPermaLink: "true"}, PubDate: "Tue, 30 Oct 2018 23:14:00 GMT", Source: "", }, @@ -172,7 +172,7 @@ var testRssFeedXML = RssFeedXml{ Category: "", Comments: "", Enclosure: (*RssEnclosure)(nil), - Guid: "http://example.com/test/1540941180", + Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941180", IsPermaLink: "true"}, PubDate: "Tue, 30 Oct 2018 23:13:00 GMT", Source: "", }, diff --git a/feed.go b/feed.go index 790a1b6..929c226 100644 --- a/feed.go +++ b/feed.go @@ -32,6 +32,7 @@ type Item struct { Author *Author Description string // used as description in rss, summary in atom Id string // used as guid in rss, id in atom + IsPermaLink string // an optional parameter for guid in rss Updated time.Time Created time.Time Enclosure *Enclosure diff --git a/rss.go b/rss.go index 8c227a8..9326cef 100644 --- a/rss.go +++ b/rss.go @@ -74,9 +74,9 @@ type RssItem struct { Category string `xml:"category,omitempty"` Comments string `xml:"comments,omitempty"` Enclosure *RssEnclosure - Guid string `xml:"guid,omitempty"` // Id used - PubDate string `xml:"pubDate,omitempty"` // created or updated - Source string `xml:"source,omitempty"` + Guid *RssGuid // Id used + PubDate string `xml:"pubDate,omitempty"` // created or updated + Source string `xml:"source,omitempty"` } type RssEnclosure struct { @@ -87,6 +87,13 @@ type RssEnclosure struct { Type string `xml:"type,attr"` } +type RssGuid struct { + //RSS 2.0 http://inessential.com/2002/09/01.php#a2 + XMLName xml.Name `xml:"guid"` + Id string `xml:",chardata"` + IsPermaLink string `xml:"isPermaLink,attr,omitempty"` // "true", "false", or an empty string +} + type Rss struct { *Feed } @@ -96,9 +103,11 @@ func newRssItem(i *Item) *RssItem { item := &RssItem{ Title: i.Title, Description: i.Description, - Guid: i.Id, PubDate: anyTimeFormat(time.RFC1123Z, i.Created, i.Updated), } + if i.Id != "" { + item.Guid = &RssGuid{Id: i.Id, IsPermaLink: i.IsPermaLink} + } if i.Link != nil { item.Link = i.Link.Href }