base: tidy up sliding sync code around e2ee

This commit is contained in:
Benjamin Bouvier
2024-10-01 15:40:18 +02:00
parent 59d3608c32
commit 2283c28503
3 changed files with 29 additions and 29 deletions

View File

@@ -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<OwnedRoomId, RoomInfoNotableUpdateReasons>,
) -> Result<Vec<Raw<ruma::events::AnyToDeviceEvent>>> {
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 {

View File

@@ -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<Vec<Raw<AnyToDeviceEvent>>> {
if extensions.is_empty() {
return Ok(Default::default());
to_device: Option<&v5::response::ToDevice>,
e2ee: &v5::response::E2EE,
) -> Result<Option<Vec<Raw<AnyToDeviceEvent>>>> {
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.

View File

@@ -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(())
}