This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathhandle_rss.go
91 lines (78 loc) · 2.2 KB
/
handle_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// This code is in Public Domain. Take all the code you want, I'll just write more.
package main
import (
"fmt"
"io/ioutil"
"net/http"
"time"
atom "github.com/kjk/atomgenerator"
)
func buildForumURL(r *http.Request, forum *Forum) string {
return fmt.Sprintf("http://%s/%s", r.Host, forum.ForumUrl)
}
func buildTopicURL(r *http.Request, forum *Forum, p *Post) string {
return fmt.Sprintf("http://%s/%s/topic?id=%d&post=%d", r.Host, forum.ForumUrl, p.Topic.Id, p.Id)
}
func buildTopicID(r *http.Request, forum *Forum, p *Post) string {
pubDateStr := p.CreatedOn.Format("2006-01-02")
url := fmt.Sprintf("/%s/topic?id=%d&post=%d", forum.ForumUrl, p.Topic.Id, p.Id)
return fmt.Sprintf("tag:%s,%s:%s", r.Host, pubDateStr, url)
}
func handleRss2(w http.ResponseWriter, r *http.Request, all bool) {
forum := mustGetForum(w, r)
if forum == nil {
return
}
var posts []*Post
if all {
posts = forum.Store.GetRecentPosts(25)
} else {
topics, _ := forum.Store.GetTopics(25, 0, false)
posts = make([]*Post, len(topics), len(topics))
for i, t := range topics {
posts[i] = &t.Posts[0]
}
}
pubTime := time.Now()
if len(posts) > 0 {
pubTime = posts[0].CreatedOn
}
feed := &atom.Feed{
Title: forum.Title,
Link: buildForumURL(r, forum),
PubDate: pubTime,
}
for _, p := range posts {
sha1 := p.MessageSha1
msgFilePath := forum.Store.MessageFilePath(sha1)
msg, err := ioutil.ReadFile(msgFilePath)
msgStr := ""
if err != nil {
msgStr = fmt.Sprintf("Error: failed to fetch a message with sha1 %x, file: %s", sha1[:], msgFilePath)
} else {
msgStr = msgToHtml(string(msg))
}
//id := fmt.Sprintf("tag:forums.fofou.org,1999:%s-topic-%d-post-%d", forum.ForumUrl, p.Topic.Id, p.Id)
e := &atom.Entry{
Id: buildTopicID(r, forum, p),
Title: p.Topic.Subject,
PubDate: p.CreatedOn,
Link: buildTopicURL(r, forum, p),
Content: msgStr,
}
feed.AddEntry(e)
}
s, err := feed.GenXml()
if err != nil {
s = []byte("Failed to generate XML feed")
}
w.Write(s)
}
// url: /{forum}/rss
func handleRss(w http.ResponseWriter, r *http.Request) {
handleRss2(w, r, false)
}
// url: /{forum}/rssall
func handleRssAll(w http.ResponseWriter, r *http.Request) {
handleRss2(w, r, true)
}