Skip to content

Commit 535d1ec

Browse files
committed
feat(sdk): Use TagName in set/remove tag methods
1 parent 52deff1 commit 535d1ec

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

crates/matrix-sdk/src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use ruma::{
3333
},
3434
error::{FromHttpResponseError, IntoHttpError, MatrixError as RumaApiError, ServerError},
3535
},
36+
events::tag::InvalidUserTagName,
3637
identifiers::Error as IdentifierError,
3738
};
3839
use serde_json::Error as JsonError;
@@ -157,6 +158,10 @@ pub enum Error {
157158
#[cfg(feature = "qrcode")]
158159
#[error(transparent)]
159160
QrCodeScanError(#[from] ScanError),
161+
162+
/// An error encountered when trying to parse a user tag name.
163+
#[error(transparent)]
164+
UserTagName(#[from] InvalidUserTagName),
160165
}
161166

162167
/// Error for the room key importing functionality.

crates/matrix-sdk/src/room/common.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ use ruma::{
1010
tag::{create_tag, delete_tag},
1111
},
1212
events::{
13-
room::history_visibility::HistoryVisibility, tag::TagInfo, AnyStateEvent,
14-
AnySyncStateEvent, EventType,
13+
room::history_visibility::HistoryVisibility,
14+
tag::{TagInfo, TagName},
15+
AnyStateEvent, AnySyncStateEvent, EventType,
1516
},
1617
serde::Raw,
1718
EventId, UserId,
@@ -460,7 +461,8 @@ impl Common {
460461
/// # Example
461462
///
462463
/// ```no_run
463-
/// # use ruma::events::tag::TagInfo;
464+
/// # use std::str::FromStr;
465+
/// # use ruma::events::tag::{TagInfo, TagName, UserTagName};
464466
/// # futures::executor::block_on(async {
465467
/// # let homeserver = url::Url::parse("http://localhost:8080")?;
466468
/// # let mut client = matrix_sdk::Client::new(homeserver)?;
@@ -470,14 +472,20 @@ impl Common {
470472
/// if let Some(room) = client.get_joined_room(&room_id) {
471473
/// let mut tag_info = TagInfo::new();
472474
/// tag_info.order = Some(0.9);
475+
/// let user_tag = UserTagName::from_str("u.work")?;
473476
///
474-
/// room.set_tag("u.work", tag_info ).await?;
477+
/// room.set_tag(TagName::User(user_tag), tag_info ).await?;
475478
/// }
476479
/// # Result::<_, matrix_sdk::Error>::Ok(()) });
477480
/// ```
478-
pub async fn set_tag(&self, tag: &str, tag_info: TagInfo) -> HttpResult<create_tag::Response> {
481+
pub async fn set_tag(
482+
&self,
483+
tag: TagName,
484+
tag_info: TagInfo,
485+
) -> HttpResult<create_tag::Response> {
479486
let user_id = self.client.user_id().await.ok_or(HttpError::AuthenticationRequired)?;
480-
let request = create_tag::Request::new(&user_id, self.inner.room_id(), tag, tag_info);
487+
let request =
488+
create_tag::Request::new(&user_id, self.inner.room_id(), tag.as_ref(), tag_info);
481489
self.client.send(request, None).await
482490
}
483491

@@ -487,9 +495,9 @@ impl Common {
487495
///
488496
/// # Arguments
489497
/// * `tag` - The tag to remove.
490-
pub async fn remove_tag(&self, tag: &str) -> HttpResult<delete_tag::Response> {
498+
pub async fn remove_tag(&self, tag: TagName) -> HttpResult<delete_tag::Response> {
491499
let user_id = self.client.user_id().await.ok_or(HttpError::AuthenticationRequired)?;
492-
let request = delete_tag::Request::new(&user_id, self.inner.room_id(), tag);
500+
let request = delete_tag::Request::new(&user_id, self.inner.room_id(), tag.as_ref());
493501
self.client.send(request, None).await
494502
}
495503
}

0 commit comments

Comments
 (0)