remove several services.globals config wrappers

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-03-31 02:28:01 +00:00
parent d60920c728
commit d3b65af616
12 changed files with 34 additions and 84 deletions

View file

@ -146,7 +146,7 @@ pub(crate) async fn register_route(
let is_guest = body.kind == RegistrationKind::Guest; let is_guest = body.kind == RegistrationKind::Guest;
let emergency_mode_enabled = services.config.emergency_password.is_some(); let emergency_mode_enabled = services.config.emergency_password.is_some();
if !services.globals.allow_registration() && body.appservice_info.is_none() { if !services.config.allow_registration && body.appservice_info.is_none() {
match (body.username.as_ref(), body.initial_device_display_name.as_ref()) { match (body.username.as_ref(), body.initial_device_display_name.as_ref()) {
| (Some(username), Some(device_display_name)) => { | (Some(username), Some(device_display_name)) => {
info!(%is_guest, user = %username, device_name = %device_display_name, "Rejecting registration attempt as registration is disabled"); info!(%is_guest, user = %username, device_name = %device_display_name, "Rejecting registration attempt as registration is disabled");
@ -166,8 +166,8 @@ pub(crate) async fn register_route(
} }
if is_guest if is_guest
&& (!services.globals.allow_guest_registration() && (!services.config.allow_guest_registration
|| (services.globals.allow_registration() || (services.config.allow_registration
&& services.globals.registration_token.is_some())) && services.globals.registration_token.is_some()))
{ {
info!( info!(
@ -441,7 +441,7 @@ pub(crate) async fn register_route(
} }
// log in conduit admin channel if a guest registered // log in conduit admin channel if a guest registered
if body.appservice_info.is_none() && is_guest && services.globals.log_guest_registrations() { if body.appservice_info.is_none() && is_guest && services.config.log_guest_registrations {
debug_info!("New guest user \"{user_id}\" registered on this server."); debug_info!("New guest user \"{user_id}\" registered on this server.");
if !device_display_name.is_empty() { if !device_display_name.is_empty() {
@ -490,7 +490,7 @@ pub(crate) async fn register_route(
if body.appservice_info.is_none() if body.appservice_info.is_none()
&& !services.server.config.auto_join_rooms.is_empty() && !services.server.config.auto_join_rooms.is_empty()
&& (services.globals.allow_guests_auto_join_rooms() || !is_guest) && (services.config.allow_guests_auto_join_rooms || !is_guest)
{ {
for room in &services.server.config.auto_join_rooms { for room in &services.server.config.auto_join_rooms {
let Ok(room_id) = services.rooms.alias.resolve(room).await else { let Ok(room_id) = services.rooms.alias.resolve(room).await else {

View file

@ -491,7 +491,7 @@ pub(crate) async fn invite_user_route(
) -> Result<invite_user::v3::Response> { ) -> Result<invite_user::v3::Response> {
let sender_user = body.sender_user(); let sender_user = body.sender_user();
if !services.users.is_admin(sender_user).await && services.globals.block_non_admin_invites() { if !services.users.is_admin(sender_user).await && services.config.block_non_admin_invites {
info!( info!(
"User {sender_user} is not an admin and attempted to send an invite to room {}", "User {sender_user} is not an admin and attempted to send an invite to room {}",
&body.room_id &body.room_id
@ -1628,7 +1628,7 @@ pub(crate) async fn invite_helper(
reason: Option<String>, reason: Option<String>,
is_direct: bool, is_direct: bool,
) -> Result { ) -> Result {
if !services.users.is_admin(sender_user).await && services.globals.block_non_admin_invites() { if !services.users.is_admin(sender_user).await && services.config.block_non_admin_invites {
info!( info!(
"User {sender_user} is not an admin and attempted to send an invite to room \ "User {sender_user} is not an admin and attempted to send an invite to room \
{room_id}" {room_id}"

View file

@ -1,12 +1,10 @@
use std::time::Duration; use std::time::Duration;
use axum::extract::State; use axum::extract::State;
use ruma::api::client::{ use conduwuit::{Err, Result};
error::ErrorKind, use ruma::api::client::presence::{get_presence, set_presence};
presence::{get_presence, set_presence},
};
use crate::{Error, Result, Ruma}; use crate::Ruma;
/// # `PUT /_matrix/client/r0/presence/{userId}/status` /// # `PUT /_matrix/client/r0/presence/{userId}/status`
/// ///
@ -15,24 +13,17 @@ pub(crate) async fn set_presence_route(
State(services): State<crate::State>, State(services): State<crate::State>,
body: Ruma<set_presence::v3::Request>, body: Ruma<set_presence::v3::Request>,
) -> Result<set_presence::v3::Response> { ) -> Result<set_presence::v3::Response> {
if !services.globals.allow_local_presence() { if !services.config.allow_local_presence {
return Err(Error::BadRequest( return Err!(Request(Forbidden("Presence is disabled on this server")));
ErrorKind::forbidden(),
"Presence is disabled on this server",
));
} }
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if body.sender_user() != body.user_id && body.appservice_info.is_none() {
if sender_user != &body.user_id && body.appservice_info.is_none() { return Err!(Request(InvalidParam("Not allowed to set presence of other users")));
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Not allowed to set presence of other users",
));
} }
services services
.presence .presence
.set_presence(sender_user, &body.presence, None, None, body.status_msg.clone()) .set_presence(body.sender_user(), &body.presence, None, None, body.status_msg.clone())
.await?; .await?;
Ok(set_presence::v3::Response {}) Ok(set_presence::v3::Response {})
@ -47,21 +38,15 @@ pub(crate) async fn get_presence_route(
State(services): State<crate::State>, State(services): State<crate::State>,
body: Ruma<get_presence::v3::Request>, body: Ruma<get_presence::v3::Request>,
) -> Result<get_presence::v3::Response> { ) -> Result<get_presence::v3::Response> {
if !services.globals.allow_local_presence() { if !services.config.allow_local_presence {
return Err(Error::BadRequest( return Err!(Request(Forbidden("Presence is disabled on this server",)));
ErrorKind::forbidden(),
"Presence is disabled on this server",
));
} }
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut presence_event = None; let mut presence_event = None;
let has_shared_rooms = services let has_shared_rooms = services
.rooms .rooms
.state_cache .state_cache
.user_sees_user(sender_user, &body.user_id) .user_sees_user(body.sender_user(), &body.user_id)
.await; .await;
if has_shared_rooms { if has_shared_rooms {
@ -99,9 +84,6 @@ pub(crate) async fn get_presence_route(
presence: presence.content.presence, presence: presence.content.presence,
}) })
}, },
| _ => Err(Error::BadRequest( | _ => Err!(Request(NotFound("Presence state for this user was not found"))),
ErrorKind::NotFound,
"Presence state for this user was not found",
)),
} }
} }

View file

@ -52,7 +52,7 @@ pub(crate) async fn set_displayname_route(
update_displayname(&services, &body.user_id, body.displayname.clone(), &all_joined_rooms) update_displayname(&services, &body.user_id, body.displayname.clone(), &all_joined_rooms)
.await; .await;
if services.globals.allow_local_presence() { if services.config.allow_local_presence {
// Presence update // Presence update
services services
.presence .presence
@ -147,7 +147,7 @@ pub(crate) async fn set_avatar_url_route(
) )
.await; .await;
if services.globals.allow_local_presence() { if services.config.allow_local_presence {
// Presence update // Presence update
services services
.presence .presence

View file

@ -50,7 +50,7 @@ pub(crate) async fn set_read_marker_route(
} }
// ping presence // ping presence
if services.globals.allow_local_presence() { if services.config.allow_local_presence {
services services
.presence .presence
.ping_presence(sender_user, &ruma::presence::PresenceState::Online) .ping_presence(sender_user, &ruma::presence::PresenceState::Online)
@ -126,7 +126,7 @@ pub(crate) async fn create_receipt_route(
} }
// ping presence // ping presence
if services.globals.allow_local_presence() { if services.config.allow_local_presence {
services services
.presence .presence
.ping_presence(sender_user, &ruma::presence::PresenceState::Online) .ping_presence(sender_user, &ruma::presence::PresenceState::Online)

View file

@ -372,7 +372,7 @@ pub(crate) async fn create_room_route(
// Silently skip encryption events if they are not allowed // Silently skip encryption events if they are not allowed
if pdu_builder.event_type == TimelineEventType::RoomEncryption if pdu_builder.event_type == TimelineEventType::RoomEncryption
&& !services.globals.allow_encryption() && !services.config.allow_encryption
{ {
continue; continue;
} }

View file

@ -25,8 +25,7 @@ pub(crate) async fn send_message_event_route(
let appservice_info = body.appservice_info.as_ref(); let appservice_info = body.appservice_info.as_ref();
// Forbid m.room.encrypted if encryption is disabled // Forbid m.room.encrypted if encryption is disabled
if MessageLikeEventType::RoomEncrypted == body.event_type if MessageLikeEventType::RoomEncrypted == body.event_type && !services.config.allow_encryption
&& !services.globals.allow_encryption()
{ {
return Err!(Request(Forbidden("Encryption has been disabled"))); return Err!(Request(Forbidden("Encryption has been disabled")));
} }

View file

@ -118,7 +118,7 @@ pub(crate) async fn sync_events_route(
let (sender_user, sender_device) = body.sender(); let (sender_user, sender_device) = body.sender();
// Presence update // Presence update
if services.globals.allow_local_presence() { if services.config.allow_local_presence {
services services
.presence .presence
.ping_presence(sender_user, &body.body.set_presence) .ping_presence(sender_user, &body.body.set_presence)
@ -279,8 +279,8 @@ pub(crate) async fn build_sync_events(
}); });
let presence_updates: OptionFuture<_> = services let presence_updates: OptionFuture<_> = services
.globals .config
.allow_local_presence() .allow_local_presence
.then(|| process_presence_updates(services, since, sender_user)) .then(|| process_presence_updates(services, since, sender_user))
.into(); .into();

View file

@ -64,7 +64,7 @@ pub(crate) async fn create_typing_event_route(
} }
// ping presence // ping presence
if services.globals.allow_local_presence() { if services.config.allow_local_presence {
services services
.presence .presence
.ping_presence(&body.user_id, &ruma::presence::PresenceState::Online) .ping_presence(&body.user_id, &ruma::presence::PresenceState::Online)

View file

@ -205,7 +205,7 @@ pub(crate) async fn delete_timezone_key_route(
services.users.set_timezone(&body.user_id, None); services.users.set_timezone(&body.user_id, None);
if services.globals.allow_local_presence() { if services.config.allow_local_presence {
// Presence update // Presence update
services services
.presence .presence
@ -233,7 +233,7 @@ pub(crate) async fn set_timezone_key_route(
services.users.set_timezone(&body.user_id, body.tz.clone()); services.users.set_timezone(&body.user_id, body.tz.clone());
if services.globals.allow_local_presence() { if services.config.allow_local_presence {
// Presence update // Presence update
services services
.presence .presence
@ -326,7 +326,7 @@ pub(crate) async fn set_profile_key_route(
); );
} }
if services.globals.allow_local_presence() { if services.config.allow_local_presence {
// Presence update // Presence update
services services
.presence .presence
@ -385,7 +385,7 @@ pub(crate) async fn delete_profile_key_route(
.set_profile_key(&body.user_id, &body.key_name, None); .set_profile_key(&body.user_id, &body.key_name, None);
} }
if services.globals.allow_local_presence() { if services.config.allow_local_presence {
// Presence update // Presence update
services services
.presence .presence

View file

@ -103,8 +103,7 @@ pub(crate) async fn create_invite_route(
return Err!(Request(Forbidden("This room is banned on this homeserver."))); return Err!(Request(Forbidden("This room is banned on this homeserver.")));
} }
if services.globals.block_non_admin_invites() && !services.users.is_admin(&invited_user).await if services.config.block_non_admin_invites && !services.users.is_admin(&invited_user).await {
{
return Err!(Request(Forbidden("This server does not allow room invites."))); return Err!(Request(Forbidden("This server does not allow room invites.")));
} }

View file

@ -111,20 +111,6 @@ impl Service {
#[inline] #[inline]
pub fn server_name(&self) -> &ServerName { self.server.name.as_ref() } pub fn server_name(&self) -> &ServerName { self.server.name.as_ref() }
pub fn allow_registration(&self) -> bool { self.server.config.allow_registration }
pub fn allow_guest_registration(&self) -> bool { self.server.config.allow_guest_registration }
pub fn allow_guests_auto_join_rooms(&self) -> bool {
self.server.config.allow_guests_auto_join_rooms
}
pub fn log_guest_registrations(&self) -> bool { self.server.config.log_guest_registrations }
pub fn allow_encryption(&self) -> bool { self.server.config.allow_encryption }
pub fn allow_federation(&self) -> bool { self.server.config.allow_federation }
pub fn allow_public_room_directory_over_federation(&self) -> bool { pub fn allow_public_room_directory_over_federation(&self) -> bool {
self.server self.server
.config .config
@ -183,22 +169,6 @@ impl Service {
pub fn forbidden_usernames(&self) -> &RegexSet { &self.server.config.forbidden_usernames } pub fn forbidden_usernames(&self) -> &RegexSet { &self.server.config.forbidden_usernames }
pub fn allow_local_presence(&self) -> bool { self.server.config.allow_local_presence }
pub fn allow_incoming_presence(&self) -> bool { self.server.config.allow_incoming_presence }
pub fn allow_outgoing_presence(&self) -> bool { self.server.config.allow_outgoing_presence }
pub fn allow_incoming_read_receipts(&self) -> bool {
self.server.config.allow_incoming_read_receipts
}
pub fn allow_outgoing_read_receipts(&self) -> bool {
self.server.config.allow_outgoing_read_receipts
}
pub fn block_non_admin_invites(&self) -> bool { self.server.config.block_non_admin_invites }
/// checks if `user_id` is local to us via server_name comparison /// checks if `user_id` is local to us via server_name comparison
#[inline] #[inline]
pub fn user_is_local(&self, user_id: &UserId) -> bool { pub fn user_is_local(&self, user_id: &UserId) -> bool {