1use ruma::api::client::push::{set_pusher, PusherIds};
19
20use crate::{Client, Result};
21
22#[derive(Debug, Clone)]
26pub struct Pusher {
27 client: Client,
29}
30
31impl Pusher {
32 pub(crate) fn new(client: Client) -> Self {
33 Self { client }
34 }
35
36 pub async fn set(&self, pusher: ruma::api::client::push::Pusher) -> Result<()> {
38 let request = set_pusher::v3::Request::post(pusher);
39 self.client.send(request).await?;
40 Ok(())
41 }
42
43 pub async fn delete(&self, pusher_ids: PusherIds) -> Result<()> {
45 let request = set_pusher::v3::Request::delete(pusher_ids);
46 self.client.send(request).await?;
47 Ok(())
48 }
49}
50
51#[cfg(all(test, not(target_arch = "wasm32")))]
53mod tests {
54 use matrix_sdk_test::{async_test, test_json};
55 use ruma::{
56 api::client::push::{PusherIds, PusherInit, PusherKind},
57 push::HttpPusherData,
58 };
59 use wiremock::{
60 matchers::{method, path},
61 Mock, MockServer, ResponseTemplate,
62 };
63
64 use crate::test_utils::logged_in_client;
65
66 async fn mock_api(server: MockServer) {
67 Mock::given(method("POST"))
68 .and(path("_matrix/client/r0/pushers/set"))
69 .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EMPTY))
70 .mount(&server)
71 .await;
72 }
73
74 #[async_test]
75 async fn test_set_pusher() {
76 let server = MockServer::start().await;
77 let client = logged_in_client(Some(server.uri())).await;
78 mock_api(server).await;
79
80 let pusher = PusherInit {
82 ids: PusherIds::new("pushKey".to_owned(), "app_id".to_owned()),
83 app_display_name: "name".to_owned(),
84 kind: PusherKind::Http(HttpPusherData::new("dummy".to_owned())),
85 lang: "EN".to_owned(),
86 device_display_name: "name".to_owned(),
87 profile_tag: None,
88 };
89
90 let response = client.pusher().set(pusher.into()).await;
91
92 assert!(response.is_ok());
93 }
94
95 #[async_test]
96 async fn test_delete_pusher() {
97 let server = MockServer::start().await;
98 let client = logged_in_client(Some(server.uri())).await;
99 mock_api(server).await;
100
101 let pusher_ids = PusherIds::new("pushKey".to_owned(), "app_id".to_owned());
103
104 let response = client.pusher().delete(pusher_ids).await;
105
106 assert!(response.is_ok());
107 }
108}