Skip to content

Commit fffdd34

Browse files
authored
Merge pull request #5559 from matrix-org/kaylendog/encrypt-poll-sync
This fixes a small issue encountered while developing the integration test for encrypted state events. If the first sync response received from the server after enabling encryption does not contain the m.room.encryption event, the client will report the encryption state as unknown, even if the next sync response does contain the event. - [x] Modify Room::enable_encryption_inner to spin on the room encryption state, rather than bailing after the first sync response is received. Signed-off-by: Skye Elliot
2 parents b6433de + 24502d2 commit fffdd34

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

crates/matrix-sdk/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file.
88

99
### Features
1010

11+
- `Room::enable_encryption` and `Room::enable_encryption_with_state_event_encryption` will poll
12+
the encryption state for up to 3 seconds, rather than checking once after a single sync has
13+
completed.
14+
([#5559](https://github.com/matrix-org/matrix-rust-sdk/pull/5559))
1115
- Add `Room::enable_encryption_with_state` to enable E2E encryption with encrypted state event
1216
support, gated behind the `experimental-encrypted-state-events` feature.
1317
([#5557](https://github.com/matrix-org/matrix-rust-sdk/pull/5557))

crates/matrix-sdk/src/room/mod.rs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,29 +1906,62 @@ impl Room {
19061906
}
19071907
self.send_state_event(content).await?;
19081908

1909+
// Spin on the sync beat event, since the first sync we receive might not
1910+
// include the encryption event.
1911+
//
19091912
// TODO do we want to return an error here if we time out? This
19101913
// could be quite useful if someone wants to enable encryption and
19111914
// send a message right after it's enabled.
1912-
_ = timeout(self.client.inner.sync_beat.listen(), SYNC_WAIT_TIME).await;
1915+
let res = timeout(
1916+
async {
1917+
loop {
1918+
// Listen for sync events, then check if the encryption state is known.
1919+
self.client.inner.sync_beat.listen().await;
1920+
let _sync_lock = self.client.base_client().sync_lock().lock().await;
1921+
if !self.inner.encryption_state().is_unknown() {
1922+
break;
1923+
}
1924+
}
1925+
},
1926+
SYNC_WAIT_TIME,
1927+
)
1928+
.await;
19131929

1914-
// If after waiting for a sync, we don't have the encryption state we expect,
1915-
// assume the local encryption state is incorrect; this will cause
1916-
// the SDK to re-request it later for confirmation, instead of
1917-
// assuming it's sync'd and correct (and not encrypted).
19181930
let _sync_lock = self.client.base_client().sync_lock().lock().await;
1919-
if !self.inner.encryption_state().is_encrypted() {
1920-
debug!("still not marked as encrypted, marking encryption state as missing");
19211931

1922-
let mut room_info = self.clone_info();
1923-
room_info.mark_encryption_state_missing();
1924-
let mut changes = StateChanges::default();
1925-
changes.add_room(room_info.clone());
1932+
// If encryption was enabled, return.
1933+
#[cfg(not(feature = "experimental-encrypted-state-events"))]
1934+
if res.is_ok() && self.inner.encryption_state().is_encrypted() {
1935+
debug!("room successfully marked as encrypted");
1936+
return Ok(());
1937+
}
19261938

1927-
self.client.state_store().save_changes(&changes).await?;
1928-
self.set_room_info(room_info, RoomInfoNotableUpdateReasons::empty());
1929-
} else {
1939+
// If encryption with state event encryption was enabled, return.
1940+
#[cfg(feature = "experimental-encrypted-state-events")]
1941+
if res.is_ok() && {
1942+
if encrypted_state_events {
1943+
self.inner.encryption_state().is_state_encrypted()
1944+
} else {
1945+
self.inner.encryption_state().is_encrypted()
1946+
}
1947+
} {
19301948
debug!("room successfully marked as encrypted");
1949+
return Ok(());
19311950
}
1951+
1952+
// If after waiting for multiple syncs, we don't have the encryption state we
1953+
// expect, assume the local encryption state is incorrect; this will
1954+
// cause the SDK to re-request it later for confirmation, instead of
1955+
// assuming it's sync'd and correct (and not encrypted).
1956+
debug!("still not marked as encrypted, marking encryption state as missing");
1957+
1958+
let mut room_info = self.clone_info();
1959+
room_info.mark_encryption_state_missing();
1960+
let mut changes = StateChanges::default();
1961+
changes.add_room(room_info.clone());
1962+
1963+
self.client.state_store().save_changes(&changes).await?;
1964+
self.set_room_info(room_info, RoomInfoNotableUpdateReasons::empty());
19321965
}
19331966

19341967
Ok(())

0 commit comments

Comments
 (0)