From 3e2f531caa83f07b67af91933fa73771f6d714f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 28 Jul 2023 12:40:55 +0200 Subject: [PATCH] Split out the receive_sync_changes method into a helper This splits the method out so the bigger chunks doesn't persist the changes in the store. This will be useful if we need to hijack the changes and persist them in a different store. --- crates/matrix-sdk-crypto/src/machine.rs | 30 ++++++++++++++++--------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/crates/matrix-sdk-crypto/src/machine.rs b/crates/matrix-sdk-crypto/src/machine.rs index 9f359ebeb..760c5ea5a 100644 --- a/crates/matrix-sdk-crypto/src/machine.rs +++ b/crates/matrix-sdk-crypto/src/machine.rs @@ -1126,6 +1126,22 @@ impl OlmMachine { &self, sync_changes: EncryptionSyncChanges<'_>, ) -> OlmResult<(Vec>, Vec)> { + let (events, changes) = self.preprocess_sync_changes(sync_changes).await?; + + // Technically save_changes also does the same work, so if it's slow we could + // refactor this to do it only once. + let room_key_updates: Vec<_> = + changes.inbound_group_sessions.iter().map(RoomKeyInfo::from).collect(); + + self.store().save_changes(changes).await?; + + Ok((events, room_key_updates)) + } + + pub(crate) async fn preprocess_sync_changes( + &self, + sync_changes: EncryptionSyncChanges<'_>, + ) -> OlmResult<(Vec>, Changes)> { // Remove verification objects that have expired or are done. let mut events = self.inner.verification_machine.garbage_collect(); @@ -1154,21 +1170,13 @@ impl OlmMachine { events.push(raw_event); } - // Technically save_changes also does the same work, so if it's slow we could - // refactor this to do it only once. - let room_key_updates: Vec<_> = - changes.inbound_group_sessions.iter().map(RoomKeyInfo::from).collect(); - let changed_sessions = self.inner.key_request_machine.collect_incoming_key_requests().await?; changes.sessions.extend(changed_sessions); - changes.next_batch_token = sync_changes.next_batch_token; - self.store().save_changes(changes).await?; - - Ok((events, room_key_updates)) + Ok((events, changes)) } /// Request a room key from our devices. @@ -1419,8 +1427,8 @@ impl OlmMachine { /// of tracked users /// /// Any users that hadn't been seen before will be flagged for a key query - /// immediately, and whenever `receive_sync_changes` receives a - /// "changed" notification for that user in the future. + /// immediately, and whenever [`OlmMachine::receive_sync_changes()`] + /// receives a "changed" notification for that user in the future. /// /// Users that were already in the list are unaffected. pub async fn update_tracked_users(