From 6c7dbb814bcec79b9b3292ac2743adf355d41c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Sat, 5 Sep 2020 18:04:15 +0200 Subject: [PATCH] matrix-sdk: Add a convenience method to get our own devices. --- matrix_sdk/src/client.rs | 42 ++++++++++++++++++++++++++++ matrix_sdk_test/src/test_json/mod.rs | 24 ++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index eb7c0ff9..d5a09661 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -41,6 +41,7 @@ use matrix_sdk_base::{CryptoStoreError, OutgoingRequests, ToDeviceRequest}; use matrix_sdk_common::{ api::r0::{ account::register, + device::get_devices, directory::{get_public_rooms, get_public_rooms_filtered}, membership::{ ban_user, forget_room, @@ -1111,6 +1112,35 @@ impl Client { self.send(request).await } + /// Get information of all our own devices. + /// + /// # Examples + /// + /// ```no_run + /// # use matrix_sdk::{Client, SyncSettings}; + /// # use futures::executor::block_on; + /// # use url::Url; + /// # use std::convert::TryFrom; + /// # block_on(async { + /// # let homeserver = Url::parse("http://localhost:8080").unwrap(); + /// # let mut client = Client::new(homeserver).unwrap(); + /// let response = client.devices().await.expect("Can't get devices from server"); + /// + /// for device in response.devices { + /// println!( + /// "Device: {} {}", + /// device.device_id, + /// device.display_name.as_deref().unwrap_or("") + /// ); + /// } + /// # }); + /// ``` + pub async fn devices(&self) -> Result { + let request = get_devices::Request {}; + + self.send(request).await + } + /// Synchronize the client's state with the latest state on the server. /// /// If a `StateStore` is provided and this is the initial sync state will @@ -1548,6 +1578,18 @@ mod test { assert!(logged_in, "Client should be logged in"); } + #[tokio::test] + async fn devices() { + let client = logged_in_client().await; + + let _m = mock("GET", "/_matrix/client/r0/devices") + .with_status(200) + .with_body(test_json::DEVICES.to_string()) + .create(); + + assert!(client.devices().await.is_ok()); + } + #[tokio::test] async fn test_join_leave_room() { let homeserver = Url::from_str(&mockito::server_url()).unwrap(); diff --git a/matrix_sdk_test/src/test_json/mod.rs b/matrix_sdk_test/src/test_json/mod.rs index b85e0bad..d3e72df5 100644 --- a/matrix_sdk_test/src/test_json/mod.rs +++ b/matrix_sdk_test/src/test_json/mod.rs @@ -4,6 +4,9 @@ //! When running `cargo publish` no external folders are allowed so all the //! test data needs to be contained within this crate. +use lazy_static::lazy_static; +use serde_json::{json, Value as JsonValue}; + pub mod events; pub mod sync; @@ -14,3 +17,24 @@ pub use events::{ ROOM_ID, ROOM_MESSAGES, TYPING, }; pub use sync::{DEFAULT_SYNC_SUMMARY, INVITE_SYNC, LEAVE_SYNC, LEAVE_SYNC_EVENT, MORE_SYNC, SYNC}; + +lazy_static! { + pub static ref DEVICES: JsonValue = json!({ + "devices": [ + { + "device_id": "BNYQQWUMXO", + "display_name": "Client 1", + "last_seen_ip": "-", + "last_seen_ts": 1596117733037u64, + "user_id": "@example:localhost" + }, + { + "device_id": "LEBKSEUSNR", + "display_name": "Client 2", + "last_seen_ip": "-", + "last_seen_ts": 1599057006985u64, + "user_id": "@example:localhost" + } + ] + }); +}