refactor(base): Use Notification wherever possible.

This patch groups arguments behind the `Notification` struct
if it makes sense. This patch also uses the new method
`Notification::push_notification_from_event_if` method to replace
duplicated code.
This commit is contained in:
Ivan Enderlin
2025-04-14 13:49:50 +02:00
parent c16bc6b435
commit aeca1f1495
4 changed files with 31 additions and 48 deletions

View File

@@ -238,9 +238,7 @@ pub async fn update_invited_room(
(&raw_events, &events),
&room,
&mut room_info,
notification.push_rules,
notification.notifications,
notification.state_store,
notification,
)
.await?;

View File

@@ -117,18 +117,14 @@ pub mod sync {
pub mod stripped {
use std::{collections::BTreeMap, iter};
use ruma::{
events::AnyStrippedStateEvent,
push::{Action, Ruleset},
OwnedRoomId,
};
use ruma::{events::AnyStrippedStateEvent, push::Action};
use tracing::instrument;
use super::{super::timeline, Context, Raw};
use crate::{
deserialized_responses::RawAnySyncOrStrippedTimelineEvent, store::BaseStateStore,
sync::Notification, Result, Room, RoomInfo,
use super::{
super::{notification, timeline},
Context, Raw,
};
use crate::{Result, Room, RoomInfo};
/// Collect [`AnyStrippedStateEvent`] to [`AnyStrippedStateEvent`].
pub fn collect(
@@ -164,9 +160,7 @@ pub mod stripped {
(raw_events, events): (&[Raw<AnyStrippedStateEvent>], &[AnyStrippedStateEvent]),
room: &Room,
room_info: &mut RoomInfo,
push_rules: &Ruleset,
notifications: &mut BTreeMap<OwnedRoomId, Vec<Notification>>,
state_store: &BaseStateStore,
mut notification: notification::Notification<'_>,
) -> Result<()> {
let mut state_events = BTreeMap::new();
@@ -186,20 +180,19 @@ pub mod stripped {
// We need to check for notifications after we have handled all state
// events, to make sure we have the full push context.
if let Some(push_context) =
timeline::get_push_room_context(context, room, room_info, state_store).await?
timeline::get_push_room_context(context, room, room_info, notification.state_store)
.await?
{
let room_id = room.room_id();
// Check every event again for notification.
for event in state_events.values().flat_map(|map| map.values()) {
let actions = push_rules.get_actions(event, &push_context);
if actions.iter().any(Action::should_notify) {
notifications.entry(room.room_id().to_owned()).or_default().push(
Notification {
actions: actions.to_owned(),
event: RawAnySyncOrStrippedTimelineEvent::Stripped(event.clone()),
},
);
}
notification.push_notification_from_event_if(
room_id,
&push_context,
event,
Action::should_notify,
);
}
}

View File

@@ -32,9 +32,8 @@ use super::Context;
#[cfg(feature = "e2e-encryption")]
use super::{e2ee, verification};
use crate::{
deserialized_responses::RawAnySyncOrStrippedTimelineEvent,
store::{BaseStateStore, StateStoreExt as _},
sync::{Notification, Timeline},
sync::Timeline,
Result, Room, RoomInfo,
};
@@ -51,7 +50,7 @@ pub async fn build<'notification, 'e2ee>(
room: &Room,
room_info: &mut RoomInfo,
timeline_inputs: builder::Timeline,
notification_inputs: builder::Notification<'notification>,
mut notification_inputs: builder::Notification<'notification>,
#[cfg(feature = "e2e-encryption")] e2ee: builder::E2EE<'e2ee>,
) -> Result<Timeline> {
let mut timeline = Timeline::new(timeline_inputs.limited, timeline_inputs.prev_batch);
@@ -140,22 +139,13 @@ pub async fn build<'notification, 'e2ee>(
.await?;
}
if let Some(context) = &push_context {
let actions =
notification_inputs.push_rules.get_actions(timeline_event.raw(), context);
if actions.iter().any(Action::should_notify) {
notification_inputs
.notifications
.entry(room_id.to_owned())
.or_default()
.push(Notification {
actions: actions.to_owned(),
event: RawAnySyncOrStrippedTimelineEvent::Sync(
timeline_event.raw().clone(),
),
});
}
if let Some(push_context) = &push_context {
let actions = notification_inputs.push_notification_from_event_if(
room_id,
push_context,
timeline_event.raw(),
Action::should_notify,
);
timeline_event.push_actions = Some(actions.to_owned());
}

View File

@@ -405,9 +405,11 @@ impl BaseClient {
(&raw_events, &events),
&room,
&mut room_info,
&push_rules,
notifications,
&self.state_store,
processors::notification::Notification::new(
&push_rules,
notifications,
&self.state_store,
),
)
.await?;
}