Refactor Socials

kcoram/uplift
Jan-Lukas Else 2019-12-22 20:27:12 +01:00
parent 8e975c9d52
commit cf4dd37484
5 changed files with 65 additions and 50 deletions

2
cdn.go
View File

@ -15,7 +15,7 @@ type BunnyCdn struct {
key string key string
} }
func (cdn BunnyCdn) Purge(url string) { func (cdn *BunnyCdn) Purge(url string) {
client := &http.Client{} client := &http.Client{}
req, _ := http.NewRequest("POST", "https://bunnycdn.com/api/purge?url="+url, nil) req, _ := http.NewRequest("POST", "https://bunnycdn.com/api/purge?url="+url, nil)
req.Header.Add("Content-Type", "application/json") req.Header.Add("Content-Type", "application/json")

View File

@ -11,11 +11,10 @@ var (
BlogUrl string BlogUrl string
GiteaEndpoint string GiteaEndpoint string
GiteaToken string GiteaToken string
MicroblogUrl string
MicroblogToken string
IgnoredWebmentionUrls []string IgnoredWebmentionUrls []string
SyndicationTargets []SyndicationTarget SyndicationTargets []SyndicationTarget
SelectedCdn Cdn SelectedCdn Cdn
SelectedSocials Socials
) )
type SyndicationTarget struct { type SyndicationTarget struct {
@ -41,17 +40,6 @@ func init() {
log.Fatal(err) log.Fatal(err)
} }
GiteaToken = giteaToken GiteaToken = giteaToken
// 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 {
@ -68,13 +56,25 @@ func init() {
SelectedCdn = func() Cdn { SelectedCdn = func() Cdn {
// BunnyCDN (optional) // BunnyCDN (optional)
bunnyCdnKey, err := bunnyCdnKey() bunnyCdnKey, err := bunnyCdnKey()
if err != nil { if err == nil {
log.Println(err) return &BunnyCdn{key: bunnyCdnKey}
} else {
return BunnyCdn{key: bunnyCdnKey}
} }
return nil return nil
}() }()
// Find configured social networks
SelectedSocials = func() Socials {
var socials []Social
// Microblog.pub
microblogUrl, err1 := microblogUrl()
microblogToken, err2 := microblogToken()
if err1 == nil && err2 == nil {
socials = append(socials, &MicroblogPub{
url: microblogUrl,
token: microblogToken,
})
}
return socials
}()
} }
func giteaEndpoint() (string, error) { func giteaEndpoint() (string, error) {

View File

@ -1,31 +0,0 @@
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

@ -87,7 +87,7 @@ func HandleMicroPub(w http.ResponseWriter, r *http.Request) {
time.Sleep(3 * time.Second) time.Sleep(3 * time.Second)
// Send webmentions // Send webmentions
go SendWebmentions(location) go SendWebmentions(location)
go PostToMicroblog(location, entry.title) go SelectedSocials.Post(location, entry.title)
}() }()
return return
} }

46
social.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"bytes"
"encoding/json"
"net/http"
)
type Social interface {
Post(location string, text string)
}
type Socials []Social
// Post to all socials
func (socials *Socials) Post(location string, text string) {
for _, social := range *socials {
social.Post(location, text)
}
}
// Microblog.pub
type MicroblogPub struct {
url string
token string
}
func (social *MicroblogPub) Post(location string, text string) {
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", social.url+"api/new_note", bytes.NewBuffer(bytesRepresentation))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+social.token)
_, _ = client.Do(req)
}