Add support for syndication

kcoram/uplift
Jan-Lukas Else 2019-12-22 13:58:50 +01:00
parent 6fc525ce5e
commit da07e28de1
5 changed files with 63 additions and 12 deletions

View File

@ -15,8 +15,14 @@ var (
MicroblogUrl string MicroblogUrl string
MicroblogToken string MicroblogToken string
IgnoredWebmentionUrls []string IgnoredWebmentionUrls []string
SyndicationTargets []SyndicationTarget
) )
type SyndicationTarget struct {
Uid string `json:"uid"`
Name string `json:"name"`
}
func init() { func init() {
// Blog URL (required) // Blog URL (required)
blogUrl, err := blogUrl() blogUrl, err := blogUrl()
@ -58,6 +64,12 @@ func init() {
log.Println(err) log.Println(err)
} }
IgnoredWebmentionUrls = ignoredWebmentionUrls IgnoredWebmentionUrls = ignoredWebmentionUrls
// Syndication Targets (optional)
syndicationTargets, err := syndicationTargets()
if err != nil {
log.Println(err)
}
SyndicationTargets = syndicationTargets
} }
func giteaEndpoint() (string, error) { func giteaEndpoint() (string, error) {
@ -111,7 +123,22 @@ func microblogToken() (string, error) {
func ignoredWebmentionUrls() ([]string, error) { func ignoredWebmentionUrls() ([]string, error) {
webmentionIgnored := os.Getenv("WEBMENTION_IGNORED") webmentionIgnored := os.Getenv("WEBMENTION_IGNORED")
if len(webmentionIgnored) == 0 { if len(webmentionIgnored) == 0 {
return nil, errors.New("WEBMENTION_IGNORED not set, no URLs are ignored on Webmention sending") return make([]string, 0), errors.New("WEBMENTION_IGNORED not set, no URLs are ignored on Webmention sending")
} }
return strings.Split(webmentionIgnored, ","), nil return strings.Split(webmentionIgnored, ","), nil
} }
func syndicationTargets() ([]SyndicationTarget, error) {
syndication := os.Getenv("SYNDICATION")
targets := make([]SyndicationTarget, 0)
if len(syndication) == 0 {
return targets, errors.New("SYNDICATION not set, no targets are returned when querying for syndication targets")
}
for _, url := range strings.Split(syndication, ",") {
targets = append(targets, SyndicationTarget{
Uid: url,
Name: url,
})
}
return targets, nil
}

View File

@ -27,6 +27,7 @@ type Entry struct {
replyTitle string replyTitle string
likeLink string likeLink string
likeTitle string likeTitle string
syndicate []string
filename string filename string
location string location string
token string token string
@ -101,6 +102,13 @@ func createEntryFromValueMap(values map[string][]string) (*Entry, error) {
if likeOf, ok := values["like-of"]; ok { if likeOf, ok := values["like-of"]; ok {
entry.likeLink = likeOf[0] entry.likeLink = likeOf[0]
} }
if syndicate, ok := values["mp-syndicate-to"]; ok {
entry.syndicate = syndicate
} else if syndicates, ok := values["mp-syndicate-to[]"]; ok {
entry.syndicate = syndicates
} else {
entry.syndicate = nil
}
if token, ok := values["access_token"]; ok { if token, ok := values["access_token"]; ok {
entry.token = "Bearer " + token[0] entry.token = "Bearer " + token[0]
} }
@ -136,6 +144,9 @@ func createEntryFromMicroformat(mfEntry *MicroformatItem) (*Entry, error) {
if len(mfEntry.Properties.LikeOf) == 1 { if len(mfEntry.Properties.LikeOf) == 1 {
entry.likeLink = mfEntry.Properties.LikeOf[0] entry.likeLink = mfEntry.Properties.LikeOf[0]
} }
if len(mfEntry.Properties.MpSyndicateTo) > 0 {
entry.syndicate = mfEntry.Properties.MpSyndicateTo
}
err := computeExtraSettings(entry) err := computeExtraSettings(entry)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -15,4 +15,5 @@ type MicroformatProperties struct {
InReplyTo []string `json:"in-reply-to,omitempty"` InReplyTo []string `json:"in-reply-to,omitempty"`
LikeOf []string `json:"like-of,omitempty"` LikeOf []string `json:"like-of,omitempty"`
MpSlug []string `json:"mp-slug,omitempty"` MpSlug []string `json:"mp-slug,omitempty"`
MpSyndicateTo []string `json:"mp-syndicate-to,omitempty"`
} }

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/json"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
@ -12,7 +13,13 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) {
if q := r.URL.Query().Get("q"); q == "syndicate-to" { if q := r.URL.Query().Get("q"); q == "syndicate-to" {
w.Header().Add("Content-type", "application/json") w.Header().Add("Content-type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("[]")) jsonBytes, err := json.Marshal(SyndicationTargets)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(err.Error()))
return
}
_, _ = w.Write(jsonBytes)
return return
} else if url := r.URL.Query().Get("url"); q == "source" { } else if url := r.URL.Query().Get("url"); q == "source" {
limit := r.URL.Query().Get("limit") limit := r.URL.Query().Get("limit")

View File

@ -14,6 +14,7 @@ type HugoFrontmatter struct {
Tags []string `yaml:"tags,omitempty"` Tags []string `yaml:"tags,omitempty"`
ExternalURL string `yaml:"externalURL,omitempty"` ExternalURL string `yaml:"externalURL,omitempty"`
Indieweb HugoFrontmatterIndieweb `yaml:"indieweb,omitempty"` Indieweb HugoFrontmatterIndieweb `yaml:"indieweb,omitempty"`
Syndicate []string `yaml:"syndicate,omitempty"`
} }
type HugoFrontmatterIndieweb struct { type HugoFrontmatterIndieweb struct {
@ -49,6 +50,7 @@ func writeFrontMatter(entry *Entry) (frontmatter string, err error) {
Title: entry.likeTitle, Title: entry.likeTitle,
}, },
}, },
Syndicate: entry.syndicate,
} }
yamlBytes, err := yaml.Marshal(&writeFrontmatter) yamlBytes, err := yaml.Marshal(&writeFrontmatter)
if err != nil { if err != nil {
@ -108,6 +110,9 @@ func readFrontMatter(frontmatter string, entry *Entry) (err error) {
if len(parsedFrontmatter.Indieweb.Like.Title) > 0 { if len(parsedFrontmatter.Indieweb.Like.Title) > 0 {
entry.replyTitle = parsedFrontmatter.Indieweb.Like.Title entry.replyTitle = parsedFrontmatter.Indieweb.Like.Title
} }
if len(parsedFrontmatter.Syndicate) > 0 {
entry.syndicate = parsedFrontmatter.Syndicate
}
return return
} }