Skip to content

Commit

Permalink
crypto: pass unable_to_decrypt_info into UtdCause::determine
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Oct 21, 2024
1 parent 1bc35af commit 7278319
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
74 changes: 62 additions & 12 deletions crates/matrix-sdk-crypto/src/types/events/utd_cause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ enum Membership {

impl UtdCause {
/// Decide the cause of this UTD, based on the evidence we have.
pub fn determine(raw_event: Option<&Raw<AnySyncTimelineEvent>>) -> Self {
pub fn determine(
raw_event: Option<&Raw<AnySyncTimelineEvent>>,
unable_to_decrypt_info: &UnableToDecryptInfo,
) -> Self {
// TODO: in future, use more information to give a richer answer. E.g.
// is this event device-historical? Was the Olm communication disrupted?
// Did the sender refuse to send the key because we're not verified?
Expand All @@ -76,6 +79,7 @@ impl UtdCause {

#[cfg(test)]
mod tests {
use matrix_sdk_common::deserialized_responses::{UnableToDecryptInfo, UnableToDecryptReason};
use ruma::{events::AnySyncTimelineEvent, serde::Raw};
use serde_json::{json, value::to_raw_value};

Expand All @@ -85,21 +89,45 @@ mod tests {
fn a_missing_raw_event_means_we_guess_unknown() {
// When we don't provide any JSON to check for membership, then we guess the UTD
// is unknown.
assert_eq!(UtdCause::determine(None), UtdCause::Unknown);
assert_eq!(
UtdCause::determine(
None,
&UnableToDecryptInfo {
session_id: None,
reason: UnableToDecryptReason::MissingMegolmSession,
}
),
UtdCause::Unknown
);
}

#[test]
fn if_there_is_no_membership_info_we_guess_unknown() {
// If our JSON contains no membership info, then we guess the UTD is unknown.
assert_eq!(UtdCause::determine(Some(&raw_event(json!({})))), UtdCause::Unknown);
assert_eq!(
UtdCause::determine(
Some(&raw_event(json!({}))),
&UnableToDecryptInfo {
session_id: None,
reason: UnableToDecryptReason::MissingMegolmSession
}
),
UtdCause::Unknown
);
}

#[test]
fn if_membership_info_cant_be_parsed_we_guess_unknown() {
// If our JSON contains a membership property but not the JSON we expected, then
// we guess the UTD is unknown.
assert_eq!(
UtdCause::determine(Some(&raw_event(json!({ "unsigned": { "membership": 3 } })))),
UtdCause::determine(
Some(&raw_event(json!({ "unsigned": { "membership": 3 } }))),
&UnableToDecryptInfo {
session_id: None,
reason: UnableToDecryptReason::MissingMegolmSession
}
),
UtdCause::Unknown
);
}
Expand All @@ -109,9 +137,13 @@ mod tests {
// If membership=invite then we expected to be sent the keys so the cause of the
// UTD is unknown.
assert_eq!(
UtdCause::determine(Some(&raw_event(
json!({ "unsigned": { "membership": "invite" } }),
))),
UtdCause::determine(
Some(&raw_event(json!({ "unsigned": { "membership": "invite" } }),)),
&UnableToDecryptInfo {
session_id: None,
reason: UnableToDecryptReason::MissingMegolmSession
}
),
UtdCause::Unknown
);
}
Expand All @@ -121,7 +153,13 @@ mod tests {
// If membership=join then we expected to be sent the keys so the cause of the
// UTD is unknown.
assert_eq!(
UtdCause::determine(Some(&raw_event(json!({ "unsigned": { "membership": "join" } })))),
UtdCause::determine(
Some(&raw_event(json!({ "unsigned": { "membership": "join" } }))),
&UnableToDecryptInfo {
session_id: None,
reason: UnableToDecryptReason::MissingMegolmSession
}
),
UtdCause::Unknown
);
}
Expand All @@ -131,7 +169,13 @@ mod tests {
// If membership=leave then we have an explanation for why we can't decrypt,
// until we have MSC3061.
assert_eq!(
UtdCause::determine(Some(&raw_event(json!({ "unsigned": { "membership": "leave" } })))),
UtdCause::determine(
Some(&raw_event(json!({ "unsigned": { "membership": "leave" } }))),
&UnableToDecryptInfo {
session_id: None,
reason: UnableToDecryptReason::MissingMegolmSession
}
),
UtdCause::SentBeforeWeJoined
);
}
Expand All @@ -140,9 +184,15 @@ mod tests {
fn if_unstable_prefix_membership_is_leave_we_guess_membership() {
// Before MSC4115 is merged, we support the unstable prefix too.
assert_eq!(
UtdCause::determine(Some(&raw_event(
json!({ "unsigned": { "io.element.msc4115.membership": "leave" } })
))),
UtdCause::determine(
Some(&raw_event(
json!({ "unsigned": { "io.element.msc4115.membership": "leave" } })
)),
&UnableToDecryptInfo {
session_id: None,
reason: UnableToDecryptReason::MissingMegolmSession
}
),
UtdCause::SentBeforeWeJoined
);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/matrix-sdk-ui/src/timeline/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,10 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
}
},

TimelineEventKind::UnableToDecrypt { content, .. } => {
TimelineEventKind::UnableToDecrypt { content, unable_to_decrypt_info } => {
// TODO: Handle replacements if the replaced event is also UTD
let raw_event = self.ctx.flow.raw_event();
let cause = UtdCause::determine(raw_event);
let cause = UtdCause::determine(raw_event, &unable_to_decrypt_info);
self.add_item(TimelineItemContent::unable_to_decrypt(content, cause), None);

// Let the hook know that we ran into an unable-to-decrypt that is added to the
Expand Down

0 comments on commit 7278319

Please sign in to comment.