Skip to content

Commit

Permalink
feat(room): add methods to customise a Room's privacy settings
Browse files Browse the repository at this point in the history
These include:
- Updating the canonical alias.
- Creating and removing a room alias from the room directory.
- Enabling encryption in a room.
- Updating the join rule of a room.
- Updating the room history visiblity.
  • Loading branch information
jmartinesp committed Dec 11, 2024
1 parent d5e7a9c commit 088325e
Show file tree
Hide file tree
Showing 7 changed files with 642 additions and 19 deletions.
11 changes: 0 additions & 11 deletions bindings/matrix-sdk-ffi/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1152,17 +1152,6 @@ impl Client {
let alias = RoomAliasId::parse(alias)?;
self.inner.is_room_alias_available(&alias).await.map_err(Into::into)
}

/// Creates a new room alias associated with the provided room id.
pub async fn create_room_alias(
&self,
room_alias: String,
room_id: String,
) -> Result<(), ClientError> {
let room_alias = RoomAliasId::parse(room_alias)?;
let room_id = RoomId::parse(room_id)?;
self.inner.create_room_alias(&room_alias, &room_id).await.map_err(Into::into)
}
}

#[matrix_sdk_ffi_macros::export(callback_interface)]
Expand Down
6 changes: 6 additions & 0 deletions bindings/matrix-sdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ impl From<RoomSendQueueError> for ClientError {
}
}

impl From<NotYetImplemented> for ClientError {
fn from(_: NotYetImplemented) -> Self {
Self::new("This functionality is not implemented yet.")
}
}

/// Bindings version of the sdk type replacing OwnedUserId/DeviceIds with simple
/// String.
///
Expand Down
96 changes: 94 additions & 2 deletions bindings/matrix-sdk-ffi/src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use ruma::{
call::notify,
room::{
avatar::ImageInfo as RumaAvatarImageInfo,
message::RoomMessageEventContentWithoutRelation,
history_visibility::HistoryVisibility as RumaHistoryVisibility,
join_rules::JoinRule as RumaJoinRule, message::RoomMessageEventContentWithoutRelation,
power_levels::RoomPowerLevels as RumaPowerLevels, MediaSource,
},
AnyMessageLikeEventContent, AnySyncTimelineEvent, TimelineEventType,
Expand All @@ -33,7 +34,8 @@ use tracing::error;
use super::RUNTIME;
use crate::{
chunk_iterator::ChunkIterator,
error::{ClientError, MediaInfoError, RoomError},
client::JoinRule,
error::{ClientError, MediaInfoError, NotYetImplemented, RoomError},
event::{MessageLikeEventType, RoomMessageEventMessageType, StateEventType},
identity_status_change::IdentityStatusChange,
room_info::RoomInfo,
Expand Down Expand Up @@ -911,6 +913,37 @@ impl Room {
room_event_cache.clear().await?;
Ok(())
}

/// Update the canonical alias of the room.
///
/// Note that publishing the alias in the room directory is done separately.
pub async fn update_canonical_alias(
&self,
new_alias: Option<String>,
) -> Result<(), ClientError> {
let new_alias = new_alias.map(TryInto::try_into).transpose()?;
self.inner.update_canonical_alias(new_alias).await.map_err(Into::into)
}

/// Enable End-to-end encryption in this room.
pub async fn enable_encryption(&self) -> Result<(), ClientError> {
self.inner.enable_encryption().await.map_err(Into::into)
}

/// Update room history visibility for this room.
pub async fn update_history_visibility(
&self,
visibility: RoomHistoryVisibility,
) -> Result<(), ClientError> {
let visibility: RumaHistoryVisibility = visibility.try_into()?;
self.inner.update_room_history_visibility(visibility).await.map_err(Into::into)
}

/// Update the join rule for this room.
pub async fn update_join_rules(&self, new_rule: JoinRule) -> Result<(), ClientError> {
let new_rule: RumaJoinRule = new_rule.try_into()?;
self.inner.update_join_rule(new_rule).await.map_err(Into::into)
}
}

/// Generates a `matrix.to` permalink to the given room alias.
Expand Down Expand Up @@ -1144,3 +1177,62 @@ impl TryFrom<ComposerDraftType> for SdkComposerDraftType {
Ok(draft_type)
}
}

