client: Create DM as encrypted by default

If `e2e-encryption` feature is enabled.

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille
2023-12-18 13:04:39 +01:00
parent 0f6efc391a
commit 738f7f0336
4 changed files with 21 additions and 25 deletions
+14 -19
View File
@@ -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<Room> {
let mut request = assign!(create_room::v3::Request::new(), {
pub async fn create_dm(&self, user_id: &UserId) -> Result<Room> {
#[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
}
@@ -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
+1 -1
View File
@@ -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::{
@@ -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);