From c6ed2d19633fd6f4235d2b3cb6b3c72eb2fcec46 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Wed, 4 Jun 2025 12:22:21 +0200 Subject: [PATCH] refactor(sdk): compute push actions before creating a decrypted `TimelineEvent` This reduces the number of callers to `set_push_actions()`, which should be minimally used. --- crates/matrix-sdk-ui/src/timeline/traits.rs | 14 ++++---------- crates/matrix-sdk/src/room/mod.rs | 14 ++++---------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/crates/matrix-sdk-ui/src/timeline/traits.rs b/crates/matrix-sdk-ui/src/timeline/traits.rs index f89322f92..4eea48e05 100644 --- a/crates/matrix-sdk-ui/src/timeline/traits.rs +++ b/crates/matrix-sdk-ui/src/timeline/traits.rs @@ -337,23 +337,17 @@ impl Decryptor for (matrix_sdk_base::crypto::OlmMachine, ruma::OwnedRoomId) { let decryption_settings = DecryptionSettings { sender_device_trust_requirement: TrustRequirement::Untrusted }; - let mut timeline_event = match olm_machine + match olm_machine .try_decrypt_room_event(raw.cast_ref(), room_id, &decryption_settings) .await? { RoomEventDecryptionResult::Decrypted(decrypted) => { - TimelineEvent::from_decrypted(decrypted, None) + let push_actions = push_ctx.map(|push_ctx| push_ctx.for_event(&decrypted.event)); + Ok(TimelineEvent::from_decrypted(decrypted, push_actions)) } RoomEventDecryptionResult::UnableToDecrypt(utd_info) => { - TimelineEvent::new_utd_event(raw.clone(), utd_info) + Ok(TimelineEvent::new_utd_event(raw.clone(), utd_info)) } - }; - - // Fill the push actions here, to mimic what `Room::decrypt_event` does. - if let Some(push_ctx) = push_ctx { - timeline_event.set_push_actions(push_ctx.for_event(timeline_event.raw())); } - - Ok(timeline_event) } } diff --git a/crates/matrix-sdk/src/room/mod.rs b/crates/matrix-sdk/src/room/mod.rs index c35c73276..14d67d524 100644 --- a/crates/matrix-sdk/src/room/mod.rs +++ b/crates/matrix-sdk/src/room/mod.rs @@ -1502,28 +1502,22 @@ impl Room { sender_device_trust_requirement: self.client.base_client().decryption_trust_requirement, }; - let mut event: TimelineEvent = match machine + match machine .try_decrypt_room_event(event.cast_ref(), self.inner.room_id(), &decryption_settings) .await? { RoomEventDecryptionResult::Decrypted(decrypted) => { - // Note: the push actions are set just afterwards. - TimelineEvent::from_decrypted(decrypted, None) + let push_actions = push_ctx.map(|push_ctx| push_ctx.for_event(&decrypted.event)); + Ok(TimelineEvent::from_decrypted(decrypted, push_actions)) } RoomEventDecryptionResult::UnableToDecrypt(utd_info) => { self.client .encryption() .backups() .maybe_download_room_key(self.room_id().to_owned(), event.clone()); - TimelineEvent::new_utd_event(event.clone().cast(), utd_info) + Ok(TimelineEvent::new_utd_event(event.clone().cast(), utd_info)) } - }; - - if let Some(push_ctx) = push_ctx { - event.set_push_actions(push_ctx.for_event(event.raw())); } - - Ok(event) } /// Fetches the [`EncryptionInfo`] for the supplied session_id.