From 8351858be7e3e58354288445583d3d45b533eb40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 12 Aug 2020 12:48:22 +0200 Subject: [PATCH] crypto: Expose a method to get a users device. --- matrix_sdk_crypto/Cargo.toml | 1 + matrix_sdk_crypto/src/machine.rs | 38 ++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/matrix_sdk_crypto/Cargo.toml b/matrix_sdk_crypto/Cargo.toml index 64f2d2f3..d532a09d 100644 --- a/matrix_sdk_crypto/Cargo.toml +++ b/matrix_sdk_crypto/Cargo.toml @@ -46,6 +46,7 @@ features = ["runtime-tokio", "sqlite"] [dev-dependencies] tokio = { version = "0.2.22", features = ["rt-threaded", "macros"] } +futures = "0.3.5" proptest = "0.10.0" serde_json = "1.0.57" tempfile = "3.1.0" diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index 7cad462b..7a5486bf 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -23,6 +23,8 @@ use std::{ }; use dashmap::DashMap; +use serde_json::Value; +use tracing::{debug, error, info, instrument, trace, warn}; use api::r0::{ keys::{claim_keys, get_keys, upload_keys, DeviceKeys, OneTimeKey}, @@ -44,8 +46,6 @@ use matrix_sdk_common::{ uuid::Uuid, Raw, }; -use serde_json::Value; -use tracing::{debug, error, info, instrument, trace, warn}; #[cfg(feature = "sqlite-cryptostore")] use super::store::sqlite::SqliteStore; @@ -1231,6 +1231,40 @@ impl OlmMachine { pub async fn users_for_key_query(&self) -> HashSet { self.store.users_for_key_query() } + + /// 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. + /// + /// # Example + /// + /// ``` + /// # use std::convert::TryFrom; + /// # use matrix_sdk_crypto::OlmMachine; + /// # use matrix_sdk_common::identifiers::UserId; + /// # use futures::executor::block_on; + /// # let alice = UserId::try_from("@alice:example.org").unwrap(); + /// # let machine = OlmMachine::new(&alice, "DEVICEID".into()); + /// # block_on(async { + /// let device = machine.get_device(&alice, "DEVICEID".into()).await; + /// + /// println!("{:?}", device); + /// # }); + /// ``` + pub async fn get_device(&self, user_id: &UserId, device_id: &DeviceId) -> Option { + self.store + .get_device(user_id, device_id) + .await + .ok() + .flatten() + } } #[cfg(test)]