#[derive(Debug, Clone, uniffi::Enum)]
pub enum RoomHistoryVisibility {
/// Previous events are accessible to newly joined members from the point
/// they were invited onwards.
///
/// Events stop being accessible when the member's state changes to
/// something other than *invite* or *join*.
Invited,

/// Previous events are accessible to newly joined members from the point
/// they joined the room onwards.
/// Events stop being accessible when the member's state changes to
/// something other than *join*.
Joined,

/// Previous events are always accessible to newly joined members.
///
/// All events in the room are accessible, even those sent when the member
/// was not a part of the room.
Shared,

/// All events while this is the `HistoryVisibility` value may be shared by
/// any participating homeserver with anyone, regardless of whether they
/// have ever joined the room.
WorldReadable,

/// A custom visibility value.
Custom { value: String },
}

impl TryFrom<RumaHistoryVisibility> for RoomHistoryVisibility {
type Error = NotYetImplemented;
fn try_from(value: RumaHistoryVisibility) -> Result<Self, Self::Error> {
match value {
RumaHistoryVisibility::Invited => Ok(RoomHistoryVisibility::Invited),
RumaHistoryVisibility::Shared => Ok(RoomHistoryVisibility::Shared),
RumaHistoryVisibility::WorldReadable => Ok(RoomHistoryVisibility::WorldReadable),
RumaHistoryVisibility::Joined => Ok(RoomHistoryVisibility::Joined),
RumaHistoryVisibility::_Custom(_) => {
Ok(RoomHistoryVisibility::Custom { value: value.to_string() })
}
_ => Err(NotYetImplemented),
}
}
}

impl TryFrom<RoomHistoryVisibility> for RumaHistoryVisibility {
type Error = NotYetImplemented;
fn try_from(value: RoomHistoryVisibility) -> Result<Self, Self::Error> {
match value {
RoomHistoryVisibility::Invited => Ok(RumaHistoryVisibility::Invited),
RoomHistoryVisibility::Shared => Ok(RumaHistoryVisibility::Shared),
RoomHistoryVisibility::Joined => Ok(RumaHistoryVisibility::Joined),
RoomHistoryVisibility::WorldReadable => Ok(RumaHistoryVisibility::WorldReadable),
RoomHistoryVisibility::Custom { .. } => Err(NotYetImplemented),
}
}
}
13 changes: 10 additions & 3 deletions crates/matrix-sdk/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use ruma::{
api::{
client::{
account::whoami,
alias::{create_alias, get_alias},
alias::{create_alias, delete_alias, get_alias},
device::{delete_devices, get_devices, update_device},
directory::{get_public_rooms, get_public_rooms_filtered},
discovery::{
Expand Down Expand Up @@ -1210,13 +1210,20 @@ impl Client {
}
}

/// Creates a new room alias associated with a room.
/// Adds a new room alias associated with a room to the room directory.
pub async fn create_room_alias(&self, alias: &RoomAliasId, room_id: &RoomId) -> HttpResult<()> {
let request = create_alias::v3::Request::new(alias.to_owned(), room_id.to_owned());
self.send(request, None).await?;
Ok(())
}

/// Removes a room alias from the room directory.
pub async fn remove_room_alias(&self, alias: &RoomAliasId) -> HttpResult<()> {
let request = delete_alias::v3::Request::new(alias.to_owned());
self.send(request, None).await?;
Ok(())
}

/// Update the homeserver from the login response well-known if needed.
///
/// # Arguments
Expand Down Expand Up @@ -3156,7 +3163,7 @@ pub(crate) mod tests {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;

server.mock_create_room_alias().ok().expect(1).mount().await;
server.mock_room_directory_create_room_alias().ok().expect(1).mount().await;

let ret = client
.create_room_alias(
Expand Down
3 changes: 3 additions & 0 deletions crates/matrix-sdk/src/room/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ mod member;
mod messages;
pub mod power_levels;

/// Contains all the functionality for modifying the privacy settings in a room.
pub mod privacy_settings;

/// A struct containing methods that are common for Joined, Invited and Left
/// Rooms
#[derive(Debug, Clone)]
Expand Down
Loading

0 comments on commit 088325e

Please sign in to comment.