aboutsummaryrefslogtreecommitdiff
path: root/mastodon/lists.go
blob: d323b7909f4289708ff0edb605e51779e414fd6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package mastodon

import (
	"context"
	"fmt"
	"net/http"
	"net/url"
)

// List is metadata for a list of users.
type List struct {
	ID    string `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 string) ([]*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 string) ([]*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 string.
func (c *Client) GetList(ctx context.Context, id string) (*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 string, 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 string) 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 string, accounts ...string) 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 string, accounts ...string) 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)
}