Skip to content

Commit 360c2d7

Browse files
committed
refactor(crypto): Turn should_recalculate function into an associated function for SenderData
This allows us to use the function in more places where SenderData is used.
1 parent 683f0f4 commit 360c2d7

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

crates/matrix-sdk-crypto/src/machine/mod.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,26 +1790,7 @@ impl OlmMachine {
17901790
session: &InboundGroupSession,
17911791
sender: &UserId,
17921792
) -> MegolmResult<SenderData> {
1793-
/// Whether we should recalculate the Megolm sender's data, given the
1794-
/// current sender data. We only want to recalculate if it might
1795-
/// increase trust and allow us to decrypt messages that we
1796-
/// otherwise might refuse to decrypt.
1797-
///
1798-
/// We recalculate for all states except:
1799-
///
1800-
/// - SenderUnverified: the sender is trusted enough that we will
1801-
/// decrypt their messages in all cases, or
1802-
/// - SenderVerified: the sender is the most trusted they can be.
1803-
fn should_recalculate_sender_data(sender_data: &SenderData) -> bool {
1804-
matches!(
1805-
sender_data,
1806-
SenderData::UnknownDevice { .. }
1807-
| SenderData::DeviceInfo { .. }
1808-
| SenderData::VerificationViolation { .. }
1809-
)
1810-
}
1811-
1812-
let sender_data = if should_recalculate_sender_data(&session.sender_data) {
1793+
let sender_data = if session.sender_data.should_recalculate() {
18131794
// The session is not sure of the sender yet. Try to find a matching device
18141795
// belonging to the claimed sender of the recently-received event.
18151796
//

crates/matrix-sdk-crypto/src/olm/group_sessions/sender_data.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,25 @@ pub enum SenderData {
161161
}
162162

163163
impl SenderData {
164+
/// Whether we should recalculate the Megolm sender's data, given the
165+
/// current sender data. We only want to recalculate if it might
166+
/// increase trust and allow us to decrypt messages that we
167+
/// otherwise might refuse to decrypt.
168+
///
169+
/// We recalculate for all states except:
170+
///
171+
/// - SenderUnverified: the sender is trusted enough that we will decrypt
172+
/// their messages in all cases, or
173+
/// - SenderVerified: the sender is the most trusted they can be.
174+
pub fn should_recalculate(&self) -> bool {
175+
matches!(
176+
self,
177+
SenderData::UnknownDevice { .. }
178+
| SenderData::DeviceInfo { .. }
179+
| SenderData::VerificationViolation { .. }
180+
)
181+
}
182+
164183
/// Create a [`SenderData`] which contains no device info.
165184
pub fn unknown() -> Self {
166185
Self::UnknownDevice { legacy_session: false, owner_check_failed: false }
@@ -234,7 +253,7 @@ impl SenderData {
234253
let cross_signed = sender_device.is_cross_signed_by_owner();
235254

236255
if cross_signed {
237-
Self::from_cross_signed_device(sender_device)
256+
SenderData::from_cross_signed_device(sender_device)
238257
} else {
239258
// We have device keys, but they are not signed by the sender
240259
SenderData::device_info(sender_device.as_device_keys().clone())

0 commit comments

Comments
 (0)