forked from ccbrown/gggtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss_handler.go
More file actions
110 lines (102 loc) · 2.9 KB
/
Copy pathrss_handler.go
File metadata and controls
110 lines (102 loc) · 2.9 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package server
import (
"fmt"
"time"
"github.com/labstack/echo"
)
type rssGUID struct {
IsPermalink bool `xml:"isPermaLink,attr"`
GUID string `xml:",chardata"`
}
type rssItem struct {
Title string `xml:"title"`
GUID rssGUID `xml:"guid"`
Description string `xml:"description"`
Link string `xml:"link"`
PubDate string `xml:"pubDate"`
}
type rssAtomLink struct {
HRef string `xml:"href,attr"`
Rel string `xml:"rel,attr"`
Type string `xml:"type,attr"`
}
type rssChannel struct {
Title string `xml:"title"`
Description string `xml:"description"`
Link string `xml:"link"`
AtomLink rssAtomLink `xml:"atom:link"`
Items []rssItem `xml:"item"`
}
type rssResponse struct {
XMLName bool `xml:"rss"`
Version string `xml:"version,attr"`
Atom string `xml:"xmlns:atom,attr"`
Channel rssChannel `xml:"channel"`
}
func RSSHandler(db Database) echo.HandlerFunc {
return func(c echo.Context) error {
activity, _, err := db.Activity(LocaleForRequest(c.Request()), c.QueryParam("next"), 50)
if err != nil {
return err
}
response := rssResponse{
Version: "2.0",
Atom: "http://www.w3.org/2005/Atom",
Channel: rssChannel{
Title: "GGG Tracker",
Description: "Latest activity from Grinding Gear Games",
Link: AbsoluteURL(c, ""),
AtomLink: rssAtomLink{
HRef: AbsoluteURL(c, "/rss"),
Rel: "self",
Type: "application/rss+xml",
},
},
}
for _, a := range activity {
switch a.(type) {
case *ForumPost:
post := a.(*ForumPost)
response.Channel.Items = append(response.Channel.Items, rssItem{
Title: post.Poster + " - " + post.ThreadTitle,
Link: post.PostURL(),
GUID: rssGUID{
IsPermalink: false,
GUID: fmt.Sprintf("poe-forum-post-%v", post.Id),
},
Description: post.BodyHTML,
PubDate: post.Time.Format(time.RFC1123Z),
})
case *RedditComment:
comment := a.(*RedditComment)
response.Channel.Items = append(response.Channel.Items, rssItem{
Title: comment.Author + " - " + comment.PostTitle,
Link: comment.CommentURL(),
GUID: rssGUID{
IsPermalink: false,
GUID: "reddit-comment-" + comment.Id,
},
Description: comment.BodyHTML,
PubDate: comment.Time.Format(time.RFC1123Z),
})
case *RedditPost:
post := a.(*RedditPost)
item := rssItem{
Title: post.Author + " - " + post.Title,
Link: "https://www.reddit.com" + post.Permalink,
GUID: rssGUID{
IsPermalink: false,
GUID: "reddit-post-" + post.Id,
},
Description: post.BodyHTML,
PubDate: post.Time.Format(time.RFC1123Z),
}
if item.Description == "" {
item.Description = "<a href=\"" + post.URL + "\">" + post.URL + "</a>"
}
response.Channel.Items = append(response.Channel.Items, item)
}
}
return c.XML(200, response)
}
}