Skip to content

Commit 9fbb9cb

Browse files
toger5poljar
authored andcommitted
WidgetDriver: refactor Filter
This commit simplifies the filter public api. Rethinking the public api we only need: - to know if events can be sent based on the capabilities - to know if events can be sent to the widget (read) based on the capabilities - if it even makes sense to sent a cs api read request or if all possibly returned events would not match the type. To simplify the code in the machine it also made sense to add `From` implementation to the FilterInputs instead of gathering the relevant data from all kinds of Raw events. The new api is simpler: All possible events we need to check can be converted into filter inputs (using `into()`). `capabilites` has two allow_read/allow_send that consume filter inputs. `capabilites` can be asked if there is any filter for specific event types to allow not send unnecassary requests.
1 parent 4e64f28 commit 9fbb9cb

File tree

3 files changed

+260
-142
lines changed

3 files changed

+260
-142
lines changed

crates/matrix-sdk/src/widget/capabilities.rs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818
use std::fmt;
1919

2020
use async_trait::async_trait;
21-
use ruma::{events::AnyTimelineEvent, serde::Raw};
2221
use serde::{ser::SerializeSeq, Deserialize, Deserializer, Serialize, Serializer};
23-
use tracing::{debug, error};
22+
use tracing::{debug, warn};
2423

25-
use super::{filter::FilterInput, Filter, MessageLikeEventFilter, StateEventFilter};
24+
use super::{
25+
filter::{Filter, FilterInput},
26+
MessageLikeEventFilter, StateEventFilter,
27+
};
2628

2729
/// Must be implemented by a component that provides functionality of deciding
2830
/// whether a widget is allowed to use certain capabilities (typically by
@@ -56,17 +58,47 @@ pub struct Capabilities {
5658
}
5759

5860
impl Capabilities {
59-
/// Tells if a given raw event matches the read filter.
60-
pub fn raw_event_matches_read_filter(&self, raw: &Raw<AnyTimelineEvent>) -> bool {
61-
let filter_in = match raw.deserialize_as::<FilterInput>() {
62-
Ok(filter) => filter,
63-
Err(err) => {
64-
error!("Failed to deserialize raw event as MatrixEventFilterInput: {err}");
65-
return false;
61+
/// Checks if a given event is allowed to be forwarded to the widget.
62+
///
63+
/// - `event_filter_input` is a minimized event respresntation that contains
64+
/// only the information needed to check if the widget is allowed to
65+
/// receive the event. (See [`FilterInput`])
66+
pub(super) fn allow_reading<'a>(
67+
&self,
68+
event_filter_input: impl TryInto<FilterInput<'a>>,
69+
) -> bool {
70+
match &event_filter_input.try_into() {
71+
Err(_) => {
72+
warn!("Failed to convert event into filter input for `allow_reading`.");
73+
false
6674
}
67-
};
75+
Ok(filter_input) => self.read.iter().any(|f| f.matches(filter_input)),
76+
}
77+
}
78+
79+
/// Checks if a given event is allowed to be sent by the widget.
80+
///
81+
/// - `event_filter_input` is a minimized event respresntation that contains
82+
/// only the information needed to check if the widget is allowed to send
83+
/// the event to a matrix room. (See [`FilterInput`])
84+
pub(super) fn allow_sending<'a>(
85+
&self,
86+
event_filter_input: impl TryInto<FilterInput<'a>>,
87+
) -> bool {
88+
match &event_filter_input.try_into() {
89+
Err(_) => {
90+
warn!("Failed to convert event into filter input for `allow_sending`.");
91+
false
92+
}
93+
Ok(filter_input) => self.send.iter().any(|f| f.matches(filter_input)),
94+
}
95+
}
6896

69-
self.read.iter().any(|f| f.matches(&filter_in))
97+
/// Checks if a filter exists for the given event type, useful for
98+
/// optimization. Avoids unnecessary read event requests when no matching
99+
/// filter is present.
100+
pub(super) fn has_read_filter_for_type(&self, event_type: &str) -> bool {
101+
self.read.iter().any(|f| f.filter_event_type() == event_type)
70102
}
71103
}
72104

0 commit comments

Comments
 (0)