forked from jlelse/GoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blogroll.go
148 lines (139 loc) · 3.92 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
package main
import (
"bytes"
"context"
"log"
"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"
func (a *goBlog) serveBlogroll(w http.ResponseWriter, r *http.Request) {
blog, bc := a.getBlog(r)
outlines, err, _ := a.blogrollCacheGroup.Do(blog, func() (any, error) {
return a.getBlogrollOutlines(blog)
})
if err != nil {
log.Printf("Failed to get outlines: %v", err)
a.serveError(w, r, "", http.StatusInternalServerError)
return
}
c := bc.Blogroll
can := bc.getRelativePath(defaultIfEmpty(c.Path, defaultBlogrollPath))
a.render(w, r, a.renderBlogroll, &renderData{
Canonical: a.getFullAddress(can),
Data: &blogrollRenderData{
title: c.Title,
description: c.Description,
outlines: outlines.([]*opml.Outline),
download: can + ".opml",
},
})
}
func (a *goBlog) serveBlogrollExport(w http.ResponseWriter, r *http.Request) {
blog, _ := a.getBlog(r)
outlines, err, _ := a.blogrollCacheGroup.Do(blog, func() (any, error) {
return a.getBlogrollOutlines(blog)
})
if err != nil {
log.Printf("Failed to get outlines: %v", err)
a.serveError(w, r, "", http.StatusInternalServerError)
return
}
opmlBuf := bufferpool.Get()
defer bufferpool.Put(opmlBuf)
if err = opml.Render(opmlBuf, &opml.OPML{
Version: "2.0",
DateCreated: time.Now().UTC(),
Outlines: outlines.([]*opml.Outline),
}); err != nil {
log.Printf("Failed to render OPML: %v", err)
a.serveError(w, r, "", http.StatusInternalServerError)
return
}
w.Header().Set(contentType, contenttype.XMLUTF8)
_ = a.min.Get().Minify(contenttype.XML, w, opmlBuf)
}
func (a *goBlog) getBlogrollOutlines(blog string) ([]*opml.Outline, error) {
config := a.cfg.Blogs[blog].Blogroll
if cache := a.db.loadOutlineCache(blog); cache != nil {
return cache, nil
}
rb := requests.URL(config.Opml).Client(a.httpClient).UserAgent(appUserAgent)
if config.AuthHeader != "" && config.AuthValue != "" {
rb.Header(config.AuthHeader, config.AuthValue)
}
var o *opml.OPML
err := rb.Handle(func(r *http.Response) (err error) {
defer r.Body.Close()
o, err = opml.Parse(r.Body)
return
}).Fetch(context.Background())
if err != nil {
return nil, err
}
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)
}
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
}