From 759a9b0e1853bd52c3520ff8130feb47ed37be22 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 1 Oct 2024 16:50:57 +0200 Subject: [PATCH] base: make the dependency to push rules explicit when processing a room's subpart of a response --- crates/matrix-sdk-base/src/client.rs | 23 ++++++++++--------- .../src/response_processors.rs | 5 ++++ .../matrix-sdk-base/src/sliding_sync/mod.rs | 16 ++++++------- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/crates/matrix-sdk-base/src/client.rs b/crates/matrix-sdk-base/src/client.rs index 0fa57e8d9..cdebfdc91 100644 --- a/crates/matrix-sdk-base/src/client.rs +++ b/crates/matrix-sdk-base/src/client.rs @@ -953,10 +953,9 @@ impl BaseClient { let mut ambiguity_cache = AmbiguityCache::new(self.store.inner.clone()); - let global_account_data_events = - ProcessAccountData::process(&response.account_data.events).apply(&mut changes); + let account_data = AccountDataProcessor::process(&response.account_data.events); - let push_rules = self.get_push_rules(&changes).await?; + let push_rules = self.get_push_rules(&account_data).await?; let mut new_rooms = RoomUpdates::default(); let mut notifications = Default::default(); @@ -1173,6 +1172,7 @@ impl BaseClient { // because we want to have the push rules in place before we process // rooms and their events, but we want to create the rooms before we // process the `m.direct` account data event. + let global_account_data_events = account_data.apply(&mut changes); let has_new_direct_room_data = global_account_data_events .iter() .any(|event| event.event_type() == GlobalAccountDataEventType::Direct); @@ -1463,14 +1463,15 @@ impl BaseClient { /// Get the push rules. /// - /// Gets the push rules from `changes` if they have been updated, otherwise - /// get them from the store. As a fallback, uses - /// `Ruleset::server_default` if the user is logged in. - pub async fn get_push_rules(&self, changes: &StateChanges) -> Result { - if let Some(event) = changes - .account_data - .get(&GlobalAccountDataEventType::PushRules) - .and_then(|ev| ev.deserialize_as::().ok()) + /// Gets the push rules previously processed, otherwise get them from the + /// store. As a fallback, uses [`Ruleset::server_default`] if the user + /// is logged in. + pub(crate) async fn get_push_rules( + &self, + account_data: &AccountDataProcessor, + ) -> Result { + if let Some(event) = + account_data.push_rules().and_then(|ev| ev.deserialize_as::().ok()) { Ok(event.content.global) } else if let Some(event) = self diff --git a/crates/matrix-sdk-base/src/response_processors.rs b/crates/matrix-sdk-base/src/response_processors.rs index 25a8b253a..40b9b0785 100644 --- a/crates/matrix-sdk-base/src/response_processors.rs +++ b/crates/matrix-sdk-base/src/response_processors.rs @@ -51,6 +51,11 @@ impl AccountDataProcessor { Self { raw_by_type, parsed_events } } + /// Returns the push rules found by this processor. + pub fn push_rules(&self) -> Option<&Raw> { + self.raw_by_type.get(&GlobalAccountDataEventType::PushRules) + } + /// Applies the processed data to the state changes. pub fn apply(mut self, changes: &mut StateChanges) -> Vec { mem::swap(&mut changes.account_data, &mut self.raw_by_type); diff --git a/crates/matrix-sdk-base/src/sliding_sync/mod.rs b/crates/matrix-sdk-base/src/sliding_sync/mod.rs index 680e0b37e..75435fc68 100644 --- a/crates/matrix-sdk-base/src/sliding_sync/mod.rs +++ b/crates/matrix-sdk-base/src/sliding_sync/mod.rs @@ -164,16 +164,11 @@ impl BaseClient { let store = self.store.clone(); let mut ambiguity_cache = AmbiguityCache::new(store.inner.clone()); - let account_data = &extensions.account_data; - let global_account_data_events = if !account_data.is_empty() { - ProcessAccountData::process(&account_data.global).apply(&mut changes) - } else { - Vec::new() - }; + let account_data = AccountDataProcessor::process(&extensions.account_data.global); let mut new_rooms = RoomUpdates::default(); let mut notifications = Default::default(); - let mut rooms_account_data = account_data.rooms.clone(); + let mut rooms_account_data = extensions.account_data.rooms.clone(); for (room_id, response_room_data) in rooms { let (room_info, joined_room, left_room, invited_room) = self @@ -182,6 +177,7 @@ impl BaseClient { response_room_data, &mut rooms_account_data, &store, + &account_data, &mut changes, &mut room_info_notable_updates, &mut notifications, @@ -309,6 +305,7 @@ impl BaseClient { // because we want to have the push rules in place before we process // rooms and their events, but we want to create the rooms before we // process the `m.direct` account data event. + let global_account_data_events = account_data.apply(&mut changes); let has_new_direct_room_data = global_account_data_events .iter() .any(|event| event.event_type() == GlobalAccountDataEventType::Direct); @@ -354,7 +351,7 @@ impl BaseClient { notifications, // FIXME not yet supported by sliding sync. presence: Default::default(), - account_data: account_data.global.clone(), + account_data: extensions.account_data.global.clone(), to_device: Default::default(), }) } @@ -366,6 +363,7 @@ impl BaseClient { room_data: &http::response::Room, rooms_account_data: &mut BTreeMap>>, store: &Store, + account_data: &AccountDataProcessor, changes: &mut StateChanges, room_info_notable_updates: &mut BTreeMap, notifications: &mut BTreeMap>, @@ -451,7 +449,7 @@ impl BaseClient { Default::default() }; - let push_rules = self.get_push_rules(changes).await?; + let push_rules = self.get_push_rules(account_data).await?; if let Some(invite_state) = &room_data.invite_state { self.handle_invited_state(