| @@ -24,6 +24,7 @@ use matrix_sdk::{ |
| 24 | 24 | room::Visibility, |
| 25 | 25 | space::get_hierarchy::v1::Request as SpaceHierarchyRequest, |
| 26 | 26 | }, |
| 27 | assign, |
| 27 | 28 | events::{ |
| 28 | 29 | key::verification::{ |
| 29 | 30 | done::{OriginalSyncKeyVerificationDoneEvent, ToDeviceKeyVerificationDoneEvent}, |
| @@ -35,16 +36,22 @@ use matrix_sdk::{ |
| 35 | 36 | presence::PresenceEvent, |
| 36 | 37 | reaction::ReactionEventContent, |
| 37 | 38 | room::{ |
| 39 | encryption::RoomEncryptionEventContent, |
| 38 | 40 | message::{MessageType, RoomMessageEventContent}, |
| 39 | 41 | name::RoomNameEventContent, |
| 40 | 42 | redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent}, |
| 41 | 43 | }, |
| 42 | 44 | tag::Tags, |
| 43 | 45 | typing::SyncTypingEvent, |
| 46 | AnyInitialStateEvent, |
| 44 | 47 | AnyTimelineEvent, |
| 48 | EmptyStateKey, |
| 49 | InitialStateEvent, |
| 45 | 50 | SyncMessageLikeEvent, |
| 46 | 51 | SyncStateEvent, |
| 47 | 52 | }, |
| 53 | serde::Raw, |
| 54 | EventEncryptionAlgorithm, |
| 48 | 55 | OwnedRoomId, |
| 49 | 56 | OwnedRoomOrAliasId, |
| 50 | 57 | OwnedUserId, |
| @@ -58,7 +65,16 @@ use matrix_sdk::{ |
| 58 | 65 | use modalkit::editing::action::{EditInfo, InfoMessage, UIError}; |
| 59 | 66 | |
| 60 | 67 | use crate::{ |
| 61 | | base::{AsyncProgramStore, EventLocation, IambError, IambResult, Receipts, VerifyAction}, |
| 68 | base::{ |
| 69 | AsyncProgramStore, |
| 70 | CreateRoomFlags, |
| 71 | CreateRoomType, |
| 72 | EventLocation, |
| 73 | IambError, |
| 74 | IambResult, |
| 75 | Receipts, |
| 76 | VerifyAction, |
| 77 | }, |
| 62 | 78 | message::MessageFetchResult, |
| 63 | 79 | ApplicationSettings, |
| 64 | 80 | }; |
| @@ -71,6 +87,69 @@ fn initial_devname() -> String { |
| 71 | 87 | format!("{} on {}", IAMB_DEVICE_NAME, gethostname().to_string_lossy()) |
| 72 | 88 | } |
| 73 | 89 | |
| 90 | pub async fn create_room( |
| 91 | client: &Client, |
| 92 | room_alias_name: Option<&str>, |
| 93 | rt: CreateRoomType, |
| 94 | flags: CreateRoomFlags, |
| 95 | ) -> IambResult<OwnedRoomId> { |
| 96 | let creation_content = None; |
| 97 | let mut initial_state = vec![]; |
| 98 | let is_direct; |
| 99 | let preset; |
| 100 | let mut invite = vec![]; |
| 101 | |
| 102 | let visibility = if flags.contains(CreateRoomFlags::PUBLIC) { |
| 103 | Visibility::Public |
| 104 | } else { |
| 105 | Visibility::Private |
| 106 | }; |
| 107 | |
| 108 | match rt { |
| 109 | CreateRoomType::Direct(user) => { |
| 110 | invite.push(user); |
| 111 | is_direct = true; |
| 112 | preset = Some(RoomPreset::TrustedPrivateChat); |
| 113 | }, |
| 114 | } |
| 115 | |
| 116 | // Set up encryption. |
| 117 | if flags.contains(CreateRoomFlags::ENCRYPTED) { |
| 118 | // XXX: Once matrix-sdk uses ruma 0.8, then this can skip the cast. |
| 119 | let algo = EventEncryptionAlgorithm::MegolmV1AesSha2; |
| 120 | let content = RoomEncryptionEventContent::new(algo); |
| 121 | let encr = InitialStateEvent { content, state_key: EmptyStateKey }; |
| 122 | let encr_raw = Raw::new(&encr).map_err(IambError::from)?; |
| 123 | let encr_raw = encr_raw.cast::<AnyInitialStateEvent>(); |
| 124 | initial_state.push(encr_raw); |
| 125 | } |
| 126 | |
| 127 | let request = assign!(CreateRoomRequest::new(), { |
| 128 | room_alias_name, |
| 129 | creation_content, |
| 130 | initial_state: initial_state.as_slice(), |
| 131 | invite: invite.as_slice(), |
| 132 | is_direct, |
| 133 | visibility, |
| 134 | preset, |
| 135 | }); |
| 136 | |
| 137 | let resp = client.create_room(request).await.map_err(IambError::from)?; |
| 138 | |
| 139 | if is_direct { |
| 140 | if let Some(room) = client.get_room(&resp.room_id) { |
| 141 | room.set_is_direct(true).await.map_err(IambError::from)?; |
| 142 | } else { |
| 143 | error!( |
| 144 | room_id = resp.room_id.as_str(), |
| 145 | "Couldn't set is_direct for new direct message room" |
| 146 | ); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return Ok(resp.room_id); |
| 151 | } |
| 152 | |
| 74 | 153 | #[derive(Debug)] |
| 75 | 154 | pub enum LoginStyle { |
| 76 | 155 | SessionRestore(Session), |
| @@ -788,15 +867,11 @@ impl ClientWorker { |
| 788 | 867 | } |
| 789 | 868 | } |
| 790 | 869 | |
| 791 | | let mut request = CreateRoomRequest::new(); |
| 792 | | let invite = [user.clone()]; |
| 793 | | request.is_direct = true; |
| 794 | | request.invite = &invite; |
| 795 | | request.visibility = Visibility::Private; |
| 796 | | request.preset = Some(RoomPreset::PrivateChat); |
| 870 | let rt = CreateRoomType::Direct(user.clone()); |
| 871 | let flags = CreateRoomFlags::ENCRYPTED; |
| 797 | 872 | |
| 798 | | match self.client.create_room(request).await { |
| 799 | | Ok(resp) => self.get_room(resp.room_id).await, |
| 873 | match create_room(&self.client, None, rt, flags).await { |
| 874 | Ok(room_id) => self.get_room(room_id).await, |
| 800 | 875 | Err(e) => { |
| 801 | 876 | error!( |
| 802 | 877 | user_id = user.as_str(), |