matrix_sdk/
pusher.rs

1// Copyright 2024 The Matrix.org Foundation C.I.C.
2// Copyright 2024 Hanadi Tamimi
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//     http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! High-level pusher API.
17
18use ruma::api::client::push::{set_pusher, PusherIds};
19
20use crate::{Client, Result};
21
22/// A high-level API to interact with the pusher API.
23///
24/// All the methods in this struct send a request to the homeserver.
25#[derive(Debug, Clone)]
26pub struct Pusher {
27    /// The underlying HTTP client.
28    client: Client,
29}
30
31impl Pusher {
32    pub(crate) fn new(client: Client) -> Self {
33        Self { client }
34    }
35
36    /// Sets a given pusher
37    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    /// Deletes a pusher by its ids
44    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// The http mocking library is not supported for wasm32
52#[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        // prepare dummy pusher
81        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        // prepare pusher ids
102        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}