mastodon_api\methods/announcements.rs
1use crate::MastodonClient;
2use crate::error::Result;
3use crate::models::announcement::{Announcement, AnnouncementReaction};
4
5/// Handler for announcement-related API endpoints.
6pub struct AnnouncementsHandler<'a> {
7 client: &'a MastodonClient,
8}
9
10impl<'a> AnnouncementsHandler<'a> {
11 /// Creates a new `AnnouncementsHandler` for the given client.
12 pub fn new(client: &'a MastodonClient) -> Self {
13 Self { client }
14 }
15
16 /// Fetches all active announcements from the server.
17 ///
18 /// Returns:
19 /// - `Result<Vec<Announcement>>`: The announcements.
20 ///
21 /// Corresponds to `GET /api/v1/announcements`.
22 pub async fn list(&self) -> Result<Vec<Announcement>> {
23 let url = format!("{}/api/v1/announcements", self.client.base_url());
24 let req = self.client.http_client().get(&url);
25 self.client.send(req).await
26 }
27
28 /// Dismisses an announcement for the authenticated user.
29 ///
30 /// Parameters:
31 /// - `id`: The ID of the announcement to dismiss.
32 ///
33 /// Returns:
34 /// - `Result<()>`: Success if the announcement was dismissed.
35 ///
36 /// Corresponds to `POST /api/v1/announcements/:id/dismiss`.
37 pub async fn dismiss(&self, id: &str) -> Result<()> {
38 let url = format!(
39 "{}/api/v1/announcements/{}/dismiss",
40 self.client.base_url(),
41 id
42 );
43 let req = self.client.http_client().post(&url);
44 // Mastodon returns 200 OK with empty body for this endpoint.
45 // MastodonClient::send expects T: DeserializeOwned, so we might need a workaround for empty bodies
46 // if send is strictly requiring a JSON body.
47 // Let's check MastodonClient::send in src/lib.rs.
48 self.client.send(req).await
49 }
50
51 /// Adds a reaction to an announcement.
52 ///
53 /// Parameters:
54 /// - `id`: The ID of the announcement.
55 /// - `name`: The name of the reaction (emoji).
56 ///
57 /// Returns:
58 /// - `Result<AnnouncementReaction>`: The updated reaction.
59 ///
60 /// Corresponds to `POST /api/v1/announcements/:id/reactions/:name`.
61 pub async fn add_reaction(&self, id: &str, name: &str) -> Result<AnnouncementReaction> {
62 let url = format!(
63 "{}/api/v1/announcements/{}/reactions/{}",
64 self.client.base_url(),
65 id,
66 name
67 );
68 let req = self.client.http_client().post(&url);
69 self.client.send(req).await
70 }
71
72 /// Removes a reaction from an announcement.
73 ///
74 /// Parameters:
75 /// - `id`: The ID of the announcement.
76 /// - `name`: The name of the reaction (emoji).
77 ///
78 /// Returns:
79 /// - `Result<AnnouncementReaction>`: The updated reaction.
80 ///
81 /// Corresponds to `DELETE /api/v1/announcements/:id/reactions/:name`.
82 pub async fn remove_reaction(&self, id: &str, name: &str) -> Result<AnnouncementReaction> {
83 let url = format!(
84 "{}/api/v1/announcements/{}/reactions/{}",
85 self.client.base_url(),
86 id,
87 name
88 );
89 let req = self.client.http_client().delete(&url);
90 self.client.send(req).await
91 }
92}