mastodon_api\methods/
push.rs1use crate::MastodonClient;
2use crate::error::Result;
3use crate::models::push::{WebPushAlerts, WebPushSubscription};
4
5pub struct PushHandler<'a> {
7 client: &'a MastodonClient,
8}
9
10impl<'a> PushHandler<'a> {
11 pub fn new(client: &'a MastodonClient) -> Self {
13 Self { client }
14 }
15
16 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 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 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 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}