From e175ab79c75dd5da549660542c72f553ed45aec0 Mon Sep 17 00:00:00 2001 From: Emelie Date: Mon, 9 Aug 2021 13:38:27 +0200 Subject: [PATCH] matrix-sdk: Add helpers for room tags --- matrix_sdk/src/room/joined.rs | 48 ++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/matrix_sdk/src/room/joined.rs b/matrix_sdk/src/room/joined.rs index 04d56a20..5de22dc6 100644 --- a/matrix_sdk/src/room/joined.rs +++ b/matrix_sdk/src/room/joined.rs @@ -25,6 +25,7 @@ use ruma::{ receipt::create_receipt, redact::redact_event, state::send_state_event, + tag::{create_tag, delete_tag}, typing::create_typing_event::{Request as TypingRequest, Typing}, }, assign, @@ -36,6 +37,7 @@ use ruma::{ }, EncryptedFile, }, + tag::TagInfo, AnyMessageEventContent, AnyStateEventContent, }, receipt::ReceiptType, @@ -44,7 +46,7 @@ use ruma::{ #[cfg(feature = "encryption")] use tracing::instrument; -use crate::{room::Common, BaseRoom, Client, Result, RoomType}; +use crate::{room::Common, BaseRoom, Client, Error, Result, RoomType}; const TYPING_NOTICE_TIMEOUT: Duration = Duration::from_secs(4); const TYPING_NOTICE_RESEND_TIMEOUT: Duration = Duration::from_secs(3); @@ -613,4 +615,48 @@ impl Joined { self.client.send(request, None).await } + + /// Adds a tag to the room, or updates it if it already exists. + /// + /// Returns the `[create_tag::Response]` from the server. + /// + /// # Arguments + /// * `tag` - The tag to add or update. + /// + /// * `tag_info` - Information about the tag, generally containing the + /// `order` parameter. + /// + /// # Example + /// + /// ```no_run + /// # use ruma::events::tag::TagInfo; + /// # futures::executor::block_on(async { + /// # let homeserver = url::Url::parse("http://localhost:8080").unwrap(); + /// # let mut client = matrix_sdk::Client::new(homeserver).unwrap(); + /// # let room_id = matrix_sdk::ruma::room_id!("!test:localhost"); + /// # let room = client + /// # .get_joined_room(&room_id) + /// # .unwrap(); + /// let mut tag_info = TagInfo::new(); + /// tag_info.order = Some(0.9); + /// room.set_tag("u.work", tag_info ); + /// # }) + /// ``` + pub async fn set_tag(&self, tag: &str, tag_info: TagInfo) -> Result { + let user_id = self.client.user_id().await.ok_or(Error::AuthenticationRequired)?; + let request = create_tag::Request::new(&user_id, self.inner.room_id(), tag, tag_info); + self.client.send(request, None).await + } + + /// Removes a tag from the room. + /// + /// Returns the `[delete_tag::Response]` from the server. + /// + /// # Arguments + /// * `tag` - The tag to remove. + pub async fn remove_tag(&self, tag: &str) -> Result { + let user_id = self.client.user_id().await.ok_or(Error::AuthenticationRequired)?; + let request = delete_tag::Request::new(&user_id, self.inner.room_id(), tag); + self.client.send(request, None).await + } }