Skip to content

Commit 9601624

Browse files
kaylendogHywan
authored andcommitted
feat(base): Add EncryptionState::StateEncrypted
Signed-off-by: Skye Elliot <[email protected]>
1 parent 1ea2162 commit 9601624

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
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/src/room/encryption.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ pub enum EncryptionState {
3636
/// The room is encrypted.
3737
Encrypted,
3838

39+
/// The room is encrypted, additionally requiring state events to be
40+
/// encrypted.
41+
#[cfg(feature = "experimental-encrypted-state-events")]
42+
StateEncrypted,
43+
3944
/// The room is not encrypted.
4045
NotEncrypted,
4146

@@ -46,10 +51,25 @@ pub enum EncryptionState {
4651

4752
impl EncryptionState {
4853
/// Check whether `EncryptionState` is [`Encrypted`][Self::Encrypted].
54+
#[cfg(not(feature = "experimental-encrypted-state-events"))]
4955
pub fn is_encrypted(&self) -> bool {
5056
matches!(self, Self::Encrypted)
5157
}
5258

59+
/// Check whether `EncryptionState` is [`Encrypted`][Self::Encrypted] or
60+
/// [`StateEncrypted`][Self::StateEncrypted].
61+
#[cfg(feature = "experimental-encrypted-state-events")]
62+
pub fn is_encrypted(&self) -> bool {
63+
matches!(self, Self::Encrypted | Self::StateEncrypted)
64+
}
65+
66+
/// Check whether `EncryptionState` is
67+
/// [`StateEncrypted`][Self::StateEncrypted].
68+
#[cfg(feature = "experimental-encrypted-state-events")]
69+
pub fn is_state_encrypted(&self) -> bool {
70+
matches!(self, Self::StateEncrypted)
71+
}
72+
5373
/// Check whether `EncryptionState` is [`Unknown`][Self::Unknown].
5474
pub fn is_unknown(&self) -> bool {
5575
matches!(self, Self::Unknown)
@@ -155,5 +175,16 @@ mod tests {
155175
assert!(EncryptionState::Unknown.is_encrypted().not());
156176
assert!(EncryptionState::Encrypted.is_encrypted());
157177
assert!(EncryptionState::NotEncrypted.is_encrypted().not());
178+
179+
#[cfg(feature = "experimental-encrypted-state-events")]
180+
{
181+
assert!(EncryptionState::StateEncrypted.is_unknown().not());
182+
assert!(EncryptionState::StateEncrypted.is_encrypted());
183+
184+
assert!(EncryptionState::Unknown.is_state_encrypted().not());
185+
assert!(EncryptionState::Encrypted.is_state_encrypted().not());
186+
assert!(EncryptionState::StateEncrypted.is_state_encrypted());
187+
assert!(EncryptionState::NotEncrypted.is_state_encrypted().not());
188+
}
158189
}
159190
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ impl RoomInfo {
616616
}
617617

618618
/// Returns the encryption state of this room.
619+
#[cfg(not(feature = "experimental-encrypted-state-events"))]
619620
pub fn encryption_state(&self) -> EncryptionState {
620621
if !self.encryption_state_synced {
621622
EncryptionState::Unknown
@@ -626,6 +627,26 @@ impl RoomInfo {
626627
}
627628
}
628629

630+
/// Returns the encryption state of this room.
631+
#[cfg(feature = "experimental-encrypted-state-events")]
632+
pub fn encryption_state(&self) -> EncryptionState {
633+
if !self.encryption_state_synced {
634+
EncryptionState::Unknown
635+
} else {
636+
self.base_info
637+
.encryption
638+
.as_ref()
639+
.map(|state| {
640+
if state.encrypt_state_events {
641+
EncryptionState::StateEncrypted
642+
} else {
643+
EncryptionState::Encrypted
644+
}
645+
})
646+
.unwrap_or(EncryptionState::NotEncrypted)
647+
}
648+
}
649+
629650
/// Set the encryption event content in this room.
630651
pub fn set_encryption_event(&mut self, event: Option<RoomEncryptionEventContent>) {
631652
self.base_info.encryption = event;

0 commit comments

Comments
 (0)