Post to Microblog.pub instance

kcoram/uplift
Jan-Lukas Else 2019-12-12 22:36:05 +01:00
parent 988ff42365
commit 6c48c016cc
3 changed files with 63 additions and 4 deletions

View File

@ -12,6 +12,8 @@ var (
GiteaEndpoint string GiteaEndpoint string
GiteaToken string GiteaToken string
BunnyCdnKey string BunnyCdnKey string
MicroblogUrl string
MicroblogToken string
IgnoredWebmentionUrls []string IgnoredWebmentionUrls []string
) )
@ -39,6 +41,17 @@ func init() {
log.Println(err) log.Println(err)
} }
BunnyCdnKey = bunnyCdnKey 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) // Ignored Webmention URLs (optional)
ignoredWebmentionUrls, err := ignoredWebmentionUrls() ignoredWebmentionUrls, err := ignoredWebmentionUrls()
if err != nil { if err != nil {
@ -79,6 +92,22 @@ func bunnyCdnKey() (string, error) {
return bunnyCDNKey, nil 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) { func ignoredWebmentionUrls() ([]string, error) {
webmentionIgnored := os.Getenv("WEBMENTION_IGNORED") webmentionIgnored := os.Getenv("WEBMENTION_IGNORED")
if len(webmentionIgnored) == 0 { if len(webmentionIgnored) == 0 {

31
microblogpub.go Normal file
View File

@ -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)
}

View File

@ -71,11 +71,10 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) {
go func() { go func() {
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
Purge(location) Purge(location)
time.Sleep(3 * time.Second)
// Send webmentions // Send webmentions
go func() { go SendWebmentions(location)
time.Sleep(3 * time.Second) go PostToMicroblog(location, entry.title)
SendWebmentions(location)
}()
}() }()
return return
} }