Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(room): add methods to customise a Room's privacy settings #4401

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was just moved to a different file, to have the functions grouped.

&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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're starting to use that more and more, I wonder if NotYetImplemented should contain more information, i.e. so we know which exact feature isn't yet implemented. (if you agree, this can be part of a separate PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, but as you suggest I'd rather make this change in a new PR.

Self::new("This functionality is not implemented yet.")
}
}

/// Bindings version of the sdk type replacing OwnedUserId/DeviceIds with simple
/// String.
///
Expand Down
104 changes: 102 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, RoomVisibility},
error::{ClientError, MediaInfoError, NotYetImplemented, RoomError},
event::{MessageLikeEventType, RoomMessageEventMessageType, StateEventType},
identity_status_change::IdentityStatusChange,
room_info::RoomInfo,
Expand Down Expand Up @@ -911,6 +913,45 @@ 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(
Velin92 marked this conversation as resolved.
Show resolved Hide resolved
&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)
}

/// Update the room's visibility in the room directory.
pub async fn update_room_visibility(
&self,
visibility: RoomVisibility,
) -> Result<(), ClientError> {
self.inner.update_room_visibility(visibility.into()).await.map_err(Into::into)
}
}

/// Generates a `matrix.to` permalink to the given room alias.
Expand Down Expand Up @@ -1144,3 +1185,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),
}
}
}
5 changes: 4 additions & 1 deletion bindings/matrix-sdk-ffi/src/room_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tracing::warn;
use crate::{
client::JoinRule,
notification_settings::RoomNotificationMode,
room::{Membership, RoomHero},
room::{Membership, RoomHero, RoomHistoryVisibility},
room_member::RoomMember,
};

Expand Down Expand Up @@ -60,6 +60,8 @@ pub struct RoomInfo {
pinned_event_ids: Vec<String>,
/// The join rule for this room, if known.
join_rule: Option<JoinRule>,
/// The history visibility for this room, if known.
history_visibility: Option<RoomHistoryVisibility>,
}

impl RoomInfo {
Expand Down Expand Up @@ -128,6 +130,7 @@ impl RoomInfo {
num_unread_mentions: room.num_unread_mentions(),
pinned_event_ids,
join_rule: join_rule.ok(),
history_visibility: room.history_visibility().and_then(|h| h.try_into().ok()),
})
}
}
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
Loading