mastodon_api\methods/accounts.rs
1use crate::MastodonClient;
2use crate::error::Result;
3use crate::models::Account;
4
5/// Handler for account-related API endpoints.
6pub struct AccountsHandler<'a> {
7 client: &'a MastodonClient,
8}
9
10impl<'a> AccountsHandler<'a> {
11 /// Creates a new `AccountsHandler` for the given client.
12 ///
13 /// Parameters:
14 /// - `client`: The client to use for making requests.
15 ///
16 /// Returns:
17 /// - `AccountsHandler`: The created accounts handler.
18 pub fn new(client: &'a MastodonClient) -> Self {
19 Self { client }
20 }
21
22 /// Verifies the authenticated user's credentials and returns their account info.
23 ///
24 /// Returns:
25 /// - `Result<Account>`: The verified account.
26 ///
27 /// Corresponds to `GET /api/v1/accounts/verify_credentials`.
28 pub async fn verify_credentials(&self) -> Result<Account> {
29 let url = format!(
30 "{}/api/v1/accounts/verify_credentials",
31 self.client.base_url()
32 );
33 let req = self.client.http_client().get(&url);
34 self.client.send(req).await
35 }
36
37 /// Fetches an account by its ID.
38 ///
39 /// Parameters:
40 /// - `id`: The ID of the account to fetch.
41 ///
42 /// Returns:
43 /// - `Result<Account>`: The fetched account.
44 ///
45 /// Corresponds to `GET /api/v1/accounts/:id`.
46 pub async fn get(&self, id: &str) -> Result<Account> {
47 let url = format!("{}/api/v1/accounts/{}", self.client.base_url(), id);
48 let req = self.client.http_client().get(&url);
49 self.client.send(req).await
50 }
51
52 /// Follows the given account.
53 ///
54 /// Parameters:
55 /// - `id`: The ID of the account to follow.
56 ///
57 /// Returns:
58 /// - `Result<crate::models::Relationship>`: The followed relationship.
59 ///
60 /// Corresponds to `POST /api/v1/accounts/:id/follow`.
61 pub async fn follow(&self, id: &str) -> Result<crate::models::Relationship> {
62 let url = format!("{}/api/v1/accounts/{}/follow", self.client.base_url(), id);
63 let req = self.client.http_client().post(&url);
64 self.client.send(req).await
65 }
66
67 /// Unfollows the given account.
68 ///
69 /// Parameters:
70 /// - `id`: The ID of the account to unfollow.
71 ///
72 /// Returns:
73 /// - `Result<crate::models::Relationship>`: The unfollowed relationship.
74 ///
75 /// Corresponds to `POST /api/v1/accounts/:id/unfollow`.
76 pub async fn unfollow(&self, id: &str) -> Result<crate::models::Relationship> {
77 let url = format!("{}/api/v1/accounts/{}/unfollow", self.client.base_url(), id);
78 let req = self.client.http_client().post(&url);
79 self.client.send(req).await
80 }
81
82 /// Blocks the given account.
83 ///
84 /// Parameters:
85 /// - `id`: The ID of the account to block.
86 ///
87 /// Returns:
88 /// - `Result<crate::models::Relationship>`: The blocked relationship.
89 ///
90 /// Corresponds to `POST /api/v1/accounts/:id/block`.
91 pub async fn block(&self, id: &str) -> Result<crate::models::Relationship> {
92 let url = format!("{}/api/v1/accounts/{}/block", self.client.base_url(), id);
93 let req = self.client.http_client().post(&url);
94 self.client.send(req).await
95 }
96
97 /// Mutes the given account.
98 ///
99 /// Parameters:
100 /// - `id`: The ID of the account to mute.
101 ///
102 /// Returns:
103 /// - `Result<crate::models::Relationship>`: The muted relationship.
104 ///
105 /// Corresponds to `POST /api/v1/accounts/:id/mute`.
106 pub async fn mute(&self, id: &str) -> Result<crate::models::Relationship> {
107 let url = format!("{}/api/v1/accounts/{}/mute", self.client.base_url(), id);
108 let req = self.client.http_client().post(&url);
109 self.client.send(req).await
110 }
111
112 /// Pins the given account to the authenticated user's profile.
113 ///
114 /// Parameters:
115 /// - `id`: The ID of the account to pin.
116 ///
117 /// Returns:
118 /// - `Result<crate::models::Relationship>`: The updated relationship.
119 ///
120 /// Corresponds to `POST /api/v1/accounts/:id/pin`.
121 pub async fn pin(&self, id: &str) -> Result<crate::models::Relationship> {
122 let url = format!("{}/api/v1/accounts/{}/pin", self.client.base_url(), id);
123 let req = self.client.http_client().post(&url);
124 self.client.send(req).await
125 }
126
127 /// Unpins the given account from the profile.
128 ///
129 /// Parameters:
130 /// - `id`: The ID of the account to unpin.
131 ///
132 /// Returns:
133 /// - `Result<crate::models::Relationship>`: The updated relationship.
134 ///
135 /// Corresponds to `POST /api/v1/accounts/:id/unpin`.
136 pub async fn unpin(&self, id: &str) -> Result<crate::models::Relationship> {
137 let url = format!("{}/api/v1/accounts/{}/unpin", self.client.base_url(), id);
138 let req = self.client.http_client().post(&url);
139 self.client.send(req).await
140 }
141
142 /// Searches for accounts matching the given query.
143 ///
144 /// Parameters:
145 /// - `query`: The query to search for.
146 /// - `limit`: The maximum number of results to return.
147 ///
148 /// Returns:
149 /// - `Result<Vec<Account>>`: The fetched accounts.
150 ///
151 /// Corresponds to `GET /api/v1/accounts/search`.
152 pub async fn search(&self, query: &str, limit: Option<u32>) -> Result<Vec<Account>> {
153 let url = format!("{}/api/v1/accounts/search", self.client.base_url());
154 let mut req = self.client.http_client().get(&url).query(&[("q", query)]);
155 if let Some(l) = limit {
156 req = req.query(&[("limit", l.to_string())]);
157 }
158 self.client.send(req).await
159 }
160
161 /// Fetches the relationship between the authenticated user and the given accounts.
162 ///
163 /// Parameters:
164 /// - `ids`: A list of account IDs to check relationships for.
165 ///
166 /// Returns:
167 /// - `Result<Vec<crate::models::Relationship>>`: The relationships.
168 ///
169 /// Corresponds to `GET /api/v1/accounts/relationships`.
170 pub async fn relationships(&self, ids: &[String]) -> Result<Vec<crate::models::Relationship>> {
171 let url = format!("{}/api/v1/accounts/relationships", self.client.base_url());
172 let mut req = self.client.http_client().get(&url);
173 for id in ids {
174 req = req.query(&[("id[]", id)]);
175 }
176 self.client.send(req).await
177 }
178}