Commit 38e62436d4
Unsigned
Layout: unified · split
src/server.rs +33 −49
| @@ -80,7 +80,8 @@ use matrix_sdk::{ | |||
| 80 | SyncStateEvent, | 80 | SyncStateEvent, |
| 81 | }, | 81 | }, |
| 82 | DeviceId, DeviceKeyAlgorithm, MilliSecondsSinceUnixEpoch, | 82 | DeviceId, DeviceKeyAlgorithm, MilliSecondsSinceUnixEpoch, |
| 83 | OwnedDeviceId, OwnedMxcUri, OwnedRoomId, OwnedUserId, RoomId, UserId, | 83 | OwnedDeviceId, OwnedMxcUri, OwnedRoomId, OwnedRoomOrAliasId, |
| 84 | OwnedUserId, RoomId, UserId, | ||
| 84 | }, | 85 | }, |
| 85 | Client, Error, | 86 | Client, Error, |
| 86 | }; | 87 | }; |
| @@ -214,59 +215,31 @@ impl MatrixServer { | |||
| 214 | Rc::downgrade(&self.inner) | 215 | Rc::downgrade(&self.inner) |
| 215 | } | 216 | } |
| 216 | 217 | ||
| 217 | /// Join a Matrix room by ID or alias | 218 | /// Join a Matrix room by ID or alias. |
| 218 | pub async fn join_room(&self, room_id_or_alias: String) { | 219 | pub async fn join_room(&self, room_id_or_alias: String) { |
| 219 | let connection = if let Some(c) = self.connection() { | 220 | let Ok(room_id_or_alias) = |
| 220 | c | 221 | room_id_or_alias.parse::<OwnedRoomOrAliasId>() |
| 221 | } else { | 222 | else { |
| 223 | self.print_error("Invalid room ID or alias."); | ||
| 224 | return; | ||
| 225 | }; | ||
| 226 | |||
| 227 | let Some(connection) = self.connection() else { | ||
| 222 | self.print_error("Not connected. Please connect first."); | 228 | self.print_error("Not connected. Please connect first."); |
| 223 | return; | 229 | return; |
| 224 | }; | 230 | }; |
| 225 | 231 | ||
| 226 | self.print_network(&format!("Joining room {}...", room_id_or_alias)); | 232 | self.print_network(&format!("Joining room {}...", room_id_or_alias)); |
| 227 | 233 | ||
| 228 | // Determine if it's a room ID or alias | 234 | let client = connection.client().clone(); |
| 229 | let result = if room_id_or_alias.starts_with('!') { | 235 | let target = room_id_or_alias.to_string(); |
| 230 | // It's a room ID | 236 | let result = connection |
| 231 | use matrix_sdk::ruma::OwnedRoomId; | 237 | .spawn(async move { |
| 232 | match room_id_or_alias.parse::<OwnedRoomId>() { | 238 | client |
| 233 | Ok(room_id) => { | 239 | .join_room_by_id_or_alias(&room_id_or_alias, &[]) |
| 234 | let client = connection.client().clone(); | 240 | .await |
| 235 | connection.spawn(async move { | 241 | }) |
| 236 | client.join_room_by_id(&room_id).await | 242 | .await; |
| 237 | }).await | ||
| 238 | } | ||
| 239 | Err(e) => { | ||
| 240 | self.print_error(&format!("Invalid room ID: {:?}", e)); | ||
| 241 | return; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | } else { | ||
| 245 | // It's a room alias (starts with # or we add it) | ||
| 246 | use matrix_sdk::ruma::OwnedRoomOrAliasId; | ||
| 247 | let alias = if room_id_or_alias.starts_with('#') { | ||
| 248 | room_id_or_alias.clone() | ||
| 249 | } else { | ||
| 250 | format!("#{}", room_id_or_alias) | ||
| 251 | }; | ||
| 252 | |||
| 253 | match alias.parse::<OwnedRoomOrAliasId>() { | ||
| 254 | Ok(room_or_alias) => { | ||
| 255 | let client = connection.client().clone(); | ||
| 256 | connection | ||
| 257 | .spawn(async move { | ||
| 258 | client | ||
| 259 | .join_room_by_id_or_alias(&room_or_alias, &[]) | ||
| 260 | .await | ||
| 261 | }) | ||
| 262 | .await | ||
| 263 | } | ||
| 264 | Err(e) => { | ||
| 265 | self.print_error(&format!("Invalid room alias: {:?}", e)); | ||
| 266 | return; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | }; | ||
| 270 | 243 | ||
| 271 | match result { | 244 | match result { |
| 272 | Ok(room) => { | 245 | Ok(room) => { |
| @@ -275,8 +248,12 @@ impl MatrixServer { | |||
| 275 | room.room_id() | 248 | room.room_id() |
| 276 | )); | 249 | )); |
| 277 | } | 250 | } |
| 278 | Err(e) => { | 251 | Err(error) => { |
| 279 | self.print_error(&format!("Failed to join room: {:?}", e)); | 252 | self.print_error(&format!( |
| 253 | "Failed to join {}: {}", | ||
| 254 | target, | ||
| 255 | format_join_error(&error) | ||
| 256 | )); | ||
| 280 | } | 257 | } |
| 281 | } | 258 | } |
| 282 | } | 259 | } |
| @@ -485,6 +462,13 @@ impl MatrixServer { | |||
| 485 | } | 462 | } |
| 486 | } | 463 | } |
| 487 | 464 | ||
| 465 | fn format_join_error(error: &Error) -> String { | ||
| 466 | error | ||
| 467 | .as_client_api_error() | ||
| 468 | .map(ToString::to_string) | ||
| 469 | .unwrap_or_else(|| error.to_string()) | ||
| 470 | } | ||
| 471 | |||
| 488 | impl Drop for MatrixServer { | 472 | impl Drop for MatrixServer { |
| 489 | fn drop(&mut self) { | 473 | fn drop(&mut self) { |
| 490 | // TODO close all the server buffers. | 474 | // TODO close all the server buffers. |