2019-12-22 19:27:12 +00:00
|
|
|
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) {
|
2020-02-19 18:27:19 +00:00
|
|
|
if len(location) == 0 {
|
|
|
|
// Doesn't make sense to have empty URL
|
|
|
|
return
|
2019-12-22 19:27:12 +00:00
|
|
|
}
|
2020-02-19 18:27:19 +00:00
|
|
|
client := &http.Client{}
|
|
|
|
var req *http.Request
|
|
|
|
if ActivityStreams {
|
|
|
|
// Just boost the Activity
|
|
|
|
note := &struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
}{
|
|
|
|
Id: location,
|
|
|
|
}
|
|
|
|
bytesRepresentation, err := json.Marshal(note)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req, _ = http.NewRequest("POST", social.url+"api/boost", bytes.NewBuffer(bytesRepresentation))
|
|
|
|
} else {
|
|
|
|
// Post the Activity
|
|
|
|
if len(text) == 0 {
|
|
|
|
text = location
|
|
|
|
}
|
|
|
|
note := &struct {
|
|
|
|
Content string `json:"content"`
|
|
|
|
}{
|
|
|
|
Content: "[" + text + "](" + location + ")",
|
|
|
|
}
|
|
|
|
bytesRepresentation, err := json.Marshal(note)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req, _ = http.NewRequest("POST", social.url+"api/new_note", bytes.NewBuffer(bytesRepresentation))
|
2019-12-22 19:27:12 +00:00
|
|
|
}
|
2020-02-19 18:27:19 +00:00
|
|
|
if req == nil {
|
2019-12-22 19:27:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
req.Header.Add("Authorization", "Bearer "+social.token)
|
|
|
|
_, _ = client.Do(req)
|
|
|
|
}
|