Skip to content

Commit

Permalink
Add stronger typing to the return value of receive_sync_changes in th…
Browse files Browse the repository at this point in the history
…e bindings (#2297)
  • Loading branch information
poljar authored Jul 19, 2023
1 parent 9f69c32 commit 24e3ccf
Showing 1 changed file with 58 additions and 8 deletions.
66 changes: 58 additions & 8 deletions bindings/matrix-sdk-crypto-ffi/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,51 @@ use crate::{
Sas, SignatureUploadRequest, StartSasResult, UserIdentity, Verification, VerificationRequest,
};

/// The return value for the [`OlmMachine::receive_sync_changes()`] method.
///
/// Will contain various information about the `/sync` changes the
/// [`OlmMachine`] processed.
#[derive(uniffi::Record)]
pub struct SyncChangesResult {
/// The, now possibly decrypted, to-device events the [`OlmMachine`]
/// received, decrypted, and processed.
to_device_events: Vec<String>,

/// Information about the room keys that were extracted out of the to-device
/// events.
room_key_infos: Vec<RoomKeyInfo>,
}

/// Information on a room key that has been received or imported.
#[derive(uniffi::Record)]
pub struct RoomKeyInfo {
/// The [messaging algorithm] that this key is used for. Will be one of the
/// `m.megolm.*` algorithms.
///
/// [messaging algorithm]: https://spec.matrix.org/v1.6/client-server-api/#messaging-algorithms
pub algorithm: String,

/// The room where the key is used.
pub room_id: String,

/// The Curve25519 key of the device which initiated the session originally.
pub sender_key: String,

/// The ID of the session that the key is for.
pub session_id: String,
}

impl From<matrix_sdk_crypto::store::RoomKeyInfo> for RoomKeyInfo {
fn from(value: matrix_sdk_crypto::store::RoomKeyInfo) -> Self {
Self {
algorithm: value.algorithm.to_string(),
room_id: value.room_id.to_string(),
sender_key: value.sender_key.to_base64(),
session_id: value.session_id,
}
}
}

/// A high level state machine that handles E2EE for Matrix.
#[derive(uniffi::Object)]
pub struct OlmMachine {
Expand Down Expand Up @@ -474,7 +519,7 @@ impl OlmMachine {
device_changes: DeviceLists,
key_counts: HashMap<String, i32>,
unused_fallback_keys: Option<Vec<String>>,
) -> Result<String, CryptoStoreError> {
) -> Result<SyncChangesResult, CryptoStoreError> {
let to_device: ToDevice = serde_json::from_str(&events)?;
let device_changes: RumaDeviceLists = device_changes.into();
let key_counts: BTreeMap<DeviceKeyAlgorithm, UInt> = key_counts
Expand All @@ -492,14 +537,19 @@ impl OlmMachine {
let unused_fallback_keys: Option<Vec<DeviceKeyAlgorithm>> =
unused_fallback_keys.map(|u| u.into_iter().map(DeviceKeyAlgorithm::from).collect());

let events = self.runtime.block_on(self.inner.receive_sync_changes(
to_device.events,
&device_changes,
&key_counts,
unused_fallback_keys.as_deref(),
))?;
let (to_device_events, room_key_infos) =
self.runtime.block_on(self.inner.receive_sync_changes(
to_device.events,
&device_changes,
&key_counts,
unused_fallback_keys.as_deref(),
))?;

let to_device_events =
to_device_events.into_iter().map(|event| event.json().get().to_owned()).collect();
let room_key_infos = room_key_infos.into_iter().map(|info| info.into()).collect();

Ok(serde_json::to_string(&events)?)
Ok(SyncChangesResult { to_device_events, room_key_infos })
}

/// Add the given list of users to be tracked, triggering a key query
Expand Down

0 comments on commit 24e3ccf

Please sign in to comment.