Skip to content

Commit eb2fb30

Browse files
authored
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 go-gitea/gitea#28734 - Closes #78 - Closes #109 Signed-off-by: Yarden Shoham <git@yardenshoham.com>
1 parent 51ef1e4 commit eb2fb30

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

consume_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var testRssFeedXML = RssFeedXml{
4646
Category: "",
4747
Comments: "",
4848
Enclosure: (*RssEnclosure)(nil),
49-
Guid: "http://example.com/test/1540941720",
49+
Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941720", IsPermaLink: "true"},
5050
PubDate: "Tue, 30 Oct 2018 23:22:00 GMT",
5151
Source: "",
5252
},
@@ -60,7 +60,7 @@ var testRssFeedXML = RssFeedXml{
6060
Category: "",
6161
Comments: "",
6262
Enclosure: (*RssEnclosure)(nil),
63-
Guid: "http://example.com/test/1540941660",
63+
Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941660", IsPermaLink: "true"},
6464
PubDate: "Tue, 30 Oct 2018 23:21:00 GMT",
6565
Source: "",
6666
},
@@ -74,7 +74,7 @@ var testRssFeedXML = RssFeedXml{
7474
Category: "",
7575
Comments: "",
7676
Enclosure: (*RssEnclosure)(nil),
77-
Guid: "http://example.com/test/1540941600",
77+
Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941600", IsPermaLink: "true"},
7878
PubDate: "Tue, 30 Oct 2018 23:20:00 GMT",
7979
Source: "",
8080
},
@@ -88,7 +88,7 @@ var testRssFeedXML = RssFeedXml{
8888
Category: "",
8989
Comments: "",
9090
Enclosure: (*RssEnclosure)(nil),
91-
Guid: "http://example.com/test/1540941540",
91+
Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941540", IsPermaLink: "true"},
9292
PubDate: "Tue, 30 Oct 2018 23:19:00 GMT",
9393
Source: "",
9494
},
@@ -102,7 +102,7 @@ var testRssFeedXML = RssFeedXml{
102102
Category: "",
103103
Comments: "",
104104
Enclosure: (*RssEnclosure)(nil),
105-
Guid: "http://example.com/test/1540941480",
105+
Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941480", IsPermaLink: "true"},
106106
PubDate: "Tue, 30 Oct 2018 23:18:00 GMT",
107107
Source: "",
108108
},
@@ -116,7 +116,7 @@ var testRssFeedXML = RssFeedXml{
116116
Category: "",
117117
Comments: "",
118118
Enclosure: (*RssEnclosure)(nil),
119-
Guid: "http://example.com/test/1540941420",
119+
Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941420", IsPermaLink: "true"},
120120
PubDate: "Tue, 30 Oct 2018 23:17:00 GMT",
121121
Source: "",
122122
},
@@ -130,7 +130,7 @@ var testRssFeedXML = RssFeedXml{
130130
Category: "",
131131
Comments: "",
132132
Enclosure: (*RssEnclosure)(nil),
133-
Guid: "http://example.com/test/1540941360",
133+
Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941360", IsPermaLink: "true"},
134134
PubDate: "Tue, 30 Oct 2018 23:16:00 GMT",
135135
Source: "",
136136
},
@@ -144,7 +144,7 @@ var testRssFeedXML = RssFeedXml{
144144
Category: "",
145145
Comments: "",
146146
Enclosure: (*RssEnclosure)(nil),
147-
Guid: "http://example.com/test/1540941300",
147+
Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941300", IsPermaLink: "true"},
148148
PubDate: "Tue, 30 Oct 2018 23:15:00 GMT",
149149
Source: "",
150150
},
@@ -158,7 +158,7 @@ var testRssFeedXML = RssFeedXml{
158158
Category: "",
159159
Comments: "",
160160
Enclosure: (*RssEnclosure)(nil),
161-
Guid: "http://example.com/test/1540941240",
161+
Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941240", IsPermaLink: "true"},
162162
PubDate: "Tue, 30 Oct 2018 23:14:00 GMT",
163163
Source: "",
164164
},
@@ -172,7 +172,7 @@ var testRssFeedXML = RssFeedXml{
172172
Category: "",
173173
Comments: "",
174174
Enclosure: (*RssEnclosure)(nil),
175-
Guid: "http://example.com/test/1540941180",
175+
Guid: &RssGuid{XMLName: xml.Name{Local: "guid"}, Id: "http://example.com/test/1540941180", IsPermaLink: "true"},
176176
PubDate: "Tue, 30 Oct 2018 23:13:00 GMT",
177177
Source: "",
178178
},

feed.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Item struct {
3232
Author *Author
3333
Description string // used as description in rss, summary in atom
3434
Id string // used as guid in rss, id in atom
35+
IsPermaLink string // an optional parameter for guid in rss
3536
Updated time.Time
3637
Created time.Time
3738
Enclosure *Enclosure

rss.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ type RssItem struct {
7474
Category string `xml:"category,omitempty"`
7575
Comments string `xml:"comments,omitempty"`
7676
Enclosure *RssEnclosure
77-
Guid string `xml:"guid,omitempty"` // Id used
78-
PubDate string `xml:"pubDate,omitempty"` // created or updated
79-
Source string `xml:"source,omitempty"`
77+
Guid *RssGuid // Id used
78+
PubDate string `xml:"pubDate,omitempty"` // created or updated
79+
Source string `xml:"source,omitempty"`
8080
}
8181

8282
type RssEnclosure struct {
@@ -87,6 +87,13 @@ type RssEnclosure struct {
8787
Type string `xml:"type,attr"`
8888
}
8989

90+
type RssGuid struct {
91+
//RSS 2.0 <guid isPermaLink="true">http://inessential.com/2002/09/01.php#a2</guid>
92+
XMLName xml.Name `xml:"guid"`
93+
Id string `xml:",chardata"`
94+
IsPermaLink string `xml:"isPermaLink,attr,omitempty"` // "true", "false", or an empty string
95+
}
96+
9097
type Rss struct {
9198
*Feed
9299
}
@@ -96,9 +103,11 @@ func newRssItem(i *Item) *RssItem {
96103
item := &RssItem{
97104
Title: i.Title,
98105
Description: i.Description,
99-
Guid: i.Id,
100106
PubDate: anyTimeFormat(time.RFC1123Z, i.Created, i.Updated),
101107
}
108+
if i.Id != "" {
109+
item.Guid = &RssGuid{Id: i.Id, IsPermaLink: i.IsPermaLink}
110+
}
102111
if i.Link != nil {
103112
item.Link = i.Link.Href
104113
}

0 commit comments

Comments
 (0)