2017-04-13 09:47:00 +02:00
|
|
|
package mastodon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-04-13 19:39:34 +02:00
|
|
|
"fmt"
|
2017-04-13 09:47:00 +02:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-04-13 19:16:52 +02:00
|
|
|
"path"
|
2017-04-13 09:47:00 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Server string
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
AccessToken string
|
|
|
|
}
|
|
|
|
|
|
|
|
type client struct {
|
|
|
|
http.Client
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(config *Config) *client {
|
|
|
|
return &client{
|
|
|
|
Client: *http.DefaultClient,
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) Authenticate(username, password string) error {
|
|
|
|
params := url.Values{}
|
|
|
|
params.Set("client_id", c.config.ClientID)
|
|
|
|
params.Set("client_secret", c.config.ClientSecret)
|
|
|
|
params.Set("grant_type", "password")
|
|
|
|
params.Set("username", username)
|
|
|
|
params.Set("password", password)
|
2017-04-13 19:16:52 +02:00
|
|
|
params.Set("scope", "read write follow")
|
|
|
|
|
|
|
|
url, err := url.Parse(c.config.Server)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
url.Path = path.Join(url.Path, "/oauth/token")
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", url.String(), strings.NewReader(params.Encode()))
|
2017-04-13 09:47:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp, err := c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
res := struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
}{}
|
2017-04-13 19:16:52 +02:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(&res)
|
2017-04-13 09:47:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.config.AccessToken = res.AccessToken
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-13 19:39:34 +02:00
|
|
|
type Account struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Acct string `json:"acct"`
|
|
|
|
DisplayName string `json:"display_name"`
|
|
|
|
Locked bool `json:"locked"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
FollowersCount int64 `json:"followers_count"`
|
|
|
|
FollowingCount int64 `json:"following_count"`
|
|
|
|
StatusesCount int64 `json:"statuses_count"`
|
|
|
|
Note string `json:"note"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
Avatar string `json:"avatar"`
|
|
|
|
AvatarStatic string `json:"avatar_static"`
|
|
|
|
Header string `json:"header"`
|
|
|
|
HeaderStatic string `json:"header_static"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) GetAccount(id int) (*Account, error) {
|
|
|
|
url, err := url.Parse(c.config.Server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
url.Path = path.Join(url.Path, fmt.Sprintf("/api/v1/accounts/%d", id))
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
|
|
|
resp, err := c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
account := &Account{}
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
2017-04-13 19:16:52 +02:00
|
|
|
type Visibility int64
|
|
|
|
|
|
|
|
type Toot struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
InReplyToID int64 `json:"in_reply_to_id"`
|
|
|
|
MediaIDs []int64 `json:"in_reply_to_id"`
|
|
|
|
Sensitive bool `json:"sensitive"`
|
|
|
|
SpoilerText string `json:"spoiler_text"`
|
|
|
|
Visibility string `json:"visibility"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Status struct {
|
2017-04-13 19:39:34 +02:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
InReplyToID interface{} `json:"in_reply_to_id"`
|
|
|
|
InReplyToAccountID interface{} `json:"in_reply_to_account_id"`
|
|
|
|
Sensitive bool `json:"sensitive"`
|
|
|
|
SpoilerText string `json:"spoiler_text"`
|
|
|
|
Visibility string `json:"visibility"`
|
|
|
|
Application interface{} `json:"application"`
|
|
|
|
Account Account `json:"account"`
|
|
|
|
MediaAttachments []interface{} `json:"media_attachments"`
|
|
|
|
Mentions []interface{} `json:"mentions"`
|
|
|
|
Tags []interface{} `json:"tags"`
|
|
|
|
URI string `json:"uri"`
|
|
|
|
Content string `json:"content"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
ReblogsCount int64 `json:"reblogs_count"`
|
|
|
|
FavouritesCount int64 `json:"favourites_count"`
|
|
|
|
Reblog interface{} `json:"reblog"`
|
|
|
|
Favourited interface{} `json:"favourited"`
|
|
|
|
Reblogged interface{} `json:"reblogged"`
|
2017-04-13 09:47:00 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 19:16:52 +02:00
|
|
|
func (c *client) GetTimelineHome() ([]*Status, error) {
|
|
|
|
url, err := url.Parse(c.config.Server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
url.Path = path.Join(url.Path, "/api/v1/timelines/home")
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", url.String(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
|
|
|
resp, err := c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var statuses []*Status
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&statuses)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return statuses, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *client) PostStatus(toot *Toot) (*Status, error) {
|
|
|
|
params := url.Values{}
|
|
|
|
params.Set("status", toot.Status)
|
|
|
|
//params.Set("in_reply_to_id", fmt.Sprint(toot.InReplyToID))
|
|
|
|
// TODO: media_ids, senstitive, spoiler_text, visibility
|
|
|
|
//params.Set("visibility", "public")
|
|
|
|
|
|
|
|
url, err := url.Parse(c.config.Server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
url.Path = path.Join(url.Path, "/api/v1/statuses")
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", url.String(), strings.NewReader(params.Encode()))
|
2017-04-13 09:47:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
|
|
|
resp, err := c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2017-04-13 19:16:52 +02:00
|
|
|
var status Status
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&status)
|
2017-04-13 09:47:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-13 19:16:52 +02:00
|
|
|
return &status, nil
|
2017-04-13 09:47:00 +02:00
|
|
|
}
|
2017-04-13 14:45:44 +02:00
|
|
|
|
2017-04-13 17:12:04 +02:00
|
|
|
// AppConfig is a setting for registering applications.
|
2017-04-13 14:45:44 +02:00
|
|
|
type AppConfig struct {
|
2017-04-13 17:12:04 +02:00
|
|
|
http.Client
|
2017-04-13 14:45:44 +02:00
|
|
|
Server string
|
|
|
|
ClientName string
|
|
|
|
|
|
|
|
// Where the user should be redirected after authorization (for no redirect, use urn:ietf:wg:oauth:2.0:oob)
|
|
|
|
RedirectURIs string
|
|
|
|
|
|
|
|
// This can be a space-separated list of the following items: "read", "write" and "follow".
|
|
|
|
Scopes string
|
|
|
|
|
|
|
|
// Optional.
|
|
|
|
Website string
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:12:04 +02:00
|
|
|
// Application is mastodon application.
|
|
|
|
type Application struct {
|
2017-04-13 19:16:52 +02:00
|
|
|
ID int64 `json:"id"`
|
2017-04-13 14:45:44 +02:00
|
|
|
RedirectURI string `json:"redirect_uri"`
|
|
|
|
ClientID string `json:"client_id"`
|
|
|
|
ClientSecret string `json:"client_secret"`
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:12:04 +02:00
|
|
|
// RegisterApp returns the mastodon application.
|
|
|
|
func RegisterApp(appConfig *AppConfig) (*Application, error) {
|
2017-04-13 14:45:44 +02:00
|
|
|
params := url.Values{}
|
2017-04-13 17:12:04 +02:00
|
|
|
params.Set("client_name", appConfig.ClientName)
|
|
|
|
params.Set("redirect_uris", appConfig.RedirectURIs)
|
|
|
|
params.Set("scopes", appConfig.Scopes)
|
|
|
|
params.Set("website", appConfig.Website)
|
2017-04-13 14:45:44 +02:00
|
|
|
|
2017-04-13 17:12:04 +02:00
|
|
|
url, err := url.Parse(appConfig.Server)
|
2017-04-13 14:45:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-13 19:16:52 +02:00
|
|
|
url.Path = path.Join(url.Path, "/api/v1/apps")
|
2017-04-13 14:45:44 +02:00
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", url.String(), strings.NewReader(params.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-13 17:12:04 +02:00
|
|
|
resp, err := appConfig.Do(req)
|
2017-04-13 14:45:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2017-04-13 17:12:04 +02:00
|
|
|
app := &Application{}
|
2017-04-13 14:45:44 +02:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(app)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return app, nil
|
|
|
|
}
|