From da07e28de1276b9ab7a78aacc3fc9b994fc62747 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 22 Dec 2019 13:58:50 +0100 Subject: [PATCH] Add support for syndication --- config.go | 31 +++++++++++++++++++++++++++++-- entry.go | 11 +++++++++++ microformat.go | 19 ++++++++++--------- micropub.go | 9 ++++++++- post.go | 5 +++++ 5 files changed, 63 insertions(+), 12 deletions(-) diff --git a/config.go b/config.go index 9979690..aaa8635 100644 --- a/config.go +++ b/config.go @@ -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 -} \ No newline at end of file +} + +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 +} diff --git a/entry.go b/entry.go index 9431e03..5da92bc 100644 --- a/entry.go +++ b/entry.go @@ -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 diff --git a/microformat.go b/microformat.go index 2993565..5092774 100644 --- a/microformat.go +++ b/microformat.go @@ -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"` } diff --git a/micropub.go b/micropub.go index 84c0840..ff3a85e 100644 --- a/micropub.go +++ b/micropub.go @@ -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") diff --git a/post.go b/post.go index 5155be1..fe73b30 100644 --- a/post.go +++ b/post.go @@ -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 }