de-global services

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-27 07:17:07 +00:00
parent 7e50db4193
commit 2f85a5c1ac
36 changed files with 327 additions and 336 deletions

View file

@ -370,7 +370,7 @@ pub(crate) async fn register_route(
if let Some(room_id_server_name) = room.server_name() {
if let Err(e) = join_room_by_id_helper(
services,
&services,
&user_id,
room,
Some("Automatically joining this room upon registration".to_owned()),
@ -562,11 +562,11 @@ pub(crate) async fn deactivate_route(
.rooms_joined(sender_user)
.filter_map(Result::ok)
.collect();
super::update_displayname(services, sender_user.clone(), None, all_joined_rooms.clone()).await?;
super::update_avatar_url(services, sender_user.clone(), None, None, all_joined_rooms).await?;
super::update_displayname(&services, sender_user.clone(), None, all_joined_rooms.clone()).await?;
super::update_avatar_url(&services, sender_user.clone(), None, None, all_joined_rooms).await?;
// Make the user leave all rooms before deactivation
super::leave_all_rooms(services, sender_user).await;
super::leave_all_rooms(&services, sender_user).await;
info!("User {sender_user} deactivated their account.");
services

View file

@ -107,7 +107,7 @@ pub(crate) async fn get_alias_route(
return Err(Error::BadRequest(ErrorKind::NotFound, "Room with alias not found."));
};
let servers = room_available_servers(services, &room_id, &room_alias, &pre_servers);
let servers = room_available_servers(&services, &room_id, &room_alias, &pre_servers);
debug!(?room_alias, ?room_id, "available servers: {servers:?}");
Ok(get_alias::v3::Response::new(room_id, servers))

View file

@ -20,7 +20,7 @@ pub(crate) async fn set_global_account_data_route(
State(services): State<crate::State>, body: Ruma<set_global_account_data::v3::Request>,
) -> Result<set_global_account_data::v3::Response> {
set_account_data(
services,
&services,
None,
&body.sender_user,
&body.event_type.to_string(),
@ -37,7 +37,7 @@ pub(crate) async fn set_room_account_data_route(
State(services): State<crate::State>, body: Ruma<set_room_account_data::v3::Request>,
) -> Result<set_room_account_data::v3::Response> {
set_account_data(
services,
&services,
Some(&body.room_id),
&body.sender_user,
&body.event_type.to_string(),

View file

@ -48,7 +48,7 @@ pub(crate) async fn get_public_rooms_filtered_route(
}
let response = get_public_rooms_filtered_helper(
services,
&services,
body.server.as_deref(),
body.limit,
body.since.as_deref(),
@ -88,7 +88,7 @@ pub(crate) async fn get_public_rooms_route(
}
let response = get_public_rooms_filtered_helper(
services,
&services,
body.server.as_deref(),
body.limit,
body.since.as_deref(),
@ -124,7 +124,7 @@ pub(crate) async fn set_room_visibility_route(
return Err(Error::BadRequest(ErrorKind::NotFound, "Room not found"));
}
if !user_can_publish_room(services, sender_user, &body.room_id)? {
if !user_can_publish_room(&services, sender_user, &body.room_id)? {
return Err(Error::BadRequest(
ErrorKind::forbidden(),
"User is not allowed to publish this room",

View file

@ -77,7 +77,7 @@ pub(crate) async fn get_keys_route(
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
get_keys_helper(
services,
&services,
Some(sender_user),
&body.device_keys,
|u| u == sender_user,
@ -92,7 +92,7 @@ pub(crate) async fn get_keys_route(
pub(crate) async fn claim_keys_route(
State(services): State<crate::State>, body: Ruma<claim_keys::v3::Request>,
) -> Result<claim_keys::v3::Response> {
claim_keys_helper(services, &body.one_time_keys).await
claim_keys_helper(&services, &body.one_time_keys).await
}
/// # `POST /_matrix/client/r0/keys/device_signing/upload`

View file

@ -76,12 +76,12 @@ pub(crate) async fn get_media_preview_route(
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let url = &body.url;
if !url_preview_allowed(services, url) {
if !url_preview_allowed(&services, url) {
warn!(%sender_user, "URL is not allowed to be previewed: {url}");
return Err(Error::BadRequest(ErrorKind::forbidden(), "URL is not allowed to be previewed"));
}
match get_url_preview(services, url).await {
match get_url_preview(&services, url).await {
Ok(preview) => {
let res = serde_json::value::to_raw_value(&preview).map_err(|e| {
error!(%sender_user, "Failed to convert UrlPreviewData into a serde json value: {e}");
@ -221,7 +221,7 @@ pub(crate) async fn get_content_route(
})
} else if !services.globals.server_is_ours(&body.server_name) && body.allow_remote {
let response = get_remote_content(
services,
&services,
&mxc,
&body.server_name,
body.media_id.clone(),
@ -311,7 +311,7 @@ pub(crate) async fn get_content_as_filename_route(
})
} else if !services.globals.server_is_ours(&body.server_name) && body.allow_remote {
match get_remote_content(
services,
&services,
&mxc,
&body.server_name,
body.media_id.clone(),

View file

@ -167,7 +167,7 @@ pub(crate) async fn join_room_by_id_route(
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
banned_room_check(
services,
&services,
sender_user,
Some(&body.room_id),
body.room_id.server_name(),
@ -202,7 +202,7 @@ pub(crate) async fn join_room_by_id_route(
}
join_room_by_id_helper(
services,
&services,
sender_user,
&body.room_id,
body.reason.clone(),
@ -231,7 +231,7 @@ pub(crate) async fn join_room_by_id_or_alias_route(
let (servers, room_id) = match OwnedRoomId::try_from(body.room_id_or_alias) {
Ok(room_id) => {
banned_room_check(services, sender_user, Some(&room_id), room_id.server_name(), client).await?;
banned_room_check(&services, sender_user, Some(&room_id), room_id.server_name(), client).await?;
let mut servers = body.server_name.clone();
servers.extend(
@ -270,7 +270,7 @@ pub(crate) async fn join_room_by_id_or_alias_route(
.await?;
let (room_id, mut pre_servers) = response;
banned_room_check(services, sender_user, Some(&room_id), Some(room_alias.server_name()), client).await?;
banned_room_check(&services, sender_user, Some(&room_id), Some(room_alias.server_name()), client).await?;
let mut servers = body.server_name;
if let Some(pre_servers) = &mut pre_servers {
@ -303,7 +303,7 @@ pub(crate) async fn join_room_by_id_or_alias_route(
};
let join_room_response = join_room_by_id_helper(
services,
&services,
sender_user,
&room_id,
body.reason.clone(),
@ -327,7 +327,7 @@ pub(crate) async fn leave_room_route(
) -> Result<leave_room::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
leave_room(services, sender_user, &body.room_id, body.reason.clone()).await?;
leave_room(&services, sender_user, &body.room_id, body.reason.clone()).await?;
Ok(leave_room::v3::Response::new())
}
@ -353,13 +353,13 @@ pub(crate) async fn invite_user_route(
));
}
banned_room_check(services, sender_user, Some(&body.room_id), body.room_id.server_name(), client).await?;
banned_room_check(&services, sender_user, Some(&body.room_id), body.room_id.server_name(), client).await?;
if let invite_user::v3::InvitationRecipient::UserId {
user_id,
} = &body.recipient
{
invite_helper(services, sender_user, user_id, &body.room_id, body.reason.clone(), false).await?;
invite_helper(&services, sender_user, user_id, &body.room_id, body.reason.clone(), false).await?;
Ok(invite_user::v3::Response {})
} else {
Err(Error::BadRequest(ErrorKind::NotFound, "User not found."))

View file

@ -146,7 +146,7 @@ pub(crate) async fn get_message_events_route(
.timeline
.pdus_after(sender_user, &body.room_id, from)?
.filter_map(Result::ok) // Filter out buggy events
.filter(|(_, pdu)| { contains_url_filter(pdu, &body.filter) && visibility_filter(services, pdu, sender_user, &body.room_id)
.filter(|(_, pdu)| { contains_url_filter(pdu, &body.filter) && visibility_filter(&services, pdu, sender_user, &body.room_id)
})
.take_while(|&(k, _)| Some(k) != to) // Stop at `to`
@ -193,7 +193,7 @@ pub(crate) async fn get_message_events_route(
.timeline
.pdus_until(sender_user, &body.room_id, from)?
.filter_map(Result::ok) // Filter out buggy events
.filter(|(_, pdu)| {contains_url_filter(pdu, &body.filter) && visibility_filter(services, pdu, sender_user, &body.room_id)})
.filter(|(_, pdu)| {contains_url_filter(pdu, &body.filter) && visibility_filter(&services, pdu, sender_user, &body.room_id)})
.take_while(|&(k, _)| Some(k) != to) // Stop at `to`
.take(limit)
.collect();

View file

@ -33,7 +33,7 @@ pub(crate) async fn set_displayname_route(
.filter_map(Result::ok)
.collect();
update_displayname(services, sender_user.clone(), body.displayname.clone(), all_joined_rooms).await?;
update_displayname(&services, sender_user.clone(), body.displayname.clone(), all_joined_rooms).await?;
if services.globals.allow_local_presence() {
// Presence update
@ -118,7 +118,7 @@ pub(crate) async fn set_avatar_url_route(
.collect();
update_avatar_url(
services,
&services,
sender_user.clone(),
body.avatar_url.clone(),
body.blurhash.clone(),

View file

@ -40,7 +40,7 @@ pub(crate) async fn report_event_route(
};
is_report_valid(
services,
&services,
&pdu.event_id,
&body.room_id,
sender_user,

View file

@ -79,7 +79,7 @@ pub(crate) async fn create_room_route(
}
let room_id: OwnedRoomId = if let Some(custom_room_id) = &body.room_id {
custom_room_id_check(services, custom_room_id)?
custom_room_id_check(&services, custom_room_id)?
} else {
RoomId::new(&services.globals.config.server_name)
};
@ -96,7 +96,7 @@ pub(crate) async fn create_room_route(
let state_lock = services.rooms.state.mutex.lock(&room_id).await;
let alias: Option<OwnedRoomAliasId> = if let Some(alias) = &body.room_alias_name {
Some(room_alias_check(services, alias, &body.appservice_info).await?)
Some(room_alias_check(&services, alias, &body.appservice_info).await?)
} else {
None
};
@ -438,7 +438,7 @@ pub(crate) async fn create_room_route(
// 8. Events implied by invite (and TODO: invite_3pid)
drop(state_lock);
for user_id in &body.invite {
if let Err(e) = invite_helper(services, sender_user, user_id, &room_id, None, body.is_direct).await {
if let Err(e) = invite_helper(&services, sender_user, user_id, &room_id, None, body.is_direct).await {
warn!(%e, "Failed to send invite");
}
}

View file

@ -37,7 +37,7 @@ pub(crate) async fn send_state_event_for_key_route(
Ok(send_state_event::v3::Response {
event_id: send_state_event_for_key_helper(
services,
&services,
sender_user,
&body.room_id,
&body.event_type,

View file

@ -137,7 +137,7 @@ pub(crate) async fn sync_events_route(
);
if services.globals.allow_local_presence() {
process_presence_updates(services, &mut presence_updates, since, &sender_user).await?;
process_presence_updates(&services, &mut presence_updates, since, &sender_user).await?;
}
let all_joined_rooms = services
@ -152,7 +152,7 @@ pub(crate) async fn sync_events_route(
for room_id in all_joined_rooms {
let room_id = room_id?;
if let Ok(joined_room) = load_joined_room(
services,
&services,
&sender_user,
&sender_device,
&room_id,
@ -182,7 +182,7 @@ pub(crate) async fn sync_events_route(
.collect();
for result in all_left_rooms {
handle_left_room(
services,
&services,
since,
&result?.0,
&sender_user,
@ -1214,7 +1214,7 @@ pub(crate) async fn sync_events_v4_route(
match new_membership {
MembershipState::Join => {
// A new user joined an encrypted room
if !share_encrypted_room(services, &sender_user, &user_id, room_id)? {
if !share_encrypted_room(&services, &sender_user, &user_id, room_id)? {
device_list_changes.insert(user_id);
}
},
@ -1243,7 +1243,7 @@ pub(crate) async fn sync_events_v4_route(
.filter(|user_id| {
// Only send keys if the sender doesn't share an encrypted room with the target
// already
!share_encrypted_room(services, &sender_user, user_id, room_id).unwrap_or(false)
!share_encrypted_room(&services, &sender_user, user_id, room_id).unwrap_or(false)
}),
);
}
@ -1407,7 +1407,8 @@ pub(crate) async fn sync_events_v4_route(
for (room_id, (required_state_request, timeline_limit, roomsince)) in &todo_rooms {
let roomsincecount = PduCount::Normal(*roomsince);
let (timeline_pdus, limited) = load_timeline(services, &sender_user, room_id, roomsincecount, *timeline_limit)?;
let (timeline_pdus, limited) =
load_timeline(&services, &sender_user, room_id, roomsincecount, *timeline_limit)?;
if roomsince != &0 && timeline_pdus.is_empty() {
continue;

View file

@ -1,4 +1,4 @@
#![recursion_limit = "160"]
#![recursion_limit = "192"]
pub mod client;
pub mod router;

View file

@ -4,6 +4,8 @@ mod handler;
mod request;
mod response;
use std::sync::Arc;
use axum::{
response::IntoResponse,
routing::{any, get, post},
@ -16,7 +18,7 @@ use self::handler::RouterExt;
pub(super) use self::{args::Args as Ruma, response::RumaResponse};
use crate::{client, server};
pub type State = &'static service::Services;
pub type State = Arc<service::Services>;
pub fn build(router: Router<State>, server: &Server) -> Router<State> {
let config = &server.config;

View file

@ -7,7 +7,7 @@ use ruma::{api::IncomingRequest, CanonicalJsonValue, OwnedDeviceId, OwnedServerN
use service::Services;
use super::{auth, auth::Auth, request, request::Request};
use crate::service::appservice::RegistrationInfo;
use crate::{service::appservice::RegistrationInfo, State};
/// Extractor for Ruma request structs
pub(crate) struct Args<T> {
@ -36,14 +36,13 @@ pub(crate) struct Args<T> {
}
#[async_trait]
impl<T, S> FromRequest<S, Body> for Args<T>
impl<T> FromRequest<State, Body> for Args<T>
where
T: IncomingRequest,
{
type Rejection = Error;
async fn from_request(request: hyper::Request<Body>, _: &S) -> Result<Self, Self::Rejection> {
let services = service::services(); // ???
async fn from_request(request: hyper::Request<Body>, services: &State) -> Result<Self, Self::Rejection> {
let mut request = request::from(services, request).await?;
let mut json_body = serde_json::from_slice::<CanonicalJsonValue>(&request.body).ok();
let auth = auth::auth(services, &mut request, &json_body, &T::METADATA).await?;

View file

@ -82,7 +82,7 @@ pub(crate) async fn create_join_event_template_route(
.state_cache
.is_left(&body.user_id, &body.room_id)
.unwrap_or(true))
&& user_can_perform_restricted_join(services, &body.user_id, &body.room_id, &room_version_id)?
&& user_can_perform_restricted_join(&services, &body.user_id, &body.room_id, &room_version_id)?
{
let auth_user = services
.rooms

View file

@ -26,7 +26,7 @@ pub(crate) async fn get_public_rooms_filtered_route(
}
let response = crate::client::get_public_rooms_filtered_helper(
services,
&services,
None,
body.limit,
body.since.as_deref(),
@ -60,7 +60,7 @@ pub(crate) async fn get_public_rooms_route(
}
let response = crate::client::get_public_rooms_filtered_helper(
services,
&services,
None,
body.limit,
body.since.as_deref(),

View file

@ -62,8 +62,8 @@ pub(crate) async fn send_transaction_message_route(
"Starting txn",
);
let resolved_map = handle_pdus(services, &client, &body, origin, &txn_start_time).await?;
handle_edus(services, &client, &body, origin).await?;
let resolved_map = handle_pdus(&services, &client, &body, origin, &txn_start_time).await?;
handle_edus(&services, &client, &body, origin).await?;
debug!(
pdus = ?body.pdus.len(),

View file

@ -241,7 +241,7 @@ pub(crate) async fn create_join_event_v1_route(
}
}
let room_state = create_join_event(services, origin, &body.room_id, &body.pdu).await?;
let room_state = create_join_event(&services, origin, &body.room_id, &body.pdu).await?;
Ok(create_join_event::v1::Response {
room_state,
@ -286,7 +286,7 @@ pub(crate) async fn create_join_event_v2_route(
auth_chain,
state,
event,
} = create_join_event(services, origin, &body.room_id, &body.pdu).await?;
} = create_join_event(&services, origin, &body.room_id, &body.pdu).await?;
let room_state = create_join_event::v2::RoomState {
members_omitted: false,
auth_chain,

View file

@ -28,7 +28,7 @@ pub(crate) async fn create_leave_event_v1_route(
) -> Result<create_leave_event::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
create_leave_event(services, origin, &body.room_id, &body.pdu).await?;
create_leave_event(&services, origin, &body.room_id, &body.pdu).await?;
Ok(create_leave_event::v1::Response::new())
}
@ -41,7 +41,7 @@ pub(crate) async fn create_leave_event_v2_route(
) -> Result<create_leave_event::v2::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
create_leave_event(services, origin, &body.room_id, &body.pdu).await?;
create_leave_event(&services, origin, &body.room_id, &body.pdu).await?;
Ok(create_leave_event::v2::Response::new())
}

View file

@ -84,7 +84,7 @@ pub(crate) async fn get_keys_route(
}
let result = get_keys_helper(
services,
&services,
None,
&body.device_keys,
|u| Some(u.server_name()) == body.origin.as_deref(),
@ -116,7 +116,7 @@ pub(crate) async fn claim_keys_route(
));
}
let result = claim_keys_helper(services, &body.one_time_keys).await?;
let result = claim_keys_helper(&services, &body.one_time_keys).await?;
Ok(claim_keys::v1::Response {
one_time_keys: result.one_time_keys,