Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk): when an encryption event is received, mark the room encryption as set #3602

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a doc comment, documentation is nowadays visible over the FFI, also please document that the correctness of this might depend on specifying m.room.encryption as required state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if 0162f6b works for you.

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());
}
}
Loading