-
Notifications
You must be signed in to change notification settings - Fork 26
/
blogroll.go
172 lines (161 loc) · 4.45 KB
/
blogroll.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"bytes"
"cmp"
"context"
"io"
"net/http"
"sort"
"strings"
"time"
"github.com/carlmjohnson/requests"
"github.com/kaorimatz/go-opml"
"github.com/samber/lo"
"go.goblog.app/app/pkgs/bufferpool"
"go.goblog.app/app/pkgs/contenttype"
)
const (
defaultBlogrollPath = "/blogroll"
blogrollRefreshSubpath = "/refresh"
blogrollDownloadFile = ".opml"
)
func (bc *configBlog) getBlogrollPath() (bool, string) {
if blogroll := bc.Blogroll; blogroll != nil && blogroll.Enabled {
path := bc.getRelativePath(cmp.Or(blogroll.Path, defaultBlogrollPath))
return true, path
}
return false, ""
}
func (a *goBlog) serveBlogroll(w http.ResponseWriter, r *http.Request) {
blog, bc := a.getBlog(r)
outlines, err, _ := a.blogrollCacheGroup.Do(blog, func() ([]*opml.Outline, error) {
return a.getBlogrollOutlines(blog)
})
if err != nil {
a.error("Blogroll: Failed to get outlines", "err", err)
a.serveError(w, r, "", http.StatusInternalServerError)
return
}
c := bc.Blogroll
_, can := bc.getBlogrollPath()
a.render(w, r, a.renderBlogroll, &renderData{
Canonical: a.getFullAddress(can),
Data: &blogrollRenderData{
title: c.Title,
description: c.Description,
outlines: outlines,
download: can + blogrollDownloadFile,
refresh: can + blogrollRefreshSubpath,
},
})
}
func (a *goBlog) serveBlogrollExport(w http.ResponseWriter, r *http.Request) {
blog, _ := a.getBlog(r)
outlines, err, _ := a.blogrollCacheGroup.Do(blog, func() ([]*opml.Outline, error) {
return a.getBlogrollOutlines(blog)
})
if err != nil {
a.error("Blogroll: Failed to get outlines", "err", err)
a.serveError(w, r, "", http.StatusInternalServerError)
return
}
pr, pw := io.Pipe()
go func() {
_ = pw.CloseWithError(opml.Render(pw, &opml.OPML{
Version: "2.0",
DateCreated: time.Now().UTC(),
Outlines: outlines,
}))
}()
w.Header().Set(contentType, contenttype.XMLUTF8)
_ = pr.CloseWithError(a.min.Get().Minify(contenttype.XML, w, pr))
}
func (a *goBlog) refreshBlogroll(w http.ResponseWriter, r *http.Request) {
blog, bc := a.getBlog(r)
a.db.clearPersistentCache("blogroll_" + blog)
a.cache.purge()
_, brPath := bc.getBlogrollPath()
http.Redirect(w, r, brPath, http.StatusFound)
}
func (a *goBlog) getBlogrollOutlines(blog string) ([]*opml.Outline, error) {
// Get config
config := a.cfg.Blogs[blog].Blogroll
// Check cache
if cache := a.db.loadOutlineCache(blog); cache != nil {
return cache, nil
}
// Make request and parse OPML
pr, pw := io.Pipe()
rb := requests.URL(config.Opml).Client(a.httpClient).ToWriter(pw)
if config.AuthHeader != "" && config.AuthValue != "" {
rb.Header(config.AuthHeader, config.AuthValue)
}
go func() {
_ = pw.CloseWithError(rb.Fetch(context.Background()))
}()
o, err := opml.Parse(pr)
_ = pr.CloseWithError(err)
if err != nil {
return nil, err
}
// Filter and sort
outlines := o.Outlines
if len(config.Categories) > 0 {
filtered := []*opml.Outline{}
for _, category := range config.Categories {
if outline, ok := lo.Find(outlines, func(outline *opml.Outline) bool {
return outline.Title == category || outline.Text == category
}); ok && outline != nil {
outline.Outlines = sortOutlines(outline.Outlines)
filtered = append(filtered, outline)
}
}
outlines = filtered
} else {
outlines = sortOutlines(outlines)
}
// Cache
a.db.cacheOutlines(blog, outlines)
return outlines, nil
}
func (db *database) cacheOutlines(blog string, outlines []*opml.Outline) {
opmlBuffer := bufferpool.Get()
_ = opml.Render(opmlBuffer, &opml.OPML{
Version: "2.0",
DateCreated: time.Now().UTC(),
Outlines: outlines,
})
_ = db.cachePersistently("blogroll_"+blog, opmlBuffer.Bytes())
bufferpool.Put(opmlBuffer)
}
func (db *database) loadOutlineCache(blog string) []*opml.Outline {
data, err := db.retrievePersistentCache("blogroll_" + blog)
if err != nil || data == nil {
return nil
}
o, err := opml.Parse(bytes.NewReader(data))
if err != nil {
return nil
}
if time.Since(o.DateCreated).Minutes() > 60 {
return nil
}
return o.Outlines
}
func sortOutlines(outlines []*opml.Outline) []*opml.Outline {
sort.Slice(outlines, func(i, j int) bool {
name1 := outlines[i].Title
if name1 == "" {
name1 = outlines[i].Text
}
name2 := outlines[j].Title
if name2 == "" {
name2 = outlines[j].Text
}
return strings.ToLower(name1) < strings.ToLower(name2)
})
for _, outline := range outlines {
outline.Outlines = sortOutlines(outline.Outlines)
}
return outlines
}