base: make the dependency to push rules explicit when processing a room's subpart of a response

This commit is contained in:
Benjamin Bouvier
2024-10-01 16:50:57 +02:00
parent 0a854cdbf7
commit 759a9b0e18
3 changed files with 24 additions and 20 deletions

View File

@@ -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<Ruleset> {
if let Some(event) = changes
.account_data
.get(&GlobalAccountDataEventType::PushRules)
.and_then(|ev| ev.deserialize_as::<PushRulesEvent>().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<Ruleset> {
if let Some(event) =
account_data.push_rules().and_then(|ev| ev.deserialize_as::<PushRulesEvent>().ok())
{
Ok(event.content.global)
} else if let Some(event) = self

View File

@@ -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<AnyGlobalAccountDataEvent>> {
self.raw_by_type.get(&GlobalAccountDataEventType::PushRules)
}
/// Applies the processed data to the state changes.
pub fn apply(mut self, changes: &mut StateChanges) -> Vec<AnyGlobalAccountDataEvent> {
mem::swap(&mut changes.account_data, &mut self.raw_by_type);

View File

@@ -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<OwnedRoomId, Vec<Raw<AnyRoomAccountDataEvent>>>,
store: &Store,
account_data: &AccountDataProcessor,
changes: &mut StateChanges,
room_info_notable_updates: &mut BTreeMap<OwnedRoomId, RoomInfoNotableUpdateReasons>,
notifications: &mut BTreeMap<OwnedRoomId, Vec<Notification>>,
@@ -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(