Skip to content

Commit 24e3ccf

Browse files
authored
Add stronger typing to the return value of receive_sync_changes in the bindings (#2297)
1 parent 9f69c32 commit 24e3ccf

File tree

1 file changed

+58
-8
lines changed

1 file changed

+58
-8
lines changed

bindings/matrix-sdk-crypto-ffi/src/machine.rs

+58-8
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,51 @@ use crate::{
5858
Sas, SignatureUploadRequest, StartSasResult, UserIdentity, Verification, VerificationRequest,
5959
};
6060

61+
/// The return value for the [`OlmMachine::receive_sync_changes()`] method.
62+
///
63+
/// Will contain various information about the `/sync` changes the
64+
/// [`OlmMachine`] processed.
65+
#[derive(uniffi::Record)]
66+
pub struct SyncChangesResult {
67+
/// The, now possibly decrypted, to-device events the [`OlmMachine`]
68+
/// received, decrypted, and processed.
69+
to_device_events: Vec<String>,
70+
71+
/// Information about the room keys that were extracted out of the to-device
72+
/// events.
73+
room_key_infos: Vec<RoomKeyInfo>,
74+
}
75+
76+
/// Information on a room key that has been received or imported.
77+
#[derive(uniffi::Record)]
78+
pub struct RoomKeyInfo {
79+
/// The [messaging algorithm] that this key is used for. Will be one of the
80+
/// `m.megolm.*` algorithms.
81+
///
82+
/// [messaging algorithm]: https://spec.matrix.org/v1.6/client-server-api/#messaging-algorithms
83+
pub algorithm: String,
84+
85+
/// The room where the key is used.
86+
pub room_id: String,
87+
88+
/// The Curve25519 key of the device which initiated the session originally.
89+
pub sender_key: String,
90+
91+
/// The ID of the session that the key is for.
92+
pub session_id: String,
93+
}
94+
95+
impl From<matrix_sdk_crypto::store::RoomKeyInfo> for RoomKeyInfo {
96+
fn from(value: matrix_sdk_crypto::store::RoomKeyInfo) -> Self {
97+
Self {
98+
algorithm: value.algorithm.to_string(),
99+
room_id: value.room_id.to_string(),
100+
sender_key: value.sender_key.to_base64(),
101+
session_id: value.session_id,
102+
}
103+
}
104+
}
105+
61106
/// A high level state machine that handles E2EE for Matrix.
62107
#[derive(uniffi::Object)]
63108
pub struct OlmMachine {
@@ -474,7 +519,7 @@ impl OlmMachine {
474519
device_changes: DeviceLists,
475520
key_counts: HashMap<String, i32>,
476521
unused_fallback_keys: Option<Vec<String>>,
477-
) -> Result<String, CryptoStoreError> {
522+
) -> Result<SyncChangesResult, CryptoStoreError> {
478523
let to_device: ToDevice = serde_json::from_str(&events)?;
479524
let device_changes: RumaDeviceLists = device_changes.into();
480525
let key_counts: BTreeMap<DeviceKeyAlgorithm, UInt> = key_counts
@@ -492,14 +537,19 @@ impl OlmMachine {
492537
let unused_fallback_keys: Option<Vec<DeviceKeyAlgorithm>> =
493538
unused_fallback_keys.map(|u| u.into_iter().map(DeviceKeyAlgorithm::from).collect());
494539

495-
let events = self.runtime.block_on(self.inner.receive_sync_changes(
496-
to_device.events,
497-
&device_changes,
498-
&key_counts,
499-
unused_fallback_keys.as_deref(),
500-
))?;
540+
let (to_device_events, room_key_infos) =
541+
self.runtime.block_on(self.inner.receive_sync_changes(
542+
to_device.events,
543+
&device_changes,
544+
&key_counts,
545+
unused_fallback_keys.as_deref(),
546+
))?;
547+
548+
let to_device_events =
549+
to_device_events.into_iter().map(|event| event.json().get().to_owned()).collect();
550+
let room_key_infos = room_key_infos.into_iter().map(|info| info.into()).collect();
501551

502-
Ok(serde_json::to_string(&events)?)
552+
Ok(SyncChangesResult { to_device_events, room_key_infos })
503553
}
504554

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

0 commit comments

Comments
 (0)