1- use matrix_sdk:: {
2- room_preview:: {
3- JoinRoomPreviewAction as SdkJoinRoomPreviewAction , LeaveRoomPreviewAction as SdkLeaveRoomPreviewAction , RoomPreview as SdkRoomPreview ,
4- RoomPreviewActions ,
5- } ,
6- Client , RoomState ,
7- } ;
1+ use std:: sync:: Arc ;
2+
3+ use matrix_sdk:: room_preview:: RoomPreview as SdkRoomPreview ;
84use ruma:: space:: SpaceRoomJoinRule ;
95
106use crate :: { client:: JoinRule , error:: ClientError , room:: Membership } ;
117
12- #[ derive( uniffi:: Record ) ]
13- pub struct RoomPreview {
14- 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 } ,
23- }
24-
8+ /// A room preview for a room. It's intended to be used to represent rooms that
9+ /// aren't joined yet.
2510#[ derive( uniffi:: Object ) ]
26- pub struct JoinRoomPreviewAction {
27- inner : SdkJoinRoomPreviewAction ,
11+ pub struct RoomPreview {
12+ inner : Arc < SdkRoomPreview > ,
2813}
2914
3015#[ matrix_sdk_ffi_macros:: export]
31- impl JoinRoomPreviewAction {
32- async fn run ( & self ) {
33- self . inner . run ( ) . unwrap ( )
16+ impl RoomPreview {
17+ /// Returns the room info the preview contains.
18+ pub fn info ( & self ) -> RoomPreviewInfo {
19+ let info = & self . inner ;
20+ RoomPreviewInfo {
21+ room_id : info. room_id . to_string ( ) ,
22+ canonical_alias : info. canonical_alias . as_ref ( ) . map ( |alias| alias. to_string ( ) ) ,
23+ name : info. name . clone ( ) ,
24+ topic : info. topic . clone ( ) ,
25+ avatar_url : info. avatar_url . as_ref ( ) . map ( |url| url. to_string ( ) ) ,
26+ num_joined_members : info. num_joined_members ,
27+ room_type : info. room_type . as_ref ( ) . map ( |room_type| room_type. to_string ( ) ) ,
28+ is_history_world_readable : info. is_world_readable ,
29+ membership : info. state . map ( |state| state. into ( ) ) ,
30+ join_rule : info. join_rule . clone ( ) . into ( ) ,
31+ }
3432 }
35- }
3633
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- }
34+ /// Leave the room if the room preview state is either joined, invited or
35+ /// knocked.
36+ ///
37+ /// Will return an error otherwise.
38+ pub async fn leave ( & self ) -> Result < ( ) , ClientError > {
39+ self . inner . leave ( ) . await . map_err ( Into :: into )
4340 }
4441}
4542
46- struct
47-
4843impl RoomPreview {
49- pub ( crate ) fn try_from_sdk( info : SdkRoomPreview , client : Client ) -> Result < Self , ClientError > {
50- 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 ( ) ,
61- } ;
62-
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- } ;
78- Ok ( Self { info, room_preview_actions } )
44+ pub ( crate ) fn from_sdk ( room_preview : SdkRoomPreview ) -> Self {
45+ Self { inner : Arc :: new ( room_preview) }
7946 }
8047}
8148
@@ -98,23 +65,28 @@ pub struct RoomPreviewInfo {
9865 pub room_type : Option < String > ,
9966 /// Is the history world-readable for this room?
10067 pub is_history_world_readable : bool ,
101- /// Is the room joined by the current user?
102- pub membership : Membership ,
103- /// is the join rule public for this room?
68+ /// The membership state for the current user, if known.
69+ pub membership : Option < Membership > ,
70+ /// The join rule for this room (private, public, knock, etc.).
10471 pub join_rule : JoinRule ,
10572}
10673
10774impl From < SpaceRoomJoinRule > for JoinRule {
10875 fn from ( join_rule : SpaceRoomJoinRule ) -> Self {
10976 match join_rule {
110- SpaceRoomJoinRule :: Public => JoinRule :: Public ,
111- SpaceRoomJoinRule :: Private => JoinRule :: Private ,
11277 SpaceRoomJoinRule :: Invite => JoinRule :: Invite ,
11378 SpaceRoomJoinRule :: Knock => JoinRule :: Knock ,
114- SpaceRoomJoinRule :: KnockRestricted => JoinRule :: KnockRestricted { rules : Vec :: new ( ) } ,
79+ SpaceRoomJoinRule :: Private => JoinRule :: Private ,
11580 SpaceRoomJoinRule :: Restricted => JoinRule :: Restricted { rules : Vec :: new ( ) } ,
116- // For the `_Custom` case, assume it's private
117- _ => JoinRule :: Private ,
81+ SpaceRoomJoinRule :: KnockRestricted => JoinRule :: KnockRestricted { rules : Vec :: new ( ) } ,
82+ SpaceRoomJoinRule :: Public => JoinRule :: Public ,
83+ _ => {
84+ // Since we have no way to get the _Custom contents, assume it's private.
85+ // Warning! If new join rules are introduced we may be mapping wrong values
86+ // here, but there's no way to match
87+ // `SpaceRoomJoinRule::_Custom(_)` and have an exhaustive match.
88+ JoinRule :: Private
89+ }
11890 }
11991 }
12092}
0 commit comments