Skip to content

Commit 7345c02

Browse files
committed
feat(base): Add EncryptionState::Encrypted { encrypt_state_events } flag
Add `EncryptionState::is_state_encrypted()`, similar to `::is_encrypted()` but requires the `encrypt_state_events` flag is false. Signed-off-by: Skye Elliot <[email protected]>
1 parent 247ec1d commit 7345c02

File tree

6 files changed

+46
-13
lines changed

6 files changed

+46
-13
lines changed

crates/matrix-sdk-base/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ All notable changes to this project will be documented in this file.
77
## [Unreleased] - ReleaseDate
88

99
### Features
10+
11+
- Add `EncryptionState::StateEncrypted` to represent rooms supporting encrypted
12+
state events. Feature-gated behind `experimental-encrypted-state-events`.
13+
([#5523](https://github.com/matrix-org/matrix-rust-sdk/pull/5523))
1014
- [**breaking**] The `state` field of `JoinedRoomUpdate` and `LeftRoomUpdate`
1115
now uses the `State` enum, depending on whether the state changes were
1216
received in the `state` field or the `state_after` field.

crates/matrix-sdk-base/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ automatic-room-key-forwarding = [
3131
experimental-send-custom-to-device = [
3232
"matrix-sdk-crypto?/experimental-send-custom-to-device",
3333
]
34+
experimental-encrypted-state-events = [
35+
"ruma/unstable-msc3414",
36+
]
3437
uniffi = ["dep:uniffi", "matrix-sdk-crypto?/uniffi", "matrix-sdk-common/uniffi"]
3538

3639
# Private feature, see

crates/matrix-sdk-base/src/room/encryption.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ impl Room {
3333
#[derive(Debug)]
3434
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
3535
pub enum EncryptionState {
36-
/// The room is encrypted.
37-
Encrypted,
36+
/// The room is encrypted, possibly including state events.
37+
Encrypted {
38+
/// Determines whether experimental state encryption is enabled.
39+
encrypt_state_events: bool,
40+
},
3841

3942
/// The room is not encrypted.
4043
NotEncrypted,
@@ -47,7 +50,13 @@ pub enum EncryptionState {
4750
impl EncryptionState {
4851
/// Check whether `EncryptionState` is [`Encrypted`][Self::Encrypted].
4952
pub fn is_encrypted(&self) -> bool {
50-
matches!(self, Self::Encrypted)
53+
matches!(self, Self::Encrypted { .. })
54+
}
55+
56+
/// Check whether `EncryptionState` is [`Encrypted`][Self::Encrypted] with
57+
/// state encryption enabled.
58+
pub fn is_state_encrypted(&self) -> bool {
59+
matches!(self, Self::Encrypted { encrypt_state_events: true })
5160
}
5261

5362
/// Check whether `EncryptionState` is [`Unknown`][Self::Unknown].
@@ -129,7 +138,7 @@ mod tests {
129138
));
130139
receive_state_events(&room, vec![&encryption_event]);
131140

132-
assert_matches!(room.encryption_state(), EncryptionState::Encrypted);
141+
assert_matches!(room.encryption_state(), EncryptionState::Encrypted { .. });
133142
}
134143

135144
#[test]
@@ -149,11 +158,20 @@ mod tests {
149158
#[test]
150159
fn test_encryption_state() {
151160
assert!(EncryptionState::Unknown.is_unknown());
152-
assert!(EncryptionState::Encrypted.is_unknown().not());
161+
assert!(EncryptionState::Encrypted { encrypt_state_events: false }.is_unknown().not());
162+
assert!(EncryptionState::Encrypted { encrypt_state_events: true }.is_unknown().not());
153163
assert!(EncryptionState::NotEncrypted.is_unknown().not());
154164

155165
assert!(EncryptionState::Unknown.is_encrypted().not());
156-
assert!(EncryptionState::Encrypted.is_encrypted());
166+
assert!(EncryptionState::Encrypted { encrypt_state_events: false }.is_encrypted());
167+
assert!(EncryptionState::Encrypted { encrypt_state_events: true }.is_encrypted());
157168
assert!(EncryptionState::NotEncrypted.is_encrypted().not());
169+
170+
assert!(EncryptionState::Unknown.is_state_encrypted().not());
171+
assert!(
172+
EncryptionState::Encrypted { encrypt_state_events: false }.is_state_encrypted().not()
173+
);
174+
assert!(EncryptionState::Encrypted { encrypt_state_events: true }.is_state_encrypted());
175+
assert!(EncryptionState::NotEncrypted.is_state_encrypted().not());
158176
}
159177
}

crates/matrix-sdk-base/src/room/room_info.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,13 +616,21 @@ impl RoomInfo {
616616
}
617617

618618
/// Returns the encryption state of this room.
619+
#[allow(unused_variables)] // `state` only unused when state encryption disabled
619620
pub fn encryption_state(&self) -> EncryptionState {
620621
if !self.encryption_state_synced {
621622
EncryptionState::Unknown
622-
} else if self.base_info.encryption.is_some() {
623-
EncryptionState::Encrypted
624623
} else {
625-
EncryptionState::NotEncrypted
624+
self.base_info
625+
.encryption
626+
.as_ref()
627+
.map(|state| EncryptionState::Encrypted {
628+
#[cfg(not(feature = "experimental-encrypted-state-events"))]
629+
encrypt_state_events: false,
630+
#[cfg(feature = "experimental-encrypted-state-events")]
631+
encrypt_state_events: state.encrypt_state_events,
632+
})
633+
.unwrap_or(EncryptionState::NotEncrypted)
626634
}
627635
}
628636

crates/matrix-sdk-base/src/sliding_sync.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,11 +2437,11 @@ mod tests {
24372437
// They are both encrypted, yepee.
24382438
assert_matches!(
24392439
client.get_room(room_id_0).unwrap().encryption_state(),
2440-
EncryptionState::Encrypted
2440+
EncryptionState::Encrypted { .. }
24412441
);
24422442
assert_matches!(
24432443
client.get_room(room_id_1).unwrap().encryption_state(),
2444-
EncryptionState::Encrypted
2444+
EncryptionState::Encrypted { .. }
24452445
);
24462446
// This one is not encrypted because it has received nothing.
24472447
assert_matches!(
@@ -2492,7 +2492,7 @@ mod tests {
24922492
// is encrypted.
24932493
assert_matches!(
24942494
client.get_room(room_id_0).unwrap().encryption_state(),
2495-
EncryptionState::Encrypted
2495+
EncryptionState::Encrypted { .. }
24962496
);
24972497
// Unknown, because the absence of `m.room.encryption` when not requested
24982498
// means we don't know what the state is.

crates/matrix-sdk/tests/integration/room/joined.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,7 +1084,7 @@ async fn test_enable_encryption_doesnt_stay_unknown() {
10841084
assert_matches!(room.encryption_state(), EncryptionState::Unknown);
10851085

10861086
assert!(room.latest_encryption_state().await.unwrap().is_encrypted());
1087-
assert_matches!(room.encryption_state(), EncryptionState::Encrypted);
1087+
assert_matches!(room.encryption_state(), EncryptionState::Encrypted { .. });
10881088
}
10891089

10901090
#[async_test]

0 commit comments

Comments
 (0)