mastodon_api\methods/
push.rs

1use crate::MastodonClient;
2use crate::error::Result;
3use crate::models::push::{WebPushAlerts, WebPushSubscription};
4
5/// Handler for Web Push API endpoints.
6pub struct PushHandler<'a> {
7    client: &'a MastodonClient,
8}
9
10impl<'a> PushHandler<'a> {
11    /// Creates a new `PushHandler` for the given client.
12    pub fn new(client: &'a MastodonClient) -> Self {
13        Self { client }
14    }
15
16    /// Fetches the current Web Push subscription.
17    ///
18    /// Returns:
19    /// - `Result<WebPushSubscription>`: The subscription.
20    ///
21    /// Corresponds to `GET /api/v1/push/subscription`.
22    pub async fn get_subscription(&self) -> Result<WebPushSubscription> {
23        let url = format!("{}/api/v1/push/subscription", self.client.base_url());
24        let req = self.client.http_client().get(&url);
25        self.client.send(req).await
26    }
27
28    /// Creates or updates a Web Push subscription.
29    ///
30    /// Parameters:
31    /// - `endpoint`: The endpoint of the Web Push subscription.
32    /// - `p256dh`: The p256dh public key.
33    /// - `auth`: The authentication secret.
34    /// - `alerts`: The alerts to enable.
35    ///
36    /// Returns:
37    /// - `Result<WebPushSubscription>`: The created/updated subscription.
38    ///
39    /// Corresponds to `POST /api/v1/push/subscription`.
40    pub async fn subscribe(
41        &self,
42        endpoint: &str,
43        p256dh: &str,
44        auth: &str,
45        alerts: Option<WebPushAlerts>,
46    ) -> Result<WebPushSubscription> {
47        let url = format!("{}/api/v1/push/subscription", self.client.base_url());
48        let mut form = vec![
49            ("subscription[endpoint]", endpoint.to_string()),
50            ("subscription[keys][p256dh]", p256dh.to_string()),
51            ("subscription[keys][auth]", auth.to_string()),
52        ];
53
54        if let Some(a) = alerts {
55            if a.follow {
56                form.push(("data[alerts][follow]", "true".to_string()));
57            }
58            if a.favourite {
59                form.push(("data[alerts][favourite]", "true".to_string()));
60            }
61            if a.reblog {
62                form.push(("data[alerts][reblog]", "true".to_string()));
63            }
64            if a.mention {
65                form.push(("data[alerts][mention]", "true".to_string()));
66            }
67            if a.poll {
68                form.push(("data[alerts][poll]", "true".to_string()));
69            }
70            if a.status {
71                form.push(("data[alerts][status]", "true".to_string()));
72            }
73        }
74
75        let req = self.client.http_client().post(&url).form(&form);
76        self.client.send(req).await
77    }
78
79    /// Updates existing Web Push alerts.
80    ///
81    /// Parameters:
82    /// - `alerts`: The updated alerts.
83    ///
84    /// Returns:
85    /// - `Result<WebPushSubscription>`: The updated subscription.
86    ///
87    /// Corresponds to `PUT /api/v1/push/subscription`.
88    pub async fn update_alerts(&self, alerts: WebPushAlerts) -> Result<WebPushSubscription> {
89        let url = format!("{}/api/v1/push/subscription", self.client.base_url());
90        let mut form = vec![];
91        if alerts.follow {
92            form.push(("data[alerts][follow]", "true".to_string()));
93        }
94        if alerts.favourite {
95            form.push(("data[alerts][favourite]", "true".to_string()));
96        }
97        if alerts.reblog {
98            form.push(("data[alerts][reblog]", "true".to_string()));
99        }
100        if alerts.mention {
101            form.push(("data[alerts][mention]", "true".to_string()));
102        }
103        if alerts.poll {
104            form.push(("data[alerts][poll]", "true".to_string()));
105        }
106        if alerts.status {
107            form.push(("data[alerts][status]", "true".to_string()));
108        }
109
110        let req = self.client.http_client().put(&url).form(&form);
111        self.client.send(req).await
112    }
113
114    /// Deletes the current Web Push subscription.
115    ///
116    /// Returns:
117    /// - `Result<()>`: Success if the subscription was deleted.
118    ///
119    /// Corresponds to `DELETE /api/v1/push/subscription`.
120    pub async fn unsubscribe(&self) -> Result<()> {
121        let url = format!("{}/api/v1/push/subscription", self.client.base_url());
122        let req = self.client.http_client().delete(&url);
123        self.client.send(req).await
124    }
125}