simplify unduly by requiring client state to be initialized once

This commit is contained in:
gutmet 2022-11-20 20:36:27 +01:00
parent ac39fb12dd
commit 7274b5e624
10 changed files with 180 additions and 254 deletions

View File

@ -49,20 +49,19 @@ type AccountSource struct {
Fields *[]Field `json:"fields"` Fields *[]Field `json:"fields"`
} }
// GetAccount return Account. func GetAccount(id ID) (*Account, error) {
func (c *Client) GetAccount(id ID) (*Account, error) {
var account Account var account Account
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s", url.PathEscape(string(id))), nil, &account, nil) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s", url.PathEscape(string(id))), nil, &account, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &account, nil return &account, nil
} }
// GetAccountCurrentUser returns the Account of current user. func GetCurrentAccount() (*Account, error) {
func (c *Client) GetAccountCurrentUser() (*Account, error) { checkInit()
var account Account var account Account
err := c.doAPI(http.MethodGet, "/api/v1/accounts/verify_credentials", nil, &account, nil) err := doAPI(http.MethodGet, "/api/v1/accounts/verify_credentials", nil, &account, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -84,8 +83,7 @@ type Profile struct {
Header string Header string
} }
// AccountUpdate updates the information of the current user. func UpdateCurrentAccount(profile *Profile) (*Account, error) {
func (c *Client) AccountUpdate(profile *Profile) (*Account, error) {
params := url.Values{} params := url.Values{}
if profile.DisplayName != nil { if profile.DisplayName != nil {
params.Set("display_name", *profile.DisplayName) params.Set("display_name", *profile.DisplayName)
@ -121,59 +119,54 @@ func (c *Client) AccountUpdate(profile *Profile) (*Account, error) {
} }
var account Account var account Account
err := c.doAPI(http.MethodPatch, "/api/v1/accounts/update_credentials", params, &account, nil) err := doAPI(http.MethodPatch, "/api/v1/accounts/update_credentials", params, &account, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &account, nil return &account, nil
} }
// GetAccountStatuses return statuses by specified account. func GetAccountStatuses(id ID, pg *Pagination) ([]*Status, error) {
func (c *Client) GetAccountStatuses(id ID, pg *Pagination) ([]*Status, error) {
var statuses []*Status var statuses []*Status
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/statuses", url.PathEscape(string(id))), nil, &statuses, pg) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/statuses", url.PathEscape(string(id))), nil, &statuses, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return statuses, nil return statuses, nil
} }
// GetAccountPinnedStatuses returns statuses pinned by specified accuont. func GetAccountPinnedStatuses(id ID) ([]*Status, error) {
func (c *Client) GetAccountPinnedStatuses(id ID) ([]*Status, error) {
var statuses []*Status var statuses []*Status
params := url.Values{} params := url.Values{}
params.Set("pinned", "true") params.Set("pinned", "true")
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/statuses", url.PathEscape(string(id))), params, &statuses, nil) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/statuses", url.PathEscape(string(id))), params, &statuses, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return statuses, nil return statuses, nil
} }
// GetAccountFollowers returns followers list. func GetAccountFollowers(id ID, pg *Pagination) ([]*Account, error) {
func (c *Client) GetAccountFollowers(id ID, pg *Pagination) ([]*Account, error) {
var accounts []*Account var accounts []*Account
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/followers", url.PathEscape(string(id))), nil, &accounts, pg) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/followers", url.PathEscape(string(id))), nil, &accounts, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return accounts, nil return accounts, nil
} }
// GetAccountFollowing returns following list. func GetAccountFollowing(id ID, pg *Pagination) ([]*Account, error) {
func (c *Client) GetAccountFollowing(id ID, pg *Pagination) ([]*Account, error) {
var accounts []*Account var accounts []*Account
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/following", url.PathEscape(string(id))), nil, &accounts, pg) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/following", url.PathEscape(string(id))), nil, &accounts, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return accounts, nil return accounts, nil
} }
// GetBlocks returns block list. func GetBlockList(pg *Pagination) ([]*Account, error) {
func (c *Client) GetBlocks(pg *Pagination) ([]*Account, error) {
var accounts []*Account var accounts []*Account
err := c.doAPI(http.MethodGet, "/api/v1/blocks", nil, &accounts, pg) err := doAPI(http.MethodGet, "/api/v1/blocks", nil, &accounts, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -194,132 +187,119 @@ type Relationship struct {
Endorsed bool `json:"endorsed"` Endorsed bool `json:"endorsed"`
} }
// AccountFollow follows the account. func FollowAccount(id ID) (*Relationship, error) {
func (c *Client) AccountFollow(id ID) (*Relationship, error) {
var relationship Relationship var relationship Relationship
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/follow", url.PathEscape(string(id))), nil, &relationship, nil) err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/follow", url.PathEscape(string(id))), nil, &relationship, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &relationship, nil return &relationship, nil
} }
// AccountUnfollow unfollows the account. func UnfollowAccount(id ID) (*Relationship, error) {
func (c *Client) AccountUnfollow(id ID) (*Relationship, error) {
var relationship Relationship var relationship Relationship
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/unfollow", url.PathEscape(string(id))), nil, &relationship, nil) err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/unfollow", url.PathEscape(string(id))), nil, &relationship, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &relationship, nil return &relationship, nil
} }
// AccountBlock blocks the account. func BlockAccount(id ID) (*Relationship, error) {
func (c *Client) AccountBlock(id ID) (*Relationship, error) {
var relationship Relationship var relationship Relationship
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/block", url.PathEscape(string(id))), nil, &relationship, nil) err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/block", url.PathEscape(string(id))), nil, &relationship, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &relationship, nil return &relationship, nil
} }
// AccountUnblock unblocks the account. func UnblockAccount(id ID) (*Relationship, error) {
func (c *Client) AccountUnblock(id ID) (*Relationship, error) {
var relationship Relationship var relationship Relationship
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/unblock", url.PathEscape(string(id))), nil, &relationship, nil) err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/unblock", url.PathEscape(string(id))), nil, &relationship, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &relationship, nil return &relationship, nil
} }
// AccountMute mutes the account. func MuteAccount(id ID) (*Relationship, error) {
func (c *Client) AccountMute(id ID) (*Relationship, error) {
var relationship Relationship var relationship Relationship
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/mute", url.PathEscape(string(id))), nil, &relationship, nil) err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/mute", url.PathEscape(string(id))), nil, &relationship, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &relationship, nil return &relationship, nil
} }
// AccountUnmute unmutes the account. func UnmuteAccount(id ID) (*Relationship, error) {
func (c *Client) AccountUnmute(id ID) (*Relationship, error) {
var relationship Relationship var relationship Relationship
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/unmute", url.PathEscape(string(id))), nil, &relationship, nil) err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/accounts/%s/unmute", url.PathEscape(string(id))), nil, &relationship, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &relationship, nil return &relationship, nil
} }
// GetAccountRelationships returns relationship for the account. func GetAccountRelationships(ids []string) ([]*Relationship, error) {
func (c *Client) GetAccountRelationships(ids []string) ([]*Relationship, error) {
params := url.Values{} params := url.Values{}
for _, id := range ids { for _, id := range ids {
params.Add("id[]", id) params.Add("id[]", id)
} }
var relationships []*Relationship var relationships []*Relationship
err := c.doAPI(http.MethodGet, "/api/v1/accounts/relationships", params, &relationships, nil) err := doAPI(http.MethodGet, "/api/v1/accounts/relationships", params, &relationships, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return relationships, nil return relationships, nil
} }
// AccountsSearch searches accounts by query. func SearchAccounts(q string, limit int64) ([]*Account, error) {
func (c *Client) AccountsSearch(q string, limit int64) ([]*Account, error) {
params := url.Values{} params := url.Values{}
params.Set("q", q) params.Set("q", q)
params.Set("limit", fmt.Sprint(limit)) params.Set("limit", fmt.Sprint(limit))
var accounts []*Account var accounts []*Account
err := c.doAPI(http.MethodGet, "/api/v1/accounts/search", params, &accounts, nil) err := doAPI(http.MethodGet, "/api/v1/accounts/search", params, &accounts, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return accounts, nil return accounts, nil
} }
// FollowRemoteUser sends follow-request. func FollowRemoteUser(uri string) (*Account, error) {
func (c *Client) FollowRemoteUser(uri string) (*Account, error) {
params := url.Values{} params := url.Values{}
params.Set("uri", uri) params.Set("uri", uri)
var account Account var account Account
err := c.doAPI(http.MethodPost, "/api/v1/follows", params, &account, nil) err := doAPI(http.MethodPost, "/api/v1/follows", params, &account, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &account, nil return &account, nil
} }
// GetFollowRequests returns follow requests. func GetFollowRequests(pg *Pagination) ([]*Account, error) {
func (c *Client) GetFollowRequests(pg *Pagination) ([]*Account, error) {
var accounts []*Account var accounts []*Account
err := c.doAPI(http.MethodGet, "/api/v1/follow_requests", nil, &accounts, pg) err := doAPI(http.MethodGet, "/api/v1/follow_requests", nil, &accounts, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return accounts, nil return accounts, nil
} }
// FollowRequestAuthorize authorizes the follow request of user with id. func AuthorizeFollowRequest(id ID) error {
func (c *Client) FollowRequestAuthorize(id ID) error { return doAPI(http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%s/authorize", url.PathEscape(string(id))), nil, nil, nil)
return c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%s/authorize", url.PathEscape(string(id))), nil, nil, nil)
} }
// FollowRequestReject rejects the follow request of user with id. func RejectFollowRequest(id ID) error {
func (c *Client) FollowRequestReject(id ID) error { return doAPI(http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%s/reject", url.PathEscape(string(id))), nil, nil, nil)
return c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%s/reject", url.PathEscape(string(id))), nil, nil, nil)
} }
// GetMutes returns the list of users muted by the current user. func GetMuteList(pg *Pagination) ([]*Account, error) {
func (c *Client) GetMutes(pg *Pagination) ([]*Account, error) {
var accounts []*Account var accounts []*Account
err := c.doAPI(http.MethodGet, "/api/v1/mutes", nil, &accounts, pg) err := doAPI(http.MethodGet, "/api/v1/mutes", nil, &accounts, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -39,7 +39,8 @@ type Application struct {
} }
// RegisterApp returns the mastodon application. // RegisterApp returns the mastodon application.
func RegisterApp(ctx context.Context, appConfig *AppConfig) (*Application, error) { func RegisterApp(appConfig *AppConfig) (*Application, error) {
ctx := context.Background()
params := url.Values{} params := url.Values{}
params.Set("client_name", appConfig.ClientName) params.Set("client_name", appConfig.ClientName)
if appConfig.RedirectURIs == "" { if appConfig.RedirectURIs == "" {
@ -102,10 +103,9 @@ type ApplicationVerification struct {
VapidKey string `json:"vapid_key"` VapidKey string `json:"vapid_key"`
} }
// VerifyAppCredentials returns the mastodon application. func GetAppCredentials() (*ApplicationVerification, error) {
func (c *Client) VerifyAppCredentials() (*ApplicationVerification, error) {
var application ApplicationVerification var application ApplicationVerification
err := c.doAPI(http.MethodGet, "/api/v1/apps/verify_credentials", nil, &application, nil) err := doAPI(http.MethodGet, "/api/v1/apps/verify_credentials", nil, &application, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -18,28 +18,26 @@ type Filter struct {
Irreversible bool `json:"irreversible"` Irreversible bool `json:"irreversible"`
} }
// GetFilters returns all the filters on the current account. func GetFilters() ([]*Filter, error) {
func (c *Client) GetFilters() ([]*Filter, error) {
var filters []*Filter var filters []*Filter
err := c.doAPI(http.MethodGet, "/api/v1/filters", nil, &filters, nil) err := doAPI(http.MethodGet, "/api/v1/filters", nil, &filters, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return filters, nil return filters, nil
} }
// GetFilter retrieves a filter by ID. // was ist id ?
func (c *Client) GetFilter(id ID) (*Filter, error) { func GetFilter(id ID) (*Filter, error) {
var filter Filter var filter Filter
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(string(id))), nil, &filter, nil) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(string(id))), nil, &filter, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &filter, nil return &filter, nil
} }
// CreateFilter creates a new filter. func CreateFilter(filter *Filter) (*Filter, error) {
func (c *Client) CreateFilter(filter *Filter) (*Filter, error) {
if filter == nil { if filter == nil {
return nil, errors.New("filter can't be nil") return nil, errors.New("filter can't be nil")
} }
@ -66,15 +64,14 @@ func (c *Client) CreateFilter(filter *Filter) (*Filter, error) {
} }
var f Filter var f Filter
err := c.doAPI(http.MethodPost, "/api/v1/filters", params, &f, nil) err := doAPI(http.MethodPost, "/api/v1/filters", params, &f, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &f, nil return &f, nil
} }
// UpdateFilter updates a filter. func UpdateFilter(id ID, filter *Filter) (*Filter, error) {
func (c *Client) UpdateFilter(id ID, filter *Filter) (*Filter, error) {
if filter == nil { if filter == nil {
return nil, errors.New("filter can't be nil") return nil, errors.New("filter can't be nil")
} }
@ -110,14 +107,13 @@ func (c *Client) UpdateFilter(id ID, filter *Filter) (*Filter, error) {
} }
var f Filter var f Filter
err := c.doAPI(http.MethodPut, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(string(id))), params, &f, nil) err := doAPI(http.MethodPut, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(string(id))), params, &f, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &f, nil return &f, nil
} }
// DeleteFilter removes a filter. func DeleteFilter(id ID) error {
func (c *Client) DeleteFilter(id ID) error { return doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(string(id))), nil, nil, nil)
return c.doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(string(id))), nil, nil, nil)
} }

