use std::collections::BTreeMap;
use ruma::{
OwnedRoomId,
push::{Action, PushConditionRoomCtx, Ruleset},
serde::Raw,
};
use crate::{
deserialized_responses::RawAnySyncOrStrippedTimelineEvent, store::BaseStateStore, sync,
};
pub struct Notification<'a> {
pub push_rules: &'a Ruleset,
pub notifications: &'a mut BTreeMap<OwnedRoomId, Vec<sync::Notification>>,
pub state_store: &'a BaseStateStore,
}
impl<'a> Notification<'a> {
pub fn new(
push_rules: &'a Ruleset,
notifications: &'a mut BTreeMap<OwnedRoomId, Vec<sync::Notification>>,
state_store: &'a BaseStateStore,
) -> Self {
Self { push_rules, notifications, state_store }
}
fn push_notification(
&mut self,
room_id: OwnedRoomId,
actions: Vec<Action>,
event: RawAnySyncOrStrippedTimelineEvent,
) {
self.notifications.entry(room_id).or_default().push(sync::Notification { actions, event });
}
pub async fn push_notification_from_event_if<E, P>(
&mut self,
push_condition_room_ctx: &PushConditionRoomCtx,
event: &Raw<E>,
predicate: P,
) -> &[Action]
where
Raw<E>: Into<RawAnySyncOrStrippedTimelineEvent>,
P: Fn(&Action) -> bool,
{
let actions = self.push_rules.get_actions(event, push_condition_room_ctx).await;
if actions.iter().any(predicate) {
self.push_notification(
push_condition_room_ctx.room_id.clone(),
actions.to_owned(),
event.clone().into(),
);
}
actions
}
}