From 40699c395ab0c1530aecba811f5ef2ade065d99a Mon Sep 17 00:00:00 2001 From: mikoto Date: Mon, 10 Jun 2024 16:07:47 +0200 Subject: [PATCH 01/17] PoC --- conduit-example.toml | 4 ++- src/api/client_server/account.rs | 27 ++++++++++++++- src/api/client_server/membership.rs | 2 +- src/config/mod.rs | 2 ++ src/database/mod.rs | 7 ++++ src/service/globals/mod.rs | 51 +++++++++++++++++++++++++++-- 6 files changed, 88 insertions(+), 5 deletions(-) diff --git a/conduit-example.toml b/conduit-example.toml index 74cbb074..558e62ab 100644 --- a/conduit-example.toml +++ b/conduit-example.toml @@ -42,7 +42,7 @@ allow_registration = true # an account. YOU NEED TO EDIT THIS. # - Insert a password that users will have to enter on registration # - Start the line with '#' to remove the condition -registration_token = "" +# registration_token = "" allow_check_for_updates = true allow_federation = true @@ -58,6 +58,8 @@ trusted_servers = ["matrix.org"] #max_concurrent_requests = 100 # How many requests Conduit sends to other servers at the same time +join_by_default = ["home", "!oN47OFomZSVRQVCLLw"] + # Controls the log verbosity. See also [here][0]. # # [0]: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index 47ccdc83..d89c60f7 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -1,5 +1,8 @@ use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH}; -use crate::{api::client_server, services, utils, Error, Result, Ruma}; +use crate::{ + api::client_server::{self, membership::join_room_by_id_helper}, + services, utils, Error, Result, Ruma, +}; use ruma::{ api::client::{ account::{ @@ -287,6 +290,28 @@ pub async fn register_route(body: Ruma) -> Result, room_id: &RoomId, reason: Option, diff --git a/src/config/mod.rs b/src/config/mod.rs index 378ab929..9d2b9e44 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -59,6 +59,8 @@ pub struct Config { pub allow_unstable_room_versions: bool, #[serde(default = "default_default_room_version")] pub default_room_version: RoomVersionId, + #[serde(default = "Vec::new")] + pub join_by_default: Vec, #[serde(default)] pub well_known: WellKnownConfig, #[serde(default = "false_fn")] diff --git a/src/database/mod.rs b/src/database/mod.rs index 5171d4bb..577182c5 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -969,6 +969,13 @@ impl KeyValueDatabase { // This data is probably outdated db.presenceid_presence.clear()?; + // Can only return an error during the first call + services().globals.join_by_default().map_err(|e| { + tracing::error!("Invalid room ID or alias in join-by-default rooms: {}", e); + + Error::bad_config("Invalid room ID or alias in join-by-default rooms.") + })?; + services().admin.start_handler(); // Set emergency access for the conduit user diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 3325e518..70a3d47c 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -1,9 +1,11 @@ mod data; pub use data::{Data, SigningKeys}; + use ruma::{ serde::Base64, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedEventId, OwnedRoomAliasId, OwnedRoomId, OwnedServerName, OwnedUserId, RoomAliasId, }; +use tokio::sync::OnceCell; use crate::api::server_server::DestinationResponse; @@ -17,7 +19,7 @@ use ruma::{ DeviceId, RoomVersionId, ServerName, UserId, }; use std::{ - collections::{BTreeMap, HashMap}, + collections::{BTreeMap, BTreeSet, HashMap}, error::Error as StdError, fs, future::{self, Future}, @@ -33,7 +35,7 @@ use std::{ }; use tokio::sync::{broadcast, watch::Receiver, Mutex, RwLock, Semaphore}; use tower_service::Service as TowerService; -use tracing::{error, info}; +use tracing::{error, info, warn}; use base64::{engine::general_purpose, Engine as _}; @@ -68,6 +70,7 @@ pub struct Service { pub roomid_mutex_state: RwLock>>>, pub roomid_mutex_federation: RwLock>>>, // this lock will be held longer pub roomid_federationhandletime: RwLock>, + join_by_default: OnceCell>, server_user: OwnedUserId, admin_alias: OwnedRoomAliasId, pub stateres_mutex: Arc>, @@ -221,6 +224,7 @@ impl Service { roomid_mutex_insert: RwLock::new(HashMap::new()), roomid_mutex_federation: RwLock::new(HashMap::new()), roomid_federationhandletime: RwLock::new(HashMap::new()), + join_by_default: OnceCell::new(), stateres_mutex: Arc::new(Mutex::new(())), sync_receivers: RwLock::new(HashMap::new()), rotate: RotationHandler::new(), @@ -505,6 +509,49 @@ impl Service { self.config.well_known_client() } + pub fn join_by_default(&self) -> Result<&BTreeSet> { + if let Some(set) = self.join_by_default.get() { + return Ok(set); + } + + let server_name = self.config.server_name.as_str(); + let mut join_by_default = BTreeSet::new(); + + for s in self.config.join_by_default.iter().cloned() { + let next = s + .chars() + .next() + .ok_or_else(|| Error::bad_config("Invalid room ID for join-by-default."))?; + + let room_id = if next == '!' { + let id = s.split(':').next().unwrap_or(&s); + + OwnedRoomId::from_str(&format!("{id}:{server_name}",)) + .map_err(|_| Error::bad_config("Invalid room ID for join-by-default rooms."))? + } else { + let id = s.split(':').next().unwrap_or(&s).trim_start_matches('#'); + + let alias = OwnedRoomAliasId::from_str(&format!("#{id}:{server_name}",)) + .map_err(|_| Error::bad_config("Invalid room alias for join-by-default."))?; + + let Some(room_id) = services().rooms.alias.resolve_local_alias(&alias)? else { + warn!("Could not resolve Room ID for join-by-default rooms locally."); + continue; + }; + + room_id + }; + + join_by_default.insert(room_id); + } + + self.join_by_default + .set(join_by_default) + .expect("join_by_default should be set once"); + + self.join_by_default() + } + pub fn shutdown(&self) { self.shutdown.store(true, atomic::Ordering::Relaxed); // On shutdown From f9ee8d7505162952f8e22c1edf1d90dc1b73e3b3 Mon Sep 17 00:00:00 2001 From: avdb Date: Mon, 17 Jun 2024 10:59:33 +0000 Subject: [PATCH 02/17] error messages --- src/api/client_server/account.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index d89c60f7..a3f7fe7b 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -307,9 +307,9 @@ pub async fn register_route(body: Ruma) -> Result Date: Mon, 17 Jun 2024 11:01:13 +0000 Subject: [PATCH 03/17] rename config value --- src/config/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 9d2b9e44..7b1f54b4 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -60,7 +60,7 @@ pub struct Config { #[serde(default = "default_default_room_version")] pub default_room_version: RoomVersionId, #[serde(default = "Vec::new")] - pub join_by_default: Vec, + pub default_rooms: Vec, #[serde(default)] pub well_known: WellKnownConfig, #[serde(default = "false_fn")] From 31102a43c5cf7c06367a8fca335abc3541332434 Mon Sep 17 00:00:00 2001 From: mikoto Date: Mon, 10 Jun 2024 18:22:20 +0200 Subject: [PATCH 04/17] docs --- conduit-example.toml | 8 +++++--- docs/configuration.md | 1 + 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/conduit-example.toml b/conduit-example.toml index 558e62ab..db0aa43a 100644 --- a/conduit-example.toml +++ b/conduit-example.toml @@ -42,7 +42,7 @@ allow_registration = true # an account. YOU NEED TO EDIT THIS. # - Insert a password that users will have to enter on registration # - Start the line with '#' to remove the condition -# registration_token = "" +registration_token = "" allow_check_for_updates = true allow_federation = true @@ -56,9 +56,11 @@ enable_lightning_bolt = true # servers.) trusted_servers = ["matrix.org"] -#max_concurrent_requests = 100 # How many requests Conduit sends to other servers at the same time +# Rooms listed here will be joined automatically after registration. +# See examples for accepted identifiers. +#join_by_default = ["#lounge:conduit.rs", "#lounge", "lounge", "!abc123:conduit.rs", "!abc123"] -join_by_default = ["home", "!oN47OFomZSVRQVCLLw"] +#max_concurrent_requests = 100 # How many requests Conduit sends to other servers at the same time # Controls the log verbosity. See also [here][0]. # diff --git a/docs/configuration.md b/docs/configuration.md index d903a21e..edc924f1 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -44,6 +44,7 @@ The `global` section contains the following fields: | `allow_room_creation` | `boolean` | Allow users to create rooms | `true` | | `allow_unstable_room_versions` | `boolean` | Allow users to create and join rooms with unstable versions | `true` | | `default_room_version` | `string` | The default room version (`"6"`-`"10"`)| `"10"` | +| `join_by_default` | `array` | Room aliases and IDs that will be joined by default at registration | N/A | | `allow_jaeger` | `boolean` | Allow Jaeger tracing | `false` | | `tracing_flame` | `boolean` | Enable flame tracing | `false` | | `proxy` | `table` | See the [Proxy configuration](#proxy) | N/A | From f0ca5253aaa4d709a058eb21ba81861c723b95dc Mon Sep 17 00:00:00 2001 From: mikoto Date: Mon, 17 Jun 2024 14:38:25 +0200 Subject: [PATCH 05/17] tokio::spawn does not run until completion of joins --- src/api/client_server/account.rs | 38 ++++++++++---------- src/config/mod.rs | 2 +- src/database/mod.rs | 2 +- src/service/globals/mod.rs | 59 +++++++++++++++++--------------- 4 files changed, 54 insertions(+), 47 deletions(-) diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index a3f7fe7b..3ce37fa7 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -291,27 +291,29 @@ pub async fn register_route(body: Ruma) -> Result, + pub default_rooms: Vec, #[serde(default)] pub well_known: WellKnownConfig, #[serde(default = "false_fn")] diff --git a/src/database/mod.rs b/src/database/mod.rs index 577182c5..ca2c7608 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -970,7 +970,7 @@ impl KeyValueDatabase { db.presenceid_presence.clear()?; // Can only return an error during the first call - services().globals.join_by_default().map_err(|e| { + services().globals.default_rooms().map_err(|e| { tracing::error!("Invalid room ID or alias in join-by-default rooms: {}", e); Error::bad_config("Invalid room ID or alias in join-by-default rooms.") diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 70a3d47c..94a2480d 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -70,7 +70,7 @@ pub struct Service { pub roomid_mutex_state: RwLock>>>, pub roomid_mutex_federation: RwLock>>>, // this lock will be held longer pub roomid_federationhandletime: RwLock>, - join_by_default: OnceCell>, + default_rooms: OnceCell>, server_user: OwnedUserId, admin_alias: OwnedRoomAliasId, pub stateres_mutex: Arc>, @@ -224,7 +224,7 @@ impl Service { roomid_mutex_insert: RwLock::new(HashMap::new()), roomid_mutex_federation: RwLock::new(HashMap::new()), roomid_federationhandletime: RwLock::new(HashMap::new()), - join_by_default: OnceCell::new(), + default_rooms: OnceCell::new(), stateres_mutex: Arc::new(Mutex::new(())), sync_receivers: RwLock::new(HashMap::new()), rotate: RotationHandler::new(), @@ -509,47 +509,52 @@ impl Service { self.config.well_known_client() } - pub fn join_by_default(&self) -> Result<&BTreeSet> { - if let Some(set) = self.join_by_default.get() { + pub fn default_rooms(&self) -> Result<&BTreeSet> { + if let Some(set) = self.default_rooms.get() { return Ok(set); } let server_name = self.config.server_name.as_str(); - let mut join_by_default = BTreeSet::new(); + let mut default_rooms = BTreeSet::new(); - for s in self.config.join_by_default.iter().cloned() { + for s in self.config.default_rooms.iter() { let next = s .chars() .next() - .ok_or_else(|| Error::bad_config("Invalid room ID for join-by-default."))?; + .ok_or_else(|| Error::bad_config("Invalid ID for join-by-default rooms."))?; - let room_id = if next == '!' { - let id = s.split(':').next().unwrap_or(&s); + // anything that does not start '!' should be considered an alias + let room_id = match next { + '!' => OwnedRoomId::from_str(&format!( + "{}:{server_name}", + s.split(':').next().unwrap_or(&s) + )) + .map_err(|_| Error::bad_config("Invalid ID for join-by-default rooms."))?, + _ => { + let Some(room_id) = OwnedRoomAliasId::from_str(&format!( + "#{}:{server_name}", + s.split(':').next().unwrap_or(&s).trim_start_matches('#') + )) + .map_err(|_| Error::bad_config("Invalid alias for join-by-default rooms.")) + .and_then(|alias| services().rooms.alias.resolve_local_alias(&alias))? + else { + warn!("Could not resolve Room ID for join-by-default rooms locally."); - OwnedRoomId::from_str(&format!("{id}:{server_name}",)) - .map_err(|_| Error::bad_config("Invalid room ID for join-by-default rooms."))? - } else { - let id = s.split(':').next().unwrap_or(&s).trim_start_matches('#'); + continue; + }; - let alias = OwnedRoomAliasId::from_str(&format!("#{id}:{server_name}",)) - .map_err(|_| Error::bad_config("Invalid room alias for join-by-default."))?; - - let Some(room_id) = services().rooms.alias.resolve_local_alias(&alias)? else { - warn!("Could not resolve Room ID for join-by-default rooms locally."); - continue; - }; - - room_id + room_id + } }; - join_by_default.insert(room_id); + default_rooms.insert(room_id); } - self.join_by_default - .set(join_by_default) - .expect("join_by_default should be set once"); + self.default_rooms + .set(default_rooms) + .expect("default_rooms should be set once"); - self.join_by_default() + self.default_rooms() } pub fn shutdown(&self) { From 2089ca8e3c69f3e0da6652f8107d11bd86cf0dfa Mon Sep 17 00:00:00 2001 From: avdb13 Date: Wed, 3 Jul 2024 07:23:31 +0200 Subject: [PATCH 06/17] joining remote rooms works too now --- src/api/client_server/account.rs | 44 +++++++++--------- src/database/mod.rs | 8 ++-- src/service/globals/mod.rs | 76 +++++++++++++++++++------------- 3 files changed, 71 insertions(+), 57 deletions(-) diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index 3ce37fa7..7c288b6e 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -1,3 +1,5 @@ +use std::ops::Deref; + use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH}; use crate::{ api::client_server::{self, membership::join_room_by_id_helper}, @@ -15,7 +17,7 @@ use ruma::{ uiaa::{AuthFlow, AuthType, UiaaInfo}, }, events::{room::message::RoomMessageEventContent, GlobalAccountDataEventType}, - push, UserId, + push, RoomId, UserId, }; use tracing::{info, warn}; @@ -291,29 +293,27 @@ pub async fn register_route(body: Ruma) -> Result = default_rooms + .iter() + .map(Deref::deref) + .filter_map(RoomId::server_name) + .map(Into::into) + .collect(); - let _user_id = user_id.clone(); - let servers = [services().globals.server_name().to_owned()]; - - tokio::spawn(async move { - for room_id in default_rooms { - let _ = join_room_by_id_helper( - Some(&_user_id), - room_id, - Some("All men are equal before fish.".to_owned()), - &servers, - None, - ) - .await - .inspect_err(|e| { - tracing::warn!("Failed to join default room: {e}"); - }); + for room_id in default_rooms { + if let Err(e) = join_room_by_id_helper( + Some(&user_id), + room_id, + Some("All men are equal before fish.".to_owned()), + &servers, + None, + ) + .await + { + warn!("Failed to join default room: {}", e); } - }); + } } Ok(register::v3::Response { diff --git a/src/database/mod.rs b/src/database/mod.rs index ca2c7608..b7cda860 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -970,11 +970,11 @@ impl KeyValueDatabase { db.presenceid_presence.clear()?; // Can only return an error during the first call - services().globals.default_rooms().map_err(|e| { - tracing::error!("Invalid room ID or alias in join-by-default rooms: {}", e); + // services().globals.default_rooms().await.map_err(|e| { + // tracing::error!("Invalid room ID or alias in join-by-default rooms: {}", e); - Error::bad_config("Invalid room ID or alias in join-by-default rooms.") - })?; + // Error::bad_config("Invalid room ID or alias in join-by-default rooms.") + // })?; services().admin.start_handler(); diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 94a2480d..87215be6 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -10,7 +10,7 @@ use tokio::sync::OnceCell; use crate::api::server_server::DestinationResponse; use crate::{services, Config, Error, Result}; -use futures_util::FutureExt; +use futures_util::{FutureExt, TryFutureExt}; use hickory_resolver::TokioAsyncResolver; use hyper_util::client::legacy::connect::dns::{GaiResolver, Name as HyperName}; use reqwest::dns::{Addrs, Name, Resolve, Resolving}; @@ -509,41 +509,55 @@ impl Service { self.config.well_known_client() } - pub fn default_rooms(&self) -> Result<&BTreeSet> { - if let Some(set) = self.default_rooms.get() { - return Ok(set); + pub async fn default_rooms(&self) -> Result<&BTreeSet> { + if let Some(default_rooms) = self.default_rooms.get() { + return Ok(default_rooms); } - let server_name = self.config.server_name.as_str(); let mut default_rooms = BTreeSet::new(); - for s in self.config.default_rooms.iter() { - let next = s - .chars() - .next() - .ok_or_else(|| Error::bad_config("Invalid ID for join-by-default rooms."))?; - + for mut alias_or_id in self.config.default_rooms.iter().cloned() { // anything that does not start '!' should be considered an alias - let room_id = match next { - '!' => OwnedRoomId::from_str(&format!( - "{}:{server_name}", - s.split(':').next().unwrap_or(&s) - )) - .map_err(|_| Error::bad_config("Invalid ID for join-by-default rooms."))?, - _ => { - let Some(room_id) = OwnedRoomAliasId::from_str(&format!( - "#{}:{server_name}", - s.split(':').next().unwrap_or(&s).trim_start_matches('#') - )) - .map_err(|_| Error::bad_config("Invalid alias for join-by-default rooms.")) - .and_then(|alias| services().rooms.alias.resolve_local_alias(&alias))? - else { - warn!("Could not resolve Room ID for join-by-default rooms locally."); + // empty strings are ignored + let room_id = if Some('!') == alias_or_id.chars().next() { + if alias_or_id.split_once(':').is_none() { + alias_or_id = format!("{}:{}", alias_or_id, self.config.server_name); + } - continue; - }; + OwnedRoomId::from_str(&alias_or_id).map_err(|e| { + warn!( + "Invalid room ID ({}) for join-by-default rooms: {}", + alias_or_id, e + ); - room_id + Error::bad_config("Invalid room ID for join-by-default rooms.") + })? + } else { + if alias_or_id.split_once(':').is_none() { + alias_or_id = format!("#{}:{}", alias_or_id, self.config.server_name); + } + + let room_alias = OwnedRoomAliasId::from_str(&alias_or_id).map_err(|e| { + warn!( + "Invalid room alias ({}) for join-by-default rooms: {}", + alias_or_id, e + ); + + Error::bad_config("Invalid room alias for join-by-default rooms.") + })?; + + if room_alias.server_name() == &self.config.server_name { + services() + .rooms + .alias + .resolve_local_alias(&room_alias)? + .ok_or_else(|| { + Error::bad_config("Unknown alias for join-by-default rooms.") + })? + } else { + get_alias_helper(room_alias.clone()) + .await + .map(|res| res.room_id)? } }; @@ -552,9 +566,9 @@ impl Service { self.default_rooms .set(default_rooms) - .expect("default_rooms should be set once"); + .expect("default_rooms should not be set already"); - self.default_rooms() + Ok(self.default_rooms.get().unwrap()) } pub fn shutdown(&self) { From 7e5406602398f53066a117a6a440cba8a782bd23 Mon Sep 17 00:00:00 2001 From: avdb13 Date: Wed, 3 Jul 2024 07:51:12 +0200 Subject: [PATCH 07/17] make_join delegation --- src/api/client_server/account.rs | 27 +++++++++++++++------------ src/service/globals/mod.rs | 6 +++--- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index 7c288b6e..5ae2b566 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -293,6 +293,7 @@ pub async fn register_route(body: Ruma) -> Result = default_rooms .iter() @@ -301,19 +302,21 @@ pub async fn register_route(body: Ruma) -> Result Date: Wed, 3 Jul 2024 08:11:35 +0200 Subject: [PATCH 08/17] docs --- docs/configuration.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index edc924f1..b32b7abd 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -44,6 +44,7 @@ The `global` section contains the following fields: | `allow_room_creation` | `boolean` | Allow users to create rooms | `true` | | `allow_unstable_room_versions` | `boolean` | Allow users to create and join rooms with unstable versions | `true` | | `default_room_version` | `string` | The default room version (`"6"`-`"10"`)| `"10"` | +| `default_rooms` | `array` | The list of rooms that will be joined by default on registration | N/A | | `join_by_default` | `array` | Room aliases and IDs that will be joined by default at registration | N/A | | `allow_jaeger` | `boolean` | Allow Jaeger tracing | `false` | | `tracing_flame` | `boolean` | Enable flame tracing | `false` | @@ -110,3 +111,15 @@ exclude = ["*.clearnet.onion"] [global] {{#include ../conduit-example.toml:22:}} ``` + + +### Default rooms +There is a shorthand syntax specialized for convenience for these values. The server name is optional for local room IDs. +The same rule applies to local aliases in addition to not requiring the pound sign prefix. + +#### Example +In this example, the two configuration options are equivalent: +```toml +default_rooms = ["#home", "lounge", "!abc123", "#conduit:fachschaften.org"] +default_rooms = ["#home:conduit.rs", "#lounge:conduit.rs", "!abc123:conduit.rs", "#conduit:fachschaften.org"] +``` From a51a4fdef9995fd1ae1463acb5a410bccd5c6ff4 Mon Sep 17 00:00:00 2001 From: avdb13 Date: Thu, 4 Jul 2024 20:14:06 +0200 Subject: [PATCH 09/17] reviewed changes --- conduit-example.toml | 4 -- docs/configuration.md | 3 +- src/api/client_server/account.rs | 37 ++++++--------- src/config/mod.rs | 4 +- src/database/mod.rs | 7 --- src/service/globals/mod.rs | 79 +++++++++----------------------- 6 files changed, 39 insertions(+), 95 deletions(-) diff --git a/conduit-example.toml b/conduit-example.toml index db0aa43a..74cbb074 100644 --- a/conduit-example.toml +++ b/conduit-example.toml @@ -56,10 +56,6 @@ enable_lightning_bolt = true # servers.) trusted_servers = ["matrix.org"] -# Rooms listed here will be joined automatically after registration. -# See examples for accepted identifiers. -#join_by_default = ["#lounge:conduit.rs", "#lounge", "lounge", "!abc123:conduit.rs", "!abc123"] - #max_concurrent_requests = 100 # How many requests Conduit sends to other servers at the same time # Controls the log verbosity. See also [here][0]. diff --git a/docs/configuration.md b/docs/configuration.md index b32b7abd..93b26576 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -43,9 +43,8 @@ The `global` section contains the following fields: | `allow_federation` | `boolean` | Allow federation with other servers | `true` | | `allow_room_creation` | `boolean` | Allow users to create rooms | `true` | | `allow_unstable_room_versions` | `boolean` | Allow users to create and join rooms with unstable versions | `true` | +| `default_rooms` | `array` | The list of rooms that will be joined by default on registration | [] | | `default_room_version` | `string` | The default room version (`"6"`-`"10"`)| `"10"` | -| `default_rooms` | `array` | The list of rooms that will be joined by default on registration | N/A | -| `join_by_default` | `array` | Room aliases and IDs that will be joined by default at registration | N/A | | `allow_jaeger` | `boolean` | Allow Jaeger tracing | `false` | | `tracing_flame` | `boolean` | Enable flame tracing | `false` | | `proxy` | `table` | See the [Proxy configuration](#proxy) | N/A | diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index 5ae2b566..6b0cb66a 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -1,5 +1,3 @@ -use std::ops::Deref; - use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH}; use crate::{ api::client_server::{self, membership::join_room_by_id_helper}, @@ -17,7 +15,7 @@ use ruma::{ uiaa::{AuthFlow, AuthType, UiaaInfo}, }, events::{room::message::RoomMessageEventContent, GlobalAccountDataEventType}, - push, RoomId, UserId, + push, UserId, }; use tracing::{info, warn}; @@ -66,7 +64,7 @@ pub async fn get_register_available_route( Ok(get_username_availability::v3::Response { available: true }) } -/// # `POST /_matrix/client/r0/register` +/// # `POST /_matrix/client/r0/register /// /// Register an account on this homeserver. /// @@ -293,30 +291,25 @@ pub async fn register_route(body: Ruma) -> Result = default_rooms .iter() - .map(Deref::deref) - .filter_map(RoomId::server_name) - .map(Into::into) + .filter_map(|r| r.server_name().map(ToOwned::to_owned)) .collect(); - tokio::spawn(async move { - for room_id in default_rooms { - if let Err(e) = join_room_by_id_helper( - Some(&_user_id), - room_id, - Some("All men are equal before fish.".to_owned()), - &servers, - None, - ) - .await - { - warn!("Failed to join default room: {}", e); - } + for room_id in &default_rooms { + if let Err(e) = join_room_by_id_helper( + Some(&user_id), + room_id, + Some("All men are equal before fish.".to_owned()), + &servers, + None, + ) + .await + { + warn!("Failed to join default room: {}", e); } - }); + } } Ok(register::v3::Response { diff --git a/src/config/mod.rs b/src/config/mod.rs index 73a7eaa2..7f2d7e86 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -4,7 +4,7 @@ use std::{ net::{IpAddr, Ipv4Addr}, }; -use ruma::{OwnedServerName, RoomVersionId}; +use ruma::{OwnedRoomOrAliasId, OwnedServerName, RoomVersionId}; use serde::{de::IgnoredAny, Deserialize}; use tracing::warn; use url::Url; @@ -60,7 +60,7 @@ pub struct Config { #[serde(default = "default_default_room_version")] pub default_room_version: RoomVersionId, #[serde(default = "Vec::new")] - pub default_rooms: Vec, + pub default_rooms: Vec, #[serde(default)] pub well_known: WellKnownConfig, #[serde(default = "false_fn")] diff --git a/src/database/mod.rs b/src/database/mod.rs index b7cda860..5171d4bb 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -969,13 +969,6 @@ impl KeyValueDatabase { // This data is probably outdated db.presenceid_presence.clear()?; - // Can only return an error during the first call - // services().globals.default_rooms().await.map_err(|e| { - // tracing::error!("Invalid room ID or alias in join-by-default rooms: {}", e); - - // Error::bad_config("Invalid room ID or alias in join-by-default rooms.") - // })?; - services().admin.start_handler(); // Set emergency access for the conduit user diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index e1ee109c..367c4b4c 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -3,11 +3,10 @@ pub use data::{Data, SigningKeys}; use ruma::{ serde::Base64, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedEventId, OwnedRoomAliasId, - OwnedRoomId, OwnedServerName, OwnedUserId, RoomAliasId, + OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, OwnedUserId, RoomAliasId, }; -use tokio::sync::OnceCell; -use crate::api::server_server::DestinationResponse; +use crate::api::{client_server::get_alias_helper, server_server::DestinationResponse}; use crate::{services, Config, Error, Result}; use futures_util::FutureExt; @@ -70,7 +69,6 @@ pub struct Service { pub roomid_mutex_state: RwLock>>>, pub roomid_mutex_federation: RwLock>>>, // this lock will be held longer pub roomid_federationhandletime: RwLock>, - default_rooms: OnceCell>, server_user: OwnedUserId, admin_alias: OwnedRoomAliasId, pub stateres_mutex: Arc>, @@ -224,7 +222,6 @@ impl Service { roomid_mutex_insert: RwLock::new(HashMap::new()), roomid_mutex_federation: RwLock::new(HashMap::new()), roomid_federationhandletime: RwLock::new(HashMap::new()), - default_rooms: OnceCell::new(), stateres_mutex: Arc::new(Mutex::new(())), sync_receivers: RwLock::new(HashMap::new()), rotate: RotationHandler::new(), @@ -509,66 +506,32 @@ impl Service { self.config.well_known_client() } - pub async fn default_rooms(&self) -> Result<&BTreeSet> { - if let Some(default_rooms) = self.default_rooms.get() { - return Ok(default_rooms); - } - + pub async fn default_rooms(&self) -> Result> { let mut default_rooms = BTreeSet::new(); - for mut alias_or_id in self.config.default_rooms.iter().cloned() { - // anything that does not start '!' should be considered an alias - // empty strings are ignored - let room_id = if alias_or_id.starts_with('!') { - if alias_or_id.split_once(':').is_none() { - alias_or_id = format!("{}:{}", alias_or_id, self.config.server_name); - } + for id_or_alias in &self.config.default_rooms { + // OwnedRoomOrAliasId always starts with a '#' or '!', presence of a ':' is not validated + let localpart = id_or_alias + .as_str() + .split_once(':') + .map(|(localpart, _)| localpart) + .unwrap_or(id_or_alias.as_str()); + let server_name = id_or_alias + .server_name() + .map(ToOwned::to_owned) + .unwrap_or(self.config.server_name.clone()); - OwnedRoomId::from_str(&alias_or_id).map_err(|e| { - warn!( - "Invalid room ID ({}) for join-by-default rooms: {}", - alias_or_id, e - ); - - Error::bad_config("Invalid room ID for join-by-default rooms.") - })? - } else { - if alias_or_id.split_once(':').is_none() { - alias_or_id = format!("#{}:{}", alias_or_id, self.config.server_name); - } - - let room_alias = OwnedRoomAliasId::from_str(&alias_or_id).map_err(|e| { - warn!( - "Invalid room alias ({}) for join-by-default rooms: {}", - alias_or_id, e - ); - - Error::bad_config("Invalid room alias for join-by-default rooms.") - })?; - - if room_alias.server_name() == self.config.server_name { - services() - .rooms - .alias - .resolve_local_alias(&room_alias)? - .ok_or_else(|| { - Error::bad_config("Unknown alias for join-by-default rooms.") - })? - } else { - get_alias_helper(room_alias.clone()) - .await - .map(|res| res.room_id)? - } + let room_id = match OwnedRoomOrAliasId::from_str(&format!("{localpart}:{server_name}")) + .expect("this should always be valid") + .try_into() + { + Ok(room_id) => room_id, + Err(room_alias) => get_alias_helper(room_alias).await.map(|res| res.room_id)?, }; - default_rooms.insert(room_id); } - self.default_rooms - .set(default_rooms) - .expect("default_rooms should not be set already"); - - Ok(self.default_rooms.get().unwrap()) + Ok(default_rooms) } pub fn shutdown(&self) { From b24da3ec259470cc145b2742f69a18b6d6532b47 Mon Sep 17 00:00:00 2001 From: avdb13 Date: Thu, 4 Jul 2024 20:20:11 +0200 Subject: [PATCH 10/17] small correction --- docs/configuration.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 93b26576..a42fc33a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -113,12 +113,12 @@ exclude = ["*.clearnet.onion"] ### Default rooms -There is a shorthand syntax specialized for convenience for these values. The server name is optional for local room IDs. -The same rule applies to local aliases in addition to not requiring the pound sign prefix. #### Example +Since the validation of room aliases and IDs does validate the presence of a colon separator, +this symbol including the server name can be ommitted for local rooms. In this example, the two configuration options are equivalent: ```toml -default_rooms = ["#home", "lounge", "!abc123", "#conduit:fachschaften.org"] -default_rooms = ["#home:conduit.rs", "#lounge:conduit.rs", "!abc123:conduit.rs", "#conduit:fachschaften.org"] +default_rooms = ["#home", "!abc123", "#conduit:fachschaften.org"] +default_rooms = ["#home:conduit.rs", "!abc123:conduit.rs", "#conduit:fachschaften.org"] ``` From b4583ed1da4069fea39b54716f31a25514e31cdb Mon Sep 17 00:00:00 2001 From: avdb13 Date: Thu, 4 Jul 2024 20:20:49 +0200 Subject: [PATCH 11/17] big correction --- docs/configuration.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index a42fc33a..e80ac214 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -113,10 +113,10 @@ exclude = ["*.clearnet.onion"] ### Default rooms - -#### Example Since the validation of room aliases and IDs does validate the presence of a colon separator, this symbol including the server name can be ommitted for local rooms. + +#### Example In this example, the two configuration options are equivalent: ```toml default_rooms = ["#home", "!abc123", "#conduit:fachschaften.org"] From 7fd778a6f28f5f43d3aebedbe33125cecbbf89b8 Mon Sep 17 00:00:00 2001 From: avdb13 Date: Thu, 4 Jul 2024 20:28:30 +0200 Subject: [PATCH 12/17] promising this to be the last change --- docs/configuration.md | 1 + src/api/client_server/account.rs | 10 +++++++++- src/config/mod.rs | 5 +++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index e80ac214..2036744e 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -44,6 +44,7 @@ The `global` section contains the following fields: | `allow_room_creation` | `boolean` | Allow users to create rooms | `true` | | `allow_unstable_room_versions` | `boolean` | Allow users to create and join rooms with unstable versions | `true` | | `default_rooms` | `array` | The list of rooms that will be joined by default on registration | [] | +| `default_rooms_join_reason` | `string` | The reason for joining the rooms | "Hello from Conduit!" | | `default_room_version` | `string` | The default room version (`"6"`-`"10"`)| `"10"` | | `allow_jaeger` | `boolean` | Allow Jaeger tracing | `false` | | `tracing_flame` | `boolean` | Enable flame tracing | `false` | diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index 6b0cb66a..b3f17a62 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -301,7 +301,15 @@ pub async fn register_route(body: Ruma) -> Result, + pub default_rooms_join_reason: Option, + #[serde(default = "default_default_room_version")] + pub default_room_version: RoomVersionId, #[serde(default)] pub well_known: WellKnownConfig, #[serde(default = "false_fn")] From 4c726f4a68ed03f6c54197ccc1b9d531c4e6f659 Mon Sep 17 00:00:00 2001 From: avdb Date: Sat, 6 Jul 2024 13:47:43 +0000 Subject: [PATCH 13/17] Apply 1 suggestion(s) to 1 file(s) Co-authored-by: Matthias Ahouansou --- src/api/client_server/account.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index b3f17a62..37e3f99b 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -301,15 +301,11 @@ pub async fn register_route(body: Ruma) -> Result Date: Sat, 6 Jul 2024 16:29:13 +0200 Subject: [PATCH 14/17] ??? --- src/api/client_server/account.rs | 8 ++------ src/config/mod.rs | 11 ++++++++--- src/service/globals/mod.rs | 29 +++++++++++++++-------------- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index 37e3f99b..152b82bd 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -64,7 +64,7 @@ pub async fn get_register_available_route( Ok(get_username_availability::v3::Response { available: true }) } -/// # `POST /_matrix/client/r0/register +/// # `POST /_matrix/client/r0/register` /// /// Register an account on this homeserver. /// @@ -301,11 +301,7 @@ pub async fn register_route(body: Ruma) -> Result, - pub default_rooms_join_reason: Option, #[serde(default = "default_default_room_version")] pub default_room_version: RoomVersionId, #[serde(default)] pub well_known: WellKnownConfig, + #[serde(default)] + pub default_rooms: DefaultRoomsConfig, #[serde(default = "false_fn")] pub allow_jaeger: bool, #[serde(default = "false_fn")] @@ -104,6 +103,12 @@ pub struct WellKnownConfig { pub server: Option, } +#[derive(Clone, Debug, Deserialize, Default)] +pub struct DefaultRoomsConfig { + pub rooms: Vec, + pub join_reason: Option, +} + const DEPRECATED_KEYS: &[&str] = &["cache_capacity"]; impl Config { diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 367c4b4c..2ff2ff41 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -509,21 +509,22 @@ impl Service { pub async fn default_rooms(&self) -> Result> { let mut default_rooms = BTreeSet::new(); - for id_or_alias in &self.config.default_rooms { - // OwnedRoomOrAliasId always starts with a '#' or '!', presence of a ':' is not validated - let localpart = id_or_alias - .as_str() - .split_once(':') - .map(|(localpart, _)| localpart) - .unwrap_or(id_or_alias.as_str()); - let server_name = id_or_alias - .server_name() - .map(ToOwned::to_owned) - .unwrap_or(self.config.server_name.clone()); + for id_or_alias in &self.config.default_rooms.rooms { + // // `OwnedRoomOrAliasId` always starts with a '#' or '!', presence of a ':' is not validated + // let localpart = id_or_alias + // .as_str() + // .split_once(':') + // .map(|(localpart, _)| localpart) + // .unwrap_or(id_or_alias.as_str()); + // let server_name = id_or_alias + // .server_name() + // .map(ToOwned::to_owned) + // .unwrap_or(self.config.server_name.clone()); - let room_id = match OwnedRoomOrAliasId::from_str(&format!("{localpart}:{server_name}")) - .expect("this should always be valid") - .try_into() + let room_id = match id_or_alias.try_into() + // OwnedRoomOrAliasId::from_str(&format!("{localpart}:{server_name}")) + // .expect("this should always be valid") + // .try_into() { Ok(room_id) => room_id, Err(room_alias) => get_alias_helper(room_alias).await.map(|res| res.room_id)?, From 5e28b9158f587cae9fa6cb428c39c5ada7cfd1bc Mon Sep 17 00:00:00 2001 From: avdb13 Date: Sat, 6 Jul 2024 22:26:33 +0200 Subject: [PATCH 15/17] ok --- docs/configuration.md | 9 ++++--- src/config/mod.rs | 8 +++++-- src/service/globals/mod.rs | 49 +++++++++++++++++++------------------- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 2036744e..1810583f 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -114,12 +114,11 @@ exclude = ["*.clearnet.onion"] ### Default rooms -Since the validation of room aliases and IDs does validate the presence of a colon separator, -this symbol including the server name can be ommitted for local rooms. +This list should contain either room identifiers or aliases. +Opaque identifiers will be interpreted as incomplete, local aliases. #### Example -In this example, the two configuration options are equivalent: +In this example, the opaque identifier and alias are the same: ```toml -default_rooms = ["#home", "!abc123", "#conduit:fachschaften.org"] -default_rooms = ["#home:conduit.rs", "!abc123:conduit.rs", "#conduit:fachschaften.org"] +default_rooms = ["home", "#home:conduit.rs"] ``` diff --git a/src/config/mod.rs b/src/config/mod.rs index 71aa6176..4ab328cf 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -2,15 +2,18 @@ use std::{ collections::BTreeMap, fmt, net::{IpAddr, Ipv4Addr}, + str::FromStr, }; -use ruma::{OwnedRoomOrAliasId, OwnedServerName, RoomVersionId}; +use ruma::{serde::Raw, OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, RoomVersionId}; use serde::{de::IgnoredAny, Deserialize}; use tracing::warn; use url::Url; mod proxy; +use crate::Error; + use self::proxy::ProxyConfig; #[derive(Clone, Debug, Deserialize)] @@ -105,7 +108,8 @@ pub struct WellKnownConfig { #[derive(Clone, Debug, Deserialize, Default)] pub struct DefaultRoomsConfig { - pub rooms: Vec, + #[serde(default = "Vec::new")] + pub rooms: Vec, pub join_reason: Option, } diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 2ff2ff41..90d6df4a 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -1,9 +1,10 @@ mod data; pub use data::{Data, SigningKeys}; +use hmac::digest::typenum::Pow; use ruma::{ serde::Base64, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedEventId, OwnedRoomAliasId, - OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, OwnedUserId, RoomAliasId, + OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, OwnedUserId, RoomAliasId, RoomId, }; use crate::api::{client_server::get_alias_helper, server_server::DestinationResponse}; @@ -24,6 +25,7 @@ use std::{ future::{self, Future}, iter, net::{IpAddr, SocketAddr}, + ops::Deref, path::PathBuf, str::FromStr, sync::{ @@ -507,32 +509,31 @@ impl Service { } pub async fn default_rooms(&self) -> Result> { - let mut default_rooms = BTreeSet::new(); + let server_name = &self.config.server_name; - for id_or_alias in &self.config.default_rooms.rooms { - // // `OwnedRoomOrAliasId` always starts with a '#' or '!', presence of a ':' is not validated - // let localpart = id_or_alias - // .as_str() - // .split_once(':') - // .map(|(localpart, _)| localpart) - // .unwrap_or(id_or_alias.as_str()); - // let server_name = id_or_alias - // .server_name() - // .map(ToOwned::to_owned) - // .unwrap_or(self.config.server_name.clone()); + let fallback = || { + format!("#{s}:{server_name}") + .parse() + .map_err(|_| Error::bad_config("Invalid opaque identifier.")) + }; - let room_id = match id_or_alias.try_into() - // OwnedRoomOrAliasId::from_str(&format!("{localpart}:{server_name}")) - // .expect("this should always be valid") - // .try_into() - { - Ok(room_id) => room_id, - Err(room_alias) => get_alias_helper(room_alias).await.map(|res| res.room_id)?, - }; - default_rooms.insert(room_id); - } + let f = |s| match OwnedRoomOrAliasId::from_str(s).map(OwnedRoomId::try_from) { + Ok(Ok(room_id)) => room_id + .server_name() + .ok_or_else(|| Error::bad_config("Invalid room ID due to missing server name.")) + .map(|_| room_id), + result => get_alias_helper(result.map(Result::unwrap_err).or_else(fallback)?) + .await + .map(|response| response.room_id), + }; - Ok(default_rooms) + self.config + .default_rooms + .rooms + .iter() + .map(String::as_str) + .map(|s| f) + .collect::>() } pub fn shutdown(&self) { From f6e9cadfae581eb72bddba4bc1b4a136997d2ef4 Mon Sep 17 00:00:00 2001 From: avdb13 Date: Sat, 6 Jul 2024 22:38:25 +0200 Subject: [PATCH 16/17] ok... --- src/service/globals/mod.rs | 40 +++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 90d6df4a..9bf325d5 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -1,10 +1,9 @@ mod data; pub use data::{Data, SigningKeys}; -use hmac::digest::typenum::Pow; use ruma::{ serde::Base64, MilliSecondsSinceUnixEpoch, OwnedDeviceId, OwnedEventId, OwnedRoomAliasId, - OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, OwnedUserId, RoomAliasId, RoomId, + OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, OwnedUserId, RoomAliasId, }; use crate::api::{client_server::get_alias_helper, server_server::DestinationResponse}; @@ -25,7 +24,6 @@ use std::{ future::{self, Future}, iter, net::{IpAddr, SocketAddr}, - ops::Deref, path::PathBuf, str::FromStr, sync::{ @@ -511,29 +509,27 @@ impl Service { pub async fn default_rooms(&self) -> Result> { let server_name = &self.config.server_name; - let fallback = || { - format!("#{s}:{server_name}") - .parse() - .map_err(|_| Error::bad_config("Invalid opaque identifier.")) - }; - - let f = |s| match OwnedRoomOrAliasId::from_str(s).map(OwnedRoomId::try_from) { - Ok(Ok(room_id)) => room_id - .server_name() - .ok_or_else(|| Error::bad_config("Invalid room ID due to missing server name.")) - .map(|_| room_id), - result => get_alias_helper(result.map(Result::unwrap_err).or_else(fallback)?) + let f = |s| async move { + match OwnedRoomOrAliasId::from_str(s).map(OwnedRoomId::try_from) { + Ok(Ok(room_id)) => room_id + .server_name() + .ok_or_else(|| Error::bad_config("Invalid room ID due to missing server name.")) + .map(|_| room_id.clone()), + result => get_alias_helper(result.map(Result::unwrap_err).or_else(|_| { + format!("#{s}:{server_name}") + .parse() + .map_err(|_| Error::bad_config("Invalid opaque identifier.")) + })?) .await .map(|response| response.room_id), + } }; - self.config - .default_rooms - .rooms - .iter() - .map(String::as_str) - .map(|s| f) - .collect::>() + let rooms = self.config.default_rooms.rooms.iter().map(String::as_str); + futures_util::future::try_join_all(rooms.map(f)) + .await + .map(Vec::into_iter) + .map(BTreeSet::from_iter) } pub fn shutdown(&self) { From 33a8de934a52f9baeff2a726d98f7f1532530091 Mon Sep 17 00:00:00 2001 From: avdb13 Date: Sat, 6 Jul 2024 22:40:21 +0200 Subject: [PATCH 17/17] unused import --- src/config/mod.rs | 5 +---- src/service/globals/mod.rs | 1 + 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 4ab328cf..5f5355a1 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -2,18 +2,15 @@ use std::{ collections::BTreeMap, fmt, net::{IpAddr, Ipv4Addr}, - str::FromStr, }; -use ruma::{serde::Raw, OwnedRoomId, OwnedRoomOrAliasId, OwnedServerName, RoomVersionId}; +use ruma::{OwnedServerName, RoomVersionId}; use serde::{de::IgnoredAny, Deserialize}; use tracing::warn; use url::Url; mod proxy; -use crate::Error; - use self::proxy::ProxyConfig; #[derive(Clone, Debug, Deserialize)] diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 9bf325d5..d7306569 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -526,6 +526,7 @@ impl Service { }; let rooms = self.config.default_rooms.rooms.iter().map(String::as_str); + futures_util::future::try_join_all(rooms.map(f)) .await .map(Vec::into_iter)