Commit ec9bd32a8d

ec9bd32a8dd4d988726acde4e924698285cabea4

parent: f3906abf41

Unregistered key

vaw <git@nlih.de> · 2026-07-26 14:53 UTC
committer: <noreply@github.com>

Simplify room creation by using SDK helpers (#593)

Layout: unified · split

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