2020-07-28 19:17:07 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-22 07:20:56 +00:00
|
|
|
"context"
|
2020-07-28 19:17:07 +00:00
|
|
|
"errors"
|
2020-08-05 17:54:04 +00:00
|
|
|
"fmt"
|
2021-07-23 15:26:14 +00:00
|
|
|
"html/template"
|
2020-07-28 19:17:07 +00:00
|
|
|
"net/http"
|
2020-08-24 18:49:33 +00:00
|
|
|
"reflect"
|
2020-08-05 17:54:04 +00:00
|
|
|
"strconv"
|
2020-09-24 15:13:03 +00:00
|
|
|
"strings"
|
2021-07-19 14:32:45 +00:00
|
|
|
"sync"
|
2020-10-06 17:07:48 +00:00
|
|
|
|
2021-03-03 17:19:55 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2020-10-06 17:07:48 +00:00
|
|
|
"github.com/vcraescu/go-paginator"
|
2020-07-28 19:17:07 +00:00
|
|
|
)
|
|
|
|
|
2020-07-30 19:18:13 +00:00
|
|
|
var errPostNotFound = errors.New("post not found")
|
2020-07-28 19:17:07 +00:00
|
|
|
|
2020-10-15 15:32:46 +00:00
|
|
|
type post struct {
|
2021-07-12 14:19:28 +00:00
|
|
|
Path string
|
|
|
|
Content string
|
|
|
|
Published string
|
|
|
|
Updated string
|
|
|
|
Parameters map[string][]string
|
|
|
|
Blog string
|
|
|
|
Section string
|
|
|
|
Status postStatus
|
|
|
|
Priority int
|
2020-10-06 17:07:48 +00:00
|
|
|
// Not persisted
|
2021-08-05 06:09:34 +00:00
|
|
|
Slug string
|
|
|
|
RenderedTitle string
|
|
|
|
renderCache map[bool]template.HTML
|
|
|
|
renderMutex sync.RWMutex
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 20:56:46 +00:00
|
|
|
type postStatus string
|
|
|
|
|
|
|
|
const (
|
|
|
|
statusNil postStatus = ""
|
|
|
|
statusPublished postStatus = "published"
|
|
|
|
statusDraft postStatus = "draft"
|
2021-07-14 13:44:57 +00:00
|
|
|
statusPrivate postStatus = "private"
|
|
|
|
statusUnlisted postStatus = "unlisted"
|
2021-01-15 20:56:46 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) servePost(w http.ResponseWriter, r *http.Request) {
|
2021-08-05 06:09:34 +00:00
|
|
|
p, err := a.getPost(r.URL.Path)
|
2020-07-30 19:18:13 +00:00
|
|
|
if err == errPostNotFound {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serve404(w, r)
|
2020-07-29 14:41:36 +00:00
|
|
|
return
|
2020-07-28 19:17:07 +00:00
|
|
|
} else if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2020-07-29 14:41:36 +00:00
|
|
|
return
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
2021-02-24 12:16:33 +00:00
|
|
|
if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest {
|
2021-06-10 20:09:50 +00:00
|
|
|
if r.URL.Path == a.getRelativePath(p.Blog, "") {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveActivityStreams(p.Blog, w, r)
|
2021-03-23 06:27:12 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveActivityStreamsPost(p, w)
|
2020-10-26 16:37:31 +00:00
|
|
|
return
|
|
|
|
}
|
2020-11-01 17:37:21 +00:00
|
|
|
canonical := p.firstParameter("original")
|
|
|
|
if canonical == "" {
|
2021-06-06 12:39:42 +00:00
|
|
|
canonical = a.fullPostURL(p)
|
2020-11-01 17:37:21 +00:00
|
|
|
}
|
2020-12-23 15:53:10 +00:00
|
|
|
template := templatePost
|
2021-06-11 06:24:41 +00:00
|
|
|
if p.Path == a.getRelativePath(p.Blog, "") {
|
2020-12-23 15:53:10 +00:00
|
|
|
template = templateStaticHome
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
w.Header().Add("Link", fmt.Sprintf("<%s>; rel=shortlink", a.shortPostURL(p)))
|
|
|
|
a.render(w, r, template, &renderData{
|
2021-01-17 11:53:07 +00:00
|
|
|
BlogString: p.Blog,
|
2020-11-01 17:37:21 +00:00
|
|
|
Canonical: canonical,
|
2020-10-15 15:32:46 +00:00
|
|
|
Data: p,
|
2020-10-12 16:47:23 +00:00
|
|
|
})
|
2020-07-28 19:17:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) redirectToRandomPost(rw http.ResponseWriter, r *http.Request) {
|
2021-07-27 10:51:08 +00:00
|
|
|
randomPath, err := a.getRandomPostPath(r.Context().Value(blogKey).(string))
|
2021-03-22 07:20:56 +00:00
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(rw, r, err.Error(), http.StatusInternalServerError)
|
2021-03-22 07:20:56 +00:00
|
|
|
return
|
2021-01-20 12:38:24 +00:00
|
|
|
}
|
2021-03-22 07:20:56 +00:00
|
|
|
http.Redirect(rw, r, randomPath, http.StatusFound)
|
2021-01-20 12:38:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-24 18:49:33 +00:00
|
|
|
type postPaginationAdapter struct {
|
2020-10-19 18:25:30 +00:00
|
|
|
config *postsRequestConfig
|
2020-11-17 16:38:17 +00:00
|
|
|
nums int64
|
2021-08-05 06:09:34 +00:00
|
|
|
a *goBlog
|
2020-08-24 18:49:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 16:38:17 +00:00
|
|
|
func (p *postPaginationAdapter) Nums() (int64, error) {
|
2020-08-24 18:49:33 +00:00
|
|
|
if p.nums == 0 {
|
2021-08-05 06:09:34 +00:00
|
|
|
nums, _ := p.a.db.countPosts(p.config)
|
2020-11-17 16:38:17 +00:00
|
|
|
p.nums = int64(nums)
|
2020-08-24 18:49:33 +00:00
|
|
|
}
|
2020-11-17 16:38:17 +00:00
|
|
|
return p.nums, nil
|
2020-08-24 18:49:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *postPaginationAdapter) Slice(offset, length int, data interface{}) error {
|
2020-08-31 19:12:43 +00:00
|
|
|
modifiedConfig := *p.config
|
|
|
|
modifiedConfig.offset = offset
|
|
|
|
modifiedConfig.limit = length
|
|
|
|
|
2021-08-05 06:09:34 +00:00
|
|
|
posts, err := p.a.getPosts(&modifiedConfig)
|
2020-08-24 18:49:33 +00:00
|
|
|
reflect.ValueOf(data).Elem().Set(reflect.ValueOf(&posts).Elem())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) serveHome(w http.ResponseWriter, r *http.Request) {
|
2021-07-27 10:51:08 +00:00
|
|
|
blog := r.Context().Value(blogKey).(string)
|
2021-03-22 07:20:56 +00:00
|
|
|
if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveActivityStreams(blog, w, r)
|
2021-03-22 07:20:56 +00:00
|
|
|
return
|
2020-10-26 16:37:31 +00:00
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
2021-06-10 20:09:50 +00:00
|
|
|
path: a.getRelativePath(blog, ""),
|
2021-03-22 07:20:56 +00:00
|
|
|
})))
|
2020-08-25 18:55:32 +00:00
|
|
|
}
|
|
|
|
|
2021-07-14 13:44:57 +00:00
|
|
|
func (a *goBlog) serveDrafts(w http.ResponseWriter, r *http.Request) {
|
2021-07-27 10:51:08 +00:00
|
|
|
blog := r.Context().Value(blogKey).(string)
|
2021-07-14 13:44:57 +00:00
|
|
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
|
|
|
path: a.getRelativePath(blog, "/editor/drafts"),
|
|
|
|
title: a.ts.GetTemplateStringVariant(a.cfg.Blogs[blog].Lang, "drafts"),
|
|
|
|
status: statusDraft,
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) servePrivate(w http.ResponseWriter, r *http.Request) {
|
2021-07-27 10:51:08 +00:00
|
|
|
blog := r.Context().Value(blogKey).(string)
|
2021-07-14 13:44:57 +00:00
|
|
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
|
|
|
path: a.getRelativePath(blog, "/editor/private"),
|
|
|
|
title: a.ts.GetTemplateStringVariant(a.cfg.Blogs[blog].Lang, "privateposts"),
|
|
|
|
status: statusPrivate,
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) serveUnlisted(w http.ResponseWriter, r *http.Request) {
|
2021-07-27 10:51:08 +00:00
|
|
|
blog := r.Context().Value(blogKey).(string)
|
2021-07-14 13:44:57 +00:00
|
|
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
|
|
|
path: a.getRelativePath(blog, "/editor/unlisted"),
|
|
|
|
title: a.ts.GetTemplateStringVariant(a.cfg.Blogs[blog].Lang, "unlistedposts"),
|
|
|
|
status: statusUnlisted,
|
|
|
|
})))
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) serveDate(w http.ResponseWriter, r *http.Request) {
|
2021-03-22 07:20:56 +00:00
|
|
|
var year, month, day int
|
|
|
|
if ys := chi.URLParam(r, "year"); ys != "" && ys != "x" {
|
|
|
|
year, _ = strconv.Atoi(ys)
|
|
|
|
}
|
|
|
|
if ms := chi.URLParam(r, "month"); ms != "" && ms != "x" {
|
|
|
|
month, _ = strconv.Atoi(ms)
|
|
|
|
}
|
|
|
|
if ds := chi.URLParam(r, "day"); ds != "" {
|
|
|
|
day, _ = strconv.Atoi(ds)
|
|
|
|
}
|
|
|
|
if year == 0 && month == 0 && day == 0 {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serve404(w, r)
|
2021-03-22 07:20:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var title, dPath strings.Builder
|
2021-02-26 09:31:38 +00:00
|
|
|
if year != 0 {
|
2021-03-22 07:20:56 +00:00
|
|
|
ys := fmt.Sprintf("%0004d", year)
|
|
|
|
title.WriteString(ys)
|
|
|
|
dPath.WriteString(ys)
|
2021-02-26 09:31:38 +00:00
|
|
|
} else {
|
|
|
|
title.WriteString("XXXX")
|
2021-03-22 07:20:56 +00:00
|
|
|
dPath.WriteString("x")
|
2021-02-26 09:31:38 +00:00
|
|
|
}
|
|
|
|
if month != 0 {
|
|
|
|
title.WriteString(fmt.Sprintf("-%02d", month))
|
2021-03-22 07:20:56 +00:00
|
|
|
dPath.WriteString(fmt.Sprintf("/%02d", month))
|
2021-02-26 09:31:38 +00:00
|
|
|
} else if day != 0 {
|
|
|
|
title.WriteString("-XX")
|
2021-03-22 07:20:56 +00:00
|
|
|
dPath.WriteString("/x")
|
2021-02-26 09:31:38 +00:00
|
|
|
}
|
|
|
|
if day != 0 {
|
|
|
|
title.WriteString(fmt.Sprintf("-%02d", day))
|
2021-03-22 07:20:56 +00:00
|
|
|
dPath.WriteString(fmt.Sprintf("/%02d", day))
|
2020-12-13 14:16:47 +00:00
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
|
2021-07-27 10:51:08 +00:00
|
|
|
path: a.getRelativePath(r.Context().Value(blogKey).(string), dPath.String()),
|
2020-12-13 14:16:47 +00:00
|
|
|
year: year,
|
|
|
|
month: month,
|
2020-12-26 19:40:22 +00:00
|
|
|
day: day,
|
2021-02-26 09:31:38 +00:00
|
|
|
title: title.String(),
|
2021-03-22 07:20:56 +00:00
|
|
|
})))
|
2020-12-13 14:16:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-21 16:03:05 +00:00
|
|
|
type indexConfig struct {
|
2020-12-26 19:40:22 +00:00
|
|
|
blog string
|
|
|
|
path string
|
2021-07-12 14:19:28 +00:00
|
|
|
section *configSection
|
|
|
|
tax *configTaxonomy
|
2020-12-26 19:40:22 +00:00
|
|
|
taxValue string
|
|
|
|
parameter string
|
|
|
|
year, month, day int
|
|
|
|
title string
|
|
|
|
description string
|
|
|
|
summaryTemplate string
|
2021-07-14 13:44:57 +00:00
|
|
|
status postStatus
|
2020-09-21 16:03:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 14:19:28 +00:00
|
|
|
const defaultPhotosPath = "/photos"
|
|
|
|
|
2021-06-29 15:07:08 +00:00
|
|
|
const indexConfigKey contextKey = "indexConfig"
|
2021-03-22 07:20:56 +00:00
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
|
2021-03-22 07:20:56 +00:00
|
|
|
ic := r.Context().Value(indexConfigKey).(*indexConfig)
|
|
|
|
blog := ic.blog
|
|
|
|
if blog == "" {
|
2021-07-27 10:51:08 +00:00
|
|
|
blog, _ = r.Context().Value(blogKey).(string)
|
2021-03-22 07:20:56 +00:00
|
|
|
}
|
|
|
|
search := chi.URLParam(r, "search")
|
|
|
|
if search != "" {
|
2021-07-12 14:19:28 +00:00
|
|
|
// Decode and sanitize search
|
2021-09-01 09:14:49 +00:00
|
|
|
search = cleanHTMLText(searchDecode(search))
|
2021-03-22 07:20:56 +00:00
|
|
|
}
|
|
|
|
pageNoString := chi.URLParam(r, "page")
|
|
|
|
pageNo, _ := strconv.Atoi(pageNoString)
|
|
|
|
var sections []string
|
|
|
|
if ic.section != nil {
|
|
|
|
sections = []string{ic.section.Name}
|
|
|
|
} else {
|
2021-06-06 12:39:42 +00:00
|
|
|
for sectionKey := range a.cfg.Blogs[blog].Sections {
|
2021-03-22 07:20:56 +00:00
|
|
|
sections = append(sections, sectionKey)
|
2020-09-21 16:03:05 +00:00
|
|
|
}
|
2020-08-05 17:14:10 +00:00
|
|
|
}
|
2021-07-14 13:44:57 +00:00
|
|
|
status := ic.status
|
|
|
|
if status == statusNil {
|
|
|
|
status = statusPublished
|
|
|
|
}
|
2021-03-22 07:20:56 +00:00
|
|
|
p := paginator.New(&postPaginationAdapter{config: &postsRequestConfig{
|
|
|
|
blog: blog,
|
|
|
|
sections: sections,
|
|
|
|
taxonomy: ic.tax,
|
|
|
|
taxonomyValue: ic.taxValue,
|
|
|
|
parameter: ic.parameter,
|
|
|
|
search: search,
|
|
|
|
publishedYear: ic.year,
|
|
|
|
publishedMonth: ic.month,
|
|
|
|
publishedDay: ic.day,
|
2021-07-14 13:44:57 +00:00
|
|
|
status: status,
|
2021-07-12 14:19:28 +00:00
|
|
|
priorityOrder: true,
|
2021-08-05 06:09:34 +00:00
|
|
|
}, a: a}, a.cfg.Blogs[blog].Pagination)
|
2021-03-22 07:20:56 +00:00
|
|
|
p.SetPage(pageNo)
|
|
|
|
var posts []*post
|
|
|
|
err := p.Results(&posts)
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2021-03-22 07:20:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Meta
|
|
|
|
title := ic.title
|
|
|
|
description := ic.description
|
|
|
|
if ic.tax != nil {
|
|
|
|
title = fmt.Sprintf("%s: %s", ic.tax.Title, ic.taxValue)
|
|
|
|
} else if ic.section != nil {
|
|
|
|
title = ic.section.Title
|
|
|
|
description = ic.section.Description
|
|
|
|
} else if search != "" {
|
2021-06-06 12:39:42 +00:00
|
|
|
title = fmt.Sprintf("%s: %s", a.cfg.Blogs[blog].Search.Title, search)
|
2021-03-22 07:20:56 +00:00
|
|
|
}
|
|
|
|
// Check if feed
|
|
|
|
if ft := feedType(chi.URLParam(r, "feed")); ft != noFeed {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.generateFeed(blog, ft, w, r, posts, title, description)
|
2021-03-22 07:20:56 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Path
|
|
|
|
path := ic.path
|
|
|
|
if strings.Contains(path, searchPlaceholder) {
|
|
|
|
path = strings.ReplaceAll(path, searchPlaceholder, searchEncode(search))
|
|
|
|
}
|
|
|
|
// Navigation
|
|
|
|
var hasPrev, hasNext bool
|
|
|
|
var prevPage, nextPage int
|
|
|
|
var prevPath, nextPath string
|
|
|
|
hasPrev, _ = p.HasPrev()
|
|
|
|
if hasPrev {
|
|
|
|
prevPage, _ = p.PrevPage()
|
|
|
|
} else {
|
|
|
|
prevPage, _ = p.Page()
|
|
|
|
}
|
|
|
|
if prevPage < 2 {
|
|
|
|
prevPath = path
|
|
|
|
} else {
|
2021-06-10 20:09:50 +00:00
|
|
|
prevPath = fmt.Sprintf("%s/page/%d", strings.TrimSuffix(path, "/"), prevPage)
|
2021-03-22 07:20:56 +00:00
|
|
|
}
|
|
|
|
hasNext, _ = p.HasNext()
|
|
|
|
if hasNext {
|
|
|
|
nextPage, _ = p.NextPage()
|
|
|
|
} else {
|
|
|
|
nextPage, _ = p.Page()
|
|
|
|
}
|
2021-06-10 20:09:50 +00:00
|
|
|
nextPath = fmt.Sprintf("%s/page/%d", strings.TrimSuffix(path, "/"), nextPage)
|
2021-03-22 07:20:56 +00:00
|
|
|
summaryTemplate := ic.summaryTemplate
|
|
|
|
if summaryTemplate == "" {
|
|
|
|
summaryTemplate = templateSummary
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.render(w, r, templateIndex, &renderData{
|
2021-03-22 07:20:56 +00:00
|
|
|
BlogString: blog,
|
2021-06-10 20:09:50 +00:00
|
|
|
Canonical: a.getFullAddress(path),
|
2021-03-22 07:20:56 +00:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"Title": title,
|
|
|
|
"Description": description,
|
|
|
|
"Posts": posts,
|
|
|
|
"HasPrev": hasPrev,
|
|
|
|
"HasNext": hasNext,
|
2021-06-10 20:09:50 +00:00
|
|
|
"First": path,
|
|
|
|
"Prev": prevPath,
|
|
|
|
"Next": nextPath,
|
2021-03-22 07:20:56 +00:00
|
|
|
"SummaryTemplate": summaryTemplate,
|
|
|
|
},
|
|
|
|
})
|
2020-08-05 17:14:10 +00:00
|
|
|
}
|