refactor: Adjust create_dm_room to new room API

This commit is contained in:
Flix
2022-11-02 10:24:57 +01:00
committed by Damir Jelić
parent 956e270941
commit be7c3239a8
3 changed files with 5 additions and 62 deletions

View File

@@ -1613,46 +1613,6 @@ impl Client {
Ok(room::Joined::new(self, base_room).unwrap())
}
/// Create a direct message room with the specified user.
#[cfg(not(feature = "e2e-encryption"))]
pub async fn create_dm_room(&self, user_id: &UserId) -> Result<room::Joined> {
use ruma::{
api::client::room::create_room::v3::RoomPreset, events::direct::DirectEventContent,
};
// First we create the DM room, where we invite the user and tell the
// invitee that the room should be a DM.
let invite = &[user_id.to_owned()];
let request = assign!(ruma::api::client::room::create_room::v3::Request::new(), {
invite,
is_direct: true,
preset: Some(RoomPreset::TrustedPrivateChat),
});
let room = self.create_room(request).await?;
// Now we need to mark the room as a DM for ourselves, we fetch the
// existing `m.direct` event and append the room to the list of DMs we
// have with this user.
let mut content = self
.account()
.account_data::<DirectEventContent>()
.await?
.map(|c| c.deserialize())
.transpose()?
.unwrap_or_default();
content.entry(user_id.to_owned()).or_default().push(room.room_id().to_owned());
// TODO We should probably save the fact that we need to send this out
// because otherwise we might end up in a state where we have a DM that
// isn't marked as one.
self.account().set_account_data(content).await?;
Ok(room)
}
/// Search the homeserver's directory for public rooms with a filter.
///
/// # Arguments

View File

@@ -475,12 +475,8 @@ impl OtherUserIdentity {
room.invite_user_by_id(self.inner.user_id()).await?;
}
room.clone()
} else if let Some(room) =
self.client.create_dm_room(self.inner.user_id().to_owned()).await?
{
room
} else {
return Err(RequestVerificationError::RoomCreation(self.inner.user_id().to_owned()));
self.client.create_dm_room(self.inner.user_id().to_owned()).await?
};
let response = room

View File

@@ -38,7 +38,6 @@ pub use matrix_sdk_base::crypto::{
use matrix_sdk_base::crypto::{
CrossSigningStatus, OutgoingRequest, RoomMessageRequest, ToDeviceRequest,
};
use matrix_sdk_common::instant::Duration;
#[cfg(feature = "e2e-encryption")]
use ruma::OwnedDeviceId;
use ruma::{
@@ -227,16 +226,11 @@ impl Client {
}
#[cfg(feature = "e2e-encryption")]
pub(crate) async fn create_dm_room(
&self,
user_id: OwnedUserId,
) -> Result<Option<room::Joined>> {
pub(crate) async fn create_dm_room(&self, user_id: OwnedUserId) -> Result<room::Joined> {
use ruma::{
api::client::room::create_room::v3::RoomPreset, events::direct::DirectEventContent,
};
const SYNC_WAIT_TIME: Duration = Duration::from_secs(3);
// First we create the DM room, where we invite the user and tell the
// invitee that the room should be a DM.
let invite = &[user_id.clone()];
@@ -247,7 +241,7 @@ impl Client {
preset: Some(RoomPreset::TrustedPrivateChat),
});
let response = self.send(request, None).await?;
let room = self.create_room(request).await?;
// Now we need to mark the room as a DM for ourselves, we fetch the
// existing `m.direct` event and append the room to the list of DMs we
@@ -260,21 +254,14 @@ impl Client {
.transpose()?
.unwrap_or_default();
content.entry(user_id.to_owned()).or_default().push(response.room_id.to_owned());
content.entry(user_id.to_owned()).or_default().push(room.room_id().to_owned());
// TODO We should probably save the fact that we need to send this out
// because otherwise we might end up in a state where we have a DM that
// isn't marked as one.
self.account().set_account_data(content).await?;
// If the room is already in our store, fetch it, otherwise wait for a
// sync to be done which should put the room into our store.
if let Some(room) = self.get_joined_room(&response.room_id) {
Ok(Some(room))
} else {
self.inner.sync_beat.listen().wait_timeout(SYNC_WAIT_TIME);
Ok(self.get_joined_room(&response.room_id))
}
Ok(room)
}
/// Claim one-time keys creating new Olm sessions.