2017-04-14 16:37:29 +02:00
|
|
|
package mastodon
|
|
|
|
|
|
|
|
import (
|
2017-04-17 04:10:29 +02:00
|
|
|
"context"
|
2017-04-14 16:37:29 +02:00
|
|
|
"fmt"
|
2017-04-14 16:56:07 +02:00
|
|
|
"net/http"
|
2017-04-14 16:37:29 +02:00
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Account hold information for mastodon account.
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccount return Account.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) GetAccount(ctx context.Context, id int) (*Account, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var account Account
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d", id), nil, &account, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccountCurrentUser return Account of current user.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) GetAccountCurrentUser(ctx context.Context) (*Account, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var account Account
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/verify_credentials", nil, &account, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &account, nil
|
|
|
|
}
|
|
|
|
|
2017-04-15 17:47:44 +02:00
|
|
|
// Profile is a struct for updating profiles.
|
|
|
|
type Profile struct {
|
|
|
|
// If it is nil it will not be updated.
|
|
|
|
// If it is empty, update it with empty.
|
|
|
|
DisplayName *string
|
|
|
|
Note *string
|
|
|
|
|
|
|
|
// Set the base64 encoded character string of the image.
|
|
|
|
Avatar string
|
|
|
|
Header string
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountUpdate updates the information of the current user.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) AccountUpdate(ctx context.Context, profile *Profile) (*Account, error) {
|
2017-04-15 17:47:44 +02:00
|
|
|
params := url.Values{}
|
|
|
|
if profile.DisplayName != nil {
|
|
|
|
params.Set("display_name", *profile.DisplayName)
|
|
|
|
}
|
|
|
|
if profile.Note != nil {
|
|
|
|
params.Set("note", *profile.Note)
|
|
|
|
}
|
|
|
|
if profile.Avatar != "" {
|
|
|
|
params.Set("avatar", profile.Avatar)
|
|
|
|
}
|
|
|
|
if profile.Header != "" {
|
|
|
|
params.Set("header", profile.Header)
|
|
|
|
}
|
|
|
|
|
|
|
|
var account Account
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodPatch, "/api/v1/accounts/update_credentials", params, &account, nil)
|
2017-04-15 17:47:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &account, nil
|
|
|
|
}
|
|
|
|
|
2017-04-16 14:42:54 +02:00
|
|
|
// GetAccountStatuses return statuses by specified accuont.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) GetAccountStatuses(ctx context.Context, id int64) ([]*Status, error) {
|
2017-04-16 14:42:54 +02:00
|
|
|
var statuses []*Status
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/statuses", id), nil, &statuses, nil)
|
2017-04-16 14:42:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return statuses, nil
|
|
|
|
}
|
|
|
|
|
2017-04-14 16:37:29 +02:00
|
|
|
// GetAccountFollowers return followers list.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) GetAccountFollowers(ctx context.Context, id int64) ([]*Account, error) {
|
2017-04-18 10:14:39 +02:00
|
|
|
params := url.Values{}
|
|
|
|
var total []*Account
|
|
|
|
for {
|
|
|
|
var accounts []*Account
|
|
|
|
var next bool
|
|
|
|
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/followers", id), params, &accounts, &next)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, a := range accounts {
|
|
|
|
println(a.Username)
|
|
|
|
}
|
|
|
|
total = append(total, accounts...)
|
|
|
|
if !next {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(10 * time.Second)
|
2017-04-14 16:37:29 +02:00
|
|
|
}
|
2017-04-18 10:14:39 +02:00
|
|
|
return total, nil
|
2017-04-14 16:37:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccountFollowing return following list.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) GetAccountFollowing(ctx context.Context, id int64) ([]*Account, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var accounts []*Account
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/following", id), nil, &accounts, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return accounts, nil
|
|
|
|
}
|
|
|
|
|
2017-04-14 19:23:17 +02:00
|
|
|
// GetBlocks return block list.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) GetBlocks(ctx context.Context) ([]*Account, error) {
|
2017-04-14 19:23:17 +02:00
|
|
|
var accounts []*Account
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodGet, "/api/v1/blocks", nil, &accounts, nil)
|
2017-04-14 19:23:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return accounts, nil
|
|
|
|
}
|
|
|
|
|
2017-04-14 16:37:29 +02:00
|
|
|
// Relationship hold information for relation-ship to the account.
|
|
|
|
type Relationship struct {
|
2017-04-14 17:26:13 +02:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
Following bool `json:"following"`
|
|
|
|
FollowedBy bool `json:"followed_by"`
|
|
|
|
Blocking bool `json:"blocking"`
|
|
|
|
Muting bool `json:"muting"`
|
|
|
|
Requested bool `json:"requested"`
|
2017-04-14 16:37:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AccountFollow follow the account.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) AccountFollow(ctx context.Context, id int64) (*Relationship, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var relationship Relationship
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/follow", id), nil, &relationship, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &relationship, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountUnfollow unfollow the account.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) AccountUnfollow(ctx context.Context, id int64) (*Relationship, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var relationship Relationship
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/unfollow", id), nil, &relationship, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &relationship, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountBlock block the account.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) AccountBlock(ctx context.Context, id int64) (*Relationship, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var relationship Relationship
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/block", id), nil, &relationship, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &relationship, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountUnblock unblock the account.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) AccountUnblock(ctx context.Context, id int64) (*Relationship, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var relationship Relationship
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/unblock", id), nil, &relationship, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &relationship, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountMute mute the account.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) AccountMute(ctx context.Context, id int64) (*Relationship, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var relationship Relationship
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/mute", id), nil, &relationship, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &relationship, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountUnmute unmute the account.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) AccountUnmute(ctx context.Context, id int64) (*Relationship, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var relationship Relationship
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/unmute", id), nil, &relationship, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &relationship, nil
|
|
|
|
}
|
|
|
|
|
2017-04-18 10:14:39 +02:00
|
|
|
// GetAccountRelationships return relationship for the account.
|
2017-04-17 19:45:40 +02:00
|
|
|
func (c *Client) GetAccountRelationships(ctx context.Context, ids []int64) ([]*Relationship, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
params := url.Values{}
|
2017-04-17 19:45:40 +02:00
|
|
|
for _, id := range ids {
|
|
|
|
params.Add("id[]", fmt.Sprint(id))
|
|
|
|
}
|
2017-04-14 16:37:29 +02:00
|
|
|
|
|
|
|
var relationships []*Relationship
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/relationships", params, &relationships, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return relationships, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountsSearch search accounts by query.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) AccountsSearch(ctx context.Context, q string, limit int64) ([]*Account, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
params := url.Values{}
|
|
|
|
params.Set("q", q)
|
|
|
|
params.Set("limit", fmt.Sprint(limit))
|
|
|
|
|
|
|
|
var accounts []*Account
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/search", params, &accounts, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return accounts, nil
|
|
|
|
}
|
|
|
|
|
2017-04-16 16:38:53 +02:00
|
|
|
// FollowRemoteUser send follow-request.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) FollowRemoteUser(ctx context.Context, uri string) (*Account, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
params := url.Values{}
|
|
|
|
params.Set("uri", uri)
|
|
|
|
|
|
|
|
var account Account
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodPost, "/api/v1/follows", params, &account, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFollowRequests return follow-requests.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) GetFollowRequests(ctx context.Context) ([]*Account, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var accounts []*Account
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodGet, "/api/v1/follow_requests", nil, &accounts, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return accounts, nil
|
|
|
|
}
|
2017-04-16 17:04:25 +02:00
|
|
|
|
|
|
|
// FollowRequestAuthorize is authorize the follow request of user with id.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) FollowRequestAuthorize(ctx context.Context, id int64) error {
|
2017-04-18 10:14:39 +02:00
|
|
|
return c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%d/authorize", id), nil, nil, nil)
|
2017-04-16 17:04:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// FollowRequestReject is rejects the follow request of user with id.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) FollowRequestReject(ctx context.Context, id int64) error {
|
2017-04-18 10:14:39 +02:00
|
|
|
return c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%d/reject", id), nil, nil, nil)
|
2017-04-16 17:04:25 +02:00
|
|
|
}
|
2017-04-17 05:49:55 +02:00
|
|
|
|
|
|
|
// GetMutes returns the list of users muted by the current user.
|
|
|
|
func (c *Client) GetMutes(ctx context.Context) ([]*Account, error) {
|
|
|
|
var accounts []*Account
|
2017-04-18 10:14:39 +02:00
|
|
|
err := c.doAPI(ctx, http.MethodGet, "/api/v1/mutes", nil, &accounts, nil)
|
2017-04-17 05:49:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return accounts, nil
|
|
|
|
}
|