From 4e99278eacdf8b3cac18a8679ac7a81a144b85b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 13 Aug 2020 10:49:38 +0200 Subject: [PATCH] matrix-sdk: Expose the device methods in the Client. --- matrix_sdk/src/client.rs | 73 +++++++++++++++++++++++++++++++++++++- matrix_sdk_base/src/lib.rs | 2 +- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 5f73d959..f8a910ad 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -38,7 +38,7 @@ use tracing::{error, info, instrument}; use matrix_sdk_base::{BaseClient, BaseClientConfig, Room, Session, StateStore}; #[cfg(feature = "encryption")] -use matrix_sdk_base::Device; +use matrix_sdk_base::{CryptoStoreError, Device, UserDevices}; use matrix_sdk_common::{ api::r0::{ @@ -1482,6 +1482,77 @@ impl Client { homeserver: self.homeserver.clone(), }) } + + /// Get a specific device of a user. + /// + /// # Arguments + /// + /// * `user_id` - The unique id of the user that the device belongs to. + /// + /// * `device_id` - The unique id of the device. + /// + /// Returns a `Device` if one is found and the crypto store didn't throw an + /// error. + /// + /// This will always return None if the client hasn't been logged in. + /// + /// # Example + /// + /// ``` + /// # use std::convert::TryFrom; + /// # use matrix_sdk::{Client, identifiers::UserId}; + /// # use url::Url; + /// # use futures::executor::block_on; + /// # let alice = UserId::try_from("@alice:example.org").unwrap(); + /// # let homeserver = Url::parse("http://example.com").unwrap(); + /// # let client = Client::new(homeserver).unwrap(); + /// # block_on(async { + /// let device = client.get_device(&alice, "DEVICEID".into()).await; + /// + /// println!("{:?}", device); + /// # }); + /// ``` + #[cfg(feature = "encryption")] + #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))] + pub async fn get_device(&self, user_id: &UserId, device_id: &DeviceId) -> Option { + self.base_client.get_device(user_id, device_id).await + } + + /// Get a map holding all the devices of an user. + /// + /// This will always return an empty map if the client hasn't been logged + /// in. + /// + /// # Arguments + /// + /// * `user_id` - The unique id of the user that the devices belong to. + /// + /// # Example + /// + /// ``` + /// # use std::convert::TryFrom; + /// # use matrix_sdk::{Client, identifiers::UserId}; + /// # use url::Url; + /// # use futures::executor::block_on; + /// # let alice = UserId::try_from("@alice:example.org").unwrap(); + /// # let homeserver = Url::parse("http://example.com").unwrap(); + /// # let client = Client::new(homeserver).unwrap(); + /// # block_on(async { + /// let devices = client.get_user_devices(&alice).await.unwrap(); + /// + /// for device in devices.devices() { + /// println!("{:?}", device); + /// } + /// # }); + /// ``` + #[cfg(feature = "encryption")] + #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))] + pub async fn get_user_devices( + &self, + user_id: &UserId, + ) -> StdResult { + self.base_client.get_user_devices(user_id).await + } } #[cfg(test)] diff --git a/matrix_sdk_base/src/lib.rs b/matrix_sdk_base/src/lib.rs index 6521e1fe..ad979708 100644 --- a/matrix_sdk_base/src/lib.rs +++ b/matrix_sdk_base/src/lib.rs @@ -54,7 +54,7 @@ pub use models::Room; pub use state::{AllRooms, ClientState}; #[cfg(feature = "encryption")] -pub use matrix_sdk_crypto::{CryptoStoreError, Device, Sas, TrustState}; +pub use matrix_sdk_crypto::{CryptoStoreError, Device, Sas, TrustState, UserDevices}; #[cfg(feature = "messages")] #[cfg_attr(docsrs, doc(cfg(feature = "messages")))]