From 7066b7b4280d132e948a5ca1868f0da20e55d16e Mon Sep 17 00:00:00 2001 From: strawberry Date: Wed, 20 Mar 2024 11:19:41 -0400 Subject: [PATCH] feat: automatically join rooms on registration Signed-off-by: strawberry --- conduwuit-example.toml | 5 +++++ src/api/client_server/account.rs | 31 +++++++++++++++++++++++++++-- src/api/client_server/membership.rs | 2 +- src/config/mod.rs | 12 ++++++++++- src/service/globals/mod.rs | 2 ++ 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/conduwuit-example.toml b/conduwuit-example.toml index decaf406..8ea1d478 100644 --- a/conduwuit-example.toml +++ b/conduwuit-example.toml @@ -263,6 +263,11 @@ url_preview_check_root_domain = false # Defaults to true as this is the fastest option for federation. #query_trusted_key_servers_first = true +# List/vector of room **IDs** that conduwuit will make newly registered users join. +# +# No default. +#auto_join_rooms = [] + ### Generic database options diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index 26016802..295a08e3 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -12,10 +12,13 @@ use ruma::{ events::{room::message::RoomMessageEventContent, GlobalAccountDataEventType}, push, UserId, }; -use tracing::{info, warn}; +use tracing::{error, info, warn}; 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, join_room_by_id_helper}, + services, utils, Error, Result, Ruma, +}; const RANDOM_USER_ID_LENGTH: usize = 10; @@ -285,6 +288,30 @@ pub async fn register_route(body: Ruma) -> Result { + info!("Automatically joined room {room} for user {user_id}"); + }, + Err(e) => { + // don't return this error so we don't fail registrations + error!("Failed to automatically join room {room} for user {user_id}: {e}"); + }, + }; + } + } + } + Ok(register::v3::Response { access_token: Some(token), user_id, diff --git a/src/api/client_server/membership.rs b/src/api/client_server/membership.rs index 96082f70..6a8f00d6 100644 --- a/src/api/client_server/membership.rs +++ b/src/api/client_server/membership.rs @@ -474,7 +474,7 @@ pub async fn joined_members_route(body: Ruma) -> Re }) } -async fn join_room_by_id_helper( +pub(crate) async fn join_room_by_id_helper( sender_user: Option<&UserId>, room_id: &RoomId, reason: Option, servers: &[OwnedServerName], _third_party_signed: Option<&ThirdPartySigned>, ) -> Result { diff --git a/src/config/mod.rs b/src/config/mod.rs index 604cb8d8..e5618766 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -10,7 +10,7 @@ use either::Either; use figment::Figment; use itertools::Itertools; use regex::RegexSet; -use ruma::{OwnedServerName, RoomVersionId}; +use ruma::{OwnedRoomId, OwnedServerName, RoomVersionId}; use serde::{de::IgnoredAny, Deserialize}; use tracing::{debug, error, warn}; @@ -110,6 +110,9 @@ pub struct Config { #[serde(default = "default_turn_ttl")] pub turn_ttl: u64, + #[serde(default = "Vec::new")] + pub auto_join_rooms: Vec, + #[serde(default = "default_rocksdb_log_level")] pub rocksdb_log_level: String, #[serde(default = "default_rocksdb_max_log_file_size")] @@ -364,6 +367,13 @@ impl fmt::Display for Config { } &lst.join(", ") }), + ("Auto Join Rooms", { + let mut lst = vec![]; + for room in &self.auto_join_rooms { + lst.push(room); + } + &lst.into_iter().join(", ") + }), #[cfg(feature = "compression-zstd")] ("zstd Response Body Compression", &self.zstd_compression.to_string()), #[cfg(feature = "rocksdb")] diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 6858631e..5a6c7ea6 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -327,6 +327,8 @@ impl Service<'_> { pub fn turn_secret(&self) -> &String { &self.config.turn_secret } + pub fn auto_join_rooms(&self) -> &[OwnedRoomId] { &self.config.auto_join_rooms } + pub fn notification_push_path(&self) -> &String { &self.config.notification_push_path } pub fn emergency_password(&self) -> &Option { &self.config.emergency_password }