2017-04-14 16:37:29 +02:00
|
|
|
package mastodon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-04-14 16:56:07 +02:00
|
|
|
"net/http"
|
2017-04-14 16:37:29 +02:00
|
|
|
"net/url"
|
2019-05-14 02:51:33 +02:00
|
|
|
"strconv"
|
2017-04-14 16:37:29 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// Account holds information for a mastodon account.
|
2017-04-14 16:37:29 +02:00
|
|
|
type Account struct {
|
2022-05-17 19:35:39 +02:00
|
|
|
ID ID `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"`
|
|
|
|
Emojis []Emoji `json:"emojis"`
|
|
|
|
Moved *Account `json:"moved"`
|
|
|
|
Fields []Field `json:"fields"`
|
|
|
|
Bot bool `json:"bot"`
|
|
|
|
Discoverable bool `json:"discoverable"`
|
|
|
|
Source *AccountSource `json:"source"`
|
2018-10-20 23:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Field is a Mastodon account profile field.
|
|
|
|
type Field struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
VerifiedAt time.Time `json:"verified_at"`
|
2017-04-14 16:37:29 +02:00
|
|
|
}
|
|
|
|
|
2019-05-14 04:21:14 +02:00
|
|
|
// AccountSource is a Mastodon account profile field.
|
|
|
|
type AccountSource struct {
|
2019-08-08 11:13:49 +02:00
|
|
|
Privacy *string `json:"privacy"`
|
2019-05-14 02:51:33 +02:00
|
|
|
Sensitive *bool `json:"sensitive"`
|
2019-08-08 11:13:49 +02:00
|
|
|
Language *string `json:"language"`
|
|
|
|
Note *string `json:"note"`
|
2019-05-14 02:51:33 +02:00
|
|
|
Fields *[]Field `json:"fields"`
|
|
|
|
}
|
|
|
|
|
2017-04-14 16:37:29 +02:00
|
|
|
// GetAccount return Account.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) GetAccount(id ID) (*Account, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var account Account
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s", url.PathEscape(string(id))), nil, &account, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &account, nil
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// GetAccountCurrentUser returns the Account of current user.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) GetAccountCurrentUser() (*Account, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var account Account
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(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
|
2019-05-14 02:51:33 +02:00
|
|
|
Locked *bool
|
|
|
|
Fields *[]Field
|
2019-05-14 04:21:14 +02:00
|
|
|
Source *AccountSource
|
2017-04-15 17:47:44 +02:00
|
|
|
|
|
|
|
// Set the base64 encoded character string of the image.
|
|
|
|
Avatar string
|
|
|
|
Header string
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountUpdate updates the information of the current user.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) AccountUpdate(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)
|
|
|
|
}
|
2019-05-14 02:51:33 +02:00
|
|
|
if profile.Locked != nil {
|
|
|
|
params.Set("locked", strconv.FormatBool(*profile.Locked))
|
|
|
|
}
|
|
|
|
if profile.Fields != nil {
|
|
|
|
for idx, field := range *profile.Fields {
|
|
|
|
params.Set(fmt.Sprintf("fields_attributes[%d][name]", idx), field.Name)
|
|
|
|
params.Set(fmt.Sprintf("fields_attributes[%d][value]", idx), field.Value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if profile.Source != nil {
|
2019-05-14 04:44:39 +02:00
|
|
|
if profile.Source.Privacy != nil {
|
|
|
|
params.Set("source[privacy]", *profile.Source.Privacy)
|
2019-05-14 02:51:33 +02:00
|
|
|
}
|
|
|
|
if profile.Source.Sensitive != nil {
|
|
|
|
params.Set("source[sensitive]", strconv.FormatBool(*profile.Source.Sensitive))
|
|
|
|
}
|
2019-05-14 04:44:39 +02:00
|
|
|
if profile.Source.Language != nil {
|
|
|
|
params.Set("source[language]", *profile.Source.Language)
|
2019-05-14 02:51:33 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-15 17:47:44 +02:00
|
|
|
if profile.Avatar != "" {
|
|
|
|
params.Set("avatar", profile.Avatar)
|
|
|
|
}
|
|
|
|
if profile.Header != "" {
|
|
|
|
params.Set("header", profile.Header)
|
|
|
|
}
|
|
|
|
|
|
|
|
var account Account
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(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
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// GetAccountStatuses return statuses by specified account.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) GetAccountStatuses(id ID, pg *Pagination) ([]*Status, error) {
|
2017-04-16 14:42:54 +02:00
|
|
|
var statuses []*Status
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/statuses", url.PathEscape(string(id))), nil, &statuses, pg)
|
2017-04-16 14:42:54 +02:00
|
|
|
if err != nil {
|
2022-06-04 15:44:49 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return statuses, nil
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// GetAccountPinnedStatuses returns statuses pinned by specified accuont.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) GetAccountPinnedStatuses(id ID) ([]*Status, error) {
|
2022-06-04 15:44:49 +02:00
|
|
|
var statuses []*Status
|
|
|
|
params := url.Values{}
|
|
|
|
params.Set("pinned", "true")
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/statuses", url.PathEscape(string(id))), params, &statuses, nil)
|
2022-06-04 15:44:49 +02:00
|
|
|
if err != nil {
|
2017-05-06 16:03:19 +02:00
|
|
|
return nil, err
|
2017-04-16 14:42:54 +02:00
|
|
|
}
|
2017-05-06 16:03:19 +02:00
|
|
|
return statuses, nil
|
2017-04-16 14:42:54 +02:00
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// GetAccountFollowers returns followers list.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) GetAccountFollowers(id ID, pg *Pagination) ([]*Account, error) {
|
2017-05-04 14:56:54 +02:00
|
|
|
var accounts []*Account
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/followers", url.PathEscape(string(id))), nil, &accounts, pg)
|
2017-05-04 14:56:54 +02:00
|
|
|
if err != nil {
|
2017-05-06 16:03:19 +02:00
|
|
|
return nil, err
|
2017-04-14 16:37:29 +02:00
|
|
|
}
|
2017-05-06 16:03:19 +02:00
|
|
|
return accounts, nil
|
2017-04-14 16:37:29 +02:00
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// GetAccountFollowing returns following list.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) GetAccountFollowing(id ID, pg *Pagination) ([]*Account, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var accounts []*Account
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/following", url.PathEscape(string(id))), nil, &accounts, pg)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
2017-05-06 16:03:19 +02:00
|
|
|
return nil, err
|
2017-04-14 16:37:29 +02:00
|
|
|
}
|
2017-05-06 16:03:19 +02:00
|
|
|
return accounts, nil
|
2017-04-14 16:37:29 +02:00
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// GetBlocks returns block list.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) GetBlocks(pg *Pagination) ([]*Account, error) {
|
2017-04-14 19:23:17 +02:00
|
|
|
var accounts []*Account
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodGet, "/api/v1/blocks", nil, &accounts, pg)
|
2017-04-14 19:23:17 +02:00
|
|
|
if err != nil {
|
2017-05-06 16:03:19 +02:00
|
|
|
return nil, err
|
2017-04-14 19:23:17 +02:00
|
|
|
}
|
2017-05-06 16:03:19 +02:00
|
|
|
return accounts, nil
|
2017-04-14 19:23:17 +02:00
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// Relationship holds information for relationship to the account.
|
2017-04-14 16:37:29 +02:00
|
|
|
type Relationship struct {
|
2018-11-24 06:46:49 +01:00
|
|
|
ID ID `json:"id"`
|
|
|
|
Following bool `json:"following"`
|
|
|
|
FollowedBy bool `json:"followed_by"`
|
|
|
|
Blocking bool `json:"blocking"`
|
|
|
|
Muting bool `json:"muting"`
|
|
|
|
MutingNotifications bool `json:"muting_notifications"`
|
|
|
|
Requested bool `json:"requested"`
|
|
|
|
DomainBlocking bool `json:"domain_blocking"`
|
|
|
|
ShowingReblogs bool `json:"showing_reblogs"`
|
|
|
|
Endorsed bool `json:"endorsed"`
|
2017-04-14 16:37:29 +02:00
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// AccountFollow follows the account.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) AccountFollow(id ID) (*Relationship, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var relationship Relationship
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/follow", url.PathEscape(string(id))), nil, &relationship, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &relationship, nil
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// AccountUnfollow unfollows the account.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) AccountUnfollow(id ID) (*Relationship, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var relationship Relationship
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/unfollow", url.PathEscape(string(id))), nil, &relationship, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &relationship, nil
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// AccountBlock blocks the account.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) AccountBlock(id ID) (*Relationship, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var relationship Relationship
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/block", url.PathEscape(string(id))), nil, &relationship, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &relationship, nil
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// AccountUnblock unblocks the account.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) AccountUnblock(id ID) (*Relationship, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var relationship Relationship
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/unblock", url.PathEscape(string(id))), nil, &relationship, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &relationship, nil
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// AccountMute mutes the account.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) AccountMute(id ID) (*Relationship, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var relationship Relationship
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/mute", url.PathEscape(string(id))), nil, &relationship, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &relationship, nil
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// AccountUnmute unmutes the account.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) AccountUnmute(id ID) (*Relationship, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var relationship Relationship
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/unmute", url.PathEscape(string(id))), nil, &relationship, nil)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &relationship, nil
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// GetAccountRelationships returns relationship for the account.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) GetAccountRelationships(ids []string) ([]*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 {
|
2017-10-23 03:09:08 +02:00
|
|
|
params.Add("id[]", id)
|
2017-04-17 19:45:40 +02:00
|
|
|
}
|
2017-04-14 16:37:29 +02:00
|
|
|
|
|
|
|
var relationships []*Relationship
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(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
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// AccountsSearch searches accounts by query.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) AccountsSearch(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
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(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
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// FollowRemoteUser sends follow-request.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) FollowRemoteUser(uri string) (*Account, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
params := url.Values{}
|
|
|
|
params.Set("uri", uri)
|
|
|
|
|
|
|
|
var account Account
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(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
|
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// GetFollowRequests returns follow requests.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) GetFollowRequests(pg *Pagination) ([]*Account, error) {
|
2017-04-14 16:37:29 +02:00
|
|
|
var accounts []*Account
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodGet, "/api/v1/follow_requests", nil, &accounts, pg)
|
2017-04-14 16:37:29 +02:00
|
|
|
if err != nil {
|
2017-05-06 16:03:19 +02:00
|
|
|
return nil, err
|
2017-04-14 16:37:29 +02:00
|
|
|
}
|
2017-05-06 16:03:19 +02:00
|
|
|
return accounts, nil
|
2017-04-14 16:37:29 +02:00
|
|
|
}
|
2017-04-16 17:04:25 +02:00
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// FollowRequestAuthorize authorizes the follow request of user with id.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) FollowRequestAuthorize(id ID) error {
|
|
|
|
return c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%s/authorize", url.PathEscape(string(id))), nil, nil, nil)
|
2017-04-16 17:04:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-13 22:14:40 +01:00
|
|
|
// FollowRequestReject rejects the follow request of user with id.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) FollowRequestReject(id ID) error {
|
|
|
|
return c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%s/reject", url.PathEscape(string(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.
|
2022-11-20 18:03:36 +01:00
|
|
|
func (c *Client) GetMutes(pg *Pagination) ([]*Account, error) {
|
2017-04-17 05:49:55 +02:00
|
|
|
var accounts []*Account
|
2022-11-20 18:03:36 +01:00
|
|
|
err := c.doAPI(http.MethodGet, "/api/v1/mutes", nil, &accounts, pg)
|
2017-04-17 05:49:55 +02:00
|
|
|
if err != nil {
|
2017-05-06 16:03:19 +02:00
|
|
|
return nil, err
|
2017-04-17 05:49:55 +02:00
|
|
|
}
|
2017-05-06 16:03:19 +02:00
|
|
|
return accounts, nil
|
2017-04-17 05:49:55 +02:00
|
|
|
}
|