diff --git a/config.go b/config.go index 70453c4..9979690 100644 --- a/config.go +++ b/config.go @@ -12,6 +12,8 @@ var ( GiteaEndpoint string GiteaToken string BunnyCdnKey string + MicroblogUrl string + MicroblogToken string IgnoredWebmentionUrls []string ) @@ -39,6 +41,17 @@ func init() { log.Println(err) } BunnyCdnKey = bunnyCdnKey + // Microblog (optional) + microblogUrl, err := microblogUrl() + if err != nil { + log.Println(err) + } + MicroblogUrl = microblogUrl + microblogToken, err := microblogToken() + if err != nil { + log.Println(err) + } + MicroblogToken = microblogToken // Ignored Webmention URLs (optional) ignoredWebmentionUrls, err := ignoredWebmentionUrls() if err != nil { @@ -79,6 +92,22 @@ func bunnyCdnKey() (string, error) { return bunnyCDNKey, nil } +func microblogUrl() (string, error) { + microblogUrl := os.Getenv("MICROBLOG_URL") + if len(microblogUrl) == 0 || microblogUrl == "" { + return "", errors.New("MICROBLOG_URL not specified, microblog.pub features are deactivated") + } + return microblogUrl, nil +} + +func microblogToken() (string, error) { + microblogToken := os.Getenv("MICROBLOG_TOKEN") + if len(microblogToken) == 0 || microblogToken == "" { + return "", errors.New("MICROBLOG_TOKEN not specified, microblog.pub features are deactivated") + } + return microblogToken, nil +} + func ignoredWebmentionUrls() ([]string, error) { webmentionIgnored := os.Getenv("WEBMENTION_IGNORED") if len(webmentionIgnored) == 0 { diff --git a/microblogpub.go b/microblogpub.go new file mode 100644 index 0000000..bba126b --- /dev/null +++ b/microblogpub.go @@ -0,0 +1,31 @@ +package main + +import ( + "bytes" + "encoding/json" + "net/http" +) + +func PostToMicroblog(location string, text string) { + if len(MicroblogUrl) == 0 || len(MicroblogToken) == 0 { + // Microblog.pub deactivated + return + } + if len(text) == 0 { + text = location + } + note := &struct { + Content string `json:"content"` + }{ + Content: "[" + text + "](" + location + ")", + } + bytesRepresentation, err := json.Marshal(note) + if err != nil { + return + } + client := &http.Client{} + req, _ := http.NewRequest("POST", MicroblogUrl+"api/new_note", bytes.NewBuffer(bytesRepresentation)) + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Authorization", "Bearer "+MicroblogToken) + _, _ = client.Do(req) +} diff --git a/micropub.go b/micropub.go index f32a2dc..84c0840 100644 --- a/micropub.go +++ b/micropub.go @@ -71,11 +71,10 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) { go func() { time.Sleep(10 * time.Second) Purge(location) + time.Sleep(3 * time.Second) // Send webmentions - go func() { - time.Sleep(3 * time.Second) - SendWebmentions(location) - }() + go SendWebmentions(location) + go PostToMicroblog(location, entry.title) }() return }