View File

@ -4,7 +4,6 @@ import (
"net/http" "net/http"
) )
// Instance holds information for a mastodon instance.
type Instance struct { type Instance struct {
URI string `json:"uri"` URI string `json:"uri"`
Title string `json:"title"` Title string `json:"title"`
@ -18,24 +17,21 @@ type Instance struct {
ContactAccount *Account `json:"contact_account"` ContactAccount *Account `json:"contact_account"`
} }
// InstanceStats holds information for mastodon instance stats.
type InstanceStats struct { type InstanceStats struct {
UserCount int64 `json:"user_count"` UserCount int64 `json:"user_count"`
StatusCount int64 `json:"status_count"` StatusCount int64 `json:"status_count"`
DomainCount int64 `json:"domain_count"` DomainCount int64 `json:"domain_count"`
} }
// GetInstance returns Instance. func GetInstance() (*Instance, error) {
func (c *Client) GetInstance() (*Instance, error) {
var instance Instance var instance Instance
err := c.doAPI(http.MethodGet, "/api/v1/instance", nil, &instance, nil) err := doAPI(http.MethodGet, "/api/v1/instance", nil, &instance, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &instance, nil return &instance, nil
} }
// WeeklyActivity holds information for mastodon weekly activity.
type WeeklyActivity struct { type WeeklyActivity struct {
Week Unixtime `json:"week"` Week Unixtime `json:"week"`
Statuses int64 `json:"statuses,string"` Statuses int64 `json:"statuses,string"`
@ -43,20 +39,18 @@ type WeeklyActivity struct {
Registrations int64 `json:"registrations,string"` Registrations int64 `json:"registrations,string"`
} }
// GetInstanceActivity returns instance activity. func GetInstanceActivity() ([]*WeeklyActivity, error) {
func (c *Client) GetInstanceActivity() ([]*WeeklyActivity, error) {
var activity []*WeeklyActivity var activity []*WeeklyActivity
err := c.doAPI(http.MethodGet, "/api/v1/instance/activity", nil, &activity, nil) err := doAPI(http.MethodGet, "/api/v1/instance/activity", nil, &activity, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return activity, nil return activity, nil
} }
// GetInstancePeers returns instance peers. func GetInstancePeers() ([]string, error) {
func (c *Client) GetInstancePeers() ([]string, error) {
var peers []string var peers []string
err := c.doAPI(http.MethodGet, "/api/v1/instance/peers", nil, &peers, nil) err := doAPI(http.MethodGet, "/api/v1/instance/peers", nil, &peers, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -12,10 +12,9 @@ type List struct {
Title string `json:"title"` Title string `json:"title"`
} }
// GetLists returns all the lists on the current account. func GetLists() ([]*List, error) {
func (c *Client) GetLists() ([]*List, error) {
var lists []*List var lists []*List
err := c.doAPI(http.MethodGet, "/api/v1/lists", nil, &lists, nil) err := doAPI(http.MethodGet, "/api/v1/lists", nil, &lists, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -23,84 +22,73 @@ func (c *Client) GetLists() ([]*List, error) {
} }
// GetAccountLists returns the lists containing a given account. // GetAccountLists returns the lists containing a given account.
func (c *Client) GetAccountLists(id ID) ([]*List, error) { func GetListWithAccount(id ID) ([]*List, error) {
var lists []*List var lists []*List
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/lists", url.PathEscape(string(id))), nil, &lists, nil) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/lists", url.PathEscape(string(id))), nil, &lists, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return lists, nil return lists, nil
} }
// GetListAccounts returns the accounts in a given list. func GetAccountsInList(list *List) ([]*Account, error) {
func (c *Client) GetListAccounts(id ID) ([]*Account, error) {
var accounts []*Account var accounts []*Account
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(id))), url.Values{"limit": {"0"}}, &accounts, nil) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(list.ID))), url.Values{"limit": {"0"}}, &accounts, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return accounts, nil return accounts, nil
} }
// GetList retrieves a list by ID. func GetList(id ID) (*List, error) {
func (c *Client) GetList(id ID) (*List, error) {
var list List var list List
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/lists/%s", url.PathEscape(string(id))), nil, &list, nil) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/lists/%s", url.PathEscape(string(id))), nil, &list, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &list, nil return &list, nil
} }
// CreateList creates a new list with a given title. func CreateList(title string) (*List, error) {
func (c *Client) CreateList(title string) (*List, error) {
params := url.Values{} params := url.Values{}
params.Set("title", title) params.Set("title", title)
var list List var list List
err := c.doAPI(http.MethodPost, "/api/v1/lists", params, &list, nil) err := doAPI(http.MethodPost, "/api/v1/lists", params, &list, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &list, nil return &list, nil
} }
// RenameList assigns a new title to a list. func RenameList(list *List, newTitle string) error {
func (c *Client) RenameList(id ID, title string) (*List, error) {
params := url.Values{} params := url.Values{}
params.Set("title", title) params.Set("title", newTitle)
var list List return doAPI(http.MethodPut, fmt.Sprintf("/api/v1/lists/%s", url.PathEscape(string(list.ID))), params, nil, nil)
err := c.doAPI(http.MethodPut, fmt.Sprintf("/api/v1/lists/%s", url.PathEscape(string(id))), params, &list, nil)
if err != nil {
return nil, err
}
return &list, nil
} }
// DeleteList removes a list. func DeleteList(id ID) error {
func (c *Client) DeleteList(id ID) error { return doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/lists/%s", url.PathEscape(string(id))), nil, nil, nil)
return c.doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/lists/%s", url.PathEscape(string(id))), nil, nil, nil)
} }
// AddToList adds accounts to a list. // AddToList adds accounts to a list.
// //
// Only accounts already followed by the user can be added to a list. // Only accounts already followed by the user can be added to a list.
func (c *Client) AddToList(list ID, accounts ...ID) error { func AddToList(list ID, accounts ...ID) error {
params := url.Values{} params := url.Values{}
for _, acct := range accounts { for _, acct := range accounts {
params.Add("account_ids", string(acct)) params.Add("account_ids", string(acct))
} }
return c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(list))), params, nil, nil) return doAPI(http.MethodPost, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(list))), params, nil, nil)
} }
// RemoveFromList removes accounts from a list. func RemoveFromList(list ID, accounts ...ID) error {
func (c *Client) RemoveFromList(list ID, accounts ...ID) error {
params := url.Values{} params := url.Values{}
for _, acct := range accounts { for _, acct := range accounts {
params.Add("account_ids", string(acct)) params.Add("account_ids", string(acct))
} }
return c.doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(list))), params, nil, nil) return doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(list))), params, nil, nil)
} }

View File

@ -29,7 +29,21 @@ type Client struct {
UserAgent string UserAgent string
} }
func (c *Client) doAPI(method string, uri string, params interface{}, res interface{}, pg *Pagination) error { var client *Client
func checkInit() {
if client == nil {
panic("Well, guess who didn't read the manual.")
}
}
func Initialize(config *Config) {
client = newClient(config)
}
func doAPI(method string, uri string, params interface{}, res interface{}, pg *Pagination) error {
checkInit()
c := client
ctx := context.Background() ctx := context.Background()
u, err := url.Parse(c.Config.Server) u, err := url.Parse(c.Config.Server)
if err != nil { if err != nil {
@ -128,7 +142,7 @@ func (c *Client) doAPI(method string, uri string, params interface{}, res interf
} }
// NewClient returns a new mastodon API client. // NewClient returns a new mastodon API client.
func NewClient(config *Config) *Client { func newClient(config *Config) *Client {
return &Client{ return &Client{
Client: *http.DefaultClient, Client: *http.DefaultClient,
Config: config, Config: config,

View File

@ -34,38 +34,33 @@ type PushAlerts struct {
Mention *Sbool `json:"mention"` Mention *Sbool `json:"mention"`
} }
// GetNotifications returns notifications. func GetNotifications(pg *Pagination) ([]*Notification, error) {
func (c *Client) GetNotifications(pg *Pagination) ([]*Notification, error) {
var notifications []*Notification var notifications []*Notification
err := c.doAPI(http.MethodGet, "/api/v1/notifications", nil, &notifications, pg) err := doAPI(http.MethodGet, "/api/v1/notifications", nil, &notifications, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return notifications, nil return notifications, nil
} }
// GetNotification returns notification. func GetNotification(id ID) (*Notification, error) {
func (c *Client) GetNotification(id ID) (*Notification, error) {
var notification Notification var notification Notification
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/notifications/%v", id), nil, &notification, nil) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/notifications/%v", id), nil, &notification, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &notification, nil return &notification, nil
} }
// DismissNotification deletes a single notification. func DismissNotification(id ID) error {
func (c *Client) DismissNotification(id ID) error { return doAPI(http.MethodPost, fmt.Sprintf("/api/v1/notifications/%v/dismiss", id), nil, nil, nil)
return c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/notifications/%v/dismiss", id), nil, nil, nil)
} }
// ClearNotifications clears notifications. func ClearNotifications() error {
func (c *Client) ClearNotifications() error { return doAPI(http.MethodPost, "/api/v1/notifications/clear", nil, nil, nil)
return c.doAPI(http.MethodPost, "/api/v1/notifications/clear", nil, nil, nil)
} }
// AddPushSubscription adds a new push subscription. func AddPushSubscription(endpoint string, public ecdsa.PublicKey, shared []byte, alerts PushAlerts) (*PushSubscription, error) {
func (c *Client) AddPushSubscription(endpoint string, public ecdsa.PublicKey, shared []byte, alerts PushAlerts) (*PushSubscription, error) {
var subscription PushSubscription var subscription PushSubscription
pk := elliptic.Marshal(public.Curve, public.X, public.Y) pk := elliptic.Marshal(public.Curve, public.X, public.Y)
params := url.Values{} params := url.Values{}
@ -84,7 +79,7 @@ func (c *Client) AddPushSubscription(endpoint string, public ecdsa.PublicKey, sh
if alerts.Mention != nil { if alerts.Mention != nil {
params.Add("data[alerts][mention]", strconv.FormatBool(bool(*alerts.Mention))) params.Add("data[alerts][mention]", strconv.FormatBool(bool(*alerts.Mention)))
} }
err := c.doAPI(http.MethodPost, "/api/v1/push/subscription", params, &subscription, nil) err := doAPI(http.MethodPost, "/api/v1/push/subscription", params, &subscription, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -92,7 +87,7 @@ func (c *Client) AddPushSubscription(endpoint string, public ecdsa.PublicKey, sh
} }
// UpdatePushSubscription updates which type of notifications are sent for the active push subscription. // UpdatePushSubscription updates which type of notifications are sent for the active push subscription.
func (c *Client) UpdatePushSubscription(alerts *PushAlerts) (*PushSubscription, error) { func UpdatePushSubscription(alerts *PushAlerts) (*PushSubscription, error) {
var subscription PushSubscription var subscription PushSubscription
params := url.Values{} params := url.Values{}
if alerts.Follow != nil { if alerts.Follow != nil {
@ -107,22 +102,20 @@ func (c *Client) UpdatePushSubscription(alerts *PushAlerts) (*PushSubscription,
if alerts.Mention != nil { if alerts.Mention != nil {
params.Add("data[alerts][mention]", strconv.FormatBool(bool(*alerts.Mention))) params.Add("data[alerts][mention]", strconv.FormatBool(bool(*alerts.Mention)))
} }
err := c.doAPI(http.MethodPut, "/api/v1/push/subscription", params, &subscription, nil) err := doAPI(http.MethodPut, "/api/v1/push/subscription", params, &subscription, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &subscription, nil return &subscription, nil
} }
// RemovePushSubscription deletes the active push subscription. func RemovePushSubscription() error {
func (c *Client) RemovePushSubscription() error { return doAPI(http.MethodDelete, "/api/v1/push/subscription", nil, nil, nil)
return c.doAPI(http.MethodDelete, "/api/v1/push/subscription", nil, nil, nil)
} }
// GetPushSubscription retrieves information about the active push subscription. func GetPushSubscription() (*PushSubscription, error) {
func (c *Client) GetPushSubscription() (*PushSubscription, error) {
var subscription PushSubscription var subscription PushSubscription
err := c.doAPI(http.MethodGet, "/api/v1/push/subscription", nil, &subscription, nil) err := doAPI(http.MethodGet, "/api/v1/push/subscription", nil, &subscription, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -7,7 +7,6 @@ import (
"time" "time"
) )
// Poll holds information for mastodon polls.
type Poll struct { type Poll struct {
ID ID `json:"id"` ID ID `json:"id"`
ExpiresAt time.Time `json:"expires_at"` ExpiresAt time.Time `json:"expires_at"`
@ -21,31 +20,28 @@ type Poll struct {
Emojis []Emoji `json:"emojis"` Emojis []Emoji `json:"emojis"`
} }
// Poll holds information for a mastodon poll option.
type PollOption struct { type PollOption struct {
Title string `json:"title"` Title string `json:"title"`
VotesCount int64 `json:"votes_count"` VotesCount int64 `json:"votes_count"`
} }
// GetPoll returns poll specified by id. func GetPoll(id ID) (*Poll, error) {
func (c *Client) GetPoll(id ID) (*Poll, error) {
var poll Poll var poll Poll
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/polls/%s", id), nil, &poll, nil) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/polls/%s", id), nil, &poll, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &poll, nil return &poll, nil
} }
// PollVote votes on a poll specified by id, choices is the Poll.Options index to vote on func PollVote(id ID, choices ...int) (*Poll, error) {
func (c *Client) PollVote(id ID, choices ...int) (*Poll, error) {
params := url.Values{} params := url.Values{}
for _, c := range choices { for _, c := range choices {
params.Add("choices[]", fmt.Sprintf("%d", c)) params.Add("choices[]", fmt.Sprintf("%d", c))
} }
var poll Poll var poll Poll
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/polls/%s/votes", url.PathEscape(string(id))), params, &poll, nil) err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/polls/%s/votes", url.PathEscape(string(id))), params, &poll, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -11,18 +11,16 @@ type Report struct {
ActionTaken bool `json:"action_taken"` ActionTaken bool `json:"action_taken"`
} }
// GetReports returns report of the current user. func GetReports() ([]*Report, error) {
func (c *Client) GetReports() ([]*Report, error) {
var reports []*Report var reports []*Report
err := c.doAPI(http.MethodGet, "/api/v1/reports", nil, &reports, nil) err := doAPI(http.MethodGet, "/api/v1/reports", nil, &reports, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return reports, nil return reports, nil
} }
// Report reports the report func ReportAccount(accountID ID, ids []ID, comment string) (*Report, error) {
func (c *Client) Report(accountID ID, ids []ID, comment string) (*Report, error) {
params := url.Values{} params := url.Values{}
params.Set("account_id", string(accountID)) params.Set("account_id", string(accountID))
for _, id := range ids { for _, id := range ids {
@ -30,7 +28,7 @@ func (c *Client) Report(accountID ID, ids []ID, comment string) (*Report, error)
} }
params.Set("comment", comment) params.Set("comment", comment)
var report Report var report Report
err := c.doAPI(http.MethodPost, "/api/v1/reports", params, &report, nil) err := doAPI(http.MethodPost, "/api/v1/reports", params, &report, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

147
status.go
View File

@ -12,7 +12,6 @@ import (
"time" "time"
) )
// Status is struct to hold status.
type Status struct { type Status struct {
ID ID `json:"id"` ID ID `json:"id"`
URI string `json:"uri"` URI string `json:"uri"`
@ -44,13 +43,11 @@ type Status struct {
Pinned interface{} `json:"pinned"` Pinned interface{} `json:"pinned"`
} }
// Context holds information for a mastodon context.
type Context struct { type Context struct {
Ancestors []*Status `json:"ancestors"` Ancestors []*Status `json:"ancestors"`
Descendants []*Status `json:"descendants"` Descendants []*Status `json:"descendants"`
} }
// Card holds information for a mastodon card.
type Card struct { type Card struct {
URL string `json:"url"` URL string `json:"url"`
Title string `json:"title"` Title string `json:"title"`
@ -66,7 +63,6 @@ type Card struct {
Height int64 `json:"height"` Height int64 `json:"height"`
} }
// Conversation holds information for a mastodon conversation.
type Conversation struct { type Conversation struct {
ID ID `json:"id"` ID ID `json:"id"`
Accounts []*Account `json:"accounts"` Accounts []*Account `json:"accounts"`
@ -74,7 +70,6 @@ type Conversation struct {
LastStatus *Status `json:"last_status"` LastStatus *Status `json:"last_status"`
} }
// Media is struct to hold media.
type Media struct { type Media struct {
File io.Reader File io.Reader
Thumbnail io.Reader Thumbnail io.Reader
@ -139,180 +134,163 @@ func (m *Media) bodyAndContentType() (io.Reader, string, error) {
return &buf, mw.FormDataContentType(), nil return &buf, mw.FormDataContentType(), nil
} }
// GetFavourites returns the favorite list of the current user. func GetFavourites(pg *Pagination) ([]*Status, error) {
func (c *Client) GetFavourites(pg *Pagination) ([]*Status, error) {
var statuses []*Status var statuses []*Status
err := c.doAPI(http.MethodGet, "/api/v1/favourites", nil, &statuses, pg) err := doAPI(http.MethodGet, "/api/v1/favourites", nil, &statuses, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return statuses, nil return statuses, nil
} }
// GetBookmarks returns the bookmark list of the current user. func GetBookmarks(pg *Pagination) ([]*Status, error) {
func (c *Client) GetBookmarks(pg *Pagination) ([]*Status, error) {
var statuses []*Status var statuses []*Status
err := c.doAPI(http.MethodGet, "/api/v1/bookmarks", nil, &statuses, pg) err := doAPI(http.MethodGet, "/api/v1/bookmarks", nil, &statuses, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return statuses, nil return statuses, nil
} }
// GetStatus returns status specified by id. func GetStatus(id ID) (*Status, error) {
func (c *Client) GetStatus(id ID) (*Status, error) {
var status Status var status Status
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s", id), nil, &status, nil) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s", id), nil, &status, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &status, nil return &status, nil
} }
// GetStatusContext returns status specified by id. func GetStatusContext(id ID) (*Context, error) {
func (c *Client) GetStatusContext(id ID) (*Context, error) {
var context Context var context Context
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/context", id), nil, &context, nil) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/context", id), nil, &context, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &context, nil return &context, nil
} }
// GetStatusCard returns status specified by id. func GetStatusCard(id ID) (*Card, error) {
func (c *Client) GetStatusCard(id ID) (*Card, error) {
var card Card var card Card
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/card", id), nil, &card, nil) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/card", id), nil, &card, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &card, nil return &card, nil
} }
// GetRebloggedBy returns the account list of the user who reblogged the toot of id. func GetRebloggedBy(id ID, pg *Pagination) ([]*Account, error) {
func (c *Client) GetRebloggedBy(id ID, pg *Pagination) ([]*Account, error) {
var accounts []*Account var accounts []*Account
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/reblogged_by", id), nil, &accounts, pg) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/reblogged_by", id), nil, &accounts, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return accounts, nil return accounts, nil
} }
// GetFavouritedBy returns the account list of the user who liked the toot of id. func GetFavouritedBy(id ID, pg *Pagination) ([]*Account, error) {
func (c *Client) GetFavouritedBy(id ID, pg *Pagination) ([]*Account, error) {
var accounts []*Account var accounts []*Account
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/favourited_by", id), nil, &accounts, pg) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/statuses/%s/favourited_by", id), nil, &accounts, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return accounts, nil return accounts, nil
} }
// Reblog reblogs the toot of id and returns status of reblog. func Reblog(id ID) (*Status, error) {
func (c *Client) Reblog(id ID) (*Status, error) {
var status Status var status Status
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/reblog", id), nil, &status, nil) err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/reblog", id), nil, &status, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &status, nil return &status, nil
} }
// Unreblog unreblogs the toot of id and returns status of the original toot. func Unreblog(id ID) (*Status, error) {
func (c *Client) Unreblog(id ID) (*Status, error) {
var status Status var status Status
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unreblog", id), nil, &status, nil) err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unreblog", id), nil, &status, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &status, nil return &status, nil
} }
// Favourite favourites the toot of id and returns status of the favourite toot. func Favourite(id ID) (*Status, error) {
func (c *Client) Favourite(id ID) (*Status, error) {
var status Status var status Status
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/favourite", id), nil, &status, nil) err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/favourite", id), nil, &status, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &status, nil return &status, nil
} }
// Unfavourite unfavourites the toot of id and returns status of the unfavourite toot. func Unfavourite(id ID) (*Status, error) {
func (c *Client) Unfavourite(id ID) (*Status, error) {
var status Status var status Status
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unfavourite", id), nil, &status, nil) err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unfavourite", id), nil, &status, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &status, nil return &status, nil
} }
// Bookmark bookmarks the toot of id and returns status of the bookmark toot. func Bookmark(id ID) (*Status, error) {
func (c *Client) Bookmark(id ID) (*Status, error) {
var status Status var status Status
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/bookmark", id), nil, &status, nil) err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/bookmark", id), nil, &status, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &status, nil return &status, nil
} }
// Unbookmark is unbookmark the toot of id and return status of the unbookmark toot.
func (c *Client) Unbookmark(id ID) (*Status, error) { func (c *Client) Unbookmark(id ID) (*Status, error) {
var status Status var status Status
err := c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unbookmark", id), nil, &status, nil) err := doAPI(http.MethodPost, fmt.Sprintf("/api/v1/statuses/%s/unbookmark", id), nil, &status, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &status, nil return &status, nil
} }
// GetTimelineHome return statuses from home timeline. func (c *Client) GetHomeTimeline(pg *Pagination) ([]*Status, error) {
func (c *Client) GetTimelineHome(pg *Pagination) ([]*Status, error) {
var statuses []*Status var statuses []*Status
err := c.doAPI(http.MethodGet, "/api/v1/timelines/home", nil, &statuses, pg) err := doAPI(http.MethodGet, "/api/v1/timelines/home", nil, &statuses, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return statuses, nil return statuses, nil
} }
// GetTimelinePublic return statuses from public timeline. func GetPublicTimeline(isLocal bool, pg *Pagination) ([]*Status, error) {
func (c *Client) GetTimelinePublic(isLocal bool, pg *Pagination) ([]*Status, error) {
params := url.Values{} params := url.Values{}
if isLocal { if isLocal {
params.Set("local", "t") params.Set("local", "t")
} }
var statuses []*Status var statuses []*Status
err := c.doAPI(http.MethodGet, "/api/v1/timelines/public", params, &statuses, pg) err := doAPI(http.MethodGet, "/api/v1/timelines/public", params, &statuses, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return statuses, nil return statuses, nil
} }
// GetTimelineHashtag return statuses from tagged timeline. func GetTaggedTimeline(tag string, isLocal bool, pg *Pagination) ([]*Status, error) {
func (c *Client) GetTimelineHashtag(tag string, isLocal bool, pg *Pagination) ([]*Status, error) {
params := url.Values{} params := url.Values{}
if isLocal { if isLocal {
params.Set("local", "t") params.Set("local", "t")
} }
var statuses []*Status var statuses []*Status
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/timelines/tag/%s", url.PathEscape(tag)), params, &statuses, pg) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/timelines/tag/%s", url.PathEscape(tag)), params, &statuses, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return statuses, nil return statuses, nil
} }
// GetTimelineList return statuses from a list timeline. func GetListTimeline(id ID, pg *Pagination) ([]*Status, error) {
func (c *Client) GetTimelineList(id ID, pg *Pagination) ([]*Status, error) {
var statuses []*Status var statuses []*Status
err := c.doAPI(http.MethodGet, fmt.Sprintf("/api/v1/timelines/list/%s", url.PathEscape(string(id))), nil, &statuses, pg) err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/timelines/list/%s", url.PathEscape(string(id))), nil, &statuses, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -321,7 +299,7 @@ func (c *Client) GetTimelineList(id ID, pg *Pagination) ([]*Status, error) {
// GetTimelineMedia return statuses from media timeline. // GetTimelineMedia return statuses from media timeline.
// NOTE: This is an experimental feature of pawoo.net. // NOTE: This is an experimental feature of pawoo.net.
func (c *Client) GetTimelineMedia(isLocal bool, pg *Pagination) ([]*Status, error) { func GetMediaTimeline(isLocal bool, pg *Pagination) ([]*Status, error) {
params := url.Values{} params := url.Values{}
params.Set("media", "t") params.Set("media", "t")
if isLocal { if isLocal {
@ -329,15 +307,14 @@ func (c *Client) GetTimelineMedia(isLocal bool, pg *Pagination) ([]*Status, erro
} }
var statuses []*Status var statuses []*Status
err := c.doAPI(http.MethodGet, "/api/v1/timelines/public", params, &statuses, pg) err := doAPI(http.MethodGet, "/api/v1/timelines/public", params, &statuses, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return statuses, nil return statuses, nil
} }
// PostStatus post the toot. func PostStatus(toot *Toot) (*Status, error) {
func (c *Client) PostStatus(toot *Toot) (*Status, error) {
params := url.Values{} params := url.Values{}
params.Set("status", toot.Status) params.Set("status", toot.Status)
if toot.InReplyToID != "" { if toot.InReplyToID != "" {
@ -375,67 +352,60 @@ func (c *Client) PostStatus(toot *Toot) (*Status, error) {
} }
var status Status var status Status
err := c.doAPI(http.MethodPost, "/api/v1/statuses", params, &status, nil) err := doAPI(http.MethodPost, "/api/v1/statuses", params, &status, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &status, nil return &status, nil
} }
// DeleteStatus delete the toot. func DeleteStatus(id ID) error {
func (c *Client) DeleteStatus(id ID) error { return doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/statuses/%s", id), nil, nil, nil)
return c.doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/statuses/%s", id), nil, nil, nil)
} }
// Search search content with query. func Search(q string, resolve bool) (*Results, error) {
func (c *Client) Search(q string, resolve bool) (*Results, error) {
params := url.Values{} params := url.Values{}
params.Set("q", q) params.Set("q", q)
params.Set("resolve", fmt.Sprint(resolve)) params.Set("resolve", fmt.Sprint(resolve))
var results Results var results Results
err := c.doAPI(http.MethodGet, "/api/v2/search", params, &results, nil) err := doAPI(http.MethodGet, "/api/v2/search", params, &results, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &results, nil return &results, nil
} }
// UploadMedia upload a media attachment from a file. func UploadMedia(file string) (*Attachment, error) {
func (c *Client) UploadMedia(file string) (*Attachment, error) {
f, err := os.Open(file) f, err := os.Open(file)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer f.Close() defer f.Close()
return c.UploadMediaFromMedia(&Media{File: f}) return UploadMediaFromMedia(&Media{File: f})
} }
// UploadMediaFromBytes uploads a media attachment from a byte slice. func UploadMediaFromBytes(b []byte) (*Attachment, error) {
func (c *Client) UploadMediaFromBytes(b []byte) (*Attachment, error) { return UploadMediaFromReader(bytes.NewReader(b))
return c.UploadMediaFromReader(bytes.NewReader(b))
} }
// UploadMediaFromReader uploads a media attachment from an io.Reader. func UploadMediaFromReader(reader io.Reader) (*Attachment, error) {
func (c *Client) UploadMediaFromReader(reader io.Reader) (*Attachment, error) { return UploadMediaFromMedia(&Media{File: reader})
return c.UploadMediaFromMedia(&Media{File: reader})
} }
// UploadMediaFromMedia uploads a media attachment from a Media struct. func UploadMediaFromMedia(media *Media) (*Attachment, error) {
func (c *Client) UploadMediaFromMedia(media *Media) (*Attachment, error) {
var attachment Attachment var attachment Attachment
if err := c.doAPI(http.MethodPost, "/api/v1/media", media, &attachment, nil); err != nil { if err := doAPI(http.MethodPost, "/api/v1/media", media, &attachment, nil); err != nil {
return nil, err return nil, err
} }
return &attachment, nil return &attachment, nil
} }
// GetTimelineDirect return statuses from direct timeline. func GetDirectTimeline(pg *Pagination) ([]*Status, error) {
func (c *Client) GetTimelineDirect(pg *Pagination) ([]*Status, error) {
params := url.Values{} params := url.Values{}
var conversations []*Conversation var conversations []*Conversation
err := c.doAPI(http.MethodGet, "/api/v1/conversations", params, &conversations, pg) err := doAPI(http.MethodGet, "/api/v1/conversations", params, &conversations, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -450,24 +420,21 @@ func (c *Client) GetTimelineDirect(pg *Pagination) ([]*Status, error) {
return statuses, nil return statuses, nil
} }
// GetConversations return direct conversations. func GetConversations(pg *Pagination) ([]*Conversation, error) {
func (c *Client) GetConversations(pg *Pagination) ([]*Conversation, error) {
params := url.Values{} params := url.Values{}
var conversations []*Conversation var conversations []*Conversation
err := c.doAPI(http.MethodGet, "/api/v1/conversations", params, &conversations, pg) err := doAPI(http.MethodGet, "/api/v1/conversations", params, &conversations, pg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return conversations, nil return conversations, nil
} }
// DeleteConversation delete the conversation specified by id. func DeleteConversation(id ID) error {
func (c *Client) DeleteConversation(id ID) error { return doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/conversations/%s", id), nil, nil, nil)
return c.doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/conversations/%s", id), nil, nil, nil)
} }
// MarkConversationAsRead mark the conversation as read. func MarkConversationAsRead(id ID) error {
func (c *Client) MarkConversationAsRead(id ID) error { return doAPI(http.MethodPost, fmt.Sprintf("/api/v1/conversations/%s/read", id), nil, nil, nil)
return c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/conversations/%s/read", id), nil, nil, nil)
} }