owl-blogs/post.go

358 lines
7.5 KiB
Go
Raw Normal View History

2022-08-03 14:55:48 +00:00
package owl
2022-07-21 17:02:37 +00:00
import (
2022-07-21 17:44:07 +00:00
"bytes"
"errors"
2022-07-21 17:02:37 +00:00
"io/ioutil"
2022-09-04 15:10:40 +00:00
"net/url"
2022-08-23 15:59:17 +00:00
"os"
2022-07-21 17:02:37 +00:00
"path"
2022-09-01 19:53:06 +00:00
"sort"
2022-09-04 15:10:40 +00:00
"time"
2022-07-21 17:44:07 +00:00
"github.com/yuin/goldmark"
2022-07-27 19:26:37 +00:00
"github.com/yuin/goldmark/extension"
2022-07-21 17:44:07 +00:00
"github.com/yuin/goldmark/parser"
2022-08-21 09:31:48 +00:00
"github.com/yuin/goldmark/renderer/html"
"gopkg.in/yaml.v2"
2022-07-21 17:02:37 +00:00
)
type Post struct {
user *User
id string
title string
metaLoaded bool
meta PostMeta
2022-07-21 17:02:37 +00:00
}
type PostMeta struct {
Title string `yaml:"title"`
Aliases []string `yaml:"aliases"`
Date string `yaml:"date"`
2022-08-20 20:35:51 +00:00
Draft bool `yaml:"draft"`
}
type PostWebmetions struct {
Incoming []WebmentionIn `ymal:"incoming"`
Outgoing []WebmentionOut `ymal:"outgoing"`
2022-09-04 13:03:16 +00:00
}
2022-08-03 17:41:13 +00:00
func (post Post) Id() string {
return post.id
}
2022-07-21 17:02:37 +00:00
func (post Post) Dir() string {
return path.Join(post.user.Dir(), "public", post.id)
}
func (post Post) WebmentionsFile() string {
return path.Join(post.Dir(), "webmentions.yml")
2022-09-04 13:32:37 +00:00
}
2022-08-01 17:50:29 +00:00
func (post Post) MediaDir() string {
return path.Join(post.Dir(), "media")
}
2022-07-24 13:34:52 +00:00
func (post Post) UrlPath() string {
2022-08-03 17:41:13 +00:00
return post.user.UrlPath() + "posts/" + post.id + "/"
}
2022-08-13 16:47:27 +00:00
func (post Post) FullUrl() string {
return post.user.FullUrl() + "posts/" + post.id + "/"
}
2022-08-03 17:41:13 +00:00
func (post Post) UrlMediaPath(filename string) string {
return post.UrlPath() + "media/" + filename
2022-07-23 15:19:47 +00:00
}
2022-07-21 17:44:07 +00:00
func (post Post) Title() string {
return post.title
}
2022-07-21 17:02:37 +00:00
func (post Post) ContentFile() string {
return path.Join(post.Dir(), "index.md")
}
func (post *Post) Meta() PostMeta {
if !post.metaLoaded {
post.LoadMeta()
}
return post.meta
}
2022-07-21 17:02:37 +00:00
func (post Post) Content() []byte {
// read file
data, _ := ioutil.ReadFile(post.ContentFile())
return data
}
2022-07-21 17:44:07 +00:00
func (post Post) Webmentions() PostWebmetions {
2022-09-04 13:32:37 +00:00
// read status file
// return parsed webmentions
fileName := post.WebmentionsFile()
2022-09-04 13:32:37 +00:00
if !fileExists(fileName) {
return PostWebmetions{}
2022-09-04 13:32:37 +00:00
}
data, err := os.ReadFile(fileName)
if err != nil {
return PostWebmetions{}
2022-09-04 13:32:37 +00:00
}
webmentions := PostWebmetions{}
err = yaml.Unmarshal(data, &webmentions)
2022-09-04 13:32:37 +00:00
if err != nil {
return PostWebmetions{}
2022-09-04 13:32:37 +00:00
}
return webmentions
2022-09-04 13:32:37 +00:00
}
func (post Post) PersistWebmentions(webmentions PostWebmetions) error {
data, err := yaml.Marshal(webmentions)
2022-09-04 13:32:37 +00:00
if err != nil {
return err
}
err = os.WriteFile(post.WebmentionsFile(), data, 0644)
2022-09-04 13:32:37 +00:00
if err != nil {
return err
}
return nil
}
func (post Post) RenderedContent() bytes.Buffer {
2022-07-21 17:44:07 +00:00
data := post.Content()
// trim yaml block
// TODO this can be done nicer
trimmedData := bytes.TrimSpace(data)
// check first line is ---
if string(trimmedData[0:4]) == "---\n" {
trimmedData = trimmedData[4:]
// find --- end
end := bytes.Index(trimmedData, []byte("\n---\n"))
if end != -1 {
data = trimmedData[end+5:]
}
}
2022-08-21 09:31:48 +00:00
options := goldmark.WithRendererOptions()
if config, _ := post.user.repo.Config(); config.AllowRawHtml {
2022-08-21 09:31:48 +00:00
options = goldmark.WithRendererOptions(
html.WithUnsafe(),
)
}
2022-07-21 17:44:07 +00:00
markdown := goldmark.New(
2022-08-21 09:31:48 +00:00
options,
2022-07-21 17:44:07 +00:00
goldmark.WithExtensions(
// meta.Meta,
2022-07-27 19:26:37 +00:00
extension.GFM,
2022-07-21 17:44:07 +00:00
),
)
var buf bytes.Buffer
context := parser.NewContext()
if err := markdown.Convert(data, &buf, parser.WithContext(context)); err != nil {
panic(err)
}
return buf
2022-07-21 17:44:07 +00:00
}
2022-08-06 17:38:13 +00:00
func (post Post) Aliases() []string {
return post.Meta().Aliases
}
func (post *Post) LoadMeta() error {
data := post.Content()
// get yaml metadata block
meta := PostMeta{}
trimmedData := bytes.TrimSpace(data)
// check first line is ---
if string(trimmedData[0:4]) == "---\n" {
trimmedData = trimmedData[4:]
// find --- end
end := bytes.Index(trimmedData, []byte("\n---\n"))
if end != -1 {
metaData := trimmedData[:end]
err := yaml.Unmarshal(metaData, &meta)
if err != nil {
return err
}
}
}
post.meta = meta
return nil
2022-08-06 17:38:13 +00:00
}
2022-08-23 15:59:17 +00:00
func (post *Post) PersistIncomingWebmention(webmention WebmentionIn) error {
wms := post.Webmentions()
2022-09-01 19:34:33 +00:00
// if target is not in status, add it
replaced := false
for i, t := range wms.Incoming {
if t.Source == webmention.Source {
wms.Incoming[i] = webmention
replaced = true
break
}
2022-09-01 19:34:33 +00:00
}
if !replaced {
wms.Incoming = append(wms.Incoming, webmention)
2022-09-01 19:34:33 +00:00
}
return post.PersistWebmentions(wms)
}
2022-09-01 19:34:33 +00:00
func (post *Post) Webmention(source string) (WebmentionIn, error) {
wms := post.Webmentions()
for _, wm := range wms.Incoming {
if wm.Source == source {
return wm, nil
}
2022-09-01 19:34:33 +00:00
}
return WebmentionIn{}, errors.New("not found")
2022-09-01 19:34:33 +00:00
}
func (post *Post) AddIncomingWebmention(source string) error {
// Check if file already exists
2022-09-01 19:34:33 +00:00
_, err := post.Webmention(source)
if err != nil {
wms := post.Webmentions()
wms.Incoming = append(wms.Incoming, WebmentionIn{
2022-09-01 19:34:33 +00:00
Source: source,
})
2022-09-01 19:34:33 +00:00
defer post.EnrichWebmention(source)
return post.PersistWebmentions(wms)
}
2022-09-01 19:34:33 +00:00
return nil
}
2022-09-04 13:32:37 +00:00
func (post *Post) AddOutgoingWebmention(target string) error {
wms := post.Webmentions()
2022-09-04 13:32:37 +00:00
// Check if file already exists
for _, wm := range wms.Outgoing {
if wm.Target == target {
return nil
2022-09-04 13:32:37 +00:00
}
}
webmention := WebmentionOut{
Target: target,
}
wms.Outgoing = append(wms.Outgoing, webmention)
return post.PersistWebmentions(wms)
2022-09-04 13:32:37 +00:00
}
2022-09-04 15:10:40 +00:00
func (post *Post) UpdateOutgoingWebmention(webmention *WebmentionOut) error {
wms := post.Webmentions()
2022-09-04 15:10:40 +00:00
// if target is not in status, add it
replaced := false
for i, t := range wms.Outgoing {
2022-09-04 15:10:40 +00:00
if t.Target == webmention.Target {
wms.Outgoing[i] = *webmention
2022-09-04 15:10:40 +00:00
replaced = true
break
}
}
if !replaced {
wms.Outgoing = append(wms.Outgoing, *webmention)
2022-09-04 15:10:40 +00:00
}
return post.PersistWebmentions(wms)
2022-09-04 15:10:40 +00:00
}
2022-09-01 19:34:33 +00:00
func (post *Post) EnrichWebmention(source string) error {
resp, err := post.user.repo.HttpClient.Get(source)
if err == nil {
2022-09-01 19:34:33 +00:00
webmention, err := post.Webmention(source)
if err != nil {
return err
}
entry, err := post.user.repo.Parser.ParseHEntry(resp)
if err == nil {
2022-09-01 19:34:33 +00:00
webmention.Title = entry.Title
return post.PersistIncomingWebmention(webmention)
}
}
2022-09-01 19:34:33 +00:00
return err
2022-08-23 15:59:17 +00:00
}
func (post *Post) IncomingWebmentions() []WebmentionIn {
return post.Webmentions().Incoming
2022-08-23 15:59:17 +00:00
}
2022-09-01 19:53:06 +00:00
2022-09-04 13:03:16 +00:00
func (post *Post) ApprovedWebmentions() []WebmentionIn {
webmentions := post.IncomingWebmentions()
2022-09-04 13:03:16 +00:00
approved := []WebmentionIn{}
2022-09-01 19:53:06 +00:00
for _, webmention := range webmentions {
if webmention.ApprovalStatus == "approved" {
approved = append(approved, webmention)
}
}
// sort by retrieved date
sort.Slice(approved, func(i, j int) bool {
return approved[i].RetrievedAt.After(approved[j].RetrievedAt)
})
return approved
}
2022-09-04 13:32:37 +00:00
func (post *Post) OutgoingWebmentions() []WebmentionOut {
return post.Webmentions().Outgoing
2022-09-04 13:32:37 +00:00
}
// ScanForLinks scans the post content for links and adds them to the
// `status.yml` file for the post. The links are not scanned by this function.
func (post *Post) ScanForLinks() error {
// this could be done in markdown parsing, but I don't want to
// rely on goldmark for this (yet)
2022-09-06 18:00:12 +00:00
postHtml := post.RenderedContent()
links, _ := post.user.repo.Parser.ParseLinksFromString(string(postHtml.Bytes()))
2022-09-04 13:32:37 +00:00
for _, link := range links {
post.AddOutgoingWebmention(link)
}
return nil
}
2022-09-04 15:10:40 +00:00
func (post *Post) SendWebmention(webmention WebmentionOut) error {
defer post.UpdateOutgoingWebmention(&webmention)
webmention.ScannedAt = time.Now()
resp, err := post.user.repo.HttpClient.Get(webmention.Target)
2022-09-04 15:10:40 +00:00
if err != nil {
webmention.Supported = false
return err
}
2022-09-06 17:47:15 +00:00
endpoint, err := post.user.repo.Parser.GetWebmentionEndpoint(resp)
2022-09-04 15:10:40 +00:00
if err != nil {
webmention.Supported = false
return err
}
webmention.Supported = true
// send webmention
payload := url.Values{}
payload.Set("source", post.FullUrl())
payload.Set("target", webmention.Target)
_, err = post.user.repo.HttpClient.PostForm(endpoint, payload)
2022-09-04 15:10:40 +00:00
if err != nil {
return err
}
// update webmention status
webmention.LastSentAt = time.Now()
return nil
}