split join_room_by_id_helper into local and remote

prior stack frame allocated 180 KiB

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-06-09 04:22:47 +00:00
parent 6e50b07bf5
commit fc1b8326e6
2 changed files with 478 additions and 470 deletions

View File

@ -2,6 +2,6 @@ array-size-threshold = 4096
cognitive-complexity-threshold = 94 # TODO reduce me ALARA
excessive-nesting-threshold = 11 # TODO reduce me to 4 or 5
future-size-threshold = 7745 # TODO reduce me ALARA
stack-size-threshold = 178030 # reduce me ALARA
stack-size-threshold = 173577 # reduce me ALARA
too-many-lines-threshold = 700 # TODO reduce me to <= 100
type-complexity-threshold = 250 # reduce me to ~200

View File

@ -30,7 +30,7 @@ use ruma::{
OwnedUserId, RoomId, RoomVersionId, ServerName, UserId,
};
use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
use tokio::sync::RwLock;
use tokio::sync::{MutexGuard, RwLock};
use tracing::{debug, error, info, trace, warn};
use super::get_alias_helper;
@ -627,7 +627,7 @@ pub(crate) async fn joined_members_route(
pub async fn join_room_by_id_helper(
sender_user: Option<&UserId>, room_id: &RoomId, reason: Option<String>, servers: &[OwnedServerName],
_third_party_signed: Option<&ThirdPartySigned>,
third_party_signed: Option<&ThirdPartySigned>,
) -> Result<join_room_by_id::v3::Response> {
let sender_user = sender_user.expect("user is authenticated");
@ -655,6 +655,16 @@ pub async fn join_room_by_id_helper(
.state_cache
.server_in_room(services().globals.server_name(), room_id)?
{
join_room_by_id_helper_remote(sender_user, room_id, reason, servers, third_party_signed, state_lock).await
} else {
join_room_by_id_helper_local(sender_user, room_id, reason, servers, third_party_signed, state_lock).await
}
}
async fn join_room_by_id_helper_remote(
sender_user: &UserId, room_id: &RoomId, reason: Option<String>, servers: &[OwnedServerName],
_third_party_signed: Option<&ThirdPartySigned>, state_lock: MutexGuard<'_, ()>,
) -> Result<join_room_by_id::v3::Response> {
info!("Joining {room_id} over federation.");
let (make_join_response, remote_server) = make_join_request(sender_user, room_id, servers).await?;
@ -781,11 +791,10 @@ pub async fn join_room_by_id_helper(
RoomVersionId::V8 | RoomVersionId::V9 | RoomVersionId::V10 | RoomVersionId::V11 => {
if let Some(signed_raw) = &send_join_response.room_state.event {
info!(
"There is a signed event. This room is probably using restricted joins. Adding signature \
to our event"
"There is a signed event. This room is probably using restricted joins. Adding signature to \
our event"
);
let Ok((signed_event_id, signed_value)) =
gen_event_id_canonical_json(signed_raw, &room_version_id)
let Ok((signed_event_id, signed_value)) = gen_event_id_canonical_json(signed_raw, &room_version_id)
else {
// Event could not be converted to canonical json
return Err(Error::BadRequest(
@ -808,10 +817,8 @@ pub async fn join_room_by_id_helper(
"Server sent invalid signatures type",
))
.and_then(|e| {
e.get(remote_server.as_str()).ok_or(Error::BadRequest(
ErrorKind::InvalidParam,
"Server did not send its signature",
))
e.get(remote_server.as_str())
.ok_or(Error::BadRequest(ErrorKind::InvalidParam, "Server did not send its signature"))
}) {
Ok(signature) => {
join_event
@ -985,7 +992,14 @@ pub async fn join_room_by_id_helper(
.rooms
.state
.set_room_state(room_id, statehash_after_join, &state_lock)?;
} else {
Ok(join_room_by_id::v3::Response::new(room_id.to_owned()))
}
async fn join_room_by_id_helper_local(
sender_user: &UserId, room_id: &RoomId, reason: Option<String>, servers: &[OwnedServerName],
_third_party_signed: Option<&ThirdPartySigned>, state_lock: MutexGuard<'_, ()>,
) -> Result<join_room_by_id::v3::Response> {
info!("We can join locally");
let join_rules_event =
@ -1087,10 +1101,7 @@ pub async fn join_room_by_id_helper(
.iter()
.any(|server_name| !server_is_ours(server_name))
{
info!(
"We couldn't do the join locally, maybe federation can help to satisfy the restricted join \
requirements"
);
info!("We couldn't do the join locally, maybe federation can help to satisfy the restricted join requirements");
let (make_join_response, remote_server) = make_join_request(sender_user, room_id, servers).await?;
let room_version_id = match make_join_response.room_version {
@ -1167,8 +1178,7 @@ pub async fn join_room_by_id_helper(
ruma::signatures::reference_hash(&join_event_stub, &room_version_id)
.expect("ruma can calculate reference hashes")
);
let event_id =
<&EventId>::try_from(event_id.as_str()).expect("ruma's reference hashes are valid event ids");
let event_id = <&EventId>::try_from(event_id.as_str()).expect("ruma's reference hashes are valid event ids");
// Add event_id back
join_event_stub.insert("event_id".to_owned(), CanonicalJsonValue::String(event_id.as_str().to_owned()));
@ -1190,8 +1200,7 @@ pub async fn join_room_by_id_helper(
.await?;
if let Some(signed_raw) = send_join_response.room_state.event {
let Ok((signed_event_id, signed_value)) = gen_event_id_canonical_json(&signed_raw, &room_version_id)
else {
let Ok((signed_event_id, signed_value)) = gen_event_id_canonical_json(&signed_raw, &room_version_id) else {
// Event could not be converted to canonical json
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
@ -1224,7 +1233,6 @@ pub async fn join_room_by_id_helper(
} else {
return Err(error);
}
}
Ok(join_room_by_id::v3::Response::new(room_id.to_owned()))
}