From 9d0085d4dd0ab6e294725330aadccf8c5b246927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 23 Mar 2021 14:00:20 +0100 Subject: [PATCH] matrix-sdk: Add the Client level room send method back --- matrix_sdk/src/client.rs | 69 ++++++++++++++++++++++++++++++++++- matrix_sdk/src/room/common.rs | 10 +++-- matrix_sdk/src/room/mod.rs | 5 +-- 3 files changed, 75 insertions(+), 9 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 8f7da8f9..9307dd66 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -40,7 +40,8 @@ use tracing::{debug, warn}; use tracing::{error, info, instrument}; use matrix_sdk_base::{ - deserialized_responses::SyncResponse, BaseClient, BaseClientConfig, Session, Store, + deserialized_responses::SyncResponse, events::AnyMessageEventContent, BaseClient, + BaseClientConfig, Session, Store, }; #[cfg(feature = "encryption")] @@ -1002,6 +1003,72 @@ impl Client { Ok(self.http_client.upload(request, Some(timeout)).await?) } + /// Send a room message to a room. + /// + /// Returns the parsed response from the server. + /// + /// If the encryption feature is enabled this method will transparently + /// encrypt the room message if this room is encrypted. + /// + /// **Note**: This method will send an unencrypted message if the room cannot + /// be found in the store, prefer the higher level + /// [send()](room::Joined::send()) method that can be found for the + /// [Joined](room::Joined) room struct to avoid this. + /// + /// # Arguments + /// + /// * `room_id` - The unique id of the room. + /// + /// * `content` - The content of the message event. + /// + /// * `txn_id` - A unique `Uuid` that can be attached to a `MessageEvent` + /// held in its unsigned field as `transaction_id`. If not given one is + /// created for the message. + /// + /// # Example + /// ```no_run + /// # use std::sync::{Arc, RwLock}; + /// # use matrix_sdk::{Client, SyncSettings}; + /// # use url::Url; + /// # use futures::executor::block_on; + /// # use matrix_sdk::identifiers::room_id; + /// # use std::convert::TryFrom; + /// use matrix_sdk::events::{ + /// AnyMessageEventContent, + /// room::message::{MessageEventContent, TextMessageEventContent}, + /// }; + /// # block_on(async { + /// # let homeserver = Url::parse("http://localhost:8080").unwrap(); + /// # let mut client = Client::new(homeserver).unwrap(); + /// # let room_id = room_id!("!test:localhost"); + /// use matrix_sdk_common::uuid::Uuid; + /// + /// let content = AnyMessageEventContent::RoomMessage( + /// MessageEventContent::text_plain("Hello world") + /// ); + /// + /// let txn_id = Uuid::new_v4(); + /// client.send(room_id, content, Some(txn_id)).await.unwrap(); + /// # }) + /// ``` + pub async fn room_send( + &self, + room_id: &RoomId, + content: impl Into, + txn_id: Option, + ) -> Result { + #[cfg(feature = "encryption")] + if let Some(room) = self.get_joined_room(room_id) { + room.send(content, txn_id).await + } else { + let content = content.into(); + let txn_id = txn_id.unwrap_or_else(Uuid::new_v4).to_string(); + let request = send_message_event::Request::new(room_id, &txn_id, &content); + + self.send(request, None).await + } + } + /// Send an arbitrary request to the server, without updating client state. /// /// **Warning:** Because this method *does not* update the client state, it is diff --git a/matrix_sdk/src/room/common.rs b/matrix_sdk/src/room/common.rs index 46bff014..f224eb6d 100644 --- a/matrix_sdk/src/room/common.rs +++ b/matrix_sdk/src/room/common.rs @@ -1,8 +1,10 @@ -use matrix_sdk_common::api::r0::{ - membership::{get_member_events, join_room_by_id, leave_room}, - message::get_message_events, +use matrix_sdk_common::{ + api::r0::{ + membership::{get_member_events, join_room_by_id, leave_room}, + message::get_message_events, + }, + locks::Mutex, }; -use matrix_sdk_common::locks::Mutex; use std::{ops::Deref, sync::Arc}; diff --git a/matrix_sdk/src/room/mod.rs b/matrix_sdk/src/room/mod.rs index ee603cd9..6ae5812b 100644 --- a/matrix_sdk/src/room/mod.rs +++ b/matrix_sdk/src/room/mod.rs @@ -7,10 +7,7 @@ mod invited; mod joined; mod left; -pub use self::common::Common; -pub use self::invited::Invited; -pub use self::joined::Joined; -pub use self::left::Left; +pub use self::{common::Common, invited::Invited, joined::Joined, left::Left}; /// An enum that abstracts over the different states a room can be in. #[derive(Debug, Clone)]