Commit d8ac3c0ba8
Unsigned
Layout: unified · split
src/server.rs +140 −18
| @@ -74,7 +74,11 @@ use matrix_sdk::{ | ||
| 74 | 74 | media::{MediaFormat, MediaRequestParameters}, |
| 75 | 75 | room::Room, |
| 76 | 76 | ruma::{ |
| 77 | api::client::session::login::v3::Response as LoginResponse, | |
| 77 | api::client::{ | |
| 78 | error::ErrorKind, | |
| 79 | room::create_room::v3::Request as CreateRoomRequest, | |
| 80 | session::login::v3::Response as LoginResponse, | |
| 81 | }, | |
| 78 | 82 | events::{ |
| 79 | 83 | room::{member::RoomMemberEventContent, MediaSource}, |
| 80 | 84 | AnySyncStateEvent, AnySyncTimelineEvent, AnyToDeviceEvent, |
| @@ -82,7 +86,7 @@ use matrix_sdk::{ | ||
| 82 | 86 | }, |
| 83 | 87 | DeviceId, DeviceKeyAlgorithm, MilliSecondsSinceUnixEpoch, |
| 84 | 88 | OwnedDeviceId, OwnedMxcUri, OwnedRoomAliasId, OwnedRoomId, |
| 85 | OwnedRoomOrAliasId, OwnedServerName, OwnedUserId, RoomId, UserId, | |
| 89 | OwnedRoomOrAliasId, OwnedUserId, RoomAliasId, RoomId, UserId, | |
| 86 | 90 | }, |
| 87 | 91 | Client, Error, |
| 88 | 92 | }; |
| @@ -265,11 +269,7 @@ impl MatrixServer { | ||
| 265 | 269 | let result = connection |
| 266 | 270 | .spawn(async move { |
| 267 | 271 | tokio::time::timeout(JOIN_ROOM_TIMEOUT, async move { |
| 268 | let servers = | |
| 269 | resolve_alias_servers(&client, &room_id_or_alias).await; | |
| 270 | ||
| 271 | client | |
| 272 | .join_room_by_id_or_alias(&room_id_or_alias, &servers) | |
| 272 | join_room_or_create_local_alias(&client, &room_id_or_alias) | |
| 273 | 273 | .await |
| 274 | 274 | }) |
| 275 | 275 | .await |
| @@ -286,8 +286,7 @@ impl MatrixServer { | ||
| 286 | 286 | Ok(Err(error)) => { |
| 287 | 287 | self.print_error(&format!( |
| 288 | 288 | "Failed to join {}: {}", |
| 289 | target, | |
| 290 | format_join_error(&error, &target) | |
| 289 | target, error | |
| 291 | 290 | )); |
| 292 | 291 | } |
| 293 | 292 | Err(error) => { |
| @@ -606,20 +605,88 @@ fn format_join_error(error: &Error, target: &str) -> String { | ||
| 606 | 605 | } |
| 607 | 606 | } |
| 608 | 607 | |
| 609 | async fn resolve_alias_servers( | |
| 608 | #[derive(Debug, Eq, PartialEq)] | |
| 609 | enum MissingAliasAction { | |
| 610 | CreateRoom { alias_localpart: String }, | |
| 611 | RefuseForeignAlias, | |
| 612 | RefuseUnknownAccountDomain, | |
| 613 | } | |
| 614 | ||
| 615 | fn missing_alias_action( | |
| 616 | alias: &RoomAliasId, | |
| 617 | account_user_id: Option<&UserId>, | |
| 618 | ) -> MissingAliasAction { | |
| 619 | match account_user_id { | |
| 620 | Some(user_id) if alias.server_name() == user_id.server_name() => { | |
| 621 | MissingAliasAction::CreateRoom { | |
| 622 | alias_localpart: alias.alias().to_owned(), | |
| 623 | } | |
| 624 | } | |
| 625 | Some(_) => MissingAliasAction::RefuseForeignAlias, | |
| 626 | None => MissingAliasAction::RefuseUnknownAccountDomain, | |
| 627 | } | |
| 628 | } | |
| 629 | ||
| 630 | fn create_room_request(alias_localpart: String) -> CreateRoomRequest { | |
| 631 | let mut request = CreateRoomRequest::new(); | |
| 632 | request.room_alias_name = Some(alias_localpart); | |
| 633 | request | |
| 634 | } | |
| 635 | ||
| 636 | async fn join_room_or_create_local_alias( | |
| 610 | 637 | client: &Client, |
| 611 | 638 | room_id_or_alias: &OwnedRoomOrAliasId, |
| 612 | ) -> Vec<OwnedServerName> { | |
| 639 | ) -> Result<Room, String> { | |
| 613 | 640 | let Ok(alias) = room_id_or_alias.as_str().parse::<OwnedRoomAliasId>() |
| 614 | 641 | else { |
| 615 | return Vec::new(); | |
| 642 | return client | |
| 643 | .join_room_by_id_or_alias(room_id_or_alias, &[]) | |
| 644 | .await | |
| 645 | .map_err(|error| { | |
| 646 | format_join_error(&error, room_id_or_alias.as_str()) | |
| 647 | }); | |
| 616 | 648 | }; |
| 617 | 649 | |
| 618 | client | |
| 619 | .resolve_room_alias(&alias) | |
| 620 | .await | |
| 621 | .map(|response| response.servers) | |
| 622 | .unwrap_or_default() | |
| 650 | match client.resolve_room_alias(&alias).await { | |
| 651 | Ok(response) => client | |
| 652 | .join_room_by_id_or_alias(room_id_or_alias, &response.servers) | |
| 653 | .await | |
| 654 | .map_err(|error| { | |
| 655 | format_join_error(&error, room_id_or_alias.as_str()) | |
| 656 | }), | |
| 657 | Err(error) | |
| 658 | if error.client_api_error_kind() == Some(&ErrorKind::NotFound) => | |
| 659 | { | |
| 660 | match missing_alias_action(&alias, client.user_id()) { | |
| 661 | MissingAliasAction::CreateRoom { alias_localpart } => { | |
| 662 | // `room_alias_name` makes createRoom register the alias as | |
| 663 | // part of room creation, so a failed registration cannot | |
| 664 | // leave a newly created, unaliased room behind. | |
| 665 | client | |
| 666 | .create_room(create_room_request(alias_localpart)) | |
| 667 | .await | |
| 668 | .map_err(|error| { | |
| 669 | format!( | |
| 670 | "Could not create local room with alias {}: {}", | |
| 671 | alias, | |
| 672 | format_join_error(&error, alias.as_str()) | |
| 673 | ) | |
| 674 | }) | |
| 675 | } | |
| 676 | MissingAliasAction::RefuseForeignAlias => Err(format!( | |
| 677 | "Room alias {} was not found and is not local to your account; refusing to create it.", | |
| 678 | alias | |
| 679 | )), | |
| 680 | MissingAliasAction::RefuseUnknownAccountDomain => Err(format!( | |
| 681 | "Room alias {} was not found, and the account domain is unavailable; refusing to create it.", | |
| 682 | alias | |
| 683 | )), | |
| 684 | } | |
| 685 | } | |
| 686 | Err(error) => { | |
| 687 | Err(format!("Could not resolve room alias {}: {}", alias, error)) | |
| 688 | } | |
| 689 | } | |
| 623 | 690 | } |
| 624 | 691 | |
| 625 | 692 | impl Drop for MatrixServer { |
| @@ -1910,7 +1977,11 @@ impl InnerServer { | ||
| 1910 | 1977 | |
| 1911 | 1978 | #[cfg(test)] |
| 1912 | 1979 | mod tests { |
| 1913 | use super::{with_entered_runtime_until_drop, InnerServer}; | |
| 1980 | use super::{ | |
| 1981 | create_room_request, missing_alias_action, | |
| 1982 | with_entered_runtime_until_drop, InnerServer, MissingAliasAction, | |
| 1983 | }; | |
| 1984 | use matrix_sdk::ruma::{OwnedRoomAliasId, OwnedUserId}; | |
| 1914 | 1985 | use std::path::{Path, PathBuf}; |
| 1915 | 1986 | use std::rc::Rc; |
| 1916 | 1987 | use std::sync::{ |
| @@ -1979,4 +2050,55 @@ mod tests { | ||
| 1979 | 2050 | |
| 1980 | 2051 | assert_eq!(drop_context.load(Ordering::SeqCst), DROPPED_INSIDE_RUNTIME); |
| 1981 | 2052 | } |
| 2053 | ||
| 2054 | #[test] | |
| 2055 | fn creates_only_missing_aliases_on_the_account_domain() { | |
| 2056 | let alias = "#new-room:example.org" | |
| 2057 | .parse::<OwnedRoomAliasId>() | |
| 2058 | .expect("valid room alias"); | |
| 2059 | let user_id = "@alice:example.org" | |
| 2060 | .parse::<OwnedUserId>() | |
| 2061 | .expect("valid user ID"); | |
| 2062 | ||
| 2063 | assert_eq!( | |
| 2064 | missing_alias_action(&alias, Some(&user_id)), | |
| 2065 | MissingAliasAction::CreateRoom { | |
| 2066 | alias_localpart: "new-room".to_owned(), | |
| 2067 | } | |
| 2068 | ); | |
| 2069 | } | |
| 2070 | ||
| 2071 | #[test] | |
| 2072 | fn refuses_missing_foreign_aliases() { | |
| 2073 | let alias = "#new-room:elsewhere.example" | |
| 2074 | .parse::<OwnedRoomAliasId>() | |
| 2075 | .expect("valid room alias"); | |
| 2076 | let user_id = "@alice:example.org" | |
| 2077 | .parse::<OwnedUserId>() | |
| 2078 | .expect("valid user ID"); | |
| 2079 | ||
| 2080 | assert_eq!( | |
| 2081 | missing_alias_action(&alias, Some(&user_id)), | |
| 2082 | MissingAliasAction::RefuseForeignAlias | |
| 2083 | ); | |
| 2084 | } | |
| 2085 | ||
| 2086 | #[test] | |
| 2087 | fn refuses_missing_alias_without_an_account_domain() { | |
| 2088 | let alias = "#new-room:example.org" | |
| 2089 | .parse::<OwnedRoomAliasId>() | |
| 2090 | .expect("valid room alias"); | |
| 2091 | ||
| 2092 | assert_eq!( | |
| 2093 | missing_alias_action(&alias, None), | |
| 2094 | MissingAliasAction::RefuseUnknownAccountDomain | |
| 2095 | ); | |
| 2096 | } | |
| 2097 | ||
| 2098 | #[test] | |
| 2099 | fn create_room_request_registers_the_alias_during_room_creation() { | |
| 2100 | let request = create_room_request("new-room".to_owned()); | |
| 2101 | ||
| 2102 | assert_eq!(request.room_alias_name.as_deref(), Some("new-room")); | |
| 2103 | } | |
| 1982 | 2104 | } |