@@ -22,7 +22,7 @@ use serde::{ser::SerializeSeq, Deserialize, Deserializer, Serialize, Serializer}
22
22
use tracing:: { debug, warn} ;
23
23
24
24
use super :: {
25
- filter:: { Filter , FilterInput } ,
25
+ filter:: { Filter , FilterInput , ToDeviceEventFilter } ,
26
26
MessageLikeEventFilter , StateEventFilter ,
27
27
} ;
28
28
@@ -60,7 +60,7 @@ pub struct Capabilities {
60
60
impl Capabilities {
61
61
/// Checks if a given event is allowed to be forwarded to the widget.
62
62
///
63
- /// - `event_filter_input` is a minimized event respresntation that contains
63
+ /// - `event_filter_input` is a minimized event representation that contains
64
64
/// only the information needed to check if the widget is allowed to
65
65
/// receive the event. (See [`FilterInput`])
66
66
pub ( super ) fn allow_reading < ' a > (
@@ -78,7 +78,7 @@ impl Capabilities {
78
78
79
79
/// Checks if a given event is allowed to be sent by the widget.
80
80
///
81
- /// - `event_filter_input` is a minimized event respresntation that contains
81
+ /// - `event_filter_input` is a minimized event representation that contains
82
82
/// only the information needed to check if the widget is allowed to send
83
83
/// the event to a matrix room. (See [`FilterInput`])
84
84
pub ( super ) fn allow_sending < ' a > (
@@ -102,11 +102,13 @@ impl Capabilities {
102
102
}
103
103
}
104
104
105
- const SEND_EVENT : & str = "org.matrix.msc2762.send.event" ;
106
- const READ_EVENT : & str = "org.matrix.msc2762.receive.event" ;
107
- const SEND_STATE : & str = "org.matrix.msc2762.send.state_event" ;
108
- const READ_STATE : & str = "org.matrix.msc2762.receive.state_event" ;
109
- const REQUIRES_CLIENT : & str = "io.element.requires_client" ;
105
+ pub ( super ) const SEND_EVENT : & str = "org.matrix.msc2762.send.event" ;
106
+ pub ( super ) const READ_EVENT : & str = "org.matrix.msc2762.receive.event" ;
107
+ pub ( super ) const SEND_STATE : & str = "org.matrix.msc2762.send.state_event" ;
108
+ pub ( super ) const READ_STATE : & str = "org.matrix.msc2762.receive.state_event" ;
109
+ pub ( super ) const SEND_TODEVICE : & str = "org.matrix.msc3819.send.to_device" ;
110
+ pub ( super ) const READ_TODEVICE : & str = "org.matrix.msc3819.receive.to_device" ;
111
+ pub ( super ) const REQUIRES_CLIENT : & str = "io.element.requires_client" ;
110
112
pub ( super ) const SEND_DELAYED_EVENT : & str = "org.matrix.msc4157.send.delayed_event" ;
111
113
pub ( super ) const UPDATE_DELAYED_EVENT : & str = "org.matrix.msc4157.update_delayed_event" ;
112
114
@@ -121,6 +123,12 @@ impl Serialize for Capabilities {
121
123
match self . 0 {
122
124
Filter :: MessageLike ( filter) => PrintMessageLikeEventFilter ( filter) . fmt ( f) ,
123
125
Filter :: State ( filter) => PrintStateEventFilter ( filter) . fmt ( f) ,
126
+ Filter :: ToDevice ( filter) => {
127
+ // As per MSC 3819 https://github.com/matrix-org/matrix-spec-proposals/pull/3819
128
+ // ToDevice capabilities is in the form of `m.send.to_device:<event type>`
129
+ // or `m.receive.to_device:<event type>`
130
+ write ! ( f, "{}" , filter. event_type)
131
+ }
124
132
}
125
133
}
126
134
}
@@ -168,13 +176,15 @@ impl Serialize for Capabilities {
168
176
let name = match filter {
169
177
Filter :: MessageLike ( _) => READ_EVENT ,
170
178
Filter :: State ( _) => READ_STATE ,
179
+ Filter :: ToDevice ( _) => READ_TODEVICE ,
171
180
} ;
172
181
seq. serialize_element ( & format ! ( "{name}:{}" , PrintEventFilter ( filter) ) ) ?;
173
182
}
174
183
for filter in & self . send {
175
184
let name = match filter {
176
185
Filter :: MessageLike ( _) => SEND_EVENT ,
177
186
Filter :: State ( _) => SEND_STATE ,
187
+ Filter :: ToDevice ( _) => SEND_TODEVICE ,
178
188
} ;
179
189
seq. serialize_element ( & format ! ( "{name}:{}" , PrintEventFilter ( filter) ) ) ?;
180
190
}
@@ -226,6 +236,12 @@ impl<'de> Deserialize<'de> for Capabilities {
226
236
Some ( ( SEND_STATE , filter_s) ) => {
227
237
Ok ( Permission :: Send ( Filter :: State ( parse_state_event_filter ( filter_s) ) ) )
228
238
}
239
+ Some ( ( READ_TODEVICE , filter_s) ) => Ok ( Permission :: Read ( Filter :: ToDevice (
240
+ parse_to_device_event_filter ( filter_s) ,
241
+ ) ) ) ,
242
+ Some ( ( SEND_TODEVICE , filter_s) ) => Ok ( Permission :: Send ( Filter :: ToDevice (
243
+ parse_to_device_event_filter ( filter_s) ,
244
+ ) ) ) ,
229
245
_ => {
230
246
debug ! ( "Unknown capability `{s}`" ) ;
231
247
Ok ( Self :: Unknown )
@@ -252,6 +268,10 @@ impl<'de> Deserialize<'de> for Capabilities {
252
268
}
253
269
}
254
270
271
+ fn parse_to_device_event_filter ( s : & str ) -> ToDeviceEventFilter {
272
+ ToDeviceEventFilter :: new ( s. into ( ) )
273
+ }
274
+
255
275
let mut capabilities = Capabilities :: default ( ) ;
256
276
for capability in Vec :: < Permission > :: deserialize ( deserializer) ? {
257
277
match capability {
@@ -274,6 +294,7 @@ mod tests {
274
294
use ruma:: events:: StateEventType ;
275
295
276
296
use super :: * ;
297
+ use crate :: widget:: filter:: ToDeviceEventFilter ;
277
298
278
299
#[ test]
279
300
fn deserialization_of_no_capabilities ( ) {
@@ -293,8 +314,10 @@ mod tests {
293
314
"org.matrix.msc2762.receive.event:org.matrix.rageshake_request",
294
315
"org.matrix.msc2762.receive.state_event:m.room.member",
295
316
"org.matrix.msc2762.receive.state_event:org.matrix.msc3401.call.member",
317
+ "org.matrix.msc3819.receive.to_device:io.element.call.encryption_keys",
296
318
"org.matrix.msc2762.send.event:org.matrix.rageshake_request",
297
319
"org.matrix.msc2762.send.state_event:org.matrix.msc3401.call.member#@user:matrix.server",
320
+ "org.matrix.msc3819.send.to_device:io.element.call.encryption_keys",
298
321
"org.matrix.msc4157.send.delayed_event",
299
322
"org.matrix.msc4157.update_delayed_event"
300
323
]"# ;
@@ -307,6 +330,9 @@ mod tests {
307
330
) ) ,
308
331
Filter :: State ( StateEventFilter :: WithType ( StateEventType :: RoomMember ) ) ,
309
332
Filter :: State ( StateEventFilter :: WithType ( "org.matrix.msc3401.call.member" . into( ) ) ) ,
333
+ Filter :: ToDevice ( ToDeviceEventFilter :: new(
334
+ "io.element.call.encryption_keys" . into( ) ,
335
+ ) ) ,
310
336
] ,
311
337
send : vec ! [
312
338
Filter :: MessageLike ( MessageLikeEventFilter :: WithType (
@@ -316,6 +342,9 @@ mod tests {
316
342
"org.matrix.msc3401.call.member" . into( ) ,
317
343
"@user:matrix.server" . into( ) ,
318
344
) ) ,
345
+ Filter :: ToDevice ( ToDeviceEventFilter :: new(
346
+ "io.element.call.encryption_keys" . into( ) ,
347
+ ) ) ,
319
348
] ,
320
349
requires_client : true ,
321
350
update_delayed_event : true ,
@@ -335,13 +364,17 @@ mod tests {
335
364
"org.matrix.msc3401.call.member" . into( ) ,
336
365
"@user:matrix.server" . into( ) ,
337
366
) ) ,
367
+ Filter :: ToDevice ( ToDeviceEventFilter :: new(
368
+ "io.element.call.encryption_keys" . into( ) ,
369
+ ) ) ,
338
370
] ,
339
371
send : vec ! [
340
372
Filter :: MessageLike ( MessageLikeEventFilter :: WithType ( "io.element.custom" . into( ) ) ) ,
341
373
Filter :: State ( StateEventFilter :: WithTypeAndStateKey (
342
374
"org.matrix.msc3401.call.member" . into( ) ,
343
375
"@user:matrix.server" . into( ) ,
344
376
) ) ,
377
+ Filter :: ToDevice ( ToDeviceEventFilter :: new( "my.org.other.to_device_event" . into( ) ) ) ,
345
378
] ,
346
379
requires_client : true ,
347
380
update_delayed_event : false ,
0 commit comments