Skip to content

Commit d3c499d

Browse files
danderson-contstefanceriu
authored andcommitted
feat(sdk): expose append parameter on Pusher::set
Forwards the Matrix spec's append flag on POST /_matrix/client/v3/pushers/set through Pusher::set. When true, the homeserver keeps any existing pusher with the same app_id and pushkey registered for other users instead of replacing it. Needed for multi-profile clients on a single device. Breaking change: Pusher::set now takes an append: bool parameter. Signed-off-by: Daniel Anderson <daniel.anderson@toptal.com>
1 parent 4ca424f commit d3c499d

3 files changed

Lines changed: 40 additions & 15 deletions

File tree

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,7 @@ impl Client {
14681468
}
14691469

14701470
/// Registers a pusher with given parameters
1471+
#[allow(clippy::too_many_arguments)]
14711472
pub async fn set_pusher(
14721473
&self,
14731474
identifiers: PusherIdentifiers,
@@ -1487,7 +1488,7 @@ impl Client {
14871488
profile_tag,
14881489
lang,
14891490
};
1490-
self.inner.pusher().set(pusher_init.into()).await?;
1491+
self.inner.pusher().set(pusher_init.into(), false).await?;
14911492
Ok(())
14921493
}
14931494

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[**breaking**] `Pusher::set` now takes an `append: bool` parameter, forwarded to the homeserver on `POST /_matrix/client/v3/pushers/set`. Pass `true` to keep an existing pusher with the same `app_id` and `pushkey` registered for other users (e.g. multi-profile clients on a single device); pass `false` to preserve the previous default behaviour.

crates/matrix-sdk/src/pusher.rs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ impl Pusher {
3333
Self { client }
3434
}
3535

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);
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+
}
3942
self.client.send(request).await?;
4043
Ok(())
4144
}
@@ -56,9 +59,10 @@ mod tests {
5659
api::client::push::{PusherIds, PusherInit, PusherKind},
5760
push::HttpPusherData,
5861
};
62+
use serde_json::json;
5963
use wiremock::{
6064
Mock, MockServer, ResponseTemplate,
61-
matchers::{method, path},
65+
matchers::{body_partial_json, method, path},
6266
};
6367

6468
use crate::test_utils::logged_in_client;
@@ -71,23 +75,42 @@ mod tests {
7175
.await;
7276
}
7377

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 {
78+
fn dummy_pusher() -> PusherInit {
79+
PusherInit {
8280
ids: PusherIds::new("pushKey".to_owned(), "app_id".to_owned()),
8381
app_display_name: "name".to_owned(),
8482
kind: PusherKind::Http(HttpPusherData::new("dummy".to_owned())),
8583
lang: "EN".to_owned(),
8684
device_display_name: "name".to_owned(),
8785
profile_tag: None,
88-
};
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;
89112

90-
let response = client.pusher().set(pusher.into()).await;
113+
let response = client.pusher().set(dummy_pusher().into(), true).await;
91114

92115
assert!(response.is_ok());
93116
}

0 commit comments

Comments
 (0)