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;
24
23
25
24
use super :: {
26
- filter:: MatrixEventFilterInput , EventFilter , MessageLikeEventFilter , StateEventFilter ,
25
+ filter:: { Filter , FilterInput } ,
26
+ MessageLikeEventFilter , StateEventFilter ,
27
27
} ;
28
28
29
29
/// Must be implemented by a component that provides functionality of deciding
@@ -42,9 +42,9 @@ pub trait CapabilitiesProvider: Send + Sync + 'static {
42
42
#[ cfg_attr( test, derive( PartialEq ) ) ]
43
43
pub struct Capabilities {
44
44
/// Types of the messages that a widget wants to be able to fetch.
45
- pub read : Vec < EventFilter > ,
45
+ pub read : Vec < Filter > ,
46
46
/// Types of the messages that a widget wants to be able to send.
47
- pub send : Vec < EventFilter > ,
47
+ pub send : Vec < Filter > ,
48
48
/// If this capability is requested by the widget, it can not operate
49
49
/// separately from the matrix client.
50
50
///
@@ -58,17 +58,29 @@ pub struct Capabilities {
58
58
}
59
59
60
60
impl Capabilities {
61
- /// Tells if a given raw event matches the read filter.
62
- pub fn raw_event_matches_read_filter ( & self , raw : & Raw < AnyTimelineEvent > ) -> bool {
63
- let filter_in = match raw. deserialize_as :: < MatrixEventFilterInput > ( ) {
64
- Ok ( filter) => filter,
65
- Err ( err) => {
66
- error ! ( "Failed to deserialize raw event as MatrixEventFilterInput: {err}" ) ;
67
- return false ;
68
- }
69
- } ;
61
+ /// Checks if a givent 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 ( & self , event_filter_input : & FilterInput ) -> bool {
67
+ self . read . iter ( ) . any ( |f| f. matches ( event_filter_input) )
68
+ }
70
69
71
- self . read . iter ( ) . any ( |f| f. matches ( & filter_in) )
70
+ /// Checks if a givent event is allowed to be sent by the widget.
71
+ ///
72
+ /// - `event_filter_input` is a minimized event respresntation that contains
73
+ /// only the information needed to check if the widget is allowed to send
74
+ /// the event to a matrix room. (See [`FilterInput`])
75
+ pub ( super ) fn allow_sending ( & self , event_filter_input : & FilterInput ) -> bool {
76
+ self . send . iter ( ) . any ( |f| f. matches ( event_filter_input) )
77
+ }
78
+
79
+ /// Checks if a filter exists for the given event type, useful for
80
+ /// optimization. Avoids unnecessary read event requests when no matching
81
+ /// filter is present.
82
+ pub ( super ) fn has_read_filter_for_type ( & self , event_type : & str ) -> bool {
83
+ self . read . iter ( ) . any ( |f| f. filter_event_type ( ) == event_type)
72
84
}
73
85
}
74
86
@@ -85,12 +97,12 @@ impl Serialize for Capabilities {
85
97
where
86
98
S : Serializer ,
87
99
{
88
- struct PrintEventFilter < ' a > ( & ' a EventFilter ) ;
100
+ struct PrintEventFilter < ' a > ( & ' a Filter ) ;
89
101
impl fmt:: Display for PrintEventFilter < ' _ > {
90
102
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
91
103
match self . 0 {
92
- EventFilter :: MessageLike ( filter) => PrintMessageLikeEventFilter ( filter) . fmt ( f) ,
93
- EventFilter :: State ( filter) => PrintStateEventFilter ( filter) . fmt ( f) ,
104
+ Filter :: MessageLike ( filter) => PrintMessageLikeEventFilter ( filter) . fmt ( f) ,
105
+ Filter :: State ( filter) => PrintStateEventFilter ( filter) . fmt ( f) ,
94
106
}
95
107
}
96
108
}
@@ -136,15 +148,15 @@ impl Serialize for Capabilities {
136
148
}
137
149
for filter in & self . read {
138
150
let name = match filter {
139
- EventFilter :: MessageLike ( _) => READ_EVENT ,
140
- EventFilter :: State ( _) => READ_STATE ,
151
+ Filter :: MessageLike ( _) => READ_EVENT ,
152
+ Filter :: State ( _) => READ_STATE ,
141
153
} ;
142
154
seq. serialize_element ( & format ! ( "{name}:{}" , PrintEventFilter ( filter) ) ) ?;
143
155
}
144
156
for filter in & self . send {
145
157
let name = match filter {
146
- EventFilter :: MessageLike ( _) => SEND_EVENT ,
147
- EventFilter :: State ( _) => SEND_STATE ,
158
+ Filter :: MessageLike ( _) => SEND_EVENT ,
159
+ Filter :: State ( _) => SEND_STATE ,
148
160
} ;
149
161
seq. serialize_element ( & format ! ( "{name}:{}" , PrintEventFilter ( filter) ) ) ?;
150
162
}
@@ -162,8 +174,8 @@ impl<'de> Deserialize<'de> for Capabilities {
162
174
RequiresClient ,
163
175
UpdateDelayedEvent ,
164
176
SendDelayedEvent ,
165
- Read ( EventFilter ) ,
166
- Send ( EventFilter ) ,
177
+ Read ( Filter ) ,
178
+ Send ( Filter ) ,
167
179
Unknown ,
168
180
}
169
181
@@ -184,17 +196,17 @@ impl<'de> Deserialize<'de> for Capabilities {
184
196
}
185
197
186
198
match s. split_once ( ':' ) {
187
- Some ( ( READ_EVENT , filter_s) ) => Ok ( Permission :: Read ( EventFilter :: MessageLike (
199
+ Some ( ( READ_EVENT , filter_s) ) => Ok ( Permission :: Read ( Filter :: MessageLike (
188
200
parse_message_event_filter ( filter_s) ,
189
201
) ) ) ,
190
- Some ( ( SEND_EVENT , filter_s) ) => Ok ( Permission :: Send ( EventFilter :: MessageLike (
202
+ Some ( ( SEND_EVENT , filter_s) ) => Ok ( Permission :: Send ( Filter :: MessageLike (
191
203
parse_message_event_filter ( filter_s) ,
192
204
) ) ) ,
193
205
Some ( ( READ_STATE , filter_s) ) => {
194
- Ok ( Permission :: Read ( EventFilter :: State ( parse_state_event_filter ( filter_s) ) ) )
206
+ Ok ( Permission :: Read ( Filter :: State ( parse_state_event_filter ( filter_s) ) ) )
195
207
}
196
208
Some ( ( SEND_STATE , filter_s) ) => {
197
- Ok ( Permission :: Send ( EventFilter :: State ( parse_state_event_filter ( filter_s) ) ) )
209
+ Ok ( Permission :: Send ( Filter :: State ( parse_state_event_filter ( filter_s) ) ) )
198
210
}
199
211
_ => {
200
212
debug ! ( "Unknown capability `{s}`" ) ;
@@ -272,19 +284,17 @@ mod tests {
272
284
let parsed = serde_json:: from_str :: < Capabilities > ( capabilities_str) . unwrap ( ) ;
273
285
let expected = Capabilities {
274
286
read : vec ! [
275
- EventFilter :: MessageLike ( MessageLikeEventFilter :: WithType (
287
+ Filter :: MessageLike ( MessageLikeEventFilter :: WithType (
276
288
"org.matrix.rageshake_request" . into( ) ,
277
289
) ) ,
278
- EventFilter :: State ( StateEventFilter :: WithType ( StateEventType :: RoomMember ) ) ,
279
- EventFilter :: State ( StateEventFilter :: WithType (
280
- "org.matrix.msc3401.call.member" . into( ) ,
281
- ) ) ,
290
+ Filter :: State ( StateEventFilter :: WithType ( StateEventType :: RoomMember ) ) ,
291
+ Filter :: State ( StateEventFilter :: WithType ( "org.matrix.msc3401.call.member" . into( ) ) ) ,
282
292
] ,
283
293
send : vec ! [
284
- EventFilter :: MessageLike ( MessageLikeEventFilter :: WithType (
294
+ Filter :: MessageLike ( MessageLikeEventFilter :: WithType (
285
295
"org.matrix.rageshake_request" . into( ) ,
286
296
) ) ,
287
- EventFilter :: State ( StateEventFilter :: WithTypeAndStateKey (
297
+ Filter :: State ( StateEventFilter :: WithTypeAndStateKey (
288
298
"org.matrix.msc3401.call.member" . into( ) ,
289
299
"@user:matrix.server" . into( ) ,
290
300
) ) ,
@@ -301,20 +311,16 @@ mod tests {
301
311
fn serialization_and_deserialization_are_symmetrical ( ) {
302
312
let capabilities = Capabilities {
303
313
read : vec ! [
304
- EventFilter :: MessageLike ( MessageLikeEventFilter :: WithType (
305
- "io.element.custom" . into( ) ,
306
- ) ) ,
307
- EventFilter :: State ( StateEventFilter :: WithType ( StateEventType :: RoomMember ) ) ,
308
- EventFilter :: State ( StateEventFilter :: WithTypeAndStateKey (
314
+ Filter :: MessageLike ( MessageLikeEventFilter :: WithType ( "io.element.custom" . into( ) ) ) ,
315
+ Filter :: State ( StateEventFilter :: WithType ( StateEventType :: RoomMember ) ) ,
316
+ Filter :: State ( StateEventFilter :: WithTypeAndStateKey (
309
317
"org.matrix.msc3401.call.member" . into( ) ,
310
318
"@user:matrix.server" . into( ) ,
311
319
) ) ,
312
320
] ,
313
321
send : vec ! [
314
- EventFilter :: MessageLike ( MessageLikeEventFilter :: WithType (
315
- "io.element.custom" . into( ) ,
316
- ) ) ,
317
- EventFilter :: State ( StateEventFilter :: WithTypeAndStateKey (
322
+ Filter :: MessageLike ( MessageLikeEventFilter :: WithType ( "io.element.custom" . into( ) ) ) ,
323
+ Filter :: State ( StateEventFilter :: WithTypeAndStateKey (
318
324
"org.matrix.msc3401.call.member" . into( ) ,
319
325
"@user:matrix.server" . into( ) ,
320
326
) ) ,
0 commit comments