Skip to content

Commit e4e3ff6

Browse files
committed
refactor(notification client): extract the common filtering out of events in a common helper
1 parent 8409e52 commit e4e3ff6

File tree

1 file changed

+35
-42
lines changed

1 file changed

+35
-42
lines changed

crates/matrix-sdk-ui/src/notification_client.rs

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,34 @@ impl NotificationClient {
606606
get_notifications_result.remove(event_id).unwrap_or(Ok(NotificationStatus::EventNotFound))
607607
}
608608

609+
/// Given a (decrypted or not) event, figure out whether it should be
610+
/// filtered out for other client-side reasons (such as the sender being
611+
/// ignored, for instance), and returns the corresponding
612+
/// [`NotificationStatus`].
613+
async fn compute_status(
614+
&self,
615+
room: &Room,
616+
push_actions: Option<&[Action]>,
617+
raw_event: RawNotificationEvent,
618+
state_events: Vec<Raw<AnyStateEvent>>,
619+
) -> Result<NotificationStatus, Error> {
620+
if let Some(actions) = push_actions
621+
&& !actions.iter().any(|a| a.should_notify())
622+
{
623+
// The event shouldn't notify: return early.
624+
return Ok(NotificationStatus::EventFilteredOut);
625+
}
626+
627+
let notification_item =
628+
NotificationItem::new(room, raw_event, push_actions, state_events).await?;
629+
630+
if self.client.is_user_ignored(notification_item.event.sender()).await {
631+
Ok(NotificationStatus::EventFilteredOut)
632+
} else {
633+
Ok(NotificationStatus::Event(Box::new(notification_item)))
634+
}
635+
}
636+
609637
/// Get a list of full notifications, given a room id and event ids.
610638
///
611639
/// This will run a small sliding sync to retrieve the content of the
@@ -674,34 +702,10 @@ impl NotificationClient {
674702
}
675703
};
676704

677-
let should_notify = push_actions
678-
.as_ref()
679-
.is_some_and(|actions| actions.iter().any(|a| a.should_notify()));
680-
681-
if !should_notify {
682-
// The event has been filtered out by the user's push rules.
683-
batch_result.insert(event_id, Ok(NotificationStatus::EventFilteredOut));
684-
continue;
685-
}
705+
let notification_status_result =
706+
self.compute_status(&room, push_actions.as_deref(), raw_event, Vec::new()).await;
686707

687-
let notification_item =
688-
match NotificationItem::new(&room, raw_event, push_actions.as_deref(), Vec::new())
689-
.await
690-
{
691-
Ok(item) => item,
692-
Err(err) => {
693-
// Could not build the notification item, return an error.
694-
batch_result.insert(event_id, Err(err));
695-
continue;
696-
}
697-
};
698-
699-
if self.client.is_user_ignored(notification_item.event.sender()).await {
700-
batch_result.insert(event_id, Ok(NotificationStatus::EventFilteredOut));
701-
} else {
702-
batch_result
703-
.insert(event_id, Ok(NotificationStatus::Event(Box::new(notification_item))));
704-
}
708+
batch_result.insert(event_id, notification_status_result);
705709
}
706710

707711
Ok(batch_result)
@@ -740,26 +744,15 @@ impl NotificationClient {
740744
timeline_event = decrypted_event;
741745
}
742746

743-
if let Some(actions) = timeline_event.push_actions()
744-
&& !actions.iter().any(|a| a.should_notify())
745-
{
746-
return Ok(NotificationStatus::EventFilteredOut);
747-
}
748-
749747
let push_actions = timeline_event.push_actions().map(ToOwned::to_owned);
750-
let notification_item = NotificationItem::new(
748+
749+
self.compute_status(
751750
&room,
752-
RawNotificationEvent::Timeline(timeline_event.into_raw()),
753751
push_actions.as_deref(),
752+
RawNotificationEvent::Timeline(timeline_event.into_raw()),
754753
state_events,
755754
)
756-
.await?;
757-
758-
if self.client.is_user_ignored(notification_item.event.sender()).await {
759-
Ok(NotificationStatus::EventFilteredOut)
760-
} else {
761-
Ok(NotificationStatus::Event(Box::new(notification_item)))
762-
}
755+
.await
763756
}
764757
}
765758

0 commit comments

Comments
 (0)