diff --git a/crates/matrix-sdk-base/CHANGELOG.md b/crates/matrix-sdk-base/CHANGELOG.md index 8676bb689db..9433c13b7af 100644 --- a/crates/matrix-sdk-base/CHANGELOG.md +++ b/crates/matrix-sdk-base/CHANGELOG.md @@ -7,6 +7,10 @@ All notable changes to this project will be documented in this file. ## [Unreleased] - ReleaseDate ### Features + +- Add `EncryptionState::StateEncrypted` to represent rooms supporting encrypted + state events. Feature-gated behind `experimental-encrypted-state-events`. + ([#5523](https://github.com/matrix-org/matrix-rust-sdk/pull/5523)) - [**breaking**] The `state` field of `JoinedRoomUpdate` and `LeftRoomUpdate` now uses the `State` enum, depending on whether the state changes were received in the `state` field or the `state_after` field. diff --git a/crates/matrix-sdk-base/src/room/encryption.rs b/crates/matrix-sdk-base/src/room/encryption.rs index eb8b20097b2..463fa15bf74 100644 --- a/crates/matrix-sdk-base/src/room/encryption.rs +++ b/crates/matrix-sdk-base/src/room/encryption.rs @@ -36,6 +36,11 @@ pub enum EncryptionState { /// The room is encrypted. Encrypted, + /// The room is encrypted, additionally requiring state events to be + /// encrypted. + #[cfg(feature = "experimental-encrypted-state-events")] + StateEncrypted, + /// The room is not encrypted. NotEncrypted, @@ -46,10 +51,25 @@ pub enum EncryptionState { impl EncryptionState { /// Check whether `EncryptionState` is [`Encrypted`][Self::Encrypted]. + #[cfg(not(feature = "experimental-encrypted-state-events"))] pub fn is_encrypted(&self) -> bool { matches!(self, Self::Encrypted) } + /// Check whether `EncryptionState` is [`Encrypted`][Self::Encrypted] or + /// [`StateEncrypted`][Self::StateEncrypted]. + #[cfg(feature = "experimental-encrypted-state-events")] + pub fn is_encrypted(&self) -> bool { + matches!(self, Self::Encrypted | Self::StateEncrypted) + } + + /// Check whether `EncryptionState` is + /// [`StateEncrypted`][Self::StateEncrypted]. + #[cfg(feature = "experimental-encrypted-state-events")] + pub fn is_state_encrypted(&self) -> bool { + matches!(self, Self::StateEncrypted) + } + /// Check whether `EncryptionState` is [`Unknown`][Self::Unknown]. pub fn is_unknown(&self) -> bool { matches!(self, Self::Unknown) @@ -155,5 +175,16 @@ mod tests { assert!(EncryptionState::Unknown.is_encrypted().not()); assert!(EncryptionState::Encrypted.is_encrypted()); assert!(EncryptionState::NotEncrypted.is_encrypted().not()); + + #[cfg(feature = "experimental-encrypted-state-events")] + { + assert!(EncryptionState::StateEncrypted.is_unknown().not()); + assert!(EncryptionState::StateEncrypted.is_encrypted()); + + assert!(EncryptionState::Unknown.is_state_encrypted().not()); + assert!(EncryptionState::Encrypted.is_state_encrypted().not()); + assert!(EncryptionState::StateEncrypted.is_state_encrypted()); + assert!(EncryptionState::NotEncrypted.is_state_encrypted().not()); + } } } diff --git a/crates/matrix-sdk-base/src/room/room_info.rs b/crates/matrix-sdk-base/src/room/room_info.rs index cc4b20f6866..46961fbc362 100644 --- a/crates/matrix-sdk-base/src/room/room_info.rs +++ b/crates/matrix-sdk-base/src/room/room_info.rs @@ -616,6 +616,7 @@ impl RoomInfo { } /// Returns the encryption state of this room. + #[cfg(not(feature = "experimental-encrypted-state-events"))] pub fn encryption_state(&self) -> EncryptionState { if !self.encryption_state_synced { EncryptionState::Unknown @@ -626,6 +627,26 @@ impl RoomInfo { } } + /// Returns the encryption state of this room. + #[cfg(feature = "experimental-encrypted-state-events")] + pub fn encryption_state(&self) -> EncryptionState { + if !self.encryption_state_synced { + EncryptionState::Unknown + } else { + self.base_info + .encryption + .as_ref() + .map(|state| { + if state.encrypt_state_events { + EncryptionState::StateEncrypted + } else { + EncryptionState::Encrypted + } + }) + .unwrap_or(EncryptionState::NotEncrypted) + } + } + /// Set the encryption event content in this room. pub fn set_encryption_event(&mut self, event: Option) { self.base_info.encryption = event;