rust-sdk: Change the sync receiving API to make it a bit more type safe

This commit is contained in:
Damir Jelić 2021-02-10 16:31:34 +01:00
parent a557c05890
commit 0b9be11d85
3 changed files with 74 additions and 22 deletions

View File

@ -9,7 +9,6 @@ crate-type = ["cdylib", "lib"]
name = "matrix_crypto" name = "matrix_crypto"
[dependencies] [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/"} matrix-sdk-common = { git = "https://github.com/matrix-org/matrix-rust-sdk/"}
futures = { version = "0.3.12", default_features = false, features = ["executor"] } futures = { version = "0.3.12", default_features = false, features = ["executor"] }
tokio = { version = "1.1.1", default_features = false, features = ["rt-multi-thread", "time"] } 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" http = "0.2.3"
uniffi = "0.7.0" 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] [build-dependencies]
uniffi_build = "0.7.0" uniffi_build = "0.7.0"

View File

@ -1,17 +1,18 @@
use std::{collections::HashMap, convert::TryFrom, time::Duration}; use std::{
collections::{BTreeMap, HashMap},
use futures::{ convert::TryFrom,
executor::block_on, time::Duration,
future::{abortable, AbortHandle, Aborted},
Future,
}; };
use http::Response;
use futures::executor::block_on;
use tokio::{runtime::Runtime, time::sleep}; use tokio::{runtime::Runtime, time::sleep};
use matrix_sdk_common::{ use matrix_sdk_common::{
api::r0::sync::sync_events::Response as SyncResponse, api::r0::sync::sync_events::{
api::r0::to_device::send_event_to_device::METADATA, DeviceLists as RumaDeviceLists, ToDevice,
identifiers::{Error as RumaIdentifierError, UserId}, },
identifiers::{DeviceKeyAlgorithm, Error as RumaIdentifierError, UserId},
UInt,
}; };
use matrix_sdk_crypto::{ use matrix_sdk_crypto::{
store::CryptoStoreError as InnerStoreError, OlmMachine as InnerMachine, ToDeviceRequest, store::CryptoStoreError as InnerStoreError, OlmMachine as InnerMachine, ToDeviceRequest,
@ -22,6 +23,28 @@ pub struct OlmMachine {
runtime: Runtime, runtime: Runtime,
} }
pub struct DeviceLists {
pub changed: Vec<String>,
pub left: Vec<String>,
}
impl Into<RumaDeviceLists> 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)] #[derive(Debug, thiserror::Error)]
pub enum MachineCreationError { pub enum MachineCreationError {
#[error(transparent)] #[error(transparent)]
@ -71,12 +94,12 @@ impl From<ToDeviceRequest> for Request {
} }
} }
fn response_from_string(body: &str) -> Response<Vec<u8>> { // fn response_from_string(body: &str) -> Response<Vec<u8>> {
Response::builder() // Response::builder()
.status(200) // .status(200)
.body(body.as_bytes().to_vec()) // .body(body.as_bytes().to_vec())
.expect("Can't create HTTP response") // .expect("Can't create HTTP response")
} // }
impl OlmMachine { impl OlmMachine {
pub fn new(user_id: &str, device_id: &str, path: &str) -> Result<Self, MachineCreationError> { pub fn new(user_id: &str, device_id: &str, path: &str) -> Result<Self, MachineCreationError> {
@ -167,11 +190,24 @@ impl OlmMachine {
}) })
} }
pub fn receive_sync_response(&self, response: &str) { pub fn receive_sync_changes(
let response = response_from_string(response); &self,
let mut response = SyncResponse::try_from(response).expect("Can't parse response"); events: &str,
device_changes: DeviceLists,
key_counts: HashMap<String, u32>,
) {
let events: ToDevice = serde_json::from_str(events).unwrap();
let device_changes: RumaDeviceLists = device_changes.into();
let key_counts: BTreeMap<DeviceKeyAlgorithm, UInt> = 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();
} }
} }

View File

@ -11,6 +11,11 @@ enum CryptoStoreError {
"CryptoStore", "CryptoStore",
}; };
dictionary DeviceLists {
sequence<string> changed;
sequence<string> left;
};
dictionary Device { dictionary Device {
string user_id; string user_id;
string device_id; string device_id;
@ -40,7 +45,9 @@ interface OlmMachine {
[Throws=MachineCreationError] [Throws=MachineCreationError]
constructor([ByRef] string user_id, [ByRef] string device_id, [ByRef] string path); 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<DOMString, u32> key_counts);
record<DOMString, string> identity_keys(); record<DOMString, string> identity_keys();