From d8b60dfe55a082b5bb4da425c106d6560669c5ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 14 Sep 2021 17:54:24 +0200 Subject: [PATCH] feat(sdk): Add support to send out custom state events --- crates/matrix-sdk/src/room/joined.rs | 59 +++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/crates/matrix-sdk/src/room/joined.rs b/crates/matrix-sdk/src/room/joined.rs index 4fe0a6e2..c12ed9f2 100644 --- a/crates/matrix-sdk/src/room/joined.rs +++ b/crates/matrix-sdk/src/room/joined.rs @@ -41,8 +41,10 @@ use ruma::{ AnyMessageEventContent, AnyStateEventContent, }, receipt::ReceiptType, + serde::Raw, EventId, UserId, }; +use serde_json::Value; #[cfg(feature = "encryption")] use tracing::instrument; @@ -526,13 +528,16 @@ impl Joined { /// /// # Arguments /// - /// * `room_id` - The id of the room that should receive the message. - /// /// * `content` - The content of the state event. /// /// * `state_key` - A unique key which defines the overwriting semantics for /// this piece of room state. This value is often a zero-length string. /// + /// **Note**: To send custom events use the raw variant of this method, + /// [`send_state_event_raw()`]. + /// + /// [`send_state_event_raw()`]: #method.send_state_event_raw + /// /// # Example /// /// ```no_run @@ -570,6 +575,56 @@ impl Joined { self.client.send(request, None).await } + /// Send a raw room state event to the homeserver. + /// + /// Returns the parsed response from the server. + /// + /// # Arguments + /// + /// * `content` - The raw content of the state event. + /// + /// * `event_type` - The type of the event that we're sending out. + /// + /// * `state_key` - A unique key which defines the overwriting semantics for + /// this piece of room state. This value is often a zero-length string. + /// + /// # Example + /// + /// ```no_run + /// use serde_json::json; + /// + /// # futures::executor::block_on(async { + /// # let homeserver = url::Url::parse("http://localhost:8080")?; + /// # let mut client = matrix_sdk::Client::new(homeserver)?; + /// # let room_id = matrix_sdk::ruma::room_id!("!test:localhost"); + /// let content = json!({ + /// "avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF", + /// "displayname": "Alice Margatroid", + /// "membership": "join" + /// }); + /// + /// if let Some(room) = client.get_joined_room(&room_id) { + /// room.send_state_event_raw(content, "m.room.member", "").await?; + /// } + /// # anyhow::Result::<()>::Ok(()) }); + /// ``` + pub async fn send_state_event_raw( + &self, + content: Value, + event_type: &str, + state_key: &str, + ) -> Result { + let content = Raw::from_json(serde_json::value::to_raw_value(&content)?); + let request = send_state_event::Request::new_raw( + self.inner.room_id(), + event_type, + state_key, + content, + ); + + Ok(self.client.send(request, None).await?) + } + /// Strips all information out of an event of the room. /// /// Returns the [`redact_event::Response`] from the server.