From bcee5badaefd9e18e18583daf7f277b11625aaec Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 5 Aug 2025 18:16:24 +0200 Subject: [PATCH] refactor(threads): adapt to Ruma API changes related to async evaluation of push rules --- .../src/response_processors/notification.rs | 4 ++-- .../src/response_processors/state_events.rs | 14 ++++++++------ .../src/response_processors/timeline.rs | 14 ++++++++------ crates/matrix-sdk-ui/src/timeline/traits.rs | 6 +++++- crates/matrix-sdk/src/room/mod.rs | 18 +++++++++++++----- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/crates/matrix-sdk-base/src/response_processors/notification.rs b/crates/matrix-sdk-base/src/response_processors/notification.rs index fa9b09772..1050325d0 100644 --- a/crates/matrix-sdk-base/src/response_processors/notification.rs +++ b/crates/matrix-sdk-base/src/response_processors/notification.rs @@ -59,7 +59,7 @@ impl<'a> Notification<'a> { /// `push_condition_room_ctx`. (based on `Self::push_rules`). /// /// This method returns the fetched [`Action`]s. - pub fn push_notification_from_event_if( + pub async fn push_notification_from_event_if( &mut self, room_id: &RoomId, push_condition_room_ctx: &PushConditionRoomCtx, @@ -70,7 +70,7 @@ impl<'a> Notification<'a> { Raw: Into, P: Fn(&Action) -> bool, { - let actions = self.push_rules.get_actions(event, push_condition_room_ctx); + let actions = self.push_rules.get_actions(event, push_condition_room_ctx).await; if actions.iter().any(predicate) { self.push_notification(room_id, actions.to_owned(), event.clone().into()); diff --git a/crates/matrix-sdk-base/src/response_processors/state_events.rs b/crates/matrix-sdk-base/src/response_processors/state_events.rs index 86dcdd895..eb26db575 100644 --- a/crates/matrix-sdk-base/src/response_processors/state_events.rs +++ b/crates/matrix-sdk-base/src/response_processors/state_events.rs @@ -281,12 +281,14 @@ pub mod stripped { // Check every event again for notification. for event in state_events.values().flat_map(|map| map.values()) { - notification.push_notification_from_event_if( - room_id, - &push_condition_room_ctx, - event, - Action::should_notify, - ); + notification + .push_notification_from_event_if( + room_id, + &push_condition_room_ctx, + event, + Action::should_notify, + ) + .await; } } diff --git a/crates/matrix-sdk-base/src/response_processors/timeline.rs b/crates/matrix-sdk-base/src/response_processors/timeline.rs index 2d5a75352..56e114503 100644 --- a/crates/matrix-sdk-base/src/response_processors/timeline.rs +++ b/crates/matrix-sdk-base/src/response_processors/timeline.rs @@ -129,12 +129,14 @@ pub async fn build<'notification, 'e2ee>( } if let Some(push_condition_room_ctx) = &push_condition_room_ctx { - let actions = notification.push_notification_from_event_if( - room_id, - push_condition_room_ctx, - timeline_event.raw(), - Action::should_notify, - ); + let actions = notification + .push_notification_from_event_if( + room_id, + push_condition_room_ctx, + timeline_event.raw(), + Action::should_notify, + ) + .await; timeline_event.set_push_actions(actions.to_owned()); } diff --git a/crates/matrix-sdk-ui/src/timeline/traits.rs b/crates/matrix-sdk-ui/src/timeline/traits.rs index 67706ac2a..3bb8be847 100644 --- a/crates/matrix-sdk-ui/src/timeline/traits.rs +++ b/crates/matrix-sdk-ui/src/timeline/traits.rs @@ -343,7 +343,11 @@ impl Decryptor for (matrix_sdk_base::crypto::OlmMachine, ruma::OwnedRoomId) { .await? { RoomEventDecryptionResult::Decrypted(decrypted) => { - let push_actions = push_ctx.map(|push_ctx| push_ctx.for_event(&decrypted.event)); + let push_actions = if let Some(push_ctx) = push_ctx { + Some(push_ctx.for_event(&decrypted.event).await) + } else { + None + }; Ok(TimelineEvent::from_decrypted(decrypted, push_actions)) } RoomEventDecryptionResult::UnableToDecrypt(utd_info) => { diff --git a/crates/matrix-sdk/src/room/mod.rs b/crates/matrix-sdk/src/room/mod.rs index 0317c741f..1a57d68fa 100644 --- a/crates/matrix-sdk/src/room/mod.rs +++ b/crates/matrix-sdk/src/room/mod.rs @@ -217,8 +217,8 @@ impl PushContext { } /// Compute the push rules for a given event. - pub fn for_event(&self, event: &Raw) -> Vec { - self.push_rules.get_actions(event, &self.push_condition_room_ctx).to_owned() + pub async fn for_event(&self, event: &Raw) -> Vec { + self.push_rules.get_actions(event, &self.push_condition_room_ctx).await.to_owned() } } @@ -648,7 +648,7 @@ impl Room { let mut event = TimelineEvent::from_plaintext(event.cast()); if let Some(push_ctx) = push_ctx { - event.set_push_actions(push_ctx.for_event(event.raw())); + event.set_push_actions(push_ctx.for_event(event.raw()).await); } event @@ -1553,7 +1553,11 @@ impl Room { .await? { RoomEventDecryptionResult::Decrypted(decrypted) => { - let push_actions = push_ctx.map(|push_ctx| push_ctx.for_event(&decrypted.event)); + let push_actions = if let Some(push_ctx) = push_ctx { + Some(push_ctx.for_event(&decrypted.event).await) + } else { + None + }; Ok(TimelineEvent::from_decrypted(decrypted, push_actions)) } RoomEventDecryptionResult::UnableToDecrypt(utd_info) => { @@ -3045,7 +3049,11 @@ impl Room { /// Note that it is possible that no push action is returned because the /// current room state does not have all the required state events. pub async fn event_push_actions(&self, event: &Raw) -> Result>> { - Ok(self.push_context().await?.map(|ctx| ctx.for_event(event))) + if let Some(ctx) = self.push_context().await? { + Ok(Some(ctx.for_event(event).await)) + } else { + Ok(None) + } } /// The membership details of the (latest) invite for the logged-in user in