Skip to content

Commit 3d5d519

Browse files
committed
feat(sdk): Add Room::enable_encryption_with_state
Signed-off-by: Skye Elliot <[email protected]>
1 parent 4a8cbfe commit 3d5d519

File tree

1 file changed

+43
-0
lines changed
  • crates/matrix-sdk/src/room

1 file changed

+43
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,49 @@ impl Room {
19551955
Ok(())
19561956
}
19571957

1958+
/// Enable End-to-end encryption in this room, with experimental encrypted
1959+
/// state events.
1960+
#[instrument(skip_all)]
1961+
pub async fn enable_encryption_with_state(&self) -> Result<()> {
1962+
use ruma::{
1963+
events::room::encryption::RoomEncryptionEventContent, EventEncryptionAlgorithm,
1964+
};
1965+
const SYNC_WAIT_TIME: Duration = Duration::from_secs(3);
1966+
1967+
if !self.latest_encryption_state().await?.is_encrypted() {
1968+
let content =
1969+
RoomEncryptionEventContent::new(EventEncryptionAlgorithm::MegolmV1AesSha2)
1970+
.with_encrypted_state();
1971+
self.send_state_event(content).await?;
1972+
1973+
// TODO do we want to return an error here if we time out? This
1974+
// could be quite useful if someone wants to enable encryption and
1975+
// send a message right after it's enabled.
1976+
_ = timeout(self.client.inner.sync_beat.listen(), SYNC_WAIT_TIME).await;
1977+
1978+
// If after waiting for a sync, we don't have the encryption state we expect,
1979+
// assume the local encryption state is incorrect; this will cause
1980+
// the SDK to re-request it later for confirmation, instead of
1981+
// assuming it's sync'd and correct (and not encrypted).
1982+
let _sync_lock = self.client.base_client().sync_lock().lock().await;
1983+
if !self.inner.encryption_state().is_state_encrypted() {
1984+
debug!("still not marked as encrypted, marking encryption state as missing");
1985+
1986+
let mut room_info = self.clone_info();
1987+
room_info.mark_encryption_state_missing();
1988+
let mut changes = StateChanges::default();
1989+
changes.add_room(room_info.clone());
1990+
1991+
self.client.state_store().save_changes(&changes).await?;
1992+
self.set_room_info(room_info, RoomInfoNotableUpdateReasons::empty());
1993+
} else {
1994+
debug!("room successfully marked as encrypted");
1995+
}
1996+
}
1997+
1998+
Ok(())
1999+
}
2000+
19582001
/// Share a room key with users in the given room.
19592002
///
19602003
/// This will create Olm sessions with all the users/device pairs in the

0 commit comments

Comments
 (0)