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"`
}
// GetAccount return Account.
func (c *Client) GetAccount(id ID) (*Account, error) {
func GetAccount(id ID) (*Account, error) {
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 {
return nil, err
}
return &account, nil
}
// GetAccountCurrentUser returns the Account of current user.
func (c *Client) GetAccountCurrentUser() (*Account, error) {
func GetCurrentAccount() (*Account, error) {
checkInit()
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 {
return nil, err
}
@ -84,8 +83,7 @@ type Profile struct {
Header string
}
// AccountUpdate updates the information of the current user.
func (c *Client) AccountUpdate(profile *Profile) (*Account, error) {
func UpdateCurrentAccount(profile *Profile) (*Account, error) {
params := url.Values{}
if profile.DisplayName != nil {
params.Set("display_name", *profile.DisplayName)
@ -121,59 +119,54 @@ func (c *Client) AccountUpdate(profile *Profile) (*Account, error) {
}
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 {
return nil, err
}
return &account, nil
}
// GetAccountStatuses return statuses by specified account.
func (c *Client) GetAccountStatuses(id ID, pg *Pagination) ([]*Status, error) {
func GetAccountStatuses(id ID, pg *Pagination) ([]*Status, error) {
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 {
return nil, err
}
return statuses, nil
}
// GetAccountPinnedStatuses returns statuses pinned by specified accuont.
func (c *Client) GetAccountPinnedStatuses(id ID) ([]*Status, error) {
func GetAccountPinnedStatuses(id ID) ([]*Status, error) {
var statuses []*Status
params := url.Values{}
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 {
return nil, err
}
return statuses, nil
}
// GetAccountFollowers returns followers list.
func (c *Client) GetAccountFollowers(id ID, pg *Pagination) ([]*Account, error) {
func GetAccountFollowers(id ID, pg *Pagination) ([]*Account, error) {
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 {
return nil, err
}
return accounts, nil
}
// GetAccountFollowing returns following list.
func (c *Client) GetAccountFollowing(id ID, pg *Pagination) ([]*Account, error) {
func GetAccountFollowing(id ID, pg *Pagination) ([]*Account, error) {
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 {
return nil, err
}
return accounts, nil
}
// GetBlocks returns block list.
func (c *Client) GetBlocks(pg *Pagination) ([]*Account, error) {
func GetBlockList(pg *Pagination) ([]*Account, error) {
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 {
return nil, err
}
@ -194,132 +187,119 @@ type Relationship struct {
Endorsed bool `json:"endorsed"`
}
// AccountFollow follows the account.
func (c *Client) AccountFollow(id ID) (*Relationship, error) {
func FollowAccount(id ID) (*Relationship, error) {
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 {
return nil, err
}
return &relationship, nil
}
// AccountUnfollow unfollows the account.
func (c *Client) AccountUnfollow(id ID) (*Relationship, error) {
func UnfollowAccount(id ID) (*Relationship, error) {
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 {
return nil, err
}
return &relationship, nil
}
// AccountBlock blocks the account.
func (c *Client) AccountBlock(id ID) (*Relationship, error) {
func BlockAccount(id ID) (*Relationship, error) {
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 {
return nil, err
}
return &relationship, nil
}
// AccountUnblock unblocks the account.
func (c *Client) AccountUnblock(id ID) (*Relationship, error) {
func UnblockAccount(id ID) (*Relationship, error) {
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 {
return nil, err
}
return &relationship, nil
}
// AccountMute mutes the account.
func (c *Client) AccountMute(id ID) (*Relationship, error) {
func MuteAccount(id ID) (*Relationship, error) {
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 {
return nil, err
}
return &relationship, nil
}
// AccountUnmute unmutes the account.
func (c *Client) AccountUnmute(id ID) (*Relationship, error) {
func UnmuteAccount(id ID) (*Relationship, error) {
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 {
return nil, err
}
return &relationship, nil
}
// GetAccountRelationships returns relationship for the account.
func (c *Client) GetAccountRelationships(ids []string) ([]*Relationship, error) {
func GetAccountRelationships(ids []string) ([]*Relationship, error) {
params := url.Values{}
for _, id := range ids {
params.Add("id[]", id)
}
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 {
return nil, err
}
return relationships, nil
}
// AccountsSearch searches accounts by query.
func (c *Client) AccountsSearch(q string, limit int64) ([]*Account, error) {
func SearchAccounts(q string, limit int64) ([]*Account, error) {
params := url.Values{}
params.Set("q", q)
params.Set("limit", fmt.Sprint(limit))
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 {
return nil, err
}
return accounts, nil
}
// FollowRemoteUser sends follow-request.
func (c *Client) FollowRemoteUser(uri string) (*Account, error) {
func FollowRemoteUser(uri string) (*Account, error) {
params := url.Values{}
params.Set("uri", uri)
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 {
return nil, err
}
return &account, nil
}
// GetFollowRequests returns follow requests.
func (c *Client) GetFollowRequests(pg *Pagination) ([]*Account, error) {
func GetFollowRequests(pg *Pagination) ([]*Account, error) {
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 {
return nil, err
}
return accounts, nil
}
// FollowRequestAuthorize authorizes the follow request of user with id.
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)
func AuthorizeFollowRequest(id ID) error {
return 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 (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)
func RejectFollowRequest(id ID) error {
return 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 (c *Client) GetMutes(pg *Pagination) ([]*Account, error) {
func GetMuteList(pg *Pagination) ([]*Account, error) {
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 {
return nil, err
}

View File

@ -39,7 +39,8 @@ type Application struct {
}
// 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.Set("client_name", appConfig.ClientName)
if appConfig.RedirectURIs == "" {
@ -102,10 +103,9 @@ type ApplicationVerification struct {
VapidKey string `json:"vapid_key"`
}
// VerifyAppCredentials returns the mastodon application.
func (c *Client) VerifyAppCredentials() (*ApplicationVerification, error) {
func GetAppCredentials() (*ApplicationVerification, error) {
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 {
return nil, err
}

View File

@ -18,28 +18,26 @@ type Filter struct {
Irreversible bool `json:"irreversible"`
}
// GetFilters returns all the filters on the current account.
func (c *Client) GetFilters() ([]*Filter, error) {
func GetFilters() ([]*Filter, error) {
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 {
return nil, err
}
return filters, nil
}
// GetFilter retrieves a filter by ID.
func (c *Client) GetFilter(id ID) (*Filter, error) {
// was ist id ?
func GetFilter(id ID) (*Filter, error) {
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 {
return nil, err
}
return &filter, nil
}
// CreateFilter creates a new filter.
func (c *Client) CreateFilter(filter *Filter) (*Filter, error) {
func CreateFilter(filter *Filter) (*Filter, error) {
if filter == 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
err := c.doAPI(http.MethodPost, "/api/v1/filters", params, &f, nil)
err := doAPI(http.MethodPost, "/api/v1/filters", params, &f, nil)
if err != nil {
return nil, err
}
return &f, nil
}
// UpdateFilter updates a filter.
func (c *Client) UpdateFilter(id ID, filter *Filter) (*Filter, error) {
func UpdateFilter(id ID, filter *Filter) (*Filter, error) {
if filter == 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
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 {
return nil, err
}
return &f, nil
}
// DeleteFilter removes a filter.
func (c *Client) DeleteFilter(id ID) error {
return c.doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/filters/%s", url.PathEscape(string(id))), nil, nil, nil)
func DeleteFilter(id ID) error {
return 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"
)
// Instance holds information for a mastodon instance.
type Instance struct {
URI string `json:"uri"`
Title string `json:"title"`
@ -18,24 +17,21 @@ type Instance struct {
ContactAccount *Account `json:"contact_account"`
}
// InstanceStats holds information for mastodon instance stats.
type InstanceStats struct {
UserCount int64 `json:"user_count"`
StatusCount int64 `json:"status_count"`
DomainCount int64 `json:"domain_count"`
}
// GetInstance returns Instance.
func (c *Client) GetInstance() (*Instance, error) {
func GetInstance() (*Instance, error) {
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 {
return nil, err
}
return &instance, nil
}
// WeeklyActivity holds information for mastodon weekly activity.
type WeeklyActivity struct {
Week Unixtime `json:"week"`
Statuses int64 `json:"statuses,string"`
@ -43,20 +39,18 @@ type WeeklyActivity struct {
Registrations int64 `json:"registrations,string"`
}
// GetInstanceActivity returns instance activity.
func (c *Client) GetInstanceActivity() ([]*WeeklyActivity, error) {
func GetInstanceActivity() ([]*WeeklyActivity, error) {
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 {
return nil, err
}
return activity, nil
}
// GetInstancePeers returns instance peers.
func (c *Client) GetInstancePeers() ([]string, error) {
func GetInstancePeers() ([]string, error) {
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 {
return nil, err
}

View File

@ -12,10 +12,9 @@ type List struct {
Title string `json:"title"`
}
// GetLists returns all the lists on the current account.
func (c *Client) GetLists() ([]*List, error) {
func GetLists() ([]*List, error) {
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 {
return nil, err
}
@ -23,84 +22,73 @@ func (c *Client) GetLists() ([]*List, error) {
}
// 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
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 {
return nil, err
}
return lists, nil
}
// GetListAccounts returns the accounts in a given list.
func (c *Client) GetListAccounts(id ID) ([]*Account, error) {
func GetAccountsInList(list *List) ([]*Account, error) {
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 {
return nil, err
}
return accounts, nil
}
// GetList retrieves a list by ID.
func (c *Client) GetList(id ID) (*List, error) {
func GetList(id ID) (*List, error) {
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 {
return nil, err
}
return &list, nil
}
// CreateList creates a new list with a given title.
func (c *Client) CreateList(title string) (*List, error) {
func CreateList(title string) (*List, error) {
params := url.Values{}
params.Set("title", title)
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 {
return nil, err
}
return &list, nil
}
// RenameList assigns a new title to a list.
func (c *Client) RenameList(id ID, title string) (*List, error) {
func RenameList(list *List, newTitle string) error {
params := url.Values{}
params.Set("title", title)
params.Set("title", newTitle)
var list List
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
return doAPI(http.MethodPut, fmt.Sprintf("/api/v1/lists/%s", url.PathEscape(string(list.ID))), params, nil, nil)
}
// DeleteList removes a list.
func (c *Client) DeleteList(id ID) error {
return c.doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/lists/%s", url.PathEscape(string(id))), nil, nil, nil)
func DeleteList(id ID) error {
return doAPI(http.MethodDelete, fmt.Sprintf("/api/v1/lists/%s", url.PathEscape(string(id))), nil, nil, nil)
}
// AddToList adds accounts 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{}
for _, acct := range accounts {
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 (c *Client) RemoveFromList(list ID, accounts ...ID) error {
func RemoveFromList(list ID, accounts ...ID) error {
params := url.Values{}
for _, acct := range accounts {
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
}
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()
u, err := url.Parse(c.Config.Server)
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.
func NewClient(config *Config) *Client {
func newClient(config *Config) *Client {
return &Client{
Client: *http.DefaultClient,
Config: config,

View File

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

View File

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

View File

@ -11,18 +11,16 @@ type Report struct {
ActionTaken bool `json:"action_taken"`
}
// GetReports returns report of the current user.
func (c *Client) GetReports() ([]*Report, error) {
func GetReports() ([]*Report, error) {
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 {
return nil, err
}
return reports, nil
}
// Report reports the report
func (c *Client) Report(accountID ID, ids []ID, comment string) (*Report, error) {
func ReportAccount(accountID ID, ids []ID, comment string) (*Report, error) {
params := url.Values{}
params.Set("account_id", string(accountID))
for _, id := range ids {
@ -30,7 +28,7 @@ func (c *Client) Report(accountID ID, ids []ID, comment string) (*Report, error)
}
params.Set("comment", comment)
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 {
return nil, err
}

147
status.go
View File

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