add some accessors to Ar for common patterns

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-24 12:03:56 +00:00
parent 60cc07134f
commit ee92a33a4d
17 changed files with 109 additions and 147 deletions

View File

@ -18,10 +18,9 @@ use crate::{Result, Ruma};
pub(crate) async fn create_backup_version_route(
State(services): State<crate::State>, body: Ruma<create_backup_version::v3::Request>,
) -> Result<create_backup_version::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let version = services
.key_backups
.create_backup(sender_user, &body.algorithm)?;
.create_backup(body.sender_user(), &body.algorithm)?;
Ok(create_backup_version::v3::Response {
version,
@ -35,10 +34,9 @@ pub(crate) async fn create_backup_version_route(
pub(crate) async fn update_backup_version_route(
State(services): State<crate::State>, body: Ruma<update_backup_version::v3::Request>,
) -> Result<update_backup_version::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services
.key_backups
.update_backup(sender_user, &body.version, &body.algorithm)
.update_backup(body.sender_user(), &body.version, &body.algorithm)
.await?;
Ok(update_backup_version::v3::Response {})
@ -50,19 +48,25 @@ pub(crate) async fn update_backup_version_route(
pub(crate) async fn get_latest_backup_info_route(
State(services): State<crate::State>, body: Ruma<get_latest_backup_info::v3::Request>,
) -> Result<get_latest_backup_info::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let (version, algorithm) = services
.key_backups
.get_latest_backup(sender_user)
.get_latest_backup(body.sender_user())
.await
.map_err(|_| err!(Request(NotFound("Key backup does not exist."))))?;
Ok(get_latest_backup_info::v3::Response {
algorithm,
count: (UInt::try_from(services.key_backups.count_keys(sender_user, &version).await)
.expect("user backup keys count should not be that high")),
etag: services.key_backups.get_etag(sender_user, &version).await,
count: (UInt::try_from(
services
.key_backups
.count_keys(body.sender_user(), &version)
.await,
)
.expect("user backup keys count should not be that high")),
etag: services
.key_backups
.get_etag(body.sender_user(), &version)
.await,
version,
})
}
@ -73,10 +77,9 @@ pub(crate) async fn get_latest_backup_info_route(
pub(crate) async fn get_backup_info_route(
State(services): State<crate::State>, body: Ruma<get_backup_info::v3::Request>,
) -> Result<get_backup_info::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let algorithm = services
.key_backups
.get_backup(sender_user, &body.version)
.get_backup(body.sender_user(), &body.version)
.await
.map_err(|_| err!(Request(NotFound("Key backup does not exist at version {:?}", body.version))))?;
@ -84,12 +87,12 @@ pub(crate) async fn get_backup_info_route(
algorithm,
count: services
.key_backups
.count_keys(sender_user, &body.version)
.count_keys(body.sender_user(), &body.version)
.await
.try_into()?,
etag: services
.key_backups
.get_etag(sender_user, &body.version)
.get_etag(body.sender_user(), &body.version)
.await,
version: body.version.clone(),
})
@ -104,11 +107,9 @@ pub(crate) async fn get_backup_info_route(
pub(crate) async fn delete_backup_version_route(
State(services): State<crate::State>, body: Ruma<delete_backup_version::v3::Request>,
) -> Result<delete_backup_version::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services
.key_backups
.delete_backup(sender_user, &body.version)
.delete_backup(body.sender_user(), &body.version)
.await;
Ok(delete_backup_version::v3::Response {})
@ -125,11 +126,9 @@ pub(crate) async fn delete_backup_version_route(
pub(crate) async fn add_backup_keys_route(
State(services): State<crate::State>, body: Ruma<add_backup_keys::v3::Request>,
) -> Result<add_backup_keys::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
if services
.key_backups
.get_latest_backup_version(sender_user)
.get_latest_backup_version(body.sender_user())
.await
.is_ok_and(|version| version != body.version)
{
@ -142,7 +141,7 @@ pub(crate) async fn add_backup_keys_route(
for (session_id, key_data) in &room.sessions {
services
.key_backups
.add_key(sender_user, &body.version, room_id, session_id, key_data)
.add_key(body.sender_user(), &body.version, room_id, session_id, key_data)
.await?;
}
}
@ -150,12 +149,12 @@ pub(crate) async fn add_backup_keys_route(
Ok(add_backup_keys::v3::Response {
count: services
.key_backups
.count_keys(sender_user, &body.version)
.count_keys(body.sender_user(), &body.version)
.await
.try_into()?,
etag: services
.key_backups
.get_etag(sender_user, &body.version)
.get_etag(body.sender_user(), &body.version)
.await,
})
}
@ -171,11 +170,9 @@ pub(crate) async fn add_backup_keys_route(
pub(crate) async fn add_backup_keys_for_room_route(
State(services): State<crate::State>, body: Ruma<add_backup_keys_for_room::v3::Request>,
) -> Result<add_backup_keys_for_room::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
if services
.key_backups
.get_latest_backup_version(sender_user)
.get_latest_backup_version(body.sender_user())
.await
.is_ok_and(|version| version != body.version)
{
@ -187,19 +184,19 @@ pub(crate) async fn add_backup_keys_for_room_route(
for (session_id, key_data) in &body.sessions {
services
.key_backups
.add_key(sender_user, &body.version, &body.room_id, session_id, key_data)
.add_key(body.sender_user(), &body.version, &body.room_id, session_id, key_data)
.await?;
}
Ok(add_backup_keys_for_room::v3::Response {
count: services
.key_backups
.count_keys(sender_user, &body.version)
.count_keys(body.sender_user(), &body.version)
.await
.try_into()?,
etag: services
.key_backups
.get_etag(sender_user, &body.version)
.get_etag(body.sender_user(), &body.version)
.await,
})
}
@ -215,11 +212,9 @@ pub(crate) async fn add_backup_keys_for_room_route(
pub(crate) async fn add_backup_keys_for_session_route(
State(services): State<crate::State>, body: Ruma<add_backup_keys_for_session::v3::Request>,
) -> Result<add_backup_keys_for_session::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
if services
.key_backups
.get_latest_backup_version(sender_user)
.get_latest_backup_version(body.sender_user())
.await
.is_ok_and(|version| version != body.version)
{
@ -230,18 +225,24 @@ pub(crate) async fn add_backup_keys_for_session_route(
services
.key_backups
.add_key(sender_user, &body.version, &body.room_id, &body.session_id, &body.session_data)
.add_key(
body.sender_user(),
&body.version,
&body.room_id,
&body.session_id,
&body.session_data,
)
.await?;
Ok(add_backup_keys_for_session::v3::Response {
count: services
.key_backups
.count_keys(sender_user, &body.version)
.count_keys(body.sender_user(), &body.version)
.await
.try_into()?,
etag: services
.key_backups
.get_etag(sender_user, &body.version)
.get_etag(body.sender_user(), &body.version)
.await,
})
}
@ -252,11 +253,9 @@ pub(crate) async fn add_backup_keys_for_session_route(
pub(crate) async fn get_backup_keys_route(
State(services): State<crate::State>, body: Ruma<get_backup_keys::v3::Request>,
) -> Result<get_backup_keys::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let rooms = services
.key_backups
.get_all(sender_user, &body.version)
.get_all(body.sender_user(), &body.version)
.await;
Ok(get_backup_keys::v3::Response {
@ -270,11 +269,9 @@ pub(crate) async fn get_backup_keys_route(
pub(crate) async fn get_backup_keys_for_room_route(
State(services): State<crate::State>, body: Ruma<get_backup_keys_for_room::v3::Request>,
) -> Result<get_backup_keys_for_room::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sessions = services
.key_backups
.get_room(sender_user, &body.version, &body.room_id)
.get_room(body.sender_user(), &body.version, &body.room_id)
.await;
Ok(get_backup_keys_for_room::v3::Response {
@ -288,11 +285,9 @@ pub(crate) async fn get_backup_keys_for_room_route(
pub(crate) async fn get_backup_keys_for_session_route(
State(services): State<crate::State>, body: Ruma<get_backup_keys_for_session::v3::Request>,
) -> Result<get_backup_keys_for_session::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let key_data = services
.key_backups
.get_session(sender_user, &body.version, &body.room_id, &body.session_id)
.get_session(body.sender_user(), &body.version, &body.room_id, &body.session_id)
.await
.map_err(|_| err!(Request(NotFound(debug_error!("Backup key not found for this user's session.")))))?;
@ -307,22 +302,20 @@ pub(crate) async fn get_backup_keys_for_session_route(
pub(crate) async fn delete_backup_keys_route(
State(services): State<crate::State>, body: Ruma<delete_backup_keys::v3::Request>,
) -> Result<delete_backup_keys::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services
.key_backups
.delete_all_keys(sender_user, &body.version)
.delete_all_keys(body.sender_user(), &body.version)
.await;
Ok(delete_backup_keys::v3::Response {
count: services
.key_backups
.count_keys(sender_user, &body.version)
.count_keys(body.sender_user(), &body.version)
.await
.try_into()?,
etag: services
.key_backups
.get_etag(sender_user, &body.version)
.get_etag(body.sender_user(), &body.version)
.await,
})
}
@ -333,22 +326,20 @@ pub(crate) async fn delete_backup_keys_route(
pub(crate) async fn delete_backup_keys_for_room_route(
State(services): State<crate::State>, body: Ruma<delete_backup_keys_for_room::v3::Request>,
) -> Result<delete_backup_keys_for_room::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services
.key_backups
.delete_room_keys(sender_user, &body.version, &body.room_id)
.delete_room_keys(body.sender_user(), &body.version, &body.room_id)
.await;
Ok(delete_backup_keys_for_room::v3::Response {
count: services
.key_backups
.count_keys(sender_user, &body.version)
.count_keys(body.sender_user(), &body.version)
.await
.try_into()?,
etag: services
.key_backups
.get_etag(sender_user, &body.version)
.get_etag(body.sender_user(), &body.version)
.await,
})
}
@ -359,22 +350,20 @@ pub(crate) async fn delete_backup_keys_for_room_route(
pub(crate) async fn delete_backup_keys_for_session_route(
State(services): State<crate::State>, body: Ruma<delete_backup_keys_for_session::v3::Request>,
) -> Result<delete_backup_keys_for_session::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services
.key_backups
.delete_room_key(sender_user, &body.version, &body.room_id, &body.session_id)
.delete_room_key(body.sender_user(), &body.version, &body.room_id, &body.session_id)
.await;
Ok(delete_backup_keys_for_session::v3::Response {
count: services
.key_backups
.count_keys(sender_user, &body.version)
.count_keys(body.sender_user(), &body.version)
.await
.try_into()?,
etag: services
.key_backups
.get_etag(sender_user, &body.version)
.get_etag(body.sender_user(), &body.version)
.await,
})
}

View File

@ -3,17 +3,14 @@ use std::{mem, ops::Deref};
use axum::{async_trait, body::Body, extract::FromRequest};
use bytes::{BufMut, BytesMut};
use conduit::{debug, err, trace, utils::string::EMPTY, Error, Result};
use ruma::{api::IncomingRequest, CanonicalJsonValue, OwnedDeviceId, OwnedServerName, OwnedUserId, UserId};
use ruma::{api::IncomingRequest, CanonicalJsonValue, OwnedDeviceId, OwnedServerName, OwnedUserId, ServerName, UserId};
use service::Services;
use super::{auth, auth::Auth, request, request::Request};
use crate::{service::appservice::RegistrationInfo, State};
/// Extractor for Ruma request structs
pub(crate) struct Args<T>
where
T: IncomingRequest + Send + Sync + 'static,
{
pub(crate) struct Args<T> {
/// Request struct body
pub(crate) body: T,
@ -38,6 +35,17 @@ where
pub(crate) json_body: Option<CanonicalJsonValue>,
}
impl<T> Args<T>
where
T: IncomingRequest + Send + Sync + 'static,
{
#[inline]
pub(crate) fn sender_user(&self) -> &UserId { self.sender_user.as_deref().expect("user is authenticated") }
#[inline]
pub(crate) fn origin(&self) -> &ServerName { self.origin.as_deref().expect("server is authenticated") }
}
#[async_trait]
impl<T> FromRequest<State, Body> for Args<T>
where

View File

@ -38,7 +38,7 @@ macro_rules! ruma_handler {
where
Fun: Fn($($tx,)* Ruma<Req>,) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<Req::OutgoingResponse, Err>> + Send,
Req: IncomingRequest + Send + Sync,
Req: IncomingRequest + Send + Sync + 'static,
Err: IntoResponse + Send,
<Req as IncomingRequest>::OutgoingResponse: Send,
$( $tx: FromRequestParts<State> + Send + Sync + 'static, )*

View File

@ -18,12 +18,10 @@ use crate::Ruma;
pub(crate) async fn get_backfill_route(
State(services): State<crate::State>, body: Ruma<get_backfill::v1::Request>,
) -> Result<get_backfill::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if !services
@ -33,7 +31,7 @@ pub(crate) async fn get_backfill_route(
.await && !services
.rooms
.state_cache
.server_in_room(origin, &body.room_id)
.server_in_room(body.origin(), &body.room_id)
.await
{
return Err!(Request(Forbidden("Server is not in room.")));
@ -59,6 +57,7 @@ pub(crate) async fn get_backfill_route(
.try_into()
.expect("UInt could not be converted to usize");
let origin = body.origin();
let pdus = services
.rooms
.timeline

View File

@ -13,8 +13,6 @@ use crate::Ruma;
pub(crate) async fn get_event_route(
State(services): State<crate::State>, body: Ruma<get_event::v1::Request>,
) -> Result<get_event::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
let event = services
.rooms
.timeline
@ -37,7 +35,7 @@ pub(crate) async fn get_event_route(
.await && !services
.rooms
.state_cache
.server_in_room(origin, room_id)
.server_in_room(body.origin(), room_id)
.await
{
return Err!(Request(Forbidden("Server is not in room.")));
@ -46,7 +44,7 @@ pub(crate) async fn get_event_route(
if !services
.rooms
.state_accessor
.server_can_see_event(origin, room_id, &body.event_id)
.server_can_see_event(body.origin(), room_id, &body.event_id)
.await?
{
return Err!(Request(Forbidden("Server is not allowed to see event.")));

View File

@ -18,12 +18,10 @@ use crate::Ruma;
pub(crate) async fn get_event_authorization_route(
State(services): State<crate::State>, body: Ruma<get_event_authorization::v1::Request>,
) -> Result<get_event_authorization::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if !services
@ -33,7 +31,7 @@ pub(crate) async fn get_event_authorization_route(
.await && !services
.rooms
.state_cache
.server_in_room(origin, &body.room_id)
.server_in_room(body.origin(), &body.room_id)
.await
{
return Err(Error::BadRequest(ErrorKind::forbidden(), "Server is not in room."));

View File

@ -13,12 +13,10 @@ use crate::Ruma;
pub(crate) async fn get_missing_events_route(
State(services): State<crate::State>, body: Ruma<get_missing_events::v1::Request>,
) -> Result<get_missing_events::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if !services
@ -28,7 +26,7 @@ pub(crate) async fn get_missing_events_route(
.await && !services
.rooms
.state_cache
.server_in_room(origin, &body.room_id)
.server_in_room(body.origin(), &body.room_id)
.await
{
return Err(Error::BadRequest(ErrorKind::forbidden(), "Server is not in room"));
@ -71,7 +69,7 @@ pub(crate) async fn get_missing_events_route(
if !services
.rooms
.state_accessor
.server_can_see_event(origin, &body.room_id, &queued_events[i])
.server_can_see_event(body.origin(), &body.room_id, &queued_events[i])
.await?
{
i = i.saturating_add(1);

View File

@ -10,13 +10,11 @@ use crate::{Error, Result, Ruma};
pub(crate) async fn get_hierarchy_route(
State(services): State<crate::State>, body: Ruma<get_hierarchy::v1::Request>,
) -> Result<get_hierarchy::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
if services.rooms.metadata.exists(&body.room_id).await {
services
.rooms
.spaces
.get_federation_hierarchy(&body.room_id, origin, body.suggested_only)
.get_federation_hierarchy(&body.room_id, body.origin(), body.suggested_only)
.await
} else {
Err(Error::BadRequest(ErrorKind::NotFound, "Room does not exist."))

View File

@ -18,13 +18,11 @@ pub(crate) async fn create_invite_route(
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
body: Ruma<create_invite::v2::Request>,
) -> Result<create_invite::v2::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
// ACL check origin
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if !services
@ -55,10 +53,11 @@ pub(crate) async fn create_invite_route(
.globals
.config
.forbidden_remote_server_names
.contains(origin)
.contains(body.origin())
{
warn!(
"Received federated/remote invite from banned server {origin} for room ID {}. Rejecting.",
"Received federated/remote invite from banned server {} for room ID {}. Rejecting.",
body.origin(),
body.room_id
);

View File

@ -30,8 +30,7 @@ pub(crate) async fn create_join_event_template_route(
return Err(Error::BadRequest(ErrorKind::NotFound, "Room is unknown to this server."));
}
let origin = body.origin.as_ref().expect("server is authenticated");
if body.user_id.server_name() != origin {
if body.user_id.server_name() != body.origin() {
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Not allowed to join on behalf of another server/user",
@ -42,19 +41,21 @@ pub(crate) async fn create_join_event_template_route(
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if services
.globals
.config
.forbidden_remote_server_names
.contains(origin)
.contains(body.origin())
{
warn!(
"Server {origin} for remote user {} tried joining room ID {} which has a server name that is globally \
"Server {} for remote user {} tried joining room ID {} which has a server name that is globally \
forbidden. Rejecting.",
&body.user_id, &body.room_id,
body.origin(),
&body.user_id,
&body.room_id,
);
return Err(Error::BadRequest(
ErrorKind::forbidden(),

View File

@ -19,8 +19,7 @@ pub(crate) async fn create_leave_event_template_route(
return Err(Error::BadRequest(ErrorKind::NotFound, "Room is unknown to this server."));
}
let origin = body.origin.as_ref().expect("server is authenticated");
if body.user_id.server_name() != origin {
if body.user_id.server_name() != body.origin() {
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Not allowed to leave on behalf of another server/user",
@ -31,7 +30,7 @@ pub(crate) async fn create_leave_event_template_route(
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
let room_version_id = services.rooms.state.get_room_version(&body.room_id).await?;

View File

@ -41,9 +41,7 @@ pub(crate) async fn send_transaction_message_route(
State(services): State<crate::State>, InsecureClientIp(client): InsecureClientIp,
body: Ruma<send_transaction_message::v1::Request>,
) -> Result<send_transaction_message::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
if *origin != body.body.origin {
if body.origin() != body.body.origin {
return Err!(Request(Forbidden(
"Not allowed to send transactions on behalf of other servers"
)));
@ -67,19 +65,19 @@ pub(crate) async fn send_transaction_message_route(
edus = ?body.edus.len(),
elapsed = ?txn_start_time.elapsed(),
id = ?body.transaction_id,
origin =?body.origin,
origin =?body.origin(),
"Starting txn",
);
let resolved_map = handle_pdus(&services, &client, &body.pdus, origin, &txn_start_time).await?;
handle_edus(&services, &client, &body.edus, origin).await;
let resolved_map = handle_pdus(&services, &client, &body.pdus, body.origin(), &txn_start_time).await?;
handle_edus(&services, &client, &body.edus, body.origin()).await;
debug!(
pdus = ?body.pdus.len(),
edus = ?body.edus.len(),
elapsed = ?txn_start_time.elapsed(),
id = ?body.transaction_id,
origin =?body.origin,
origin =?body.origin(),
"Finished txn",
);

View File

@ -217,16 +217,15 @@ async fn create_join_event(
pub(crate) async fn create_join_event_v1_route(
State(services): State<crate::State>, body: Ruma<create_join_event::v1::Request>,
) -> Result<create_join_event::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
if services
.globals
.config
.forbidden_remote_server_names
.contains(origin)
.contains(body.origin())
{
warn!(
"Server {origin} tried joining room ID {} who has a server name that is globally forbidden. Rejecting.",
"Server {} tried joining room ID {} who has a server name that is globally forbidden. Rejecting.",
body.origin(),
&body.room_id,
);
return Err(Error::BadRequest(
@ -243,8 +242,8 @@ pub(crate) async fn create_join_event_v1_route(
.contains(&server.to_owned())
{
warn!(
"Server {origin} tried joining room ID {} which has a server name that is globally forbidden. \
Rejecting.",
"Server {} tried joining room ID {} which has a server name that is globally forbidden. Rejecting.",
body.origin(),
&body.room_id,
);
return Err(Error::BadRequest(
@ -254,7 +253,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, body.origin(), &body.room_id, &body.pdu).await?;
Ok(create_join_event::v1::Response {
room_state,
@ -267,13 +266,11 @@ pub(crate) async fn create_join_event_v1_route(
pub(crate) async fn create_join_event_v2_route(
State(services): State<crate::State>, body: Ruma<create_join_event::v2::Request>,
) -> Result<create_join_event::v2::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
if services
.globals
.config
.forbidden_remote_server_names
.contains(origin)
.contains(body.origin())
{
return Err(Error::BadRequest(
ErrorKind::forbidden(),
@ -299,7 +296,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, body.origin(), &body.room_id, &body.pdu).await?;
let room_state = create_join_event::v2::RoomState {
members_omitted: false,
auth_chain,

View File

@ -8,7 +8,7 @@ use ruma::{
room::member::{MembershipState, RoomMemberEventContent},
StateEventType,
},
OwnedServerName, OwnedUserId, RoomId, ServerName,
OwnedUserId, RoomId, ServerName,
};
use serde_json::value::RawValue as RawJsonValue;
@ -23,9 +23,7 @@ use crate::{
pub(crate) async fn create_leave_event_v1_route(
State(services): State<crate::State>, body: Ruma<create_leave_event::v1::Request>,
) -> 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, body.origin(), &body.room_id, &body.pdu).await?;
Ok(create_leave_event::v1::Response::new())
}
@ -36,9 +34,7 @@ pub(crate) async fn create_leave_event_v1_route(
pub(crate) async fn create_leave_event_v2_route(
State(services): State<crate::State>, body: Ruma<create_leave_event::v2::Request>,
) -> 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, body.origin(), &body.room_id, &body.pdu).await?;
Ok(create_leave_event::v2::Response::new())
}
@ -139,16 +135,6 @@ async fn create_leave_event(
));
}
let origin: OwnedServerName = serde_json::from_value(
serde_json::to_value(
value
.get("origin")
.ok_or_else(|| Error::BadRequest(ErrorKind::InvalidParam, "Event missing origin property."))?,
)
.expect("CanonicalJson is valid json value"),
)
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "origin is not a server name."))?;
let mutex_lock = services
.rooms
.event_handler
@ -159,7 +145,7 @@ async fn create_leave_event(
let pdu_id: Vec<u8> = services
.rooms
.event_handler
.handle_incoming_pdu(&origin, room_id, &event_id, value, true)
.handle_incoming_pdu(origin, room_id, &event_id, value, true)
.await?
.ok_or_else(|| Error::BadRequest(ErrorKind::InvalidParam, "Could not accept as timeline event."))?;

View File

@ -13,12 +13,10 @@ use crate::Ruma;
pub(crate) async fn get_room_state_route(
State(services): State<crate::State>, body: Ruma<get_room_state::v1::Request>,
) -> Result<get_room_state::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if !services
@ -28,7 +26,7 @@ pub(crate) async fn get_room_state_route(
.await && !services
.rooms
.state_cache
.server_in_room(origin, &body.room_id)
.server_in_room(body.origin(), &body.room_id)
.await
{
return Err!(Request(Forbidden("Server is not in room.")));

View File

@ -14,12 +14,10 @@ use crate::{Result, Ruma};
pub(crate) async fn get_room_state_ids_route(
State(services): State<crate::State>, body: Ruma<get_room_state_ids::v1::Request>,
) -> Result<get_room_state_ids::v1::Response> {
let origin = body.origin.as_ref().expect("server is authenticated");
services
.rooms
.event_handler
.acl_check(origin, &body.room_id)
.acl_check(body.origin(), &body.room_id)
.await?;
if !services
@ -29,7 +27,7 @@ pub(crate) async fn get_room_state_ids_route(
.await && !services
.rooms
.state_cache
.server_in_room(origin, &body.room_id)
.server_in_room(body.origin(), &body.room_id)
.await
{
return Err!(Request(Forbidden("Server is not in room.")));

View File

@ -27,8 +27,6 @@ pub(crate) async fn get_devices_route(
));
}
let origin = body.origin.as_ref().expect("server is authenticated");
let user_id = &body.user_id;
Ok(get_devices::v1::Response {
user_id: user_id.clone(),
@ -66,12 +64,12 @@ pub(crate) async fn get_devices_route(
.await,
master_key: services
.users
.get_master_key(None, &body.user_id, &|u| u.server_name() == origin)
.get_master_key(None, &body.user_id, &|u| u.server_name() == body.origin())
.await
.ok(),
self_signing_key: services
.users
.get_self_signing_key(None, &body.user_id, &|u| u.server_name() == origin)
.get_self_signing_key(None, &body.user_id, &|u| u.server_name() == body.origin())
.await
.ok(),
})