From ee92a33a4de8db924ee5e203f5b3c64dade8dcc6 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 24 Oct 2024 12:03:56 +0000 Subject: [PATCH] add some accessors to Ar for common patterns Signed-off-by: Jason Volk --- src/api/client/backup.rs | 107 ++++++++++++--------------- src/api/router/args.rs | 18 +++-- src/api/router/handler.rs | 2 +- src/api/server/backfill.rs | 7 +- src/api/server/event.rs | 6 +- src/api/server/event_auth.rs | 6 +- src/api/server/get_missing_events.rs | 8 +- src/api/server/hierarchy.rs | 4 +- src/api/server/invite.rs | 9 +-- src/api/server/make_join.rs | 13 ++-- src/api/server/make_leave.rs | 5 +- src/api/server/send.rs | 12 ++- src/api/server/send_join.rs | 19 ++--- src/api/server/send_leave.rs | 22 +----- src/api/server/state.rs | 6 +- src/api/server/state_ids.rs | 6 +- src/api/server/user.rs | 6 +- 17 files changed, 109 insertions(+), 147 deletions(-) diff --git a/src/api/client/backup.rs b/src/api/client/backup.rs index d52da80a..f435e086 100644 --- a/src/api/client/backup.rs +++ b/src/api/client/backup.rs @@ -18,10 +18,9 @@ use crate::{Result, Ruma}; pub(crate) async fn create_backup_version_route( State(services): State, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, }) } diff --git a/src/api/router/args.rs b/src/api/router/args.rs index 746e1cfc..cefacac1 100644 --- a/src/api/router/args.rs +++ b/src/api/router/args.rs @@ -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 -where - T: IncomingRequest + Send + Sync + 'static, -{ +pub(crate) struct Args { /// Request struct body pub(crate) body: T, @@ -38,6 +35,17 @@ where pub(crate) json_body: Option, } +impl Args +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 FromRequest for Args where diff --git a/src/api/router/handler.rs b/src/api/router/handler.rs index 3b7b1eeb..0022f06a 100644 --- a/src/api/router/handler.rs +++ b/src/api/router/handler.rs @@ -38,7 +38,7 @@ macro_rules! ruma_handler { where Fun: Fn($($tx,)* Ruma,) -> Fut + Send + Sync + 'static, Fut: Future> + Send, - Req: IncomingRequest + Send + Sync, + Req: IncomingRequest + Send + Sync + 'static, Err: IntoResponse + Send, ::OutgoingResponse: Send, $( $tx: FromRequestParts + Send + Sync + 'static, )* diff --git a/src/api/server/backfill.rs b/src/api/server/backfill.rs index 2bbc95ca..088b891a 100644 --- a/src/api/server/backfill.rs +++ b/src/api/server/backfill.rs @@ -18,12 +18,10 @@ use crate::Ruma; pub(crate) async fn get_backfill_route( State(services): State, body: Ruma, ) -> Result { - 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 diff --git a/src/api/server/event.rs b/src/api/server/event.rs index e4eac794..64ce3e40 100644 --- a/src/api/server/event.rs +++ b/src/api/server/event.rs @@ -13,8 +13,6 @@ use crate::Ruma; pub(crate) async fn get_event_route( State(services): State, body: Ruma, ) -> Result { - 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."))); diff --git a/src/api/server/event_auth.rs b/src/api/server/event_auth.rs index 8307a4ad..8fe96f81 100644 --- a/src/api/server/event_auth.rs +++ b/src/api/server/event_auth.rs @@ -18,12 +18,10 @@ use crate::Ruma; pub(crate) async fn get_event_authorization_route( State(services): State, body: Ruma, ) -> Result { - 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.")); diff --git a/src/api/server/get_missing_events.rs b/src/api/server/get_missing_events.rs index e267898f..aee4fbe9 100644 --- a/src/api/server/get_missing_events.rs +++ b/src/api/server/get_missing_events.rs @@ -13,12 +13,10 @@ use crate::Ruma; pub(crate) async fn get_missing_events_route( State(services): State, body: Ruma, ) -> Result { - 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); diff --git a/src/api/server/hierarchy.rs b/src/api/server/hierarchy.rs index 002bd763..e3ce7108 100644 --- a/src/api/server/hierarchy.rs +++ b/src/api/server/hierarchy.rs @@ -10,13 +10,11 @@ use crate::{Error, Result, Ruma}; pub(crate) async fn get_hierarchy_route( State(services): State, body: Ruma, ) -> Result { - 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.")) diff --git a/src/api/server/invite.rs b/src/api/server/invite.rs index a9e404c5..b30a1b58 100644 --- a/src/api/server/invite.rs +++ b/src/api/server/invite.rs @@ -18,13 +18,11 @@ pub(crate) async fn create_invite_route( State(services): State, InsecureClientIp(client): InsecureClientIp, body: Ruma, ) -> Result { - 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 ); diff --git a/src/api/server/make_join.rs b/src/api/server/make_join.rs index 85668038..c3524f0e 100644 --- a/src/api/server/make_join.rs +++ b/src/api/server/make_join.rs @@ -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(), diff --git a/src/api/server/make_leave.rs b/src/api/server/make_leave.rs index 81a32c86..33a94560 100644 --- a/src/api/server/make_leave.rs +++ b/src/api/server/make_leave.rs @@ -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?; diff --git a/src/api/server/send.rs b/src/api/server/send.rs index d5d3ffbb..2da99c93 100644 --- a/src/api/server/send.rs +++ b/src/api/server/send.rs @@ -41,9 +41,7 @@ pub(crate) async fn send_transaction_message_route( State(services): State, InsecureClientIp(client): InsecureClientIp, body: Ruma, ) -> Result { - 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", ); diff --git a/src/api/server/send_join.rs b/src/api/server/send_join.rs index d888d75e..c3273baf 100644 --- a/src/api/server/send_join.rs +++ b/src/api/server/send_join.rs @@ -217,16 +217,15 @@ async fn create_join_event( pub(crate) async fn create_join_event_v1_route( State(services): State, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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, diff --git a/src/api/server/send_leave.rs b/src/api/server/send_leave.rs index 0530f9dd..7b4a8aee 100644 --- a/src/api/server/send_leave.rs +++ b/src/api/server/send_leave.rs @@ -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, body: Ruma, ) -> Result { - 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, body: Ruma, ) -> Result { - 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 = 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."))?; diff --git a/src/api/server/state.rs b/src/api/server/state.rs index 3a27cd0a..59bb6c7b 100644 --- a/src/api/server/state.rs +++ b/src/api/server/state.rs @@ -13,12 +13,10 @@ use crate::Ruma; pub(crate) async fn get_room_state_route( State(services): State, body: Ruma, ) -> Result { - 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."))); diff --git a/src/api/server/state_ids.rs b/src/api/server/state_ids.rs index b026abf1..957a2a86 100644 --- a/src/api/server/state_ids.rs +++ b/src/api/server/state_ids.rs @@ -14,12 +14,10 @@ use crate::{Result, Ruma}; pub(crate) async fn get_room_state_ids_route( State(services): State, body: Ruma, ) -> Result { - 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."))); diff --git a/src/api/server/user.rs b/src/api/server/user.rs index 0718da58..40f330a1 100644 --- a/src/api/server/user.rs +++ b/src/api/server/user.rs @@ -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(), })