mastodon_api\methods/statuses.rs
1use crate::MastodonClient;
2use crate::error::Result;
3use crate::methods::builders::StatusBuilder;
4use crate::models::Status;
5use serde::Serialize;
6
7/// Handler for status-related API endpoints.
8pub struct StatusesHandler<'a> {
9 client: &'a MastodonClient,
10}
11
12/// Parameters for creating a new status.
13#[derive(Serialize)]
14pub struct CreateStatusParams {
15 /// The text content of the status.
16 pub status: String,
17 /// ID of the status being replied to, if any.
18 pub in_reply_to_id: Option<String>,
19 /// Whether the status should be marked as sensitive.
20 pub sensitive: bool,
21 /// Text to be shown as a warning before the status content.
22 pub spoiler_text: Option<String>,
23 /// Visibility of the status ("public", "unlisted", "private", "direct").
24 pub visibility: Option<String>,
25 /// ISO 639 language code for the status.
26 pub language: Option<String>,
27}
28
29impl<'a> StatusesHandler<'a> {
30 pub fn new(client: &'a MastodonClient) -> Self {
31 Self { client }
32 }
33
34 /// Returns a builder for creating a new status.
35 pub fn builder(&self, text: &str) -> StatusBuilder<'a> {
36 StatusBuilder::new(self.client, text)
37 }
38
39 /// Fetches a specific status by its ID.
40 ///
41 /// Parameters:
42 /// - `id`: The ID of the status to fetch.
43 ///
44 /// Returns:
45 /// - `Result<Status>`: The fetched status.
46 ///
47 /// Corresponds to `GET /api/v1/statuses/:id`.
48 pub async fn get(&self, id: &str) -> Result<Status> {
49 let url = format!("{}/api/v1/statuses/{}", self.client.base_url(), id);
50 let req = self.client.http_client().get(&url);
51 self.client.send(req).await
52 }
53
54 /// Creates a new status.
55 ///
56 /// Parameters:
57 /// - `params`: The parameters for the status to create.
58 ///
59 /// Returns:
60 /// - `Result<Status>`: The created status.
61 ///
62 /// Corresponds to `POST /api/v1/statuses`.
63 pub async fn create(&self, params: &CreateStatusParams) -> Result<Status> {
64 let url = format!("{}/api/v1/statuses", self.client.base_url());
65 let req = self.client.http_client().post(&url).json(params);
66 self.client.send(req).await
67 }
68
69 /// Creates a new status with just text.
70 ///
71 /// Parameters:
72 /// - `text`: The text content of the status.
73 ///
74 /// Returns:
75 /// - `Result<Status>`: The created status.
76 ///
77 /// This is a convenience method for simple posts.
78 pub async fn create_simple(&self, text: &str) -> Result<Status> {
79 let params = CreateStatusParams {
80 status: text.to_string(),
81 in_reply_to_id: None,
82 sensitive: false,
83 spoiler_text: None,
84 visibility: None,
85 language: None,
86 };
87 self.create(¶ms).await
88 }
89
90 /// Deletes a status by its ID.
91 ///
92 /// Parameters:
93 /// - `id`: The ID of the status to delete.
94 ///
95 /// Returns:
96 /// - `Result<Status>`: The deleted status.
97 ///
98 /// Corresponds to `DELETE /api/v1/statuses/:id`.
99 pub async fn delete(&self, id: &str) -> Result<Status> {
100 let url = format!("{}/api/v1/statuses/{}", self.client.base_url(), id);
101 let req = self.client.http_client().delete(&url);
102 self.client.send(req).await
103 }
104
105 /// Reblogs (boosts) a status.
106 ///
107 /// Parameters:
108 /// - `id`: The ID of the status to reblog.
109 ///
110 /// Returns:
111 /// - `Result<Status>`: The reblogged status.
112 ///
113 /// Corresponds to `POST /api/v1/statuses/:id/reblog`.
114 pub async fn reblog(&self, id: &str) -> Result<Status> {
115 let url = format!("{}/api/v1/statuses/{}/reblog", self.client.base_url(), id);
116 let req = self.client.http_client().post(&url);
117 self.client.send(req).await
118 }
119
120 /// Removes a reblog of a status.
121 ///
122 /// Parameters:
123 /// - `id`: The ID of the status to unreblog.
124 ///
125 /// Returns:
126 /// - `Result<Status>`: The unreblogged status.
127 ///
128 /// Corresponds to `POST /api/v1/statuses/:id/unreblog`.
129 pub async fn unreblog(&self, id: &str) -> Result<Status> {
130 let url = format!("{}/api/v1/statuses/{}/unreblog", self.client.base_url(), id);
131 let req = self.client.http_client().post(&url);
132 self.client.send(req).await
133 }
134
135 /// Favourites a status.
136 ///
137 /// Parameters:
138 /// - `id`: The ID of the status to favourite.
139 ///
140 /// Returns:
141 /// - `Result<Status>`: The favourited status.
142 ///
143 /// Corresponds to `POST /api/v1/statuses/:id/favourite`.
144 pub async fn favourite(&self, id: &str) -> Result<Status> {
145 let url = format!(
146 "{}/api/v1/statuses/{}/favourite",
147 self.client.base_url(),
148 id
149 );
150 let req = self.client.http_client().post(&url);
151 self.client.send(req).await
152 }
153
154 /// Removes a status from favourites.
155 ///
156 /// Parameters:
157 /// - `id`: The ID of the status to unfavourite.
158 ///
159 /// Returns:
160 /// - `Result<Status>`: The unfavourited status.
161 ///
162 /// Corresponds to `POST /api/v1/statuses/:id/unfavourite`.
163 pub async fn unfavourite(&self, id: &str) -> Result<Status> {
164 let url = format!(
165 "{}/api/v1/statuses/{}/unfavourite",
166 self.client.base_url(),
167 id
168 );
169 let req = self.client.http_client().post(&url);
170 self.client.send(req).await
171 }
172
173 /// Bookmarks a status.
174 ///
175 /// Parameters:
176 /// - `id`: The ID of the status to bookmark.
177 ///
178 /// Returns:
179 /// - `Result<Status>`: The bookmarked status.
180 ///
181 /// Corresponds to `POST /api/v1/statuses/:id/bookmark`.
182 pub async fn bookmark(&self, id: &str) -> Result<Status> {
183 let url = format!("{}/api/v1/statuses/{}/bookmark", self.client.base_url(), id);
184 let req = self.client.http_client().post(&url);
185 self.client.send(req).await
186 }
187
188 /// Removes a status from bookmarks.
189 ///
190 /// Parameters:
191 /// - `id`: The ID of the status to bookmark.
192 ///
193 /// Returns:
194 /// - `Result<Status>`: The bookmarked status.
195 ///
196 /// Corresponds to `POST /api/v1/statuses/:id/unbookmark`.
197 pub async fn unbookmark(&self, id: &str) -> Result<Status> {
198 let url = format!(
199 "{}/api/v1/statuses/{}/unbookmark",
200 self.client.base_url(),
201 id
202 );
203 let req = self.client.http_client().post(&url);
204 self.client.send(req).await
205 }
206}