Commit ec9bd32a8d
Unregistered key
Layout: unified · split
src/base.rs −3
| @@ -199,9 +199,6 @@ pub enum SpaceAction { | ||
| 199 | 199 | /// The type of room being created. |
| 200 | 200 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 201 | 201 | pub enum CreateRoomType { |
| 202 | /// A direct message room. | |
| 203 | Direct(OwnedUserId), | |
| 204 | ||
| 205 | 202 | /// A standard chat room. |
| 206 | 203 | Room, |
| 207 | 204 | |
src/worker.rs +20 −66
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | use std::collections::HashMap; |
| 6 | 6 | use std::convert::TryFrom; |
| 7 | 7 | use std::fmt::{Debug, Formatter}; |
| 8 | use std::ops::{Deref, DerefMut}; | |
| 8 | use std::ops::DerefMut; | |
| 9 | 9 | use std::str::FromStr; |
| 10 | 10 | use std::sync::mpsc::{sync_channel, Receiver, SyncSender}; |
| 11 | 11 | use std::sync::Arc; |
| @@ -35,7 +35,7 @@ use matrix_sdk::{ | ||
| 35 | 35 | api::client::{ |
| 36 | 36 | filter::{FilterDefinition, LazyLoadOptions, RoomEventFilter, RoomFilter}, |
| 37 | 37 | room::{ |
| 38 | create_room::v3::{CreationContent, Request as CreateRoomRequest, RoomPreset}, | |
| 38 | create_room::v3::{CreationContent, Request as CreateRoomRequest}, | |
| 39 | 39 | Visibility, |
| 40 | 40 | }, |
| 41 | 41 | space::get_hierarchy::v1::Request as SpaceHierarchyRequest, |
| @@ -122,10 +122,6 @@ fn initial_devname() -> String { | ||
| 122 | 122 | format!("{} on {}", IAMB_DEVICE_NAME, gethostname().to_string_lossy()) |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | async fn is_direct(room: &MatrixRoom) -> bool { | |
| 126 | room.deref().is_direct().await.unwrap_or_default() | |
| 127 | } | |
| 128 | ||
| 129 | 125 | pub async fn create_room( |
| 130 | 126 | client: &Client, |
| 131 | 127 | room_alias_name: Option<String>, |
| @@ -134,9 +130,6 @@ pub async fn create_room( | ||
| 134 | 130 | ) -> IambResult<OwnedRoomId> { |
| 135 | 131 | let mut creation_content = None; |
| 136 | 132 | let mut initial_state = vec![]; |
| 137 | let mut is_direct = false; | |
| 138 | let mut preset = None; | |
| 139 | let mut invite = vec![]; | |
| 140 | 133 | |
| 141 | 134 | let visibility = if flags.contains(CreateRoomFlags::PUBLIC) { |
| 142 | 135 | Visibility::Public |
| @@ -145,11 +138,6 @@ pub async fn create_room( | ||
| 145 | 138 | }; |
| 146 | 139 | |
| 147 | 140 | match rt { |
| 148 | CreateRoomType::Direct(user) => { | |
| 149 | invite.push(user); | |
| 150 | is_direct = true; | |
| 151 | preset = Some(RoomPreset::TrustedPrivateChat); | |
| 152 | }, | |
| 153 | 141 | CreateRoomType::Space => { |
| 154 | 142 | let mut cc = CreationContent::new(); |
| 155 | 143 | cc.room_type = Some(RoomType::Space); |
| @@ -173,25 +161,11 @@ pub async fn create_room( | ||
| 173 | 161 | room_alias_name, |
| 174 | 162 | creation_content, |
| 175 | 163 | initial_state, |
| 176 | invite, | |
| 177 | is_direct, | |
| 178 | 164 | visibility, |
| 179 | preset, | |
| 180 | 165 | }); |
| 181 | 166 | |
| 182 | 167 | let resp = client.create_room(request).await.map_err(IambError::from)?; |
| 183 | 168 | |
| 184 | if is_direct { | |
| 185 | if let Some(room) = client.get_room(resp.room_id()) { | |
| 186 | room.set_is_direct(true).await.map_err(IambError::from)?; | |
| 187 | } else { | |
| 188 | error!( | |
| 189 | room_id = resp.room_id().as_str(), | |
| 190 | "Couldn't set is_direct for new direct message room" | |
| 191 | ); | |
| 192 | } | |
| 193 | } | |
| 194 | ||
| 195 | 169 | return Ok(resp.room_id().to_owned()); |
| 196 | 170 | } |
| 197 | 171 | |
| @@ -465,28 +439,13 @@ async fn refresh_rooms(client: &Client, store: &AsyncProgramStore) { | ||
| 465 | 439 | let mut rooms = vec![]; |
| 466 | 440 | let mut dms = vec![]; |
| 467 | 441 | |
| 468 | for room in client.invited_rooms().into_iter() { | |
| 469 | let name = room.cached_display_name().unwrap_or(RoomDisplayName::Empty).to_string(); | |
| 470 | let tags = room.tags().await.unwrap_or_default(); | |
| 471 | ||
| 472 | names.push((room.room_id().to_owned(), name)); | |
| 473 | ||
| 474 | if is_direct(&room).await { | |
| 475 | dms.push(Arc::new((room, tags))); | |
| 476 | } else if room.is_space() { | |
| 477 | spaces.push(Arc::new((room, tags))); | |
| 478 | } else { | |
| 479 | rooms.push(Arc::new((room, tags))); | |
| 480 | } | |
| 481 | } | |
| 482 | ||
| 483 | for room in client.joined_rooms().into_iter() { | |
| 442 | for room in client.invited_rooms().into_iter().chain(client.joined_rooms().into_iter()) { | |
| 484 | 443 | let name = room.cached_display_name().unwrap_or(RoomDisplayName::Empty).to_string(); |
| 485 | 444 | let tags = room.tags().await.unwrap_or_default(); |
| 486 | 445 | |
| 487 | 446 | names.push((room.room_id().to_owned(), name)); |
| 488 | 447 | |
| 489 | if is_direct(&room).await { | |
| 448 | if room.is_direct().await.unwrap_or_default() { | |
| 490 | 449 | dms.push(Arc::new((room, tags))); |
| 491 | 450 | } else if room.is_space() { |
| 492 | 451 | spaces.push(Arc::new((room, tags))); |
| @@ -1370,29 +1329,24 @@ impl ClientWorker { | ||
| 1370 | 1329 | } |
| 1371 | 1330 | |
| 1372 | 1331 | async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<OwnedRoomId> { |
| 1373 | for room in self.client.rooms() { | |
| 1374 | if !is_direct(&room).await { | |
| 1375 | continue; | |
| 1376 | } | |
| 1377 | ||
| 1378 | if room.get_member(user.as_ref()).await.map_err(IambError::from)?.is_some() { | |
| 1379 | return Ok(room.room_id().to_owned()); | |
| 1380 | } | |
| 1332 | if let Some(room) = self.client.get_dm_room(&user) { | |
| 1333 | return Ok(room.room_id().to_owned()); | |
| 1381 | 1334 | } |
| 1382 | 1335 | |
| 1383 | let rt = CreateRoomType::Direct(user.clone()); | |
| 1384 | let flags = CreateRoomFlags::ENCRYPTED; | |
| 1385 | ||
| 1386 | create_room(&self.client, None, rt, flags).await.map_err(|e| { | |
| 1387 | error!( | |
| 1388 | user_id = user.as_str(), | |
| 1389 | err = e.to_string(), | |
| 1390 | "Failed to create direct message room" | |
| 1391 | ); | |
| 1392 | ||
| 1393 | let msg = format!("Could not open a room with {user}"); | |
| 1394 | UIError::Failure(msg) | |
| 1395 | }) | |
| 1336 | self.client | |
| 1337 | .create_dm(&user) | |
| 1338 | .await | |
| 1339 | .map(|room| room.room_id().to_owned()) | |
| 1340 | .map_err(|err| { | |
| 1341 | error!( | |
| 1342 | user_id = user.as_str(), | |
| 1343 | err = err.to_string(), | |
| 1344 | "Failed to create direct message room" | |
| 1345 | ); | |
| 1346 | ||
| 1347 | let msg = format!("Could not open a room with {user}"); | |
| 1348 | UIError::Failure(msg) | |
| 1349 | }) | |
| 1396 | 1350 | } |
| 1397 | 1351 | |
| 1398 | 1352 | async fn get_inviter(&mut self, invited: MatrixRoom) -> IambResult<Option<RoomMember>> { |