Skip to content

Commit

Permalink
feat(ffi): add Client::is_room_alias_available function
Browse files Browse the repository at this point in the history
  • Loading branch information
jmartinesp committed Nov 4, 2024
1 parent 7f7b996 commit 6828f93
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use matrix_sdk::{
},
OidcAuthorizationData, OidcSession,
},
reqwest::StatusCode,
ruma::{
api::client::{
media::get_content_thumbnail::v3::Method,
Expand All @@ -40,7 +41,7 @@ use matrix_sdk::{
EventEncryptionAlgorithm, RoomId, TransactionId, UInt, UserId,
},
sliding_sync::Version as SdkSlidingSyncVersion,
AuthApi, AuthSession, Client as MatrixClient, SessionChange, SessionTokens,
AuthApi, AuthSession, Client as MatrixClient, HttpError, SessionChange, SessionTokens,
};
use matrix_sdk_ui::notification_client::{
NotificationClient as MatrixNotificationClient,
Expand Down Expand Up @@ -1029,8 +1030,6 @@ impl Client {
room_alias: String,
) -> Result<Option<ResolvedRoomAlias>, ClientError> {
let room_alias = RoomAliasId::parse(&room_alias)?;
let response = self.inner.resolve_room_alias(&room_alias).await?;
Ok(response.into())
match self.inner.resolve_room_alias(&room_alias).await {
Ok(response) => Ok(Some(response.into())),
Err(HttpError::Reqwest(http_error)) => match http_error.status() {
Expand All @@ -1041,6 +1040,11 @@ impl Client {
}
}

/// Checks if a room alias exists in the current homeserver.
pub async fn room_alias_exists(&self, room_alias: String) -> Result<bool, ClientError> {
self.resolve_room_alias(room_alias).await.map(|ret| ret.is_some())
}

/// Given a room id, get the preview of a room, to interact with it.
///
/// The list of `via_servers` must be a list of servers that know
Expand Down Expand Up @@ -1124,6 +1128,23 @@ impl Client {

Ok(())
}

/// Checks if a room alias is available in the current homeserver.
pub async fn is_room_alias_available(&self, alias: String) -> Result<bool, ClientError> {
let alias = RoomAliasId::parse(alias)?;
match self.inner.resolve_room_alias(&alias).await {
// The room alias was resolved, so it's already in use.
Ok(_) => Ok(false),
Err(HttpError::Reqwest(error)) => {
match error.status() {
// The room alias wasn't found, so it's available.
Some(StatusCode::NOT_FOUND) => Ok(true),
_ => Err(HttpError::Reqwest(error).into()),
}
}
Err(error) => Err(error.into()),
}
}
}

#[matrix_sdk_ffi_macros::export(callback_interface)]
Expand Down

0 comments on commit 6828f93

Please sign in to comment.