remove some services() usage on startup for accessing config

Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
strawberry 2024-04-03 12:25:24 -04:00 committed by June
parent bd117bdf0d
commit 6800f91949

View file

@ -415,8 +415,8 @@ impl KeyValueDatabase {
// Matrix resource ownership is based on the server name; changing it // Matrix resource ownership is based on the server name; changing it
// requires recreating the database from scratch. // requires recreating the database from scratch.
if services().users.count()? > 0 { if services().users.count()? > 0 {
let conduit_user = UserId::parse_with_server_name("conduit", services().globals.server_name()) let conduit_user =
.expect("@conduit:server_name is valid"); UserId::parse_with_server_name("conduit", &config.server_name).expect("@conduit:server_name is valid");
if !services().users.exists(&conduit_user)? { if !services().users.exists(&conduit_user)? {
error!("The {} server user does not exist, and the database is not new.", conduit_user); error!("The {} server user does not exist, and the database is not new.", conduit_user);
@ -511,7 +511,7 @@ impl KeyValueDatabase {
for room in services().rooms.state_cache.rooms_joined(&our_user) { for room in services().rooms.state_cache.rooms_joined(&our_user) {
for user in services().rooms.state_cache.room_members(&room?) { for user in services().rooms.state_cache.room_members(&room?) {
let user = user?; let user = user?;
if user.server_name() != services().globals.server_name() { if user.server_name() != config.server_name {
info!(?user, "Migration: creating user"); info!(?user, "Migration: creating user");
services().users.create(&user, None)?; services().users.create(&user, None)?;
} }
@ -827,8 +827,7 @@ impl KeyValueDatabase {
if services().globals.database_version()? < 12 { if services().globals.database_version()? < 12 {
for username in services().users.list_local_users()? { for username in services().users.list_local_users()? {
let user = match UserId::parse_with_server_name(username.clone(), services().globals.server_name()) let user = match UserId::parse_with_server_name(username.clone(), &config.server_name) {
{
Ok(u) => u, Ok(u) => u,
Err(e) => { Err(e) => {
warn!("Invalid username {username}: {e}"); warn!("Invalid username {username}: {e}");
@ -898,8 +897,7 @@ impl KeyValueDatabase {
// updated. // updated.
if services().globals.database_version()? < 13 { if services().globals.database_version()? < 13 {
for username in services().users.list_local_users()? { for username in services().users.list_local_users()? {
let user = match UserId::parse_with_server_name(username.clone(), services().globals.server_name()) let user = match UserId::parse_with_server_name(username.clone(), &config.server_name) {
{
Ok(u) => u, Ok(u) => u,
Err(e) => { Err(e) => {
warn!("Invalid username {username}: {e}"); warn!("Invalid username {username}: {e}");
@ -967,14 +965,14 @@ impl KeyValueDatabase {
); );
{ {
let patterns = &services().globals.config.forbidden_usernames; let patterns = &config.forbidden_usernames;
if !patterns.is_empty() { if !patterns.is_empty() {
for user_id in services() for user_id in services()
.users .users
.iter() .iter()
.filter_map(Result::ok) .filter_map(Result::ok)
.filter(|user| !services().users.is_deactivated(user).unwrap_or(true)) .filter(|user| !services().users.is_deactivated(user).unwrap_or(true))
.filter(|user| user.server_name() == services().globals.server_name()) .filter(|user| user.server_name() == config.server_name)
{ {
let matches = patterns.matches(user_id.localpart()); let matches = patterns.matches(user_id.localpart());
if matches.matched_any() { if matches.matched_any() {
@ -992,7 +990,7 @@ impl KeyValueDatabase {
} }
{ {
let patterns = &services().globals.config.forbidden_alias_names; let patterns = &config.forbidden_alias_names;
if !patterns.is_empty() { if !patterns.is_empty() {
for address in services().rooms.metadata.iter_ids() { for address in services().rooms.metadata.iter_ids() {
let room_id = address?; let room_id = address?;
@ -1018,8 +1016,7 @@ impl KeyValueDatabase {
info!( info!(
"Loaded {} database with version {}", "Loaded {} database with version {}",
services().globals.config.database_backend, config.database_backend, latest_database_version
latest_database_version
); );
} else { } else {
services() services()
@ -1031,8 +1028,7 @@ impl KeyValueDatabase {
warn!( warn!(
"Created new {} database with version {}", "Created new {} database with version {}",
services().globals.config.database_backend, config.database_backend, latest_database_version
latest_database_version
); );
} }