From 7ee04300546d93e6961ec6cf7ba5c5dc3fa7d43f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 12 Aug 2020 17:17:22 +0200 Subject: [PATCH] base: Add methods to fetch user devices. --- matrix_sdk_base/Cargo.toml | 1 + matrix_sdk_base/src/client.rs | 77 ++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/matrix_sdk_base/Cargo.toml b/matrix_sdk_base/Cargo.toml index b1e919d4..b4985c42 100644 --- a/matrix_sdk_base/Cargo.toml +++ b/matrix_sdk_base/Cargo.toml @@ -36,6 +36,7 @@ default-features = false features = ["sync", "fs"] [dev-dependencies] +futures = "0.3.5" matrix-sdk-test = { version = "0.1.0", path = "../matrix_sdk_test" } http = "0.2.1" tracing-subscriber = "0.2.11" diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index dda97428..7d7c9a3d 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -52,7 +52,9 @@ use matrix_sdk_common::{ identifiers::{DeviceId, DeviceKeyAlgorithm}, }; #[cfg(feature = "encryption")] -use matrix_sdk_crypto::{CryptoStore, Device, OlmError, OlmMachine, Sas}; +use matrix_sdk_crypto::{ + CryptoStore, CryptoStoreError, Device, DeviceStore, OlmError, OlmMachine, Sas, UserDevices, +}; use zeroize::Zeroizing; #[cfg(not(target_arch = "wasm32"))] @@ -1881,6 +1883,79 @@ impl BaseClient { .as_ref() .map(|o| o.start_verification(device)) } + + /// 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_base::BaseClient; + /// # use matrix_sdk_common::identifiers::UserId; + /// # use futures::executor::block_on; + /// # let alice = UserId::try_from("@alice:example.org").unwrap(); + /// # let client = BaseClient::new().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 { + let olm = self.olm.lock().await; + olm.as_ref()?.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_base::BaseClient; + /// # use matrix_sdk_common::identifiers::UserId; + /// # use futures::executor::block_on; + /// # let alice = UserId::try_from("@alice:example.org").unwrap(); + /// # let client = BaseClient::new().unwrap(); + /// # block_on(async { + /// let devices = client.get_user_devices(&alice).await.unwrap(); + /// + /// for device in devices.devices() { + /// println!("{:?}", device); + /// } + /// # }); + /// ``` + pub async fn get_user_devices( + &self, + user_id: &UserId, + ) -> StdResult { + let olm = self.olm.lock().await; + if let Some(olm) = olm.as_ref() { + Ok(olm.get_user_devices(user_id).await?) + } else { + Ok(DeviceStore::new().user_devices(user_id)) + } + } } #[cfg(test)]