/ internal / gtsclient / lists.go
lists.go
  1  package gtsclient
  2  
  3  import (
  4  	"bytes"
  5  	"encoding/json"
  6  	"fmt"
  7  	"net/http"
  8  
  9  	"codeflow.dananglin.me.uk/apollo/enbas/internal/model"
 10  )
 11  
 12  const (
 13  	baseListPath string = "/api/v1/lists"
 14  )
 15  
 16  func (g *GTSClient) GetAllLists(_ NoRPCArgs, lists *[]model.List) error {
 17  	params := requestParameters{
 18  		httpMethod:  http.MethodGet,
 19  		url:         g.authentication.Instance + baseListPath,
 20  		requestBody: nil,
 21  		contentType: "",
 22  		output:      lists,
 23  	}
 24  
 25  	if err := g.sendRequest(params); err != nil {
 26  		return fmt.Errorf(
 27  			"received an error after sending the request to get the list of lists: %w",
 28  			err,
 29  		)
 30  	}
 31  
 32  	return nil
 33  }
 34  
 35  func (g *GTSClient) GetList(listID string, list *model.List) error {
 36  	params := requestParameters{
 37  		httpMethod:  http.MethodGet,
 38  		url:         g.authentication.Instance + baseListPath + "/" + listID,
 39  		requestBody: nil,
 40  		contentType: "",
 41  		output:      list,
 42  	}
 43  
 44  	if err := g.sendRequest(params); err != nil {
 45  		return fmt.Errorf(
 46  			"received an error after sending the request to get the list: %w",
 47  			err,
 48  		)
 49  	}
 50  
 51  	return nil
 52  }
 53  
 54  type CreateListArgs struct {
 55  	Title         string
 56  	RepliesPolicy model.ListRepliesPolicy
 57  	Exclusive     bool
 58  }
 59  
 60  func (g *GTSClient) CreateList(args CreateListArgs, list *model.List) error {
 61  	form := struct {
 62  		Title         string                  `json:"title"`
 63  		RepliesPolicy model.ListRepliesPolicy `json:"replies_policy"`
 64  		Exclusive     bool                    `json:"exclusive"`
 65  	}{
 66  		Title:         args.Title,
 67  		RepliesPolicy: args.RepliesPolicy,
 68  		Exclusive:     args.Exclusive,
 69  	}
 70  
 71  	data, err := json.Marshal(form)
 72  	if err != nil {
 73  		return fmt.Errorf("unable to marshal the form: %w", err)
 74  	}
 75  
 76  	requestBody := bytes.NewBuffer(data)
 77  
 78  	params := requestParameters{
 79  		httpMethod:  http.MethodPost,
 80  		url:         g.authentication.Instance + baseListPath,
 81  		requestBody: requestBody,
 82  		contentType: applicationJSON,
 83  		output:      list,
 84  	}
 85  
 86  	if err := g.sendRequest(params); err != nil {
 87  		return fmt.Errorf(
 88  			"received an error after sending the request to create the list: %w",
 89  			err,
 90  		)
 91  	}
 92  
 93  	return nil
 94  }
 95  
 96  func (g *GTSClient) UpdateList(listToUpdate model.List, updatedList *model.List) error {
 97  	form := struct {
 98  		Title         string                  `json:"title"`
 99  		RepliesPolicy model.ListRepliesPolicy `json:"replies_policy"`
100  	}{
101  		Title:         listToUpdate.Title,
102  		RepliesPolicy: listToUpdate.RepliesPolicy,
103  	}
104  
105  	data, err := json.Marshal(form)
106  	if err != nil {
107  		return fmt.Errorf("unable to marshal the form: %w", err)
108  	}
109  
110  	requestBody := bytes.NewBuffer(data)
111  
112  	params := requestParameters{
113  		httpMethod:  http.MethodPut,
114  		url:         g.authentication.Instance + baseListPath + "/" + listToUpdate.ID,
115  		requestBody: requestBody,
116  		contentType: applicationJSON,
117  		output:      updatedList,
118  	}
119  
120  	if err := g.sendRequest(params); err != nil {
121  		return fmt.Errorf(
122  			"received an error after sending the request to update the list: %w",
123  			err,
124  		)
125  	}
126  
127  	return nil
128  }
129  
130  func (g *GTSClient) DeleteList(listID string, _ *NoRPCResults) error {
131  	params := requestParameters{
132  		httpMethod:  http.MethodDelete,
133  		url:         g.authentication.Instance + baseListPath + "/" + listID,
134  		requestBody: nil,
135  		contentType: "",
136  		output:      nil,
137  	}
138  
139  	if err := g.sendRequest(params); err != nil {
140  		return fmt.Errorf(
141  			"received an error after sending the request to delete the list: %w",
142  			err,
143  		)
144  	}
145  
146  	return nil
147  }
148  
149  type AddAccountsToListArgs struct {
150  	ListID     string
151  	AccountIDs []string
152  }
153  
154  func (g *GTSClient) AddAccountsToList(args AddAccountsToListArgs, _ *NoRPCResults) error {
155  	form := struct {
156  		AccountIDs []string `json:"account_ids"`
157  	}{
158  		AccountIDs: args.AccountIDs,
159  	}
160  
161  	data, err := json.Marshal(form)
162  	if err != nil {
163  		return fmt.Errorf("unable to marshal the form: %w", err)
164  	}
165  
166  	requestBody := bytes.NewBuffer(data)
167  
168  	params := requestParameters{
169  		httpMethod:  http.MethodPost,
170  		url:         g.authentication.Instance + baseListPath + "/" + args.ListID + "/accounts",
171  		requestBody: requestBody,
172  		contentType: applicationJSON,
173  		output:      nil,
174  	}
175  
176  	if err := g.sendRequest(params); err != nil {
177  		return fmt.Errorf(
178  			"received an error after sending the request to add the accounts to the list: %w",
179  			err,
180  		)
181  	}
182  
183  	return nil
184  }
185  
186  type RemoveAccountsFromListArgs struct {
187  	ListID     string
188  	AccountIDs []string
189  }
190  
191  func (g *GTSClient) RemoveAccountsFromList(args RemoveAccountsFromListArgs, _ *NoRPCResults) error {
192  	form := struct {
193  		AccountIDs []string `json:"account_ids"`
194  	}{
195  		AccountIDs: args.AccountIDs,
196  	}
197  
198  	data, err := json.Marshal(form)
199  	if err != nil {
200  		return fmt.Errorf("unable to marshal the form: %w", err)
201  	}
202  
203  	requestBody := bytes.NewBuffer(data)
204  
205  	params := requestParameters{
206  		httpMethod:  http.MethodDelete,
207  		url:         g.authentication.Instance + baseListPath + "/" + args.ListID + "/accounts",
208  		requestBody: requestBody,
209  		contentType: applicationJSON,
210  		output:      nil,
211  	}
212  
213  	if err := g.sendRequest(params); err != nil {
214  		return fmt.Errorf(
215  			"received an error after sending the request to remove the accounts from the list: %w",
216  			err,
217  		)
218  	}
219  
220  	return nil
221  }
222  
223  type GetAccountsFromListArgs struct {
224  	ListID string
225  	Limit  int
226  }
227  
228  func (g *GTSClient) GetAccountsFromList(args GetAccountsFromListArgs, list *model.AccountList) error {
229  	path := fmt.Sprintf("%s/%s/accounts?limit=%d", baseListPath, args.ListID, args.Limit)
230  
231  	var accounts []model.Account
232  
233  	params := requestParameters{
234  		httpMethod:  http.MethodGet,
235  		url:         g.authentication.Instance + path,
236  		requestBody: nil,
237  		contentType: "",
238  		output:      &accounts,
239  	}
240  
241  	if err := g.sendRequest(params); err != nil {
242  		return fmt.Errorf(
243  			"received an error after sending the request to get the accounts from the list: %w",
244  			err,
245  		)
246  	}
247  
248  	*list = model.AccountList{
249  		Label:           "Accounts",
250  		Accounts:        accounts,
251  		BlockedAccounts: false,
252  	}
253  
254  	return nil
255  }