From 2283c28503a50ca4c23168a8a9f4eef3a414c959 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 1 Oct 2024 15:40:18 +0200 Subject: [PATCH] base: tidy up sliding sync code around e2ee --- crates/matrix-sdk-base/src/client.rs | 18 ++++---------- .../matrix-sdk-base/src/sliding_sync/mod.rs | 24 +++++++++---------- crates/matrix-sdk/src/sliding_sync/client.rs | 16 +++++++++---- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/crates/matrix-sdk-base/src/client.rs b/crates/matrix-sdk-base/src/client.rs index 8be5a1f1c..648d83cf4 100644 --- a/crates/matrix-sdk-base/src/client.rs +++ b/crates/matrix-sdk-base/src/client.rs @@ -789,17 +789,8 @@ impl BaseClient { pub(crate) async fn preprocess_to_device_events( &self, encryption_sync_changes: EncryptionSyncChanges<'_>, - #[cfg(feature = "experimental-sliding-sync")] changes: &mut StateChanges, - #[cfg(not(feature = "experimental-sliding-sync"))] _changes: &mut StateChanges, - #[cfg(feature = "experimental-sliding-sync")] room_info_notable_updates: &mut BTreeMap< - OwnedRoomId, - RoomInfoNotableUpdateReasons, - >, - #[cfg(not(feature = "experimental-sliding-sync"))] - _room_info_notable_updates: &mut BTreeMap< - OwnedRoomId, - RoomInfoNotableUpdateReasons, - >, + changes: &mut StateChanges, + room_info_notable_updates: &mut BTreeMap, ) -> Result>> { if let Some(o) = self.olm_machine().await.as_ref() { // Let the crypto machine handle the sync response, this @@ -815,8 +806,9 @@ impl BaseClient { self.decrypt_latest_events(&room, changes, room_info_notable_updates).await; } } - #[cfg(not(feature = "experimental-sliding-sync"))] - drop(room_key_updates); // Silence unused variable warning + + #[cfg(not(feature = "experimental-sliding-sync"))] // Silence unused variable warnings. + let _ = (room_key_updates, changes, room_info_notable_updates); Ok(events) } else { diff --git a/crates/matrix-sdk-base/src/sliding_sync/mod.rs b/crates/matrix-sdk-base/src/sliding_sync/mod.rs index 5aa0993b0..a5b774bb1 100644 --- a/crates/matrix-sdk-base/src/sliding_sync/mod.rs +++ b/crates/matrix-sdk-base/src/sliding_sync/mod.rs @@ -23,6 +23,8 @@ use std::{borrow::Cow, collections::BTreeMap}; #[cfg(feature = "e2e-encryption")] use matrix_sdk_common::deserialized_responses::SyncTimelineEvent; #[cfg(feature = "e2e-encryption")] +use ruma::api::client::sync::sync_events::v5; +#[cfg(feature = "e2e-encryption")] use ruma::events::AnyToDeviceEvent; use ruma::{ api::client::sync::sync_events::v3::{self, InvitedRoom}, @@ -59,16 +61,17 @@ impl BaseClient { /// In addition to writes to the crypto store, this may also write into the /// state store, in particular it may write latest-events to the state /// store. + /// + /// Returns whether any change happened. pub async fn process_sliding_sync_e2ee( &self, - extensions: &http::response::Extensions, - ) -> Result>> { - if extensions.is_empty() { - return Ok(Default::default()); + to_device: Option<&v5::response::ToDevice>, + e2ee: &v5::response::E2EE, + ) -> Result>>> { + if to_device.is_none() && e2ee.is_empty() { + return Ok(None); } - let http::response::Extensions { to_device, e2ee, .. } = extensions; - let to_device_events = to_device.as_ref().map(|to_device| to_device.events.clone()).unwrap_or_default(); @@ -87,9 +90,6 @@ impl BaseClient { // Process the to-device events and other related e2ee data. This returns a list // of all the to-device events that were passed in but encrypted ones // were replaced with their decrypted version. - // Passing in the default empty maps and vecs for this is completely fine, since - // the `OlmMachine` assumes empty maps/vecs mean no change in the one-time key - // counts. let to_device = self .preprocess_to_device_events( matrix_sdk_crypto::EncryptionSyncChanges { @@ -106,12 +106,12 @@ impl BaseClient { ) .await?; - trace!("ready to submit changes to store"); + trace!("ready to submit e2ee changes to store"); self.store.save_changes(&changes).await?; self.apply_changes(&changes, room_info_notable_updates); - trace!("applied changes"); + trace!("applied e2ee changes"); - Ok(to_device) + Ok(Some(to_device)) } /// Process a response from a sliding sync call. diff --git a/crates/matrix-sdk/src/sliding_sync/client.rs b/crates/matrix-sdk/src/sliding_sync/client.rs index 5310ccc02..eee133066 100644 --- a/crates/matrix-sdk/src/sliding_sync/client.rs +++ b/crates/matrix-sdk/src/sliding_sync/client.rs @@ -289,11 +289,19 @@ impl<'a> SlidingSyncResponseProcessor<'a> { // `handle_room_response` before this function), so panic is fine. assert!(self.response.is_none()); - self.to_device_events = - self.client.base_client().process_sliding_sync_e2ee(extensions).await?; + self.to_device_events = if let Some(to_device_events) = self + .client + .base_client() + .process_sliding_sync_e2ee(extensions.to_device.as_ref(), &extensions.e2ee) + .await? + { + // Some new keys might have been received, so trigger a backup if needed. + self.client.encryption().backups().maybe_trigger_backup(); - // Some new keys might have been received, so trigger a backup if needed. - self.client.encryption().backups().maybe_trigger_backup(); + to_device_events + } else { + Vec::new() + }; Ok(()) }