lists.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package mastodon
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. )
  8. // List is metadata for a list of users.
  9. type List struct {
  10. ID string `json:"id"`
  11. Title string `json:"title"`
  12. }
  13. // GetLists returns all the lists on the current account.
  14. func (c *Client) GetLists(ctx context.Context) ([]*List, error) {
  15. var lists []*List
  16. err := c.doAPI(ctx, http.MethodGet, "/api/v1/lists", nil, &lists, nil)
  17. if err != nil {
  18. return nil, err
  19. }
  20. return lists, nil
  21. }
  22. // GetAccountLists returns the lists containing a given account.
  23. func (c *Client) GetAccountLists(ctx context.Context, id string) ([]*List, error) {
  24. var lists []*List
  25. err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/accounts/%s/lists", url.PathEscape(string(id))), nil, &lists, nil)
  26. if err != nil {
  27. return nil, err
  28. }
  29. return lists, nil
  30. }
  31. // GetListAccounts returns the accounts in a given list.
  32. func (c *Client) GetListAccounts(ctx context.Context, id string) ([]*Account, error) {
  33. var accounts []*Account
  34. err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(id))), url.Values{"limit": {"0"}}, &accounts, nil)
  35. if err != nil {
  36. return nil, err
  37. }
  38. return accounts, nil
  39. }
  40. // GetList retrieves a list by string.
  41. func (c *Client) GetList(ctx context.Context, id string) (*List, error) {
  42. var list List
  43. err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/lists/%s", url.PathEscape(string(id))), nil, &list, nil)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return &list, nil
  48. }
  49. // CreateList creates a new list with a given title.
  50. func (c *Client) CreateList(ctx context.Context, title string) (*List, error) {
  51. params := url.Values{}
  52. params.Set("title", title)
  53. var list List
  54. err := c.doAPI(ctx, http.MethodPost, "/api/v1/lists", params, &list, nil)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return &list, nil
  59. }
  60. // RenameList assigns a new title to a list.
  61. func (c *Client) RenameList(ctx context.Context, id string, title string) (*List, error) {
  62. params := url.Values{}
  63. params.Set("title", title)
  64. var list List
  65. err := c.doAPI(ctx, http.MethodPut, fmt.Sprintf("/api/v1/lists/%s", url.PathEscape(string(id))), params, &list, nil)
  66. if err != nil {
  67. return nil, err
  68. }
  69. return &list, nil
  70. }
  71. // DeleteList removes a list.
  72. func (c *Client) DeleteList(ctx context.Context, id string) error {
  73. return c.doAPI(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/lists/%s", url.PathEscape(string(id))), nil, nil, nil)
  74. }
  75. // AddToList adds accounts to a list.
  76. //
  77. // Only accounts already followed by the user can be added to a list.
  78. func (c *Client) AddToList(ctx context.Context, list string, accounts ...string) error {
  79. params := url.Values{}
  80. for _, acct := range accounts {
  81. params.Add("account_ids", string(acct))
  82. }
  83. return c.doAPI(ctx, http.MethodPost, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(list))), params, nil, nil)
  84. }
  85. // RemoveFromList removes accounts from a list.
  86. func (c *Client) RemoveFromList(ctx context.Context, list string, accounts ...string) error {
  87. params := url.Values{}
  88. for _, acct := range accounts {
  89. params.Add("account_ids", string(acct))
  90. }
  91. return c.doAPI(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/lists/%s/accounts", url.PathEscape(string(list))), params, nil, nil)
  92. }