refactor for ruma identifiers optimizations

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-12-28 23:31:24 +00:00 committed by strawberry
parent d91570d0e6
commit 0a9b6c136f
19 changed files with 81 additions and 88 deletions

View file

@ -1,7 +1,9 @@
use std::collections::BTreeMap;
use axum::extract::State;
use conduwuit::{debug_info, debug_warn, error, info, pdu::PduBuilder, warn, Err, Error, Result};
use conduwuit::{
debug_info, debug_warn, err, error, info, pdu::PduBuilder, warn, Err, Error, Result,
};
use futures::FutureExt;
use ruma::{
api::client::{
@ -24,8 +26,7 @@ use ruma::{
},
int,
serde::{JsonObject, Raw},
CanonicalJsonObject, Int, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, RoomAliasId, RoomId,
RoomVersionId,
CanonicalJsonObject, Int, OwnedRoomAliasId, OwnedRoomId, OwnedUserId, RoomId, RoomVersionId,
};
use serde_json::{json, value::to_raw_value};
use service::{appservice::RegistrationInfo, Services};
@ -554,14 +555,15 @@ async fn room_alias_check(
return Err(Error::BadRequest(ErrorKind::Unknown, "Room alias name is forbidden."));
}
let full_room_alias = RoomAliasId::parse(format!(
"#{}:{}",
room_alias_name, services.globals.config.server_name
))
.map_err(|e| {
info!("Failed to parse room alias {room_alias_name}: {e}");
Error::BadRequest(ErrorKind::InvalidParam, "Invalid room alias specified.")
})?;
let server_name = services.globals.server_name();
let full_room_alias = OwnedRoomAliasId::parse(format!("#{room_alias_name}:{server_name}"))
.map_err(|e| {
err!(Request(InvalidParam(debug_error!(
?e,
?room_alias_name,
"Failed to parse room alias.",
))))
})?;
if services
.rooms
@ -620,15 +622,11 @@ fn custom_room_id_check(services: &Services, custom_room_id: &str) -> Result<Own
));
}
let full_room_id = format!("!{}:{}", custom_room_id, services.globals.config.server_name);
let server_name = services.globals.server_name();
let full_room_id = format!("!{custom_room_id}:{server_name}");
debug_info!("Full custom room ID: {full_room_id}");
RoomId::parse(full_room_id).map_err(|e| {
info!(
"User attempted to create room with custom room ID {custom_room_id} but failed \
parsing: {e}"
);
Error::BadRequest(ErrorKind::InvalidParam, "Custom room ID could not be parsed")
})
OwnedRoomId::parse(full_room_id)
.map_err(Into::into)
.inspect(|full_room_id| debug_info!(?full_room_id, "Full custom room ID"))
.inspect_err(|e| warn!(?e, ?custom_room_id, "Failed to create room with custom room ID",))
}

View file

@ -18,7 +18,7 @@ use ruma::{
},
uiaa::UserIdentifier,
},
UserId,
OwnedUserId, UserId,
};
use serde::Deserialize;
@ -83,7 +83,7 @@ pub(crate) async fn login_route(
services.globals.server_name(),
)
} else if let Some(user) = user {
UserId::parse(user)
OwnedUserId::parse(user)
} else {
warn!("Bad login type: {:?}", &body.login_info);
return Err!(Request(Forbidden("Bad login type.")));
@ -147,7 +147,7 @@ pub(crate) async fn login_route(
services.globals.server_name(),
)
} else if let Some(user) = user {
UserId::parse(user)
OwnedUserId::parse(user)
} else {
warn!("Bad login type: {:?}", &body.login_info);
return Err(Error::BadRequest(ErrorKind::forbidden(), "Bad login type."));

View file

@ -962,7 +962,7 @@ async fn calculate_state_initial(
};
// This check is in case a bad user ID made it into the database
if let Ok(uid) = UserId::parse(&state_key) {
if let Ok(uid) = OwnedUserId::parse(&state_key) {
lazy_loaded.insert(uid);
}
@ -1079,7 +1079,7 @@ async fn calculate_state_incremental(
}
if let Some(state_key) = &state_event.state_key {
let user_id = UserId::parse(state_key.clone())
let user_id = UserId::parse(state_key)
.map_err(|_| Error::bad_database("Invalid UserId in member PDU."))?;
if user_id == sender_user {
@ -1091,15 +1091,15 @@ async fn calculate_state_incremental(
match content.membership {
| MembershipState::Join => {
// A new user joined an encrypted room
if !share_encrypted_room(services, sender_user, &user_id, Some(room_id))
if !share_encrypted_room(services, sender_user, user_id, Some(room_id))
.await
{
device_list_updates.insert(user_id);
device_list_updates.insert(user_id.into());
}
},
| MembershipState::Leave => {
// Write down users that have left encrypted rooms we are in
left_encrypted_users.insert(user_id);
left_encrypted_users.insert(user_id.into());
},
| _ => {},
}

View file

@ -30,7 +30,7 @@ use ruma::{
TimelineEventType::{self, *},
},
serde::Raw,
uint, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, UInt, UserId,
uint, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, OwnedUserId, UInt,
};
use service::{rooms::read_receipt::pack_receipts, Services};
@ -243,7 +243,7 @@ pub(crate) async fn sync_events_v4_route(
if pdu.kind == RoomMember {
if let Some(state_key) = &pdu.state_key {
let user_id =
UserId::parse(state_key.clone()).map_err(|_| {
OwnedUserId::parse(state_key.clone()).map_err(|_| {
Error::bad_database("Invalid UserId in member PDU.")
})?;