Add list API support.

pull/94/head
Ben Lubar 2019-04-26 20:27:43 -05:00 committed by mattn
parent e804ee7eb2
commit 6f05c48bf6
2 changed files with 116 additions and 9 deletions

View File

@ -82,7 +82,7 @@ func main() {
* [x] GET /api/v1/accounts/:id/unblock * [x] GET /api/v1/accounts/:id/unblock
* [x] GET /api/v1/accounts/:id/mute * [x] GET /api/v1/accounts/:id/mute
* [x] GET /api/v1/accounts/:id/unmute * [x] GET /api/v1/accounts/:id/unmute
* [ ] GET /api/v1/accounts/:id/lists * [x] GET /api/v1/accounts/:id/lists
* [x] GET /api/v1/accounts/relationships * [x] GET /api/v1/accounts/relationships
* [x] GET /api/v1/accounts/search * [x] GET /api/v1/accounts/search
* [x] POST /api/v1/apps * [x] POST /api/v1/apps
@ -95,14 +95,14 @@ func main() {
* [x] GET /api/v1/instance * [x] GET /api/v1/instance
* [x] GET /api/v1/instance/activity * [x] GET /api/v1/instance/activity
* [x] GET /api/v1/instance/peers * [x] GET /api/v1/instance/peers
* [ ] GET /api/v1/lists * [x] GET /api/v1/lists
* [ ] GET /api/v1/lists/:id/accounts * [x] GET /api/v1/lists/:id/accounts
* [ ] GET /api/v1/lists/:id * [x] GET /api/v1/lists/:id
* [ ] POST /api/v1/lists * [x] POST /api/v1/lists
* [ ] PUT /api/v1/lists/:id * [x] PUT /api/v1/lists/:id
* [ ] DELETE /api/v1/lists/:id * [x] DELETE /api/v1/lists/:id
* [ ] POST /api/v1/lists/:id/accounts * [x] POST /api/v1/lists/:id/accounts
* [ ] DELETE /api/v1/lists/:id/accounts * [x] DELETE /api/v1/lists/:id/accounts
* [x] POST /api/v1/media * [x] POST /api/v1/media
* [x] GET /api/v1/mutes * [x] GET /api/v1/mutes
* [x] GET /api/v1/notifications * [x] GET /api/v1/notifications

107
lists.go 100644
View File

@ -0,0 +1,107 @@
package mastodon
import (
"context"
"fmt"
"net/http"
"net/url"
)
// List is metadata for a list of users.
type List struct {
ID ID `json:"id"`
Title string `json:"title"`
}
// GetLists returns all the lists on the current account.
func (c *Client) GetLists(ctx context.Context) ([]*List, error) {
var lists []*List
err := c.doAPI(ctx, http.MethodGet, "/api/v1/lists", nil, &lists, nil)
if err != nil {
return nil, err
}
return lists, nil
}
// GetAccountLists returns the lists containing a given account.
func (c *Client) GetAccountLists(ctx context.Context, id ID) ([]*List, error) {
var lists []*List
err := c.doAPI(ctx, 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(ctx context.Context, id ID) ([]*Account, error) {
var accounts []*Account
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(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(ctx context.Context, id ID) (*List, error) {
var list List
err := c.doAPI(ctx, 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(ctx context.Context, title string) (*List, error) {
params := url.Values{}
params.Set("title", title)
var list List
err := c.doAPI(ctx, 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(ctx context.Context, id ID, title string) (*List, error) {
params := url.Values{}
params.Set("title", title)
var list List
err := c.doAPI(ctx, 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 (c *Client) DeleteList(ctx context.Context, id ID) error {
return c.doAPI(ctx, 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(ctx context.Context, list ID, accounts ...ID) error {
params := url.Values{}
for _, acct := range accounts {
params.Add("account_ids", string(acct))
}
return c.doAPI(ctx, 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(ctx context.Context, list ID, accounts ...ID) error {
params := url.Values{}
for _, acct := range accounts {
params.Add("account_ids", string(acct))
}
return c.doAPI(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(list))), params, nil, nil)
}