atagen

matrix-rust-sdk

public
Star0
Watch0
ssh://brewery@pls.lobotomise.me:33/atagen/matrix-rust-sdk.git
crates/matrix-sdk/src/pusher.rsRust131 lines4.1 KB
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
18
use ruma::api::client::push::{PusherIds, set_pusher};
19
20
use 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)]
26
pub struct Pusher {
27
    /// The underlying HTTP client.
28
    client: Client,
29
}
30
31
impl 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, append: bool) -> Result<()> {
38
        let mut request = set_pusher::v3::Request::post(pusher);
39
        if let set_pusher::v3::PusherAction::Post(data) = &mut request.action {
40
            data.append = append;
41
        }
42
        self.client.send(request).await?;
43
        Ok(())
44
    }
45
46
    /// Deletes a pusher by its ids
47
    pub async fn delete(&self, pusher_ids: PusherIds) -> Result<()> {
48
        let request = set_pusher::v3::Request::delete(pusher_ids);
49
        self.client.send(request).await?;
50
        Ok(())
51
    }
52
}
53
54
// The http mocking library is not supported for wasm32
55
#[cfg(all(test, not(target_family = "wasm")))]
56
mod tests {
57
    use matrix_sdk_test::{async_test, test_json};
58
    use ruma::{
59
        api::client::push::{PusherIds, PusherInit, PusherKind},
60
        push::HttpPusherData,
61
    };
62
    use serde_json::json;
63
    use wiremock::{
64
        Mock, MockServer, ResponseTemplate,
65
        matchers::{body_partial_json, method, path},
66
    };
67
68
    use crate::test_utils::logged_in_client;
69
70
    async fn mock_api(server: MockServer) {
71
        Mock::given(method("POST"))
72
            .and(path("_matrix/client/r0/pushers/set"))
73
            .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EMPTY))
74
            .mount(&server)
75
            .await;
76
    }
77
78
    fn dummy_pusher() -> PusherInit {
79
        PusherInit {
80
            ids: PusherIds::new("pushKey".to_owned(), "app_id".to_owned()),
81
            app_display_name: "name".to_owned(),
82
            kind: PusherKind::Http(HttpPusherData::new("dummy".to_owned())),
83
            lang: "EN".to_owned(),
84
            device_display_name: "name".to_owned(),
85
            profile_tag: None,
86
        }
87
    }
88
89
    #[async_test]
90
    async fn test_set_pusher() {
91
        let server = MockServer::start().await;
92
        let client = logged_in_client(Some(server.uri())).await;
93
        mock_api(server).await;
94
95
        let response = client.pusher().set(dummy_pusher().into(), false).await;
96
97
        assert!(response.is_ok());
98
    }
99
100
    #[async_test]
101
    async fn test_set_pusher_forwards_append_flag() {
102
        let server = MockServer::start().await;
103
        let client = logged_in_client(Some(server.uri())).await;
104
105
        Mock::given(method("POST"))
106
            .and(path("_matrix/client/r0/pushers/set"))
107
            .and(body_partial_json(json!({ "append": true })))
108
            .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EMPTY))
109
            .expect(1)
110
            .mount(&server)
111
            .await;
112
113
        let response = client.pusher().set(dummy_pusher().into(), true).await;
114
115
        assert!(response.is_ok());
116
    }
117
118
    #[async_test]
119
    async fn test_delete_pusher() {
120
        let server = MockServer::start().await;
121
        let client = logged_in_client(Some(server.uri())).await;
122
        mock_api(server).await;
123
124
        // prepare pusher ids
125
        let pusher_ids = PusherIds::new("pushKey".to_owned(), "app_id".to_owned());
126
127
        let response = client.pusher().delete(pusher_ids).await;
128
129
        assert!(response.is_ok());
130
    }
131
}