|
18 | 18 | use std::fmt;
|
19 | 19 |
|
20 | 20 | use async_trait::async_trait;
|
21 |
| -use ruma::{events::AnyTimelineEvent, serde::Raw}; |
22 | 21 | use serde::{ser::SerializeSeq, Deserialize, Deserializer, Serialize, Serializer};
|
23 |
| -use tracing::{debug, error}; |
| 22 | +use tracing::{debug, warn}; |
24 | 23 |
|
25 |
| -use super::{filter::FilterInput, Filter, MessageLikeEventFilter, StateEventFilter}; |
| 24 | +use super::{ |
| 25 | + filter::{Filter, FilterInput}, |
| 26 | + MessageLikeEventFilter, StateEventFilter, |
| 27 | +}; |
26 | 28 |
|
27 | 29 | /// Must be implemented by a component that provides functionality of deciding
|
28 | 30 | /// whether a widget is allowed to use certain capabilities (typically by
|
@@ -56,17 +58,47 @@ pub struct Capabilities {
|
56 | 58 | }
|
57 | 59 |
|
58 | 60 | 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 |
66 | 74 | }
|
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 | + } |
68 | 96 |
|
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) |
70 | 102 | }
|
71 | 103 | }
|
72 | 104 |
|
|
0 commit comments