-
Notifications
You must be signed in to change notification settings - Fork 10
/
rss.go
77 lines (61 loc) · 1.96 KB
/
rss.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package service
import (
"time"
"github.com/EmissarySocial/emissary/model"
"github.com/benpate/data/option"
"github.com/benpate/derp"
"github.com/benpate/exp"
"github.com/gorilla/feeds"
)
// RSS service generates RSS feeds of the available streams in the database
type RSS struct {
streamService *Stream
host string
}
// NewRSS returns a fully initialized RSS service
func NewRSS(streamService *Stream, host string) *RSS {
return &RSS{
streamService: streamService,
host: host,
}
}
// Feed generates an RSS data feed based on the provided query criteria. This feed
// has a lot of incomplete data at the top level, so we're expecting the handler
// that calls this to fill in the rest of the gaps before it passes the values back
// to the requester.
func (rss RSS) Feed(criteria ...exp.Expression) (*feeds.JSONFeed, error) {
filter := exp.And(criteria...)
streams, err := rss.streamService.List(filter, option.SortDesc("publishDate"))
stream := model.NewStream()
if err != nil {
return nil, derp.Wrap(err, "service.rss.Feed", "Error loading streams")
}
result := feeds.JSONFeed{
Items: []*feeds.JSONItem{},
}
for streams.Next(&stream) {
result.Items = append(result.Items, rss.Item(stream))
}
return &result, nil
}
// Item converts a single model.Stream into a feeds.JSONItem
func (rss RSS) Item(stream model.Stream) *feeds.JSONItem {
publishDate := time.Unix(stream.PublishDate, 0)
modifiedDate := time.Unix(stream.Journal.UpdateDate, 0)
url := rss.host + "/" + stream.StreamID.Hex()
return &feeds.JSONItem{
Id: url,
Url: url,
ExternalUrl: url,
Title: stream.Document.Label,
Summary: stream.Document.Summary,
Image: stream.Document.ImageURL,
PublishedDate: &publishDate,
ModifiedDate: &modifiedDate,
Author: &feeds.JSONAuthor{
Name: stream.Document.Author.Name,
Url: stream.Document.Author.ProfileURL,
Avatar: stream.Document.Author.ImageURL,
},
}
}