Skip to content

Commit 26958b5

Browse files
committed
chore(room_preview): Add knocking action to the RoomPreviewActions too
1 parent 736f4d4 commit 26958b5

File tree

14 files changed

+568
-122
lines changed

14 files changed

+568
-122
lines changed

bindings/matrix-sdk-ffi/src/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ impl From<OidcPrompt> for SdkOidcPrompt {
18071807
}
18081808

18091809
/// The rule used for users wishing to join this room.
1810-
#[derive(uniffi::Enum)]
1810+
#[derive(Debug, Clone, uniffi::Enum)]
18111811
pub enum JoinRule {
18121812
/// Anyone can join the room without any prior action.
18131813
Public,
@@ -1836,7 +1836,7 @@ pub enum JoinRule {
18361836
}
18371837

18381838
/// An allow rule which defines a condition that allows joining a room.
1839-
#[derive(uniffi::Enum)]
1839+
#[derive(Debug, Clone, uniffi::Enum)]
18401840
pub enum AllowRule {
18411841
/// Only a member of the `room_id` Room can join the one this rule is used
18421842
/// in.

bindings/matrix-sdk-ffi/src/error.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use std::{collections::HashMap, fmt, fmt::Display};
22

33
use matrix_sdk::{
44
encryption::CryptoStoreError, event_cache::EventCacheError, oidc::OidcError, reqwest,
5-
room::edit::EditError, send_queue::RoomSendQueueError, HttpError, IdParseError,
6-
NotificationSettingsError as SdkNotificationSettingsError,
5+
room::edit::EditError, room_preview::RoomStateActionError, send_queue::RoomSendQueueError,
6+
HttpError, IdParseError, NotificationSettingsError as SdkNotificationSettingsError,
77
QueueWedgeError as SdkQueueWedgeError, StoreError,
88
};
99
use matrix_sdk_ui::{encryption_sync_service, notification_client, sync_service, timeline};
1010
use uniffi::UnexpectedUniFFICallbackError;
11-
use matrix_sdk::room_preview::RoomPreviewError;
11+
1212
use crate::room_list::RoomListError;
1313

1414
#[derive(Debug, thiserror::Error)]
@@ -155,8 +155,8 @@ impl From<RoomSendQueueError> for ClientError {
155155
}
156156
}
157157

158-
impl From<RoomPreviewError> for ClientError {
159-
fn from(e: RoomSendQueueError) -> Self {
158+
impl From<RoomStateActionError> for ClientError {
159+
fn from(e: RoomStateActionError) -> Self {
160160
Self::new(e)
161161
}
162162
}

bindings/matrix-sdk-ffi/src/room.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::{
4545
TaskHandle,
4646
};
4747

48-
#[derive(Debug, uniffi::Enum)]
48+
#[derive(Debug, Clone, uniffi::Enum)]
4949
pub enum Membership {
5050
Invited,
5151
Joined,

bindings/matrix-sdk-ffi/src/room_list.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ use matrix_sdk_ui::{
1616
timeline::default_event_filter,
1717
unable_to_decrypt_hook::UtdHookManager,
1818
};
19-
use ruma::{
20-
IdParseError, OwnedRoomOrAliasId, OwnedServerName, RoomAliasId, RoomOrAliasId, ServerName,
21-
};
19+
use ruma::{OwnedRoomOrAliasId, OwnedServerName, ServerName};
2220
use tokio::sync::RwLock;
2321

2422
use crate::{
@@ -600,7 +598,7 @@ impl RoomListItem {
600598
///
601599
/// An error will be returned if the room is a state other than invited
602600
/// or knocked.
603-
async fn preview_room(&self, via: Vec<String>) -> Result<Arc<RoomPreview>, ClientError> {
601+
async fn preview_room(&self, via: Vec<String>) -> Result<RoomPreview, ClientError> {
604602
let membership = self.membership();
605603
if !matches!(membership, Membership::Invited | Membership::Knocked) {
606604
return Err(RoomListError::IncorrectRoomMembership {
@@ -624,7 +622,7 @@ impl RoomListItem {
624622
};
625623

626624
let room_preview = client.get_room_preview(&room_or_alias_id, server_names).await?;
627-
Ok(Arc::new(RoomPreview::try_from_sdk(room_preview, client)))
625+
RoomPreview::try_from_sdk(room_preview, client)
628626
}
629627

630628
/// Build a full `Room` FFI object, filling its associated timeline.

bindings/matrix-sdk-ffi/src/room_preview.rs

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,71 @@
1+
use std::sync::Arc;
2+
13
use matrix_sdk::{
2-
room_preview::{
3-
JoinRoomPreviewAction as SdkJoinRoomPreviewAction, LeaveRoomPreviewAction as SdkLeaveRoomPreviewAction, RoomPreview as SdkRoomPreview,
4-
RoomPreviewActions,
5-
},
6-
Client, RoomState,
4+
room_preview::{RoomPreview as SdkRoomPreview, RoomPreviewActions as SdkRoomPreviewActions},
5+
Client,
76
};
8-
use ruma::space::SpaceRoomJoinRule;
7+
use ruma::{space::SpaceRoomJoinRule, ServerName};
98

109
use crate::{client::JoinRule, error::ClientError, room::Membership};
1110

11+
/// A room preview for a room. It's intended to be used to represent rooms that
12+
/// aren't joined yet.
1213
#[derive(uniffi::Record)]
1314
pub struct RoomPreview {
1415
pub info: RoomPreviewInfo,
15-
pub room_preview_actions: RoomPreviewActions,
16-
pub(crate) sdk_info: matrix_sdk::room_preview::RoomPreview,
17-
}
18-
19-
#[derive(uniffi::Enum)]
20-
pub enum RoomPreviewAction {
21-
Invited { join: JoinRoomPreviewAction, leave: LeaveRoomPreviewAction },
22-
Knocked { leave: LeaveRoomPreviewAction },
16+
pub room_preview_actions: Arc<RoomPreviewActions>,
2317
}
2418

19+
/// Actions to perform in a room preview, such as join/leave.
2520
#[derive(uniffi::Object)]
26-
pub struct JoinRoomPreviewAction {
27-
inner: SdkJoinRoomPreviewAction,
21+
pub struct RoomPreviewActions {
22+
inner: SdkRoomPreviewActions,
2823
}
2924

3025
#[matrix_sdk_ffi_macros::export]
31-
impl JoinRoomPreviewAction {
32-
async fn run(&self) {
33-
self.inner.run().unwrap()
26+
impl RoomPreviewActions {
27+
/// Join the room.
28+
async fn join(&self) -> Result<(), ClientError> {
29+
Ok(self.inner.join.run().await?)
3430
}
35-
}
3631

37-
impl From<matrix_sdk::room_preview::RoomPreviewActions> for RoomPreviewAction {
38-
fn from(actions: RoomPreviewActions) -> Self {
39-
match actions {
40-
RoomPreviewActions::Invited { join, leave } => Self::Invited { join, leave },
41-
RoomPreviewActions::Knocked { leave } => Self::Knocked { leave },
42-
}
32+
/// Leave the room.
33+
async fn leave(&self) -> Result<(), ClientError> {
34+
Ok(self.inner.leave.run().await?)
4335
}
44-
}
4536

46-
struct
37+
/// Knock on the room.
38+
async fn knock(&self, reason: Option<String>, via: Vec<String>) -> Result<(), ClientError> {
39+
let via = via.iter().map(ServerName::parse).collect::<Result<Vec<_>, _>>()?;
40+
Ok(self.inner.knock.run(reason, via).await?)
41+
}
42+
}
4743

4844
impl RoomPreview {
49-
pub(crate) fn try_from_sdk(info: SdkRoomPreview, client: Client) -> Result<Self, ClientError> {
45+
pub(crate) fn try_from_sdk(
46+
room_preview: SdkRoomPreview,
47+
client: Client,
48+
) -> Result<Self, ClientError> {
49+
let room_preview_actions = Arc::new(RoomPreviewActions {
50+
inner: SdkRoomPreviewActions::new(client, room_preview.clone()),
51+
});
52+
5053
let info = RoomPreviewInfo {
51-
room_id: info.room_id.to_string(),
52-
canonical_alias: info.canonical_alias.map(|alias| alias.to_string()),
53-
name: info.name,
54-
topic: info.topic,
55-
avatar_url: info.avatar_url.map(|url| url.to_string()),
56-
num_joined_members: info.num_joined_members,
57-
room_type: info.room_type.map(|room_type| room_type.to_string()),
58-
is_history_world_readable: info.is_world_readable,
59-
membership: info.state.map(|state| state.into()).unwrap_or_else(|| Membership::Left),
60-
join_rule: info.join_rule.into(),
54+
room_id: room_preview.room_id.to_string(),
55+
canonical_alias: room_preview.canonical_alias.map(|alias| alias.to_string()),
56+
name: room_preview.name,
57+
topic: room_preview.topic,
58+
avatar_url: room_preview.avatar_url.map(|url| url.to_string()),
59+
num_joined_members: room_preview.num_joined_members,
60+
room_type: room_preview.room_type.map(|room_type| room_type.to_string()),
61+
is_history_world_readable: room_preview.is_world_readable,
62+
membership: room_preview
63+
.state
64+
.map(|state| state.into())
65+
.unwrap_or_else(|| Membership::Left),
66+
join_rule: room_preview.join_rule.into(),
6167
};
6268

63-
let room_preview_actions = match info.membership {
64-
Membership::Invited => RoomPreviewActions::Invited {
65-
join: JoinRoomPreviewAction::new(client.clone()),
66-
leave: LeaveRoomPreviewAction::new(client.clone()),
67-
},
68-
Membership::Knocked => {
69-
RoomPreviewActions::Knocked { leave: LeaveRoomPreviewAction::new(client.clone()) }
70-
}
71-
_ => {
72-
return Err(ClientError::new(format!(
73-
"The room preview had membership {:?} instead of Invited or Knocked.",
74-
info.membership
75-
)))
76-
}
77-
};
7869
Ok(Self { info, room_preview_actions })
7970
}
8071
}

crates/matrix-sdk-base/src/rooms/normal.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,6 @@ impl RoomSummary {
186186
/// Enum keeping track in which state the room is, e.g. if our own user is
187187
/// joined, RoomState::Invited, or has left the room.
188188
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
189-
#[cfg(feature = "uniffi")]
190-
#[derive(uniffi::Enum)]
191189
pub enum RoomState {
192190
/// The room is in a joined state.
193191
Joined,

crates/matrix-sdk-base/src/sync.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use std::{collections::BTreeMap, fmt};
1919
use matrix_sdk_common::{debug::DebugRawEvent, deserialized_responses::SyncTimelineEvent};
2020
use ruma::{
2121
api::client::sync::sync_events::{
22-
v3::{InvitedRoom as InvitedRoomUpdate, KnockedRoom},
22+
v3::{InvitedRoom as InvitedRoomUpdate, KnockedRoom as KnockedRoomUpdate},
2323
UnreadNotificationsCount as RumaUnreadNotificationsCount,
2424
},
2525
events::{
@@ -78,7 +78,7 @@ pub struct RoomUpdates {
7878
/// The rooms that the user has been invited to.
7979
pub invite: BTreeMap<OwnedRoomId, InvitedRoomUpdate>,
8080
/// The rooms that the user has knocked on.
81-
pub knocked: BTreeMap<OwnedRoomId, KnockedRoom>,
81+
pub knocked: BTreeMap<OwnedRoomId, KnockedRoomUpdate>,
8282
}
8383

8484
impl RoomUpdates {
@@ -254,7 +254,7 @@ impl<'a> fmt::Debug for DebugInvitedRoomUpdates<'a> {
254254
}
255255
}
256256

257-
struct DebugKnockedRoomUpdates<'a>(&'a BTreeMap<OwnedRoomId, KnockedRoom>);
257+
struct DebugKnockedRoomUpdates<'a>(&'a BTreeMap<OwnedRoomId, KnockedRoomUpdate>);
258258

259259
#[cfg(not(tarpaulin_include))]
260260
impl<'a> fmt::Debug for DebugKnockedRoomUpdates<'a> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use ruma::{
4747
membership::{
4848
ban_user, forget_room, get_member_events,
4949
invite_user::{self, v3::InvitationRecipient},
50-
join_room_by_id, kick_user, leave_room, unban_user, Invite3pid,
50+
kick_user, leave_room, unban_user, Invite3pid,
5151
},
5252
message::send_message_event,
5353
read_marker::set_read_marker,

0 commit comments

Comments
 (0)