Skip to content

Commit 1041ed9

Browse files
authored
Give all ProcessedToDevice variants a rawEvent property (#246)
Turns out that being able to access our best guess as to the event content, irrespective of the variant, is useful. Follow-up on #236
1 parent e2f8d11 commit 1041ed9

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
`ProcessedToDeviceEvent` instead of a JSON-encoded list of JSON-encoded events.
2424
This allows making the difference between an event that was sent in clear and
2525
the same event successfully decrypted.
26-
([#236](https://github.com/matrix-org/matrix-rust-sdk-crypto-wasm/pull/236))
26+
([#236](https://github.com/matrix-org/matrix-rust-sdk-crypto-wasm/pull/236)), ([#246](https://github.com/matrix-org/matrix-rust-sdk-crypto-wasm/pull/246))
2727

2828
- A number of the properties and methods on `DecryptedRoomEvent` no longer return `undefined`
2929
([#243](https://github.com/matrix-org/matrix-rust-sdk-crypto-wasm/pull/243))

src/types.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -388,15 +388,15 @@ pub enum ProcessedToDeviceEventType {
388388
#[wasm_bindgen]
389389
#[derive(Debug, Clone)]
390390
pub struct DecryptedToDeviceEvent {
391-
/// A JSON-encoded object containing the decrypted event, as if it had been
392-
/// sent in the clear.
391+
/// The decrypted event, as if it had been sent in the clear, encoded as
392+
/// JSON.
393393
///
394394
/// Typically contains properties `type`, `sender` and `content`.
395395
///
396396
/// (For room keys or secrets, some part of the content might have been
397397
/// zeroized).
398-
#[wasm_bindgen(readonly, getter_with_clone, js_name = "decryptedRawEvent")]
399-
pub decrypted_raw_event: JsString,
398+
#[wasm_bindgen(readonly, getter_with_clone, js_name = "rawEvent")]
399+
pub raw_event: JsString,
400400

401401
/// The encryption information for the event.
402402
#[wasm_bindgen(readonly, getter_with_clone, js_name = "encryptionInfo")]
@@ -417,8 +417,8 @@ impl DecryptedToDeviceEvent {
417417
#[wasm_bindgen]
418418
#[derive(Debug, Clone)]
419419
pub struct PlainTextToDeviceEvent {
420-
/// A JSON-encoded object containing the Matrix to-device message with
421-
/// `type`, `sender` and `content` fields.
420+
/// The to-device message, containing `type`, `sender` and `content` fields,
421+
/// encoded as JSON.
422422
#[wasm_bindgen(readonly, getter_with_clone, js_name = "rawEvent")]
423423
pub raw_event: JsString,
424424
}
@@ -437,10 +437,10 @@ impl PlainTextToDeviceEvent {
437437
#[wasm_bindgen]
438438
#[derive(Debug, Clone)]
439439
pub struct UTDToDeviceEvent {
440-
/// A JSON-encoded object containing the original message of type
441-
/// `m.room.encrypted` that failed to be decrypted.
442-
#[wasm_bindgen(readonly, getter_with_clone, js_name = "wireEvent")]
443-
pub wire_event: JsString,
440+
/// The original message (of type `m.room.encrypted`) that failed to be
441+
/// decrypted, encoded as JSON.
442+
#[wasm_bindgen(readonly, getter_with_clone, js_name = "rawEvent")]
443+
pub raw_event: JsString,
444444
// TODO: Add some OlmError in the future
445445
}
446446

@@ -459,10 +459,9 @@ impl UTDToDeviceEvent {
459459
#[wasm_bindgen]
460460
#[derive(Debug, Clone)]
461461
pub struct InvalidToDeviceEvent {
462-
/// A JSON-encoded object containing the original message as received from
463-
/// sync.
464-
#[wasm_bindgen(readonly, getter_with_clone, js_name = "wireEvent")]
465-
pub wire_event: JsString,
462+
/// The original message as received from sync, encoded as JSON.
463+
#[wasm_bindgen(readonly, getter_with_clone, js_name = "rawEvent")]
464+
pub raw_event: JsString,
466465
// TODO: Add some error information here?
467466
}
468467

@@ -498,11 +497,10 @@ pub fn processed_to_device_event_to_js_value(
498497
let result = match processed_to_device_event {
499498
matrix_sdk_crypto::types::ProcessedToDeviceEvent::Decrypted { raw, encryption_info } => {
500499
match encryption_info.try_into() {
501-
Ok(encryption_info) => DecryptedToDeviceEvent {
502-
decrypted_raw_event: raw.json().get().into(),
503-
encryption_info,
500+
Ok(encryption_info) => {
501+
DecryptedToDeviceEvent { raw_event: raw.json().get().into(), encryption_info }
502+
.into()
504503
}
505-
.into(),
506504
Err(e) => {
507505
// This can only happen if we receive an encrypted to-device event which is
508506
// encrypted with an algorithm we don't recognise. This
@@ -516,13 +514,13 @@ pub fn processed_to_device_event_to_js_value(
516514
}
517515
}
518516
matrix_sdk_crypto::types::ProcessedToDeviceEvent::UnableToDecrypt(utd) => {
519-
UTDToDeviceEvent { wire_event: utd.json().get().into() }.into()
517+
UTDToDeviceEvent { raw_event: utd.json().get().into() }.into()
520518
}
521519
matrix_sdk_crypto::types::ProcessedToDeviceEvent::PlainText(plain) => {
522520
PlainTextToDeviceEvent { raw_event: plain.json().get().into() }.into()
523521
}
524522
matrix_sdk_crypto::types::ProcessedToDeviceEvent::Invalid(invalid) => {
525-
InvalidToDeviceEvent { wire_event: invalid.json().get().into() }.into()
523+
InvalidToDeviceEvent { raw_event: invalid.json().get().into() }.into()
526524
}
527525
};
528526
Some(result)

tests/machine.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ describe(OlmMachine.name, () => {
16431643
const processed = receivedToDeviceArray[0];
16441644
expect(processed.type).toEqual(ProcessedToDeviceEventType.PlainText);
16451645
expect(processed).toBeInstanceOf(PlainTextToDeviceEvent);
1646-
const toDeviceEvent = JSON.parse((processed as PlainTextToDeviceEvent).rawEvent);
1646+
const toDeviceEvent = JSON.parse(processed.rawEvent);
16471647

16481648
expect(toDeviceEvent.sender).toEqual("@alice:example.com");
16491649
expect(toDeviceEvent.type).toEqual("custom.type");
@@ -1684,7 +1684,7 @@ describe(OlmMachine.name, () => {
16841684
const processed = receivedToDeviceArray[0];
16851685
expect(processed.type).toEqual(ProcessedToDeviceEventType.UnableToDecrypt);
16861686
expect(processed).toBeInstanceOf(UTDToDeviceEvent);
1687-
const toDeviceEvent = JSON.parse((processed as UTDToDeviceEvent).wireEvent);
1687+
const toDeviceEvent = JSON.parse(processed.rawEvent);
16881688

16891689
expect(toDeviceEvent.sender).toEqual("@bob:example.org");
16901690
expect(toDeviceEvent.type).toEqual("m.room.encrypted");
@@ -1737,14 +1737,14 @@ describe(OlmMachine.name, () => {
17371737
const processed0 = receivedToDeviceArray[0];
17381738
expect(processed0.type).toEqual(ProcessedToDeviceEventType.Invalid);
17391739
expect(processed0).toBeInstanceOf(InvalidToDeviceEvent);
1740-
const toDeviceEvent0 = JSON.parse((processed0 as InvalidToDeviceEvent).wireEvent);
1740+
const toDeviceEvent0 = JSON.parse(processed0.rawEvent);
17411741
expect(toDeviceEvent0.sender).toEqual("@alice:example.com");
17421742
expect(toDeviceEvent0.content).toBeDefined();
17431743
expect(toDeviceEvent0.type).toBeUndefined();
17441744

17451745
const processed1 = receivedToDeviceArray[1];
17461746
expect(processed1.type).toEqual(ProcessedToDeviceEventType.Invalid);
1747-
const toDeviceEvent1 = JSON.parse((processed1 as InvalidToDeviceEvent).wireEvent);
1747+
const toDeviceEvent1 = JSON.parse(processed1.rawEvent);
17481748
expect(toDeviceEvent1.sender).toEqual("@bob:example.org");
17491749
expect(toDeviceEvent1.type).toEqual("m.room.encrypted");
17501750
});
@@ -1846,7 +1846,7 @@ describe(OlmMachine.name, () => {
18461846
const processed = receivedToDeviceArray[0];
18471847
expect(processed.type).toEqual(ProcessedToDeviceEventType.Decrypted);
18481848
expect(processed).toBeInstanceOf(DecryptedToDeviceEvent);
1849-
const toDeviceEvent = JSON.parse((processed as DecryptedToDeviceEvent).decryptedRawEvent);
1849+
const toDeviceEvent = JSON.parse(processed.rawEvent);
18501850

18511851
expect(toDeviceEvent.sender).toEqual("@alice:example.org");
18521852
expect(toDeviceEvent.type).toEqual("custom.type");

0 commit comments

Comments
 (0)