From 738f7f03364c8803cd5018bfe4009dbda0fa4b8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Mon, 18 Dec 2023 13:04:39 +0100 Subject: [PATCH] client: Create DM as encrypted by default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If `e2e-encryption` feature is enabled. Signed-off-by: Kévin Commaille --- crates/matrix-sdk/src/client/mod.rs | 33 ++++++++----------- .../src/encryption/identities/users.rs | 4 +-- crates/matrix-sdk/src/lib.rs | 2 +- crates/matrix-sdk/tests/integration/client.rs | 7 ++-- 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/crates/matrix-sdk/src/client/mod.rs b/crates/matrix-sdk/src/client/mod.rs index bb2aea917..dbf94e83a 100644 --- a/crates/matrix-sdk/src/client/mod.rs +++ b/crates/matrix-sdk/src/client/mod.rs @@ -139,15 +139,6 @@ pub enum SessionChange { TokensRefreshed, } -/// Whether a room should be encrypted. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Encrypted { - /// The room should be encrypted. - Yes, - /// The room should not be encrypted. - No, -} - /// An async/await enabled Matrix client. /// /// All of the state is held in an `Arc` so the `Client` can be cloned freely. @@ -1199,24 +1190,28 @@ impl Client { /// given user being invited, the room marked `is_direct` and both the /// creator and invitee getting the default maximum power level. /// + /// If the `e2e-encryption` feature is enabled, the room will also be + /// encrypted. + /// /// # Arguments /// /// * `user_id` - The ID of the user to create a DM for. - /// * `encrypted` - Whether the DM should be encrypted. - pub async fn create_dm(&self, user_id: &UserId, encrypted: Encrypted) -> Result { - let mut request = assign!(create_room::v3::Request::new(), { + pub async fn create_dm(&self, user_id: &UserId) -> Result { + #[cfg(feature = "e2e-encryption")] + let initial_state = + vec![InitialStateEvent::new(RoomEncryptionEventContent::with_recommended_defaults()) + .to_raw_any()]; + + #[cfg(not(feature = "e2e-encryption"))] + let initial_state = vec![]; + + let request = assign!(create_room::v3::Request::new(), { invite: vec![user_id.to_owned()], is_direct: true, preset: Some(create_room::v3::RoomPreset::TrustedPrivateChat), + initial_state, }); - if encrypted == Encrypted::Yes { - request.initial_state.push( - InitialStateEvent::new(RoomEncryptionEventContent::with_recommended_defaults()) - .to_raw_any(), - ); - }; - self.create_room(request).await } diff --git a/crates/matrix-sdk/src/encryption/identities/users.rs b/crates/matrix-sdk/src/encryption/identities/users.rs index 4f6d9b6b2..ac167f267 100644 --- a/crates/matrix-sdk/src/encryption/identities/users.rs +++ b/crates/matrix-sdk/src/encryption/identities/users.rs @@ -30,7 +30,7 @@ use ruma::{ }; use super::{ManualVerifyError, RequestVerificationError}; -use crate::{encryption::verification::VerificationRequest, Client, Encrypted}; +use crate::{encryption::verification::VerificationRequest, Client}; /// Updates about [`UserIdentity`]s which got received over the `/keys/query` /// endpoint. @@ -507,7 +507,7 @@ impl OtherUserIdentity { } room.clone() } else { - self.client.create_dm(self.inner.user_id(), Encrypted::Yes).await? + self.client.create_dm(self.inner.user_id()).await? }; let response = room diff --git a/crates/matrix-sdk/src/lib.rs b/crates/matrix-sdk/src/lib.rs index 0e50fde1a..138a9c1de 100644 --- a/crates/matrix-sdk/src/lib.rs +++ b/crates/matrix-sdk/src/lib.rs @@ -61,7 +61,7 @@ pub mod widget; pub use account::Account; pub use authentication::{AuthApi, AuthSession, SessionTokens}; -pub use client::{Client, ClientBuildError, ClientBuilder, Encrypted, LoopCtrl, SessionChange}; +pub use client::{Client, ClientBuildError, ClientBuilder, LoopCtrl, SessionChange}; #[cfg(feature = "image-proc")] pub use error::ImageError; pub use error::{ diff --git a/crates/matrix-sdk/tests/integration/client.rs b/crates/matrix-sdk/tests/integration/client.rs index bd4646579..7690bc516 100644 --- a/crates/matrix-sdk/tests/integration/client.rs +++ b/crates/matrix-sdk/tests/integration/client.rs @@ -6,7 +6,6 @@ use matrix_sdk::{ config::SyncSettings, media::{MediaFormat, MediaRequest, MediaThumbnailSize}, sync::RoomUpdate, - Encrypted, }; use matrix_sdk_base::RoomState; use matrix_sdk_test::{async_test, test_json, DEFAULT_TEST_ROOM_ID}; @@ -726,6 +725,7 @@ async fn test_encrypt_room_event() { ); } +#[cfg(not(feature = "e2e-encryption"))] #[async_test] async fn create_dm_non_encrypted() { let (client, server) = logged_in_client().await; @@ -774,6 +774,7 @@ async fn create_dm_non_encrypted() { client.create_dm(user_id, Encrypted::No).await.unwrap(); } +#[cfg(feature = "e2e-encryption")] #[async_test] async fn create_dm_encrypted() { let (client, server) = logged_in_client().await; @@ -833,7 +834,7 @@ async fn create_dm_encrypted() { .mount(&server) .await; - client.create_dm(user_id, Encrypted::Yes).await.unwrap(); + client.create_dm(user_id).await.unwrap(); } #[async_test] @@ -842,7 +843,7 @@ async fn create_dm_error() { let user_id = user_id!("@invitee:localhost"); // The endpoint is not mocked so we encounter a 404. - let error = client.create_dm(user_id, Encrypted::No).await.unwrap_err(); + let error = client.create_dm(user_id).await.unwrap_err(); let client_api_error = error.as_client_api_error().unwrap(); assert_eq!(client_api_error.status_code, 404);