mastodon_api\methods/notifications.rs
1use crate::MastodonClient;
2use crate::error::Result;
3use crate::models::Notification;
4
5/// Handler for notification-related API endpoints.
6pub struct NotificationsHandler<'a> {
7 client: &'a MastodonClient,
8}
9
10impl<'a> NotificationsHandler<'a> {
11 // Creates a new `NotificationsHandler` for the given client.
12 //
13 // Parameters:
14 // - `client`: The client to use for making requests.
15 //
16 // Returns:
17 // - `NotificationsHandler`: The created notifications handler.
18 pub fn new(client: &'a MastodonClient) -> Self {
19 Self { client }
20 }
21
22 /// Fetches all notifications for the authenticated user.
23 ///
24 /// Returns:
25 /// - `Result<Vec<Notification>>`: The fetched notifications.
26 ///
27 /// Corresponds to `GET /api/v1/notifications`.
28 pub async fn list(&self) -> Result<Vec<Notification>> {
29 let url = format!("{}/api/v1/notifications", 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 notification by its ID.
35 ///
36 /// Parameters:
37 /// - `id`: The ID of the notification to fetch.
38 ///
39 /// Returns:
40 /// - `Result<Notification>`: The fetched notification.
41 ///
42 /// Corresponds to `GET /api/v1/notifications/:id`.
43 pub async fn get(&self, id: &str) -> Result<Notification> {
44 let url = format!("{}/api/v1/notifications/{}", self.client.base_url(), id);
45 let req = self.client.http_client().get(&url);
46 self.client.send(req).await
47 }
48
49 /// Clears all notifications for the authenticated user.
50 ///
51 /// Returns:
52 /// - `Result<()>`: The cleared notifications.
53 ///
54 /// Corresponds to `POST /api/v1/notifications/clear`.
55 pub async fn clear(&self) -> Result<()> {
56 let url = format!("{}/api/v1/notifications/clear", self.client.base_url());
57 let req = self.client.http_client().post(&url);
58 let _: serde_json::Value = self.client.send(req).await?;
59 Ok(())
60 }
61
62 /// Dismisses a single notification by its ID.
63 ///
64 /// Parameters:
65 /// - `id`: The ID of the notification to dismiss.
66 ///
67 /// Returns:
68 /// - `Result<()>`: The dismissed notification.
69 ///
70 /// Corresponds to `POST /api/v1/notifications/:id/dismiss`.
71 pub async fn dismiss(&self, id: &str) -> Result<()> {
72 let url = format!(
73 "{}/api/v1/notifications/{}/dismiss",
74 self.client.base_url(),
75 id
76 );
77 let req = self.client.http_client().post(&url);
78 let _: serde_json::Value = self.client.send(req).await?;
79 Ok(())
80 }
81}