mastodon_api\methods/lists.rs
1use crate::MastodonClient;
2use crate::error::Result;
3use crate::models::List;
4
5/// Handler for list-related API endpoints.
6pub struct ListsHandler<'a> {
7 client: &'a MastodonClient,
8}
9
10impl<'a> ListsHandler<'a> {
11 /// Creates a new `ListsHandler` for the given client.
12 ///
13 /// Parameters:
14 /// - `client`: The client to use for making requests.
15 ///
16 /// Returns:
17 /// - `ListsHandler`: The created lists handler.
18 pub fn new(client: &'a MastodonClient) -> Self {
19 Self { client }
20 }
21
22 /// Fetches all lists for the authenticated user.
23 ///
24 /// Returns:
25 /// - `Result<Vec<List>>`: The fetched lists.
26 ///
27 /// Corresponds to `GET /api/v1/lists`.
28 pub async fn list(&self) -> Result<Vec<List>> {
29 let url = format!("{}/api/v1/lists", self.client.base_url());
30 let req = self.client.http_client().get(&url);
31 self.client.send(req).await
32 }
33
34 /// Fetches a specific list by its ID.
35 ///
36 /// Parameters:
37 /// - `id`: The ID of the list to fetch.
38 ///
39 /// Returns:
40 /// - `Result<List>`: The fetched list.
41 ///
42 /// Corresponds to `GET /api/v1/lists/:id`.
43 pub async fn get(&self, id: &str) -> Result<List> {
44 let url = format!("{}/api/v1/lists/{}", self.client.base_url(), id);
45 let req = self.client.http_client().get(&url);
46 self.client.send(req).await
47 }
48
49 /// Creates a new list with the given title.
50 ///
51 /// Parameters:
52 /// - `title`: The title of the list to create.
53 ///
54 /// Returns:
55 /// - `Result<List>`: The created list.
56 ///
57 /// Corresponds to `POST /api/v1/lists`.
58 pub async fn create(&self, title: &str) -> Result<List> {
59 let url = format!("{}/api/v1/lists", self.client.base_url());
60 let req = self
61 .client
62 .http_client()
63 .post(&url)
64 .json(&serde_json::json!({ "title": title }));
65 self.client.send(req).await
66 }
67
68 /// Deletes a list by its ID.
69 ///
70 /// Parameters:
71 /// - `id`: The ID of the list to delete.
72 ///
73 /// Returns:
74 /// - `Result<()>`: The deleted list.
75 ///
76 /// Corresponds to `DELETE /api/v1/lists/:id`.
77 pub async fn delete(&self, id: &str) -> Result<()> {
78 let url = format!("{}/api/v1/lists/{}", self.client.base_url(), id);
79 let req = self.client.http_client().delete(&url);
80 let _: serde_json::Value = self.client.send(req).await?;
81 Ok(())
82 }
83
84 /// Adds accounts to a list.
85 ///
86 /// Parameters:
87 /// - `list_id`: The ID of the list to add accounts to.
88 /// - `account_ids`: The IDs of the accounts to add to the list.
89 ///
90 /// Returns:
91 /// - `Result<()>`: The added accounts.
92 ///
93 /// Corresponds to `POST /api/v1/lists/:id/accounts`.
94 pub async fn add_accounts(&self, list_id: &str, account_ids: &[String]) -> Result<()> {
95 let url = format!(
96 "{}/api/v1/lists/{}/accounts",
97 self.client.base_url(),
98 list_id
99 );
100 let req = self
101 .client
102 .http_client()
103 .post(&url)
104 .json(&serde_json::json!({ "account_ids": account_ids }));
105 let _: serde_json::Value = self.client.send(req).await?;
106 Ok(())
107 }
108
109 /// Removes accounts from a list.
110 ///
111 /// Parameters:
112 /// - `list_id`: The ID of the list to remove accounts from.
113 /// - `account_ids`: The IDs of the accounts to remove from the list.
114 ///
115 /// Returns:
116 /// - `Result<()>`: The removed accounts.
117 ///
118 /// Corresponds to `DELETE /api/v1/lists/:id/accounts`.
119 pub async fn remove_accounts(&self, list_id: &str, account_ids: &[String]) -> Result<()> {
120 let url = format!(
121 "{}/api/v1/lists/{}/accounts",
122 self.client.base_url(),
123 list_id
124 );
125 let req = self
126 .client
127 .http_client()
128 .delete(&url)
129 .json(&serde_json::json!({ "account_ids": account_ids }));
130 let _: serde_json::Value = self.client.send(req).await?;
131 Ok(())
132 }
133}