jlelse
/
hugo-micropub
Archived
1
Fork 0

Post to Microblog.pub instance

This commit is contained in:
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
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 {

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() {
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
}