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
MicroblogToken string
IgnoredWebmentionUrls []string
SyndicationTargets []SyndicationTarget
)
type SyndicationTarget struct {
Uid string `json:"uid"`
Name string `json:"name"`
}
func init() {
// Blog URL (required)
blogUrl, err := blogUrl()
@ -58,6 +64,12 @@ func init() {
log.Println(err)
}
IgnoredWebmentionUrls = ignoredWebmentionUrls
// Syndication Targets (optional)
syndicationTargets, err := syndicationTargets()
if err != nil {
log.Println(err)
}
SyndicationTargets = syndicationTargets
}
func giteaEndpoint() (string, error) {
@ -111,7 +123,22 @@ func microblogToken() (string, error) {
func ignoredWebmentionUrls() ([]string, error) {
webmentionIgnored := os.Getenv("WEBMENTION_IGNORED")
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
}
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
likeLink string
likeTitle string
syndicate []string
filename string
location string
token string
@ -101,6 +102,13 @@ func createEntryFromValueMap(values map[string][]string) (*Entry, error) {
if likeOf, ok := values["like-of"]; ok {
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 {
entry.token = "Bearer " + token[0]
}
@ -136,6 +144,9 @@ func createEntryFromMicroformat(mfEntry *MicroformatItem) (*Entry, error) {
if len(mfEntry.Properties.LikeOf) == 1 {
entry.likeLink = mfEntry.Properties.LikeOf[0]
}
if len(mfEntry.Properties.MpSyndicateTo) > 0 {
entry.syndicate = mfEntry.Properties.MpSyndicateTo
}
err := computeExtraSettings(entry)
if err != nil {
return nil, err

View File

@ -6,13 +6,14 @@ type MicroformatItem struct {
}
type MicroformatProperties struct {
Name []string `json:"name,omitempty"`
Published []string `json:"published,omitempty"`
Updated []string `json:"updated,omitempty"`
Category []string `json:"category,omitempty"`
Content []string `json:"content,omitempty"`
Url []string `json:"url,omitempty"`
InReplyTo []string `json:"in-reply-to,omitempty"`
LikeOf []string `json:"like-of,omitempty"`
MpSlug []string `json:"mp-slug,omitempty"`
Name []string `json:"name,omitempty"`
Published []string `json:"published,omitempty"`
Updated []string `json:"updated,omitempty"`
Category []string `json:"category,omitempty"`
Content []string `json:"content,omitempty"`
Url []string `json:"url,omitempty"`
InReplyTo []string `json:"in-reply-to,omitempty"`
LikeOf []string `json:"like-of,omitempty"`
MpSlug []string `json:"mp-slug,omitempty"`
MpSyndicateTo []string `json:"mp-syndicate-to,omitempty"`
}

View File

@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"net/http"
"strconv"
"time"
@ -12,7 +13,13 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) {
if q := r.URL.Query().Get("q"); q == "syndicate-to" {
w.Header().Add("Content-type", "application/json")
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
} else if url := r.URL.Query().Get("url"); q == "source" {
limit := r.URL.Query().Get("limit")

View File

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