From 0b9be11d852511929ffc94c96198f5b49f2aa341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 10 Feb 2021 16:31:34 +0100 Subject: [PATCH] rust-sdk: Change the sync receiving API to make it a bit more type safe --- rust-sdk/Cargo.toml | 11 ++++++- rust-sdk/src/lib.rs | 76 ++++++++++++++++++++++++++++++++------------ rust-sdk/src/olm.udl | 9 +++++- 3 files changed, 74 insertions(+), 22 deletions(-) diff --git a/rust-sdk/Cargo.toml b/rust-sdk/Cargo.toml index e582648437..a7f7a5e663 100644 --- a/rust-sdk/Cargo.toml +++ b/rust-sdk/Cargo.toml @@ -9,7 +9,6 @@ crate-type = ["cdylib", "lib"] name = "matrix_crypto" [dependencies] -matrix-sdk-crypto = { git = "https://github.com/matrix-org/matrix-rust-sdk/", features = ["sled_cryptostore"] } matrix-sdk-common = { git = "https://github.com/matrix-org/matrix-rust-sdk/"} futures = { version = "0.3.12", default_features = false, features = ["executor"] } tokio = { version = "1.1.1", default_features = false, features = ["rt-multi-thread", "time"] } @@ -18,6 +17,16 @@ thiserror = "1.0.23" http = "0.2.3" uniffi = "0.7.0" +[dependencies.matrix-sdk-crypto] +git = "https://github.com/matrix-org/matrix-rust-sdk/" +features = ["sled_cryptostore"] + +[dependencies.ruma] +version = "0.0.2" +git = "https://github.com/ruma/ruma" +rev = "d6aa37c848b7f682a98c25b346899e284ffc6df7" +features = ["client-api", "compat", "unstable-pre-spec", "unstable-exhaustive-types"] + [build-dependencies] uniffi_build = "0.7.0" diff --git a/rust-sdk/src/lib.rs b/rust-sdk/src/lib.rs index cdc164f629..4033f4b659 100644 --- a/rust-sdk/src/lib.rs +++ b/rust-sdk/src/lib.rs @@ -1,17 +1,18 @@ -use std::{collections::HashMap, convert::TryFrom, time::Duration}; - -use futures::{ - executor::block_on, - future::{abortable, AbortHandle, Aborted}, - Future, +use std::{ + collections::{BTreeMap, HashMap}, + convert::TryFrom, + time::Duration, }; -use http::Response; + +use futures::executor::block_on; use tokio::{runtime::Runtime, time::sleep}; use matrix_sdk_common::{ - api::r0::sync::sync_events::Response as SyncResponse, - api::r0::to_device::send_event_to_device::METADATA, - identifiers::{Error as RumaIdentifierError, UserId}, + api::r0::sync::sync_events::{ + DeviceLists as RumaDeviceLists, ToDevice, + }, + identifiers::{DeviceKeyAlgorithm, Error as RumaIdentifierError, UserId}, + UInt, }; use matrix_sdk_crypto::{ store::CryptoStoreError as InnerStoreError, OlmMachine as InnerMachine, ToDeviceRequest, @@ -22,6 +23,28 @@ pub struct OlmMachine { runtime: Runtime, } +pub struct DeviceLists { + pub changed: Vec, + pub left: Vec, +} + +impl Into for DeviceLists { + fn into(self) -> RumaDeviceLists { + RumaDeviceLists { + changed: self + .changed + .into_iter() + .filter_map(|u| UserId::try_from(u).ok()) + .collect(), + left: self + .left + .into_iter() + .filter_map(|u| UserId::try_from(u).ok()) + .collect(), + } + } +} + #[derive(Debug, thiserror::Error)] pub enum MachineCreationError { #[error(transparent)] @@ -71,12 +94,12 @@ impl From for Request { } } -fn response_from_string(body: &str) -> Response> { - Response::builder() - .status(200) - .body(body.as_bytes().to_vec()) - .expect("Can't create HTTP response") -} +// fn response_from_string(body: &str) -> Response> { +// Response::builder() +// .status(200) +// .body(body.as_bytes().to_vec()) +// .expect("Can't create HTTP response") +// } impl OlmMachine { pub fn new(user_id: &str, device_id: &str, path: &str) -> Result { @@ -167,11 +190,24 @@ impl OlmMachine { }) } - pub fn receive_sync_response(&self, response: &str) { - let response = response_from_string(response); - let mut response = SyncResponse::try_from(response).expect("Can't parse response"); + pub fn receive_sync_changes( + &self, + events: &str, + device_changes: DeviceLists, + key_counts: HashMap, + ) { + let events: ToDevice = serde_json::from_str(events).unwrap(); + let device_changes: RumaDeviceLists = device_changes.into(); + let key_counts: BTreeMap = key_counts + .into_iter() + .map(|(k, v)| (DeviceKeyAlgorithm::from(k), v.into())) + .collect(); - block_on(self.inner.receive_sync_response(&mut response)).unwrap(); + block_on( + self.inner + .receive_sync_changes(&events, &device_changes, &key_counts), + ) + .unwrap(); } } diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index cb308e8fa3..685371f0ed 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -11,6 +11,11 @@ enum CryptoStoreError { "CryptoStore", }; +dictionary DeviceLists { + sequence changed; + sequence left; +}; + dictionary Device { string user_id; string device_id; @@ -40,7 +45,9 @@ interface OlmMachine { [Throws=MachineCreationError] constructor([ByRef] string user_id, [ByRef] string device_id, [ByRef] string path); - void receive_sync_response([ByRef] string response); + void receive_sync_changes([ByRef] string events, + DeviceLists device_changes, + record key_counts); record identity_keys();