2021-05-08 19:22:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-05-10 15:37:34 +00:00
|
|
|
"bytes"
|
2022-01-04 09:37:48 +00:00
|
|
|
"context"
|
2021-05-09 07:08:31 +00:00
|
|
|
"log"
|
2021-05-08 19:22:48 +00:00
|
|
|
"net/http"
|
2021-05-08 19:44:36 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2021-05-08 19:22:48 +00:00
|
|
|
"time"
|
|
|
|
|
2022-01-04 09:37:48 +00:00
|
|
|
"github.com/carlmjohnson/requests"
|
2021-05-08 19:22:48 +00:00
|
|
|
"github.com/kaorimatz/go-opml"
|
2022-03-16 07:28:03 +00:00
|
|
|
"github.com/samber/lo"
|
2022-02-16 12:02:08 +00:00
|
|
|
"go.goblog.app/app/pkgs/bufferpool"
|
2021-06-28 20:17:18 +00:00
|
|
|
"go.goblog.app/app/pkgs/contenttype"
|
2021-05-08 19:22:48 +00:00
|
|
|
)
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
const defaultBlogrollPath = "/blogroll"
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) serveBlogroll(w http.ResponseWriter, r *http.Request) {
|
2021-11-04 07:52:16 +00:00
|
|
|
blog, bc := a.getBlog(r)
|
2022-03-16 07:28:03 +00:00
|
|
|
outlines, err, _ := a.blogrollCacheGroup.Do(blog, func() (any, error) {
|
2021-06-06 12:39:42 +00:00
|
|
|
return a.getBlogrollOutlines(blog)
|
2021-05-08 19:22:48 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
log.Printf("Failed to get outlines: %v", err)
|
|
|
|
a.serveError(w, r, "", http.StatusInternalServerError)
|
2021-05-08 19:22:48 +00:00
|
|
|
return
|
|
|
|
}
|
2021-11-04 07:52:16 +00:00
|
|
|
c := bc.Blogroll
|
2021-12-20 13:00:11 +00:00
|
|
|
can := bc.getRelativePath(defaultIfEmpty(c.Path, defaultBlogrollPath))
|
2022-01-30 15:40:53 +00:00
|
|
|
a.render(w, r, a.renderBlogroll, &renderData{
|
2021-12-20 13:00:11 +00:00
|
|
|
Canonical: a.getFullAddress(can),
|
2022-01-20 17:22:10 +00:00
|
|
|
Data: &blogrollRenderData{
|
|
|
|
title: c.Title,
|
|
|
|
description: c.Description,
|
|
|
|
outlines: outlines.([]*opml.Outline),
|
|
|
|
download: can + ".opml",
|
2021-05-08 19:22:48 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) serveBlogrollExport(w http.ResponseWriter, r *http.Request) {
|
2021-11-04 07:52:16 +00:00
|
|
|
blog, _ := a.getBlog(r)
|
2022-03-16 07:28:03 +00:00
|
|
|
outlines, err, _ := a.blogrollCacheGroup.Do(blog, func() (any, error) {
|
2021-06-06 12:39:42 +00:00
|
|
|
return a.getBlogrollOutlines(blog)
|
2021-05-08 19:22:48 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
log.Printf("Failed to get outlines: %v", err)
|
|
|
|
a.serveError(w, r, "", http.StatusInternalServerError)
|
2021-05-08 19:22:48 +00:00
|
|
|
return
|
|
|
|
}
|
2022-02-16 12:02:08 +00:00
|
|
|
opmlBuf := bufferpool.Get()
|
|
|
|
defer bufferpool.Put(opmlBuf)
|
2022-04-10 09:46:35 +00:00
|
|
|
if err = opml.Render(opmlBuf, &opml.OPML{
|
2021-05-08 19:22:48 +00:00
|
|
|
Version: "2.0",
|
|
|
|
DateCreated: time.Now().UTC(),
|
|
|
|
Outlines: outlines.([]*opml.Outline),
|
2022-04-10 09:46:35 +00:00
|
|
|
}); err != nil {
|
2022-02-16 12:02:08 +00:00
|
|
|
log.Printf("Failed to render OPML: %v", err)
|
|
|
|
a.serveError(w, r, "", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Set(contentType, contenttype.XMLUTF8)
|
2022-04-10 09:46:35 +00:00
|
|
|
_ = a.min.Get().Minify(contenttype.XML, w, opmlBuf)
|
2021-05-08 19:22:48 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) getBlogrollOutlines(blog string) ([]*opml.Outline, error) {
|
|
|
|
config := a.cfg.Blogs[blog].Blogroll
|
|
|
|
if cache := a.db.loadOutlineCache(blog); cache != nil {
|
2021-05-10 15:37:34 +00:00
|
|
|
return cache, nil
|
2021-05-08 19:22:48 +00:00
|
|
|
}
|
2022-12-14 09:21:32 +00:00
|
|
|
rb := requests.URL(config.Opml).Client(a.httpClient)
|
2021-05-08 19:22:48 +00:00
|
|
|
if config.AuthHeader != "" && config.AuthValue != "" {
|
2022-01-04 09:37:48 +00:00
|
|
|
rb.Header(config.AuthHeader, config.AuthValue)
|
2021-05-08 19:22:48 +00:00
|
|
|
}
|
2022-01-04 09:37:48 +00:00
|
|
|
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())
|
2021-05-08 19:22:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
outlines := o.Outlines
|
|
|
|
if len(config.Categories) > 0 {
|
|
|
|
filtered := []*opml.Outline{}
|
|
|
|
for _, category := range config.Categories {
|
2022-03-16 07:28:03 +00:00
|
|
|
if outline, ok := lo.Find(outlines, func(outline *opml.Outline) bool {
|
2021-05-08 19:22:48 +00:00
|
|
|
return outline.Title == category || outline.Text == category
|
2022-03-16 07:28:03 +00:00
|
|
|
}); ok && outline != nil {
|
2021-05-08 19:44:36 +00:00
|
|
|
outline.Outlines = sortOutlines(outline.Outlines)
|
2021-05-08 19:22:48 +00:00
|
|
|
filtered = append(filtered, outline)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outlines = filtered
|
2021-05-08 19:44:36 +00:00
|
|
|
} else {
|
|
|
|
outlines = sortOutlines(outlines)
|
2021-05-08 19:22:48 +00:00
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.db.cacheOutlines(blog, outlines)
|
2021-05-08 19:22:48 +00:00
|
|
|
return outlines, nil
|
|
|
|
}
|
2021-05-08 19:44:36 +00:00
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (db *database) cacheOutlines(blog string, outlines []*opml.Outline) {
|
2022-02-22 15:52:03 +00:00
|
|
|
opmlBuffer := bufferpool.Get()
|
|
|
|
_ = opml.Render(opmlBuffer, &opml.OPML{
|
2021-05-10 15:37:34 +00:00
|
|
|
Version: "2.0",
|
|
|
|
DateCreated: time.Now().UTC(),
|
|
|
|
Outlines: outlines,
|
|
|
|
})
|
2021-06-06 12:39:42 +00:00
|
|
|
_ = db.cachePersistently("blogroll_"+blog, opmlBuffer.Bytes())
|
2022-02-22 15:52:03 +00:00
|
|
|
bufferpool.Put(opmlBuffer)
|
2021-05-10 15:37:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (db *database) loadOutlineCache(blog string) []*opml.Outline {
|
|
|
|
data, err := db.retrievePersistentCache("blogroll_" + blog)
|
2021-05-10 15:37:34 +00:00
|
|
|
if err != nil || data == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-04 09:37:48 +00:00
|
|
|
o, err := opml.Parse(bytes.NewReader(data))
|
2021-05-10 15:37:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if time.Since(o.DateCreated).Minutes() > 60 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return o.Outlines
|
|
|
|
}
|
|
|
|
|
2021-05-08 19:44:36 +00:00
|
|
|
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
|
|
|
|
}
|