Skip to content

Commit

Permalink
fix(sdk): when an encryption event is received, mark the room encryp…
Browse files Browse the repository at this point in the history
…tion as set #3602

RoomInfo::handle_state_event(...) will check this condition so we don't have to later ask the HS whether the room is encrypted or not through room::is_encrypted().

Also, as we need some way to get this info in the room list in our clients, two ways of doing this were added the FFI crate, one through RoomInfo and another one through RoomListItem.
  • Loading branch information
poljar authored Jun 25, 2024
2 parents 0701c7d + 0162f6b commit 0ab0678
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
2 changes: 2 additions & 0 deletions bindings/matrix-sdk-ffi/src/room_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct RoomInfo {
is_space: bool,
is_tombstoned: bool,
is_favourite: bool,
is_encrypted: bool,
canonical_alias: Option<String>,
alternative_aliases: Vec<String>,
membership: Membership,
Expand Down Expand Up @@ -76,6 +77,7 @@ impl RoomInfo {
is_space: room.is_space(),
is_tombstoned: room.is_tombstoned(),
is_favourite: room.is_favourite(),
is_encrypted: room.is_encrypted().await.unwrap_or(false),
canonical_alias: room.canonical_alias().map(Into::into),
alternative_aliases: room.alt_aliases().into_iter().map(Into::into).collect(),
membership: room.state().into(),
Expand Down
8 changes: 8 additions & 0 deletions bindings/matrix-sdk-ffi/src/room_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,14 @@ impl RoomListItem {
self.inner.init_timeline_with_builder(timeline_builder).map_err(RoomListError::from).await
}

/// Checks whether the room is encrypted or not.
///
/// **Note**: this info may not be reliable if you don't set up
/// `m.room.encryption` as required state.
async fn is_encrypted(&self) -> bool {
self.inner.is_encrypted().await.unwrap_or(false)
}

fn subscribe(&self, settings: Option<RoomSubscription>) {
self.inner.subscribe(settings.map(Into::into));
}
Expand Down
46 changes: 43 additions & 3 deletions crates/matrix-sdk-base/src/rooms/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,18 @@ impl RoomInfo {
///
/// Returns true if the event modified the info, false otherwise.
pub fn handle_state_event(&mut self, event: &AnySyncStateEvent) -> bool {
self.base_info.handle_state_event(event)
let ret = self.base_info.handle_state_event(event);

// If we received an `m.room.encryption` event here, and encryption got enabled,
// then we can be certain that we have synced the encryption state event, so
// mark it here as synced.
if let AnySyncStateEvent::RoomEncryption(_) = event {
if self.is_encrypted() {
self.mark_encryption_state_synced();
}
}

ret
}

/// Handle the given stripped state event.
Expand Down Expand Up @@ -1524,17 +1535,19 @@ mod tests {
},
room::{
canonical_alias::RoomCanonicalAliasEventContent,
encryption::{OriginalSyncRoomEncryptionEvent, RoomEncryptionEventContent},
member::{
MembershipState, RoomMemberEventContent, StrippedRoomMemberEvent,
SyncRoomMemberEvent,
},
name::RoomNameEventContent,
},
AnySyncStateEvent, StateEventType, StateUnsigned, SyncStateEvent,
AnySyncStateEvent, EmptyStateKey, StateEventType, StateUnsigned, SyncStateEvent,
},
room_alias_id, room_id,
serde::Raw,
user_id, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId, UserId,
user_id, EventEncryptionAlgorithm, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId,
UserId,
};
use serde_json::json;
use stream_assert::{assert_pending, assert_ready};
Expand Down Expand Up @@ -2532,4 +2545,31 @@ mod tests {
actual = compute_display_name_from_heroes(1, 0, vec!["a", "b", "c"]);
assert_eq!(DisplayName::EmptyWas("a, b, c".to_owned()), actual);
}

#[test]
fn test_encryption_is_set_when_encryption_event_is_received() {
let (_store, room) = make_room_test_helper(RoomState::Joined);

assert!(room.is_encryption_state_synced().not());
assert!(room.is_encrypted().not());

let encryption_content =
RoomEncryptionEventContent::new(EventEncryptionAlgorithm::MegolmV1AesSha2);
let encryption_event = AnySyncStateEvent::RoomEncryption(SyncStateEvent::Original(
OriginalSyncRoomEncryptionEvent {
content: encryption_content,
event_id: OwnedEventId::from_str("$1234_1").unwrap(),
sender: ALICE.to_owned(),
// we can simply use now here since this will be dropped when using a
// MinimalStateEvent in the roomInfo
origin_server_ts: timestamp(0),
state_key: EmptyStateKey,
unsigned: StateUnsigned::new(),
},
));
receive_state_events(&room, vec![&encryption_event]);

assert!(room.is_encryption_state_synced());
assert!(room.is_encrypted());
}
}

0 comments on commit 0ab0678

Please sign in to comment.