@@ -362,3 +362,135 @@ impl From<&RoomSettings> for matrix_sdk_crypto::store::RoomSettings {
362
362
}
363
363
}
364
364
}
365
+
366
+ /// The type of a {@link ProcessedToDeviceEvent}.
367
+ #[ wasm_bindgen]
368
+ #[ derive( Debug , Clone ) ]
369
+ pub enum ProcessedToDeviceEventType {
370
+ /// A successfully-decrypted encrypted to-device message.
371
+ Decrypted ,
372
+
373
+ /// An encrypted to-device message which could not be decrypted.
374
+ UnableToDecrypt ,
375
+
376
+ /// An unencrypted to-device message (sent in clear).
377
+ PlainText ,
378
+
379
+ /// An invalid to-device message that was ignored because it is missing some
380
+ /// required information to be processed (like no event `type` for
381
+ /// example)
382
+ Invalid ,
383
+ }
384
+
385
+ /// Represents an encrypted to-device event, after it has been decrypted.
386
+ #[ wasm_bindgen]
387
+ #[ derive( Debug , Clone ) ]
388
+ pub struct DecryptedToDeviceEvent {
389
+ /// A JSON-encoded object containing the decrypted event, as if it had been
390
+ /// sent in the clear.
391
+ ///
392
+ /// Typically contains properties `type`, `sender` and `content`.
393
+ ///
394
+ /// (For room keys or secrets, some part of the content might have been
395
+ /// zeroized).
396
+ #[ wasm_bindgen( readonly, getter_with_clone, js_name = "decryptedRawEvent" ) ]
397
+ pub decrypted_raw_event : JsString ,
398
+ }
399
+
400
+ #[ wasm_bindgen]
401
+ impl DecryptedToDeviceEvent {
402
+ /// The type of processed to-device event. Always {@link
403
+ /// ProcessedToDeviceEventType.Decrypted} for this type.
404
+ #[ wasm_bindgen( getter, js_name = "type" ) ]
405
+ pub fn processed_type ( & self ) -> ProcessedToDeviceEventType {
406
+ ProcessedToDeviceEventType :: Decrypted
407
+ }
408
+ }
409
+
410
+ /// Represents a to-device event sent in the clear.
411
+ #[ wasm_bindgen]
412
+ #[ derive( Debug , Clone ) ]
413
+ pub struct PlainTextToDeviceEvent {
414
+ /// A JSON-encoded object containing the Matrix to-device message with
415
+ /// `type`, `sender` and `content` fields.
416
+ #[ wasm_bindgen( readonly, getter_with_clone, js_name = "rawEvent" ) ]
417
+ pub raw_event : JsString ,
418
+ }
419
+
420
+ #[ wasm_bindgen]
421
+ impl PlainTextToDeviceEvent {
422
+ /// The type of processed to-device event. Always {@link
423
+ /// ProcessedToDeviceEventType.PlainText} for this type.
424
+ #[ wasm_bindgen( getter, js_name = "type" ) ]
425
+ pub fn processed_type ( & self ) -> ProcessedToDeviceEventType {
426
+ ProcessedToDeviceEventType :: PlainText
427
+ }
428
+ }
429
+
430
+ /// Represents an encrypted to-device event that could not be decrypted.
431
+ #[ wasm_bindgen]
432
+ #[ derive( Debug , Clone ) ]
433
+ pub struct UTDToDeviceEvent {
434
+ /// A JSON-encoded object containing the original message of type
435
+ /// `m.room.encrypted` that failed to be decrypted.
436
+ #[ wasm_bindgen( readonly, getter_with_clone, js_name = "wireEvent" ) ]
437
+ pub wire_event : JsString ,
438
+ // TODO: Add some OlmError in the future
439
+ }
440
+
441
+ #[ wasm_bindgen]
442
+ impl UTDToDeviceEvent {
443
+ /// The type of processed to-device event. Always {@link
444
+ /// ProcessedToDeviceEventType.UnableToDecrypt} for this type.
445
+ #[ wasm_bindgen( getter, js_name = "type" ) ]
446
+ pub fn processed_type ( & self ) -> ProcessedToDeviceEventType {
447
+ ProcessedToDeviceEventType :: UnableToDecrypt
448
+ }
449
+ }
450
+
451
+ /// Represents an invalid to-device event that was ignored (because it is
452
+ /// missing some mandatory fields, for example).
453
+ #[ wasm_bindgen]
454
+ #[ derive( Debug , Clone ) ]
455
+ pub struct InvalidToDeviceEvent {
456
+ /// A JSON-encoded object containing the original message as received from
457
+ /// sync.
458
+ #[ wasm_bindgen( readonly, getter_with_clone, js_name = "wireEvent" ) ]
459
+ pub wire_event : JsString ,
460
+ // TODO: Add some error information here?
461
+ }
462
+
463
+ #[ wasm_bindgen]
464
+ impl InvalidToDeviceEvent {
465
+ /// The type of processed to-device event. Always {@link
466
+ /// ProcessedToDeviceEventType.Invalid} for this type.
467
+ #[ wasm_bindgen( getter, js_name = "type" ) ]
468
+ pub fn processed_type ( & self ) -> ProcessedToDeviceEventType {
469
+ ProcessedToDeviceEventType :: Invalid
470
+ }
471
+ }
472
+
473
+ /// Convert an `ProcessedToDeviceEvent` into a `JsValue`, ready to return to
474
+ /// JavaScript.
475
+ ///
476
+ /// JavaScript has no complex enums like Rust. To return structs of
477
+ /// different types, we have no choice other than hiding everything behind a
478
+ /// `JsValue`.
479
+ pub fn processed_to_device_event_to_js_value (
480
+ processed_to_device_event : matrix_sdk_crypto:: types:: ProcessedToDeviceEvent ,
481
+ ) -> JsValue {
482
+ match processed_to_device_event {
483
+ matrix_sdk_crypto:: types:: ProcessedToDeviceEvent :: Decrypted ( raw) => {
484
+ DecryptedToDeviceEvent { decrypted_raw_event : raw. json ( ) . get ( ) . into ( ) } . into ( )
485
+ }
486
+ matrix_sdk_crypto:: types:: ProcessedToDeviceEvent :: UnableToDecrypt ( utd) => {
487
+ UTDToDeviceEvent { wire_event : utd. json ( ) . get ( ) . into ( ) } . into ( )
488
+ }
489
+ matrix_sdk_crypto:: types:: ProcessedToDeviceEvent :: PlainText ( plain) => {
490
+ PlainTextToDeviceEvent { raw_event : plain. json ( ) . get ( ) . into ( ) } . into ( )
491
+ }
492
+ matrix_sdk_crypto:: types:: ProcessedToDeviceEvent :: Invalid ( invalid) => {
493
+ InvalidToDeviceEvent { wire_event : invalid. json ( ) . get ( ) . into ( ) } . into ( )
494
+ }
495
+ }
496
+ }
0 commit comments