jlelse
/
hugo-micropub
Archived
1
Fork 0
This repository has been archived on 2020-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
hugo-micropub/social.go

78 lines
1.5 KiB
Go

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(location) == 0 {
// Doesn't make sense to have empty URL
return
}
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))
}
if req == nil {
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+social.token)
_, _ = client.Do(req)
}
// Jsonpub
type Jsonpub struct {
url string
}
func (social *Jsonpub) Post(_ string, _ string) {
_, _ = http.Post(social.url, "", nil)
}