use ruma::api::client::push::{set_pusher, PusherIds};
use crate::{Client, Result};
#[derive(Debug, Clone)]
pub struct Pusher {
client: Client,
}
impl Pusher {
pub(crate) fn new(client: Client) -> Self {
Self { client }
}
pub async fn set(&self, pusher: ruma::api::client::push::Pusher) -> Result<()> {
let request = set_pusher::v3::Request::post(pusher);
self.client.send(request, None).await?;
Ok(())
}
pub async fn delete(&self, pusher_ids: PusherIds) -> Result<()> {
let request = set_pusher::v3::Request::delete(pusher_ids);
self.client.send(request, None).await?;
Ok(())
}
}
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
use matrix_sdk_test::{async_test, test_json};
use ruma::{
api::client::push::{PusherIds, PusherInit, PusherKind},
push::HttpPusherData,
};
use wiremock::{
matchers::{method, path},
Mock, MockServer, ResponseTemplate,
};
use crate::test_utils::logged_in_client;
async fn mock_api(server: MockServer) {
Mock::given(method("POST"))
.and(path("_matrix/client/r0/pushers/set"))
.respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EMPTY))
.mount(&server)
.await;
}
#[async_test]
async fn test_set_pusher() {
let server = MockServer::start().await;
let client = logged_in_client(Some(server.uri())).await;
mock_api(server).await;
let pusher = PusherInit {
ids: PusherIds::new("pushKey".to_owned(), "app_id".to_owned()),
app_display_name: "name".to_owned(),
kind: PusherKind::Http(HttpPusherData::new("dummy".to_owned())),
lang: "EN".to_owned(),
device_display_name: "name".to_owned(),
profile_tag: None,
};
let response = client.pusher().set(pusher.into()).await;
assert!(response.is_ok());
}
#[async_test]
async fn test_delete_pusher() {
let server = MockServer::start().await;
let client = logged_in_client(Some(server.uri())).await;
mock_api(server).await;
let pusher_ids = PusherIds::new("pushKey".to_owned(), "app_id".to_owned());
let response = client.pusher().delete(pusher_ids).await;
assert!(response.is_ok());
}
}