From 230ebd3884940d1e4bc084c21d63652a853b09ca Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Sat, 18 May 2024 18:31:36 -0700 Subject: [PATCH] don't automatically wrap in `RumaResponse` This allows us to use the `ruma_route` convenience function even when we need to add our own hacks into the responses, thus making us less reliant on Ruma. --- src/api/client_server/account.rs | 48 +++++----- src/api/client_server/alias.rs | 14 +-- src/api/client_server/backup.rs | 82 ++++++++--------- src/api/client_server/capabilities.rs | 8 +- src/api/client_server/config.rs | 22 ++--- src/api/client_server/context.rs | 6 +- src/api/client_server/device.rs | 26 +++--- src/api/client_server/directory.rs | 21 +++-- src/api/client_server/filter.rs | 12 +-- src/api/client_server/keys.rs | 32 +++---- src/api/client_server/media.rs | 43 +++++---- src/api/client_server/membership.rs | 59 ++++++------ src/api/client_server/message.rs | 16 ++-- src/api/client_server/profile.rs | 42 +++++---- src/api/client_server/push.rs | 52 +++++------ src/api/client_server/read_marker.rs | 9 +- src/api/client_server/redact.rs | 8 +- src/api/client_server/relations.rs | 54 ++++++----- src/api/client_server/report.rs | 6 +- src/api/client_server/room.rs | 24 ++--- src/api/client_server/search.rs | 8 +- src/api/client_server/session.rs | 22 ++--- src/api/client_server/space.rs | 5 +- src/api/client_server/state.rs | 18 ++-- src/api/client_server/sync.rs | 14 +-- src/api/client_server/tag.rs | 16 ++-- src/api/client_server/thirdparty.rs | 8 +- src/api/client_server/threads.rs | 8 +- src/api/client_server/to_device.rs | 8 +- src/api/client_server/typing.rs | 6 +- src/api/client_server/unversioned.rs | 6 +- src/api/client_server/user_directory.rs | 8 +- src/api/client_server/voip.rs | 8 +- src/api/server_server.rs | 116 ++++++++++++------------ src/main.rs | 8 +- 35 files changed, 438 insertions(+), 405 deletions(-) diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index 4d529b68..52e6c5fc 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -19,7 +19,9 @@ use ruma::{ use tracing::{info, warn}; use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH}; -use crate::{api::client_server, services, utils, Error, Result, Ruma}; +use crate::{ + api::client_server, services, utils, Error, Result, Ruma, RumaResponse, +}; const RANDOM_USER_ID_LENGTH: usize = 10; @@ -36,7 +38,7 @@ const RANDOM_USER_ID_LENGTH: usize = 10; /// invalid when trying to register pub(crate) async fn get_register_available_route( body: Ruma, -) -> Result { +) -> Result> { // Validate user id let user_id = UserId::parse_with_server_name( body.username.to_lowercase(), @@ -63,9 +65,9 @@ pub(crate) async fn get_register_available_route( // TODO add check for appservice namespaces // If no if check is true we have an username that's available to be used. - Ok(get_username_availability::v3::Response { + Ok(RumaResponse(get_username_availability::v3::Response { available: true, - }) + })) } /// # `POST /_matrix/client/r0/register` @@ -88,7 +90,7 @@ pub(crate) async fn get_register_available_route( #[allow(clippy::too_many_lines)] pub(crate) async fn register_route( body: Ruma, -) -> Result { +) -> Result> { if !services().globals.allow_registration() && body.appservice_info.is_none() { @@ -248,13 +250,13 @@ pub(crate) async fn register_route( // Inhibit login does not work for guests if !is_guest && body.inhibit_login { - return Ok(register::v3::Response { + return Ok(RumaResponse(register::v3::Response { access_token: None, user_id, device_id: None, refresh_token: None, expires_in: None, - }); + })); } // Generate new device id if the user didn't specify one @@ -300,13 +302,13 @@ pub(crate) async fn register_route( } } - Ok(register::v3::Response { + Ok(RumaResponse(register::v3::Response { access_token: Some(token), user_id, device_id: Some(device_id), refresh_token: None, expires_in: None, - }) + })) } /// # `POST /_matrix/client/r0/account/password` @@ -328,7 +330,7 @@ pub(crate) async fn register_route( /// - Triggers device list updates pub(crate) async fn change_password_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated"); @@ -381,7 +383,7 @@ pub(crate) async fn change_password_route( format!("User {sender_user} changed their password."), )); - Ok(change_password::v3::Response {}) + Ok(RumaResponse(change_password::v3::Response {})) } /// # `GET _matrix/client/r0/account/whoami` @@ -391,16 +393,16 @@ pub(crate) async fn change_password_route( /// Note: Also works for Application Services pub(crate) async fn whoami_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let device_id = body.sender_device.as_ref().cloned(); - Ok(whoami::v3::Response { + Ok(RumaResponse(whoami::v3::Response { user_id: sender_user.clone(), device_id, is_guest: services().users.is_deactivated(sender_user)? && body.appservice_info.is_none(), - }) + })) } /// # `POST /_matrix/client/r0/account/deactivate` @@ -416,7 +418,7 @@ pub(crate) async fn whoami_route( /// - Removes ability to log in again pub(crate) async fn deactivate_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated"); @@ -461,9 +463,9 @@ pub(crate) async fn deactivate_route( format!("User {sender_user} deactivated their account."), )); - Ok(deactivate::v3::Response { + Ok(RumaResponse(deactivate::v3::Response { id_server_unbind_result: ThirdPartyIdRemovalStatus::NoSupport, - }) + })) } /// # `GET _matrix/client/v3/account/3pid` @@ -473,11 +475,11 @@ pub(crate) async fn deactivate_route( /// - Currently always returns empty list pub(crate) async fn third_party_route( body: Ruma, -) -> Result { +) -> Result> { let _sender_user = body.sender_user.as_ref().expect("user is authenticated"); - Ok(get_3pids::v3::Response::new(Vec::new())) + Ok(RumaResponse(get_3pids::v3::Response::new(Vec::new()))) } /// # `POST /_matrix/client/v3/account/3pid/email/requestToken` @@ -489,7 +491,8 @@ pub(crate) async fn third_party_route( /// as a contact option. pub(crate) async fn request_3pid_management_token_via_email_route( _body: Ruma, -) -> Result { +) -> Result> +{ Err(Error::BadRequest( ErrorKind::ThreepidDenied, "Third party identifier is not allowed", @@ -503,9 +506,12 @@ pub(crate) async fn request_3pid_management_token_via_email_route( /// /// - 403 signals that The homeserver does not allow the third party identifier /// as a contact option. +#[rustfmt::skip] pub(crate) async fn request_3pid_management_token_via_msisdn_route( _body: Ruma, -) -> Result { +) +-> Result> +{ Err(Error::BadRequest( ErrorKind::ThreepidDenied, "Third party identifier is not allowed", diff --git a/src/api/client_server/alias.rs b/src/api/client_server/alias.rs index 5d1a0c30..41db0740 100644 --- a/src/api/client_server/alias.rs +++ b/src/api/client_server/alias.rs @@ -11,14 +11,14 @@ use ruma::{ OwnedRoomAliasId, }; -use crate::{services, Error, Result, Ruma}; +use crate::{services, Error, Result, Ruma, RumaResponse}; /// # `PUT /_matrix/client/r0/directory/room/{roomAlias}` /// /// Creates a new room alias on this server. pub(crate) async fn create_alias_route( body: Ruma, -) -> Result { +) -> Result> { if body.room_alias.server_name() != services().globals.server_name() { return Err(Error::BadRequest( ErrorKind::InvalidParam, @@ -46,7 +46,7 @@ pub(crate) async fn create_alias_route( services().rooms.alias.set_alias(&body.room_alias, &body.room_id)?; - Ok(create_alias::v3::Response::new()) + Ok(RumaResponse(create_alias::v3::Response::new())) } /// # `DELETE /_matrix/client/r0/directory/room/{roomAlias}` @@ -57,7 +57,7 @@ pub(crate) async fn create_alias_route( /// - TODO: Update canonical alias event pub(crate) async fn delete_alias_route( body: Ruma, -) -> Result { +) -> Result> { if body.room_alias.server_name() != services().globals.server_name() { return Err(Error::BadRequest( ErrorKind::InvalidParam, @@ -83,7 +83,7 @@ pub(crate) async fn delete_alias_route( // TODO: update alt_aliases? - Ok(delete_alias::v3::Response::new()) + Ok(RumaResponse(delete_alias::v3::Response::new())) } /// # `GET /_matrix/client/r0/directory/room/{roomAlias}` @@ -93,8 +93,8 @@ pub(crate) async fn delete_alias_route( /// - TODO: Suggest more servers to join via pub(crate) async fn get_alias_route( body: Ruma, -) -> Result { - get_alias_helper(body.body.room_alias).await +) -> Result> { + get_alias_helper(body.body.room_alias).await.map(RumaResponse) } pub(crate) async fn get_alias_helper( diff --git a/src/api/client_server/backup.rs b/src/api/client_server/backup.rs index 4cfffcc1..161f0fd2 100644 --- a/src/api/client_server/backup.rs +++ b/src/api/client_server/backup.rs @@ -9,21 +9,21 @@ use ruma::api::client::{ error::ErrorKind, }; -use crate::{services, Error, Result, Ruma}; +use crate::{services, Error, Result, Ruma, RumaResponse}; /// # `POST /_matrix/client/r0/room_keys/version` /// /// Creates a new backup. pub(crate) async fn create_backup_version_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let version = services().key_backups.create_backup(sender_user, &body.algorithm)?; - Ok(create_backup_version::v3::Response { + Ok(RumaResponse(create_backup_version::v3::Response { version, - }) + })) } /// # `PUT /_matrix/client/r0/room_keys/version/{version}` @@ -32,7 +32,7 @@ pub(crate) async fn create_backup_version_route( /// modified. pub(crate) async fn update_backup_version_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); services().key_backups.update_backup( sender_user, @@ -40,7 +40,7 @@ pub(crate) async fn update_backup_version_route( &body.algorithm, )?; - Ok(update_backup_version::v3::Response {}) + Ok(RumaResponse(update_backup_version::v3::Response {})) } /// # `GET /_matrix/client/r0/room_keys/version` @@ -48,7 +48,7 @@ pub(crate) async fn update_backup_version_route( /// Get information about the latest backup version. pub(crate) async fn get_latest_backup_info_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let (version, algorithm) = services() @@ -59,7 +59,7 @@ pub(crate) async fn get_latest_backup_info_route( "Key backup does not exist.", ))?; - Ok(get_latest_backup_info::v3::Response { + Ok(RumaResponse(get_latest_backup_info::v3::Response { algorithm, count: services() .key_backups @@ -68,7 +68,7 @@ pub(crate) async fn get_latest_backup_info_route( .expect("count should fit in UInt"), etag: services().key_backups.get_etag(sender_user, &version)?, version, - }) + })) } /// # `GET /_matrix/client/r0/room_keys/version` @@ -76,7 +76,7 @@ pub(crate) async fn get_latest_backup_info_route( /// Get information about an existing backup. pub(crate) async fn get_backup_info_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let algorithm = services() .key_backups @@ -86,7 +86,7 @@ pub(crate) async fn get_backup_info_route( "Key backup does not exist.", ))?; - Ok(get_backup_info::v3::Response { + Ok(RumaResponse(get_backup_info::v3::Response { algorithm, count: services() .key_backups @@ -95,7 +95,7 @@ pub(crate) async fn get_backup_info_route( .expect("count should fit in UInt"), etag: services().key_backups.get_etag(sender_user, &body.version)?, version: body.version.clone(), - }) + })) } /// # `DELETE /_matrix/client/r0/room_keys/version/{version}` @@ -106,12 +106,12 @@ pub(crate) async fn get_backup_info_route( /// to the backup pub(crate) async fn delete_backup_version_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); services().key_backups.delete_backup(sender_user, &body.version)?; - Ok(delete_backup_version::v3::Response {}) + Ok(RumaResponse(delete_backup_version::v3::Response {})) } /// # `PUT /_matrix/client/r0/room_keys/keys` @@ -124,7 +124,7 @@ pub(crate) async fn delete_backup_version_route( /// - Returns the new number of keys in this backup and the etag pub(crate) async fn add_backup_keys_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if Some(&body.version) @@ -152,14 +152,14 @@ pub(crate) async fn add_backup_keys_route( } } - Ok(add_backup_keys::v3::Response { + Ok(RumaResponse(add_backup_keys::v3::Response { count: services() .key_backups .count_keys(sender_user, &body.version)? .try_into() .expect("count should fit in UInt"), etag: services().key_backups.get_etag(sender_user, &body.version)?, - }) + })) } /// # `PUT /_matrix/client/r0/room_keys/keys/{roomId}` @@ -172,7 +172,7 @@ pub(crate) async fn add_backup_keys_route( /// - Returns the new number of keys in this backup and the etag pub(crate) async fn add_backup_keys_for_room_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if Some(&body.version) @@ -198,14 +198,14 @@ pub(crate) async fn add_backup_keys_for_room_route( )?; } - Ok(add_backup_keys_for_room::v3::Response { + Ok(RumaResponse(add_backup_keys_for_room::v3::Response { count: services() .key_backups .count_keys(sender_user, &body.version)? .try_into() .expect("count should fit in UInt"), etag: services().key_backups.get_etag(sender_user, &body.version)?, - }) + })) } /// # `PUT /_matrix/client/r0/room_keys/keys/{roomId}/{sessionId}` @@ -218,7 +218,7 @@ pub(crate) async fn add_backup_keys_for_room_route( /// - Returns the new number of keys in this backup and the etag pub(crate) async fn add_backup_keys_for_session_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if Some(&body.version) @@ -242,14 +242,14 @@ pub(crate) async fn add_backup_keys_for_session_route( &body.session_data, )?; - Ok(add_backup_keys_for_session::v3::Response { + Ok(RumaResponse(add_backup_keys_for_session::v3::Response { count: services() .key_backups .count_keys(sender_user, &body.version)? .try_into() .expect("count should fit in UInt"), etag: services().key_backups.get_etag(sender_user, &body.version)?, - }) + })) } /// # `GET /_matrix/client/r0/room_keys/keys` @@ -257,14 +257,14 @@ pub(crate) async fn add_backup_keys_for_session_route( /// Retrieves all keys from the backup. pub(crate) async fn get_backup_keys_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let rooms = services().key_backups.get_all(sender_user, &body.version)?; - Ok(get_backup_keys::v3::Response { + Ok(RumaResponse(get_backup_keys::v3::Response { rooms, - }) + })) } /// # `GET /_matrix/client/r0/room_keys/keys/{roomId}` @@ -272,7 +272,7 @@ pub(crate) async fn get_backup_keys_route( /// Retrieves all keys from the backup for a given room. pub(crate) async fn get_backup_keys_for_room_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sessions = services().key_backups.get_room( @@ -281,9 +281,9 @@ pub(crate) async fn get_backup_keys_for_room_route( &body.room_id, )?; - Ok(get_backup_keys_for_room::v3::Response { + Ok(RumaResponse(get_backup_keys_for_room::v3::Response { sessions, - }) + })) } /// # `GET /_matrix/client/r0/room_keys/keys/{roomId}/{sessionId}` @@ -291,7 +291,7 @@ pub(crate) async fn get_backup_keys_for_room_route( /// Retrieves a key from the backup. pub(crate) async fn get_backup_keys_for_session_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let key_data = services() @@ -307,9 +307,9 @@ pub(crate) async fn get_backup_keys_for_session_route( "Backup key not found for this user's session.", ))?; - Ok(get_backup_keys_for_session::v3::Response { + Ok(RumaResponse(get_backup_keys_for_session::v3::Response { key_data, - }) + })) } /// # `DELETE /_matrix/client/r0/room_keys/keys` @@ -317,19 +317,19 @@ pub(crate) async fn get_backup_keys_for_session_route( /// Delete the keys from the backup. pub(crate) async fn delete_backup_keys_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); services().key_backups.delete_all_keys(sender_user, &body.version)?; - Ok(delete_backup_keys::v3::Response { + Ok(RumaResponse(delete_backup_keys::v3::Response { count: services() .key_backups .count_keys(sender_user, &body.version)? .try_into() .expect("count should fit in UInt"), etag: services().key_backups.get_etag(sender_user, &body.version)?, - }) + })) } /// # `DELETE /_matrix/client/r0/room_keys/keys/{roomId}` @@ -337,7 +337,7 @@ pub(crate) async fn delete_backup_keys_route( /// Delete the keys from the backup for a given room. pub(crate) async fn delete_backup_keys_for_room_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); services().key_backups.delete_room_keys( @@ -346,14 +346,14 @@ pub(crate) async fn delete_backup_keys_for_room_route( &body.room_id, )?; - Ok(delete_backup_keys_for_room::v3::Response { + Ok(RumaResponse(delete_backup_keys_for_room::v3::Response { count: services() .key_backups .count_keys(sender_user, &body.version)? .try_into() .expect("count should fit in UInt"), etag: services().key_backups.get_etag(sender_user, &body.version)?, - }) + })) } /// # `DELETE /_matrix/client/r0/room_keys/keys/{roomId}/{sessionId}` @@ -361,7 +361,7 @@ pub(crate) async fn delete_backup_keys_for_room_route( /// Delete a key from the backup. pub(crate) async fn delete_backup_keys_for_session_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); services().key_backups.delete_room_key( @@ -371,12 +371,12 @@ pub(crate) async fn delete_backup_keys_for_session_route( &body.session_id, )?; - Ok(delete_backup_keys_for_session::v3::Response { + Ok(RumaResponse(delete_backup_keys_for_session::v3::Response { count: services() .key_backups .count_keys(sender_user, &body.version)? .try_into() .expect("count should fit in UInt"), etag: services().key_backups.get_etag(sender_user, &body.version)?, - }) + })) } diff --git a/src/api/client_server/capabilities.rs b/src/api/client_server/capabilities.rs index 61152bf0..410096bd 100644 --- a/src/api/client_server/capabilities.rs +++ b/src/api/client_server/capabilities.rs @@ -4,7 +4,7 @@ use ruma::api::client::discovery::get_capabilities::{ self, Capabilities, RoomVersionStability, RoomVersionsCapability, }; -use crate::{services, Result, Ruma}; +use crate::{services, Result, Ruma, RumaResponse}; /// # `GET /_matrix/client/r0/capabilities` /// @@ -12,7 +12,7 @@ use crate::{services, Result, Ruma}; /// of this server. pub(crate) async fn get_capabilities_route( _body: Ruma, -) -> Result { +) -> Result> { let mut available = BTreeMap::new(); for room_version in &services().globals.unstable_room_versions { available.insert(room_version.clone(), RoomVersionStability::Unstable); @@ -27,7 +27,7 @@ pub(crate) async fn get_capabilities_route( available, }; - Ok(get_capabilities::v3::Response { + Ok(RumaResponse(get_capabilities::v3::Response { capabilities, - }) + })) } diff --git a/src/api/client_server/config.rs b/src/api/client_server/config.rs index d4265078..2a0bcda8 100644 --- a/src/api/client_server/config.rs +++ b/src/api/client_server/config.rs @@ -14,14 +14,14 @@ use ruma::{ use serde::Deserialize; use serde_json::{json, value::RawValue as RawJsonValue}; -use crate::{services, Error, Result, Ruma}; +use crate::{services, Error, Result, Ruma, RumaResponse}; /// # `PUT /_matrix/client/r0/user/{userId}/account_data/{type}` /// /// Sets some account data for the sender user. pub(crate) async fn set_global_account_data_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let data: serde_json::Value = serde_json::from_str(body.data.json().get()) @@ -41,7 +41,7 @@ pub(crate) async fn set_global_account_data_route( }), )?; - Ok(set_global_account_data::v3::Response {}) + Ok(RumaResponse(set_global_account_data::v3::Response {})) } /// # `PUT /_matrix/client/r0/user/{userId}/rooms/{roomId}/account_data/{type}` @@ -49,7 +49,7 @@ pub(crate) async fn set_global_account_data_route( /// Sets some room account data for the sender user. pub(crate) async fn set_room_account_data_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let data: serde_json::Value = serde_json::from_str(body.data.json().get()) @@ -69,7 +69,7 @@ pub(crate) async fn set_room_account_data_route( }), )?; - Ok(set_room_account_data::v3::Response {}) + Ok(RumaResponse(set_room_account_data::v3::Response {})) } /// # `GET /_matrix/client/r0/user/{userId}/account_data/{type}` @@ -77,7 +77,7 @@ pub(crate) async fn set_room_account_data_route( /// Gets some account data for the sender user. pub(crate) async fn get_global_account_data_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let event: Box = services() @@ -92,9 +92,9 @@ pub(crate) async fn get_global_account_data_route( })? .content; - Ok(get_global_account_data::v3::Response { + Ok(RumaResponse(get_global_account_data::v3::Response { account_data, - }) + })) } /// # `GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/account_data/{type}` @@ -102,7 +102,7 @@ pub(crate) async fn get_global_account_data_route( /// Gets some room account data for the sender user. pub(crate) async fn get_room_account_data_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let event: Box = services() @@ -117,9 +117,9 @@ pub(crate) async fn get_room_account_data_route( })? .content; - Ok(get_room_account_data::v3::Response { + Ok(RumaResponse(get_room_account_data::v3::Response { account_data, - }) + })) } #[derive(Deserialize)] diff --git a/src/api/client_server/context.rs b/src/api/client_server/context.rs index 0ada0bbc..73a7453b 100644 --- a/src/api/client_server/context.rs +++ b/src/api/client_server/context.rs @@ -9,7 +9,7 @@ use ruma::{ }; use tracing::error; -use crate::{services, Error, Result, Ruma}; +use crate::{services, Error, Result, Ruma, RumaResponse}; /// # `GET /_matrix/client/r0/rooms/{roomId}/context` /// @@ -21,7 +21,7 @@ use crate::{services, Error, Result, Ruma}; #[allow(clippy::too_many_lines)] pub(crate) async fn get_context_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated"); @@ -187,5 +187,5 @@ pub(crate) async fn get_context_route( state, }; - Ok(resp) + Ok(RumaResponse(resp)) } diff --git a/src/api/client_server/device.rs b/src/api/client_server/device.rs index 59ae4710..b8cd3edc 100644 --- a/src/api/client_server/device.rs +++ b/src/api/client_server/device.rs @@ -8,14 +8,14 @@ use ruma::api::client::{ }; use super::SESSION_ID_LENGTH; -use crate::{services, utils, Error, Result, Ruma}; +use crate::{services, utils, Error, Result, Ruma, RumaResponse}; /// # `GET /_matrix/client/r0/devices` /// /// Get metadata on all devices of the sender user. pub(crate) async fn get_devices_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let devices: Vec = services() @@ -24,9 +24,9 @@ pub(crate) async fn get_devices_route( .filter_map(Result::ok) .collect(); - Ok(get_devices::v3::Response { + Ok(RumaResponse(get_devices::v3::Response { devices, - }) + })) } /// # `GET /_matrix/client/r0/devices/{deviceId}` @@ -34,7 +34,7 @@ pub(crate) async fn get_devices_route( /// Get metadata on a single device of the sender user. pub(crate) async fn get_device_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let device = services() @@ -42,9 +42,9 @@ pub(crate) async fn get_device_route( .get_device_metadata(sender_user, &body.body.device_id)? .ok_or(Error::BadRequest(ErrorKind::NotFound, "Device not found."))?; - Ok(get_device::v3::Response { + Ok(RumaResponse(get_device::v3::Response { device, - }) + })) } /// # `PUT /_matrix/client/r0/devices/{deviceId}` @@ -52,7 +52,7 @@ pub(crate) async fn get_device_route( /// Updates the metadata on a given device of the sender user. pub(crate) async fn update_device_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let mut device = services() @@ -68,7 +68,7 @@ pub(crate) async fn update_device_route( &device, )?; - Ok(update_device::v3::Response {}) + Ok(RumaResponse(update_device::v3::Response {})) } /// # `DELETE /_matrix/client/r0/devices/{deviceId}` @@ -83,7 +83,7 @@ pub(crate) async fn update_device_route( /// - Triggers device list updates pub(crate) async fn delete_device_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated"); @@ -120,7 +120,7 @@ pub(crate) async fn delete_device_route( services().users.remove_device(sender_user, &body.device_id)?; - Ok(delete_device::v3::Response {}) + Ok(RumaResponse(delete_device::v3::Response {})) } /// # `PUT /_matrix/client/r0/devices/{deviceId}` @@ -137,7 +137,7 @@ pub(crate) async fn delete_device_route( /// - Triggers device list updates pub(crate) async fn delete_devices_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated"); @@ -176,5 +176,5 @@ pub(crate) async fn delete_devices_route( services().users.remove_device(sender_user, device_id)?; } - Ok(delete_devices::v3::Response {}) + Ok(RumaResponse(delete_devices::v3::Response {})) } diff --git a/src/api/client_server/directory.rs b/src/api/client_server/directory.rs index 6776818d..2ce36036 100644 --- a/src/api/client_server/directory.rs +++ b/src/api/client_server/directory.rs @@ -29,7 +29,7 @@ use ruma::{ }; use tracing::{error, info, warn}; -use crate::{services, Error, Result, Ruma}; +use crate::{services, Error, Result, Ruma, RumaResponse}; /// # `POST /_matrix/client/r0/publicRooms` /// @@ -38,7 +38,7 @@ use crate::{services, Error, Result, Ruma}; /// - Rooms are ordered by the number of joined members pub(crate) async fn get_public_rooms_filtered_route( body: Ruma, -) -> Result { +) -> Result> { get_public_rooms_filtered_helper( body.server.as_deref(), body.limit, @@ -47,6 +47,7 @@ pub(crate) async fn get_public_rooms_filtered_route( &body.room_network, ) .await + .map(RumaResponse) } /// # `GET /_matrix/client/r0/publicRooms` @@ -56,7 +57,7 @@ pub(crate) async fn get_public_rooms_filtered_route( /// - Rooms are ordered by the number of joined members pub(crate) async fn get_public_rooms_route( body: Ruma, -) -> Result { +) -> Result> { let response = get_public_rooms_filtered_helper( body.server.as_deref(), body.limit, @@ -66,12 +67,12 @@ pub(crate) async fn get_public_rooms_route( ) .await?; - Ok(get_public_rooms::v3::Response { + Ok(RumaResponse(get_public_rooms::v3::Response { chunk: response.chunk, prev_batch: response.prev_batch, next_batch: response.next_batch, total_room_count_estimate: response.total_room_count_estimate, - }) + })) } /// # `PUT /_matrix/client/r0/directory/list/room/{roomId}` @@ -81,7 +82,7 @@ pub(crate) async fn get_public_rooms_route( /// - TODO: Access control checks pub(crate) async fn set_room_visibility_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if !services().rooms.metadata.exists(&body.room_id)? { @@ -105,7 +106,7 @@ pub(crate) async fn set_room_visibility_route( } } - Ok(set_room_visibility::v3::Response {}) + Ok(RumaResponse(set_room_visibility::v3::Response {})) } /// # `GET /_matrix/client/r0/directory/list/room/{roomId}` @@ -113,13 +114,13 @@ pub(crate) async fn set_room_visibility_route( /// Gets the visibility of a given room in the room directory. pub(crate) async fn get_room_visibility_route( body: Ruma, -) -> Result { +) -> Result> { if !services().rooms.metadata.exists(&body.room_id)? { // Return 404 if the room doesn't exist return Err(Error::BadRequest(ErrorKind::NotFound, "Room not found")); } - Ok(get_room_visibility::v3::Response { + Ok(RumaResponse(get_room_visibility::v3::Response { visibility: if services() .rooms .directory @@ -129,7 +130,7 @@ pub(crate) async fn get_room_visibility_route( } else { room::Visibility::Private }, - }) + })) } #[allow(clippy::too_many_lines)] diff --git a/src/api/client_server/filter.rs b/src/api/client_server/filter.rs index fc2f2a1c..197e3ff8 100644 --- a/src/api/client_server/filter.rs +++ b/src/api/client_server/filter.rs @@ -3,7 +3,7 @@ use ruma::api::client::{ filter::{create_filter, get_filter}, }; -use crate::{services, Error, Result, Ruma}; +use crate::{services, Error, Result, Ruma, RumaResponse}; /// # `GET /_matrix/client/r0/user/{userId}/filter/{filterId}` /// @@ -12,7 +12,7 @@ use crate::{services, Error, Result, Ruma}; /// - A user can only access their own filters pub(crate) async fn get_filter_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let Some(filter) = services().users.get_filter(sender_user, &body.filter_id)? @@ -23,7 +23,7 @@ pub(crate) async fn get_filter_route( )); }; - Ok(get_filter::v3::Response::new(filter)) + Ok(RumaResponse(get_filter::v3::Response::new(filter))) } /// # `PUT /_matrix/client/r0/user/{userId}/filter` @@ -31,9 +31,9 @@ pub(crate) async fn get_filter_route( /// Creates a new filter to be used by other endpoints. pub(crate) async fn create_filter_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); - Ok(create_filter::v3::Response::new( + Ok(RumaResponse(create_filter::v3::Response::new( services().users.create_filter(sender_user, &body.filter)?, - )) + ))) } diff --git a/src/api/client_server/keys.rs b/src/api/client_server/keys.rs index 4e68e81a..614840a1 100644 --- a/src/api/client_server/keys.rs +++ b/src/api/client_server/keys.rs @@ -23,7 +23,7 @@ use serde_json::json; use tracing::debug; use super::SESSION_ID_LENGTH; -use crate::{services, utils, Error, Result, Ruma}; +use crate::{services, utils, Error, Result, Ruma, RumaResponse}; /// # `POST /_matrix/client/r0/keys/upload` /// @@ -34,7 +34,7 @@ use crate::{services, utils, Error, Result, Ruma}; /// existing keys?) pub(crate) async fn upload_keys_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated"); @@ -64,11 +64,11 @@ pub(crate) async fn upload_keys_route( } } - Ok(upload_keys::v3::Response { + Ok(RumaResponse(upload_keys::v3::Response { one_time_key_counts: services() .users .count_one_time_keys(sender_user, sender_device)?, - }) + })) } /// # `POST /_matrix/client/r0/keys/query` @@ -81,7 +81,7 @@ pub(crate) async fn upload_keys_route( /// allowed to see pub(crate) async fn get_keys_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let response = get_keys_helper(Some(sender_user), &body.device_keys, |u| { @@ -89,7 +89,7 @@ pub(crate) async fn get_keys_route( }) .await?; - Ok(response) + Ok(RumaResponse(response)) } /// # `POST /_matrix/client/r0/keys/claim` @@ -97,10 +97,10 @@ pub(crate) async fn get_keys_route( /// Claims one-time keys pub(crate) async fn claim_keys_route( body: Ruma, -) -> Result { +) -> Result> { let response = claim_keys_helper(&body.one_time_keys).await?; - Ok(response) + Ok(RumaResponse(response)) } /// # `POST /_matrix/client/r0/keys/device_signing/upload` @@ -110,7 +110,7 @@ pub(crate) async fn claim_keys_route( /// - Requires UIAA to verify password pub(crate) async fn upload_signing_keys_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated"); @@ -156,7 +156,7 @@ pub(crate) async fn upload_signing_keys_route( )?; } - Ok(upload_signing_keys::v3::Response {}) + Ok(RumaResponse(upload_signing_keys::v3::Response {})) } /// # `POST /_matrix/client/r0/keys/signatures/upload` @@ -164,7 +164,7 @@ pub(crate) async fn upload_signing_keys_route( /// Uploads end-to-end key signatures from the sender user. pub(crate) async fn upload_signatures_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); for (user_id, keys) in &body.signed_keys { @@ -213,10 +213,10 @@ pub(crate) async fn upload_signatures_route( } } - Ok(upload_signatures::v3::Response { + Ok(RumaResponse(upload_signatures::v3::Response { // TODO: integrate failures: BTreeMap::new(), - }) + })) } /// # `POST /_matrix/client/r0/keys/changes` @@ -227,7 +227,7 @@ pub(crate) async fn upload_signatures_route( /// - TODO: left users pub(crate) async fn get_key_changes_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let mut device_list_updates = HashSet::new(); @@ -277,11 +277,11 @@ pub(crate) async fn get_key_changes_route( .filter_map(Result::ok), ); } - Ok(get_key_changes::v3::Response { + Ok(RumaResponse(get_key_changes::v3::Response { changed: device_list_updates.into_iter().collect(), // TODO left: Vec::new(), - }) + })) } #[allow(clippy::too_many_lines)] diff --git a/src/api/client_server/media.rs b/src/api/client_server/media.rs index e4b90ed3..16b72429 100644 --- a/src/api/client_server/media.rs +++ b/src/api/client_server/media.rs @@ -8,7 +8,10 @@ use ruma::api::client::{ }, }; -use crate::{service::media::FileMeta, services, utils, Error, Result, Ruma}; +use crate::{ + service::media::FileMeta, services, utils, Error, Result, Ruma, + RumaResponse, +}; const MXC_LENGTH: usize = 32; @@ -17,10 +20,10 @@ const MXC_LENGTH: usize = 32; /// Returns max upload size. pub(crate) async fn get_media_config_route( _body: Ruma, -) -> Result { - Ok(get_media_config::v3::Response { +) -> Result> { + Ok(RumaResponse(get_media_config::v3::Response { upload_size: services().globals.max_request_size().into(), - }) + })) } /// # `POST /_matrix/media/r0/upload` @@ -31,7 +34,7 @@ pub(crate) async fn get_media_config_route( /// - Media will be saved in the media/ directory pub(crate) async fn create_content_route( body: Ruma, -) -> Result { +) -> Result> { let mxc = format!( "mxc://{}/{}", services().globals.server_name(), @@ -51,10 +54,10 @@ pub(crate) async fn create_content_route( ) .await?; - Ok(create_content::v3::Response { + Ok(RumaResponse(create_content::v3::Response { content_uri: mxc.into(), blurhash: None, - }) + })) } pub(crate) async fn get_remote_content( @@ -96,7 +99,7 @@ pub(crate) async fn get_remote_content( /// - Only allows federation if `allow_remote` is true pub(crate) async fn get_content_route( body: Ruma, -) -> Result { +) -> Result> { let mxc = format!("mxc://{}/{}", body.server_name, body.media_id); if let Some(FileMeta { @@ -105,19 +108,19 @@ pub(crate) async fn get_content_route( file, }) = services().media.get(mxc.clone()).await? { - Ok(get_content::v3::Response { + Ok(RumaResponse(get_content::v3::Response { file, content_type, content_disposition, cross_origin_resource_policy: Some("cross-origin".to_owned()), - }) + })) } else if &*body.server_name != services().globals.server_name() && body.allow_remote { let remote_content_response = get_remote_content(&mxc, &body.server_name, body.media_id.clone()) .await?; - Ok(remote_content_response) + Ok(RumaResponse(remote_content_response)) } else { Err(Error::BadRequest(ErrorKind::NotFound, "Media not found.")) } @@ -130,7 +133,7 @@ pub(crate) async fn get_content_route( /// - Only allows federation if `allow_remote` is true pub(crate) async fn get_content_as_filename_route( body: Ruma, -) -> Result { +) -> Result> { let mxc = format!("mxc://{}/{}", body.server_name, body.media_id); if let Some(FileMeta { @@ -139,7 +142,7 @@ pub(crate) async fn get_content_as_filename_route( .. }) = services().media.get(mxc.clone()).await? { - Ok(get_content_as_filename::v3::Response { + Ok(RumaResponse(get_content_as_filename::v3::Response { file, content_type, content_disposition: Some(format!( @@ -147,7 +150,7 @@ pub(crate) async fn get_content_as_filename_route( body.filename )), cross_origin_resource_policy: Some("cross-origin".to_owned()), - }) + })) } else if &*body.server_name != services().globals.server_name() && body.allow_remote { @@ -155,7 +158,7 @@ pub(crate) async fn get_content_as_filename_route( get_remote_content(&mxc, &body.server_name, body.media_id.clone()) .await?; - Ok(get_content_as_filename::v3::Response { + Ok(RumaResponse(get_content_as_filename::v3::Response { content_disposition: Some(format!( "inline: filename={}", body.filename @@ -163,7 +166,7 @@ pub(crate) async fn get_content_as_filename_route( content_type: remote_content_response.content_type, file: remote_content_response.file, cross_origin_resource_policy: Some("cross-origin".to_owned()), - }) + })) } else { Err(Error::BadRequest(ErrorKind::NotFound, "Media not found.")) } @@ -176,7 +179,7 @@ pub(crate) async fn get_content_as_filename_route( /// - Only allows federation if `allow_remote` is true pub(crate) async fn get_content_thumbnail_route( body: Ruma, -) -> Result { +) -> Result> { let mxc = format!("mxc://{}/{}", body.server_name, body.media_id); if let Some(FileMeta { @@ -196,11 +199,11 @@ pub(crate) async fn get_content_thumbnail_route( ) .await? { - Ok(get_content_thumbnail::v3::Response { + Ok(RumaResponse(get_content_thumbnail::v3::Response { file, content_type, cross_origin_resource_policy: Some("cross-origin".to_owned()), - }) + })) } else if &*body.server_name != services().globals.server_name() && body.allow_remote { @@ -233,7 +236,7 @@ pub(crate) async fn get_content_thumbnail_route( ) .await?; - Ok(get_thumbnail_response) + Ok(RumaResponse(get_thumbnail_response)) } else { Err(Error::BadRequest(ErrorKind::NotFound, "Media not found.")) } diff --git a/src/api/client_server/membership.rs b/src/api/client_server/membership.rs index fa044f27..d99b1d5c 100644 --- a/src/api/client_server/membership.rs +++ b/src/api/client_server/membership.rs @@ -36,7 +36,7 @@ use tracing::{debug, error, info, warn}; use super::get_alias_helper; use crate::{ service::pdu::{gen_event_id_canonical_json, PduBuilder}, - services, utils, Error, PduEvent, Result, Ruma, + services, utils, Error, PduEvent, Result, Ruma, RumaResponse, }; /// # `POST /_matrix/client/r0/rooms/{roomId}/join` @@ -49,7 +49,7 @@ use crate::{ /// federation pub(crate) async fn join_room_by_id_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); // There is no body.server_name for /roomId/join @@ -83,6 +83,7 @@ pub(crate) async fn join_room_by_id_route( body.third_party_signed.as_ref(), ) .await + .map(RumaResponse) } /// # `POST /_matrix/client/r0/join/{roomIdOrAlias}` @@ -95,7 +96,7 @@ pub(crate) async fn join_room_by_id_route( /// federation pub(crate) async fn join_room_by_id_or_alias_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_deref().expect("user is authenticated"); let body = body.body; @@ -147,9 +148,9 @@ pub(crate) async fn join_room_by_id_or_alias_route( ) .await?; - Ok(join_room_by_id_or_alias::v3::Response { + Ok(RumaResponse(join_room_by_id_or_alias::v3::Response { room_id: join_room_response.room_id, - }) + })) } /// # `POST /_matrix/client/r0/rooms/{roomId}/leave` @@ -159,12 +160,12 @@ pub(crate) async fn join_room_by_id_or_alias_route( /// - This should always work if the user is currently joined. pub(crate) async fn leave_room_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); leave_room(sender_user, &body.room_id, body.reason.clone()).await?; - Ok(leave_room::v3::Response::new()) + Ok(RumaResponse(leave_room::v3::Response::new())) } /// # `POST /_matrix/client/r0/rooms/{roomId}/invite` @@ -172,7 +173,7 @@ pub(crate) async fn leave_room_route( /// Tries to send an invite event into the room. pub(crate) async fn invite_user_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if let invite_user::v3::InvitationRecipient::UserId { @@ -187,7 +188,7 @@ pub(crate) async fn invite_user_route( false, ) .await?; - Ok(invite_user::v3::Response {}) + Ok(RumaResponse(invite_user::v3::Response {})) } else { Err(Error::BadRequest(ErrorKind::NotFound, "User not found.")) } @@ -198,13 +199,13 @@ pub(crate) async fn invite_user_route( /// Tries to send a kick event into the room. pub(crate) async fn kick_user_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if let Ok(true) = services().rooms.state_cache.is_left(sender_user, &body.room_id) { - return Ok(kick_user::v3::Response {}); + return Ok(RumaResponse(kick_user::v3::Response {})); } let mut event: RoomMemberEventContent = serde_json::from_str( @@ -259,7 +260,7 @@ pub(crate) async fn kick_user_route( drop(state_lock); - Ok(kick_user::v3::Response::new()) + Ok(RumaResponse(kick_user::v3::Response::new())) } /// # `POST /_matrix/client/r0/rooms/{roomId}/ban` @@ -267,14 +268,14 @@ pub(crate) async fn kick_user_route( /// Tries to send a ban event into the room. pub(crate) async fn ban_user_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if let Ok(Some(membership_event)) = services().rooms.state_accessor.get_member(&body.room_id, sender_user) { if membership_event.membership == MembershipState::Ban { - return Ok(ban_user::v3::Response {}); + return Ok(RumaResponse(ban_user::v3::Response {})); } } @@ -343,7 +344,7 @@ pub(crate) async fn ban_user_route( drop(state_lock); - Ok(ban_user::v3::Response::new()) + Ok(RumaResponse(ban_user::v3::Response::new())) } /// # `POST /_matrix/client/r0/rooms/{roomId}/unban` @@ -351,14 +352,14 @@ pub(crate) async fn ban_user_route( /// Tries to send an unban event into the room. pub(crate) async fn unban_user_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if let Ok(Some(membership_event)) = services().rooms.state_accessor.get_member(&body.room_id, sender_user) { if membership_event.membership != MembershipState::Ban { - return Ok(unban_user::v3::Response {}); + return Ok(RumaResponse(unban_user::v3::Response {})); } } @@ -414,7 +415,7 @@ pub(crate) async fn unban_user_route( drop(state_lock); - Ok(unban_user::v3::Response::new()) + Ok(RumaResponse(unban_user::v3::Response::new())) } /// # `POST /_matrix/client/r0/rooms/{roomId}/forget` @@ -428,12 +429,12 @@ pub(crate) async fn unban_user_route( /// forgotten, so this has to be called from every device pub(crate) async fn forget_room_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); services().rooms.state_cache.forget(&body.room_id, sender_user)?; - Ok(forget_room::v3::Response::new()) + Ok(RumaResponse(forget_room::v3::Response::new())) } /// # `POST /_matrix/client/r0/joined_rooms` @@ -441,17 +442,17 @@ pub(crate) async fn forget_room_route( /// Lists all rooms the user has joined. pub(crate) async fn joined_rooms_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); - Ok(joined_rooms::v3::Response { + Ok(RumaResponse(joined_rooms::v3::Response { joined_rooms: services() .rooms .state_cache .rooms_joined(sender_user) .filter_map(Result::ok) .collect(), - }) + })) } /// # `POST /_matrix/client/r0/rooms/{roomId}/members` @@ -462,7 +463,7 @@ pub(crate) async fn joined_rooms_route( /// - Only works if the user is currently joined pub(crate) async fn get_member_events_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if !services() @@ -476,7 +477,7 @@ pub(crate) async fn get_member_events_route( )); } - Ok(get_member_events::v3::Response { + Ok(RumaResponse(get_member_events::v3::Response { chunk: services() .rooms .state_accessor @@ -486,7 +487,7 @@ pub(crate) async fn get_member_events_route( .filter(|(key, _)| key.0 == StateEventType::RoomMember) .map(|(_, pdu)| pdu.to_member_event()) .collect(), - }) + })) } /// # `POST /_matrix/client/r0/rooms/{roomId}/joined_members` @@ -497,7 +498,7 @@ pub(crate) async fn get_member_events_route( /// - TODO: An appservice just needs a puppet joined pub(crate) async fn joined_members_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if !services() @@ -530,9 +531,9 @@ pub(crate) async fn joined_members_route( ); } - Ok(joined_members::v3::Response { + Ok(RumaResponse(joined_members::v3::Response { joined, - }) + })) } #[allow(clippy::too_many_lines)] diff --git a/src/api/client_server/message.rs b/src/api/client_server/message.rs index 202b6c05..ef7b8947 100644 --- a/src/api/client_server/message.rs +++ b/src/api/client_server/message.rs @@ -14,7 +14,7 @@ use ruma::{ use crate::{ service::{pdu::PduBuilder, rooms::timeline::PduCount}, - services, utils, Error, Result, Ruma, + services, utils, Error, Result, Ruma, RumaResponse, }; /// # `PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId}` @@ -28,7 +28,7 @@ use crate::{ /// allowed pub(crate) async fn send_message_event_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_deref(); @@ -77,9 +77,9 @@ pub(crate) async fn send_message_event_route( .map_err(|_| { Error::bad_database("Invalid event id in txnid data.") })?; - return Ok(send_message_event::v3::Response { + return Ok(RumaResponse(send_message_event::v3::Response { event_id, - }); + })); } let mut unsigned = BTreeMap::new(); @@ -118,7 +118,9 @@ pub(crate) async fn send_message_event_route( drop(state_lock); - Ok(send_message_event::v3::Response::new((*event_id).to_owned())) + Ok(RumaResponse(send_message_event::v3::Response::new( + (*event_id).to_owned(), + ))) } /// # `GET /_matrix/client/r0/rooms/{roomId}/messages` @@ -131,7 +133,7 @@ pub(crate) async fn send_message_event_route( #[allow(clippy::too_many_lines)] pub(crate) async fn get_message_events_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated"); @@ -308,5 +310,5 @@ pub(crate) async fn get_message_events_route( } */ - Ok(resp) + Ok(RumaResponse(resp)) } diff --git a/src/api/client_server/profile.rs b/src/api/client_server/profile.rs index 4bcfc5af..f05f5b14 100644 --- a/src/api/client_server/profile.rs +++ b/src/api/client_server/profile.rs @@ -18,7 +18,9 @@ use ruma::{ use serde_json::value::to_raw_value; use tracing::warn; -use crate::{service::pdu::PduBuilder, services, Error, Result, Ruma}; +use crate::{ + service::pdu::PduBuilder, services, Error, Result, Ruma, RumaResponse, +}; /// # `PUT /_matrix/client/r0/profile/{userId}/displayname` /// @@ -27,7 +29,7 @@ use crate::{service::pdu::PduBuilder, services, Error, Result, Ruma}; /// - Also makes sure other users receive the update using presence EDUs pub(crate) async fn set_displayname_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); services().users.set_displayname(sender_user, body.displayname.clone())?; @@ -107,7 +109,7 @@ pub(crate) async fn set_displayname_route( } } - Ok(set_display_name::v3::Response {}) + Ok(RumaResponse(set_display_name::v3::Response {})) } /// # `GET /_matrix/client/r0/profile/{userId}/displayname` @@ -117,7 +119,7 @@ pub(crate) async fn set_displayname_route( /// - If user is on another server: Fetches displayname over federation pub(crate) async fn get_displayname_route( body: Ruma, -) -> Result { +) -> Result> { if body.user_id.server_name() != services().globals.server_name() { let response = services() .sending @@ -130,14 +132,14 @@ pub(crate) async fn get_displayname_route( ) .await?; - return Ok(get_display_name::v3::Response { + return Ok(RumaResponse(get_display_name::v3::Response { displayname: response.displayname, - }); + })); } - Ok(get_display_name::v3::Response { + Ok(RumaResponse(get_display_name::v3::Response { displayname: services().users.displayname(&body.user_id)?, - }) + })) } /// # `PUT /_matrix/client/r0/profile/{userId}/avatar_url` @@ -147,7 +149,7 @@ pub(crate) async fn get_displayname_route( /// - Also makes sure other users receive the update using presence EDUs pub(crate) async fn set_avatar_url_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); services().users.set_avatar_url(sender_user, body.avatar_url.clone())?; @@ -229,7 +231,7 @@ pub(crate) async fn set_avatar_url_route( }; } - Ok(set_avatar_url::v3::Response {}) + Ok(RumaResponse(set_avatar_url::v3::Response {})) } /// # `GET /_matrix/client/r0/profile/{userId}/avatar_url` @@ -240,7 +242,7 @@ pub(crate) async fn set_avatar_url_route( /// federation pub(crate) async fn get_avatar_url_route( body: Ruma, -) -> Result { +) -> Result> { if body.user_id.server_name() != services().globals.server_name() { let response = services() .sending @@ -253,16 +255,16 @@ pub(crate) async fn get_avatar_url_route( ) .await?; - return Ok(get_avatar_url::v3::Response { + return Ok(RumaResponse(get_avatar_url::v3::Response { avatar_url: response.avatar_url, blurhash: response.blurhash, - }); + })); } - Ok(get_avatar_url::v3::Response { + Ok(RumaResponse(get_avatar_url::v3::Response { avatar_url: services().users.avatar_url(&body.user_id)?, blurhash: services().users.blurhash(&body.user_id)?, - }) + })) } /// # `GET /_matrix/client/r0/profile/{userId}` @@ -272,7 +274,7 @@ pub(crate) async fn get_avatar_url_route( /// - If user is on another server: Fetches profile over federation pub(crate) async fn get_profile_route( body: Ruma, -) -> Result { +) -> Result> { if body.user_id.server_name() != services().globals.server_name() { let response = services() .sending @@ -285,11 +287,11 @@ pub(crate) async fn get_profile_route( ) .await?; - return Ok(get_profile::v3::Response { + return Ok(RumaResponse(get_profile::v3::Response { displayname: response.displayname, avatar_url: response.avatar_url, blurhash: response.blurhash, - }); + })); } if !services().users.exists(&body.user_id)? { @@ -300,9 +302,9 @@ pub(crate) async fn get_profile_route( )); } - Ok(get_profile::v3::Response { + Ok(RumaResponse(get_profile::v3::Response { avatar_url: services().users.avatar_url(&body.user_id)?, blurhash: services().users.blurhash(&body.user_id)?, displayname: services().users.displayname(&body.user_id)?, - }) + })) } diff --git a/src/api/client_server/push.rs b/src/api/client_server/push.rs index 9e101707..e000a3c5 100644 --- a/src/api/client_server/push.rs +++ b/src/api/client_server/push.rs @@ -11,14 +11,14 @@ use ruma::{ push::{AnyPushRuleRef, InsertPushRuleError, RemovePushRuleError}, }; -use crate::{services, Error, Result, Ruma}; +use crate::{services, Error, Result, Ruma, RumaResponse}; /// # `GET /_matrix/client/r0/pushrules` /// /// Retrieves the push rules event for this user. pub(crate) async fn get_pushrules_all_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let event = services() @@ -37,9 +37,9 @@ pub(crate) async fn get_pushrules_all_route( .map_err(|_| Error::bad_database("Invalid account data event in db."))? .content; - Ok(get_pushrules_all::v3::Response { + Ok(RumaResponse(get_pushrules_all::v3::Response { global: account_data.global, - }) + })) } /// # `GET /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}` @@ -47,7 +47,7 @@ pub(crate) async fn get_pushrules_all_route( /// Retrieves a single specified push rule for this user. pub(crate) async fn get_pushrule_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let event = services() @@ -72,9 +72,9 @@ pub(crate) async fn get_pushrule_route( .map(Into::into); if let Some(rule) = rule { - Ok(get_pushrule::v3::Response { + Ok(RumaResponse(get_pushrule::v3::Response { rule, - }) + })) } else { Err(Error::BadRequest(ErrorKind::NotFound, "Push rule not found.")) } @@ -85,7 +85,7 @@ pub(crate) async fn get_pushrule_route( /// Creates a single specified push rule for this user. pub(crate) async fn set_pushrule_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let body = body.body; @@ -157,7 +157,7 @@ pub(crate) async fn set_pushrule_route( .expect("to json value always works"), )?; - Ok(set_pushrule::v3::Response {}) + Ok(RumaResponse(set_pushrule::v3::Response {})) } /// # `GET /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}/actions` @@ -165,7 +165,7 @@ pub(crate) async fn set_pushrule_route( /// Gets the actions of a single specified push rule for this user. pub(crate) async fn get_pushrule_actions_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if body.scope != RuleScope::Global { @@ -200,9 +200,9 @@ pub(crate) async fn get_pushrule_actions_route( "Push rule not found.", ))?; - Ok(get_pushrule_actions::v3::Response { + Ok(RumaResponse(get_pushrule_actions::v3::Response { actions, - }) + })) } /// # `PUT /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}/actions` @@ -210,7 +210,7 @@ pub(crate) async fn get_pushrule_actions_route( /// Sets the actions of a single specified push rule for this user. pub(crate) async fn set_pushrule_actions_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if body.scope != RuleScope::Global { @@ -257,7 +257,7 @@ pub(crate) async fn set_pushrule_actions_route( .expect("to json value always works"), )?; - Ok(set_pushrule_actions::v3::Response {}) + Ok(RumaResponse(set_pushrule_actions::v3::Response {})) } /// # `GET /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}/enabled` @@ -265,7 +265,7 @@ pub(crate) async fn set_pushrule_actions_route( /// Gets the enabled status of a single specified push rule for this user. pub(crate) async fn get_pushrule_enabled_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if body.scope != RuleScope::Global { @@ -301,9 +301,9 @@ pub(crate) async fn get_pushrule_enabled_route( "Push rule not found.", ))?; - Ok(get_pushrule_enabled::v3::Response { + Ok(RumaResponse(get_pushrule_enabled::v3::Response { enabled, - }) + })) } /// # `PUT /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}/enabled` @@ -311,7 +311,7 @@ pub(crate) async fn get_pushrule_enabled_route( /// Sets the enabled status of a single specified push rule for this user. pub(crate) async fn set_pushrule_enabled_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if body.scope != RuleScope::Global { @@ -358,7 +358,7 @@ pub(crate) async fn set_pushrule_enabled_route( .expect("to json value always works"), )?; - Ok(set_pushrule_enabled::v3::Response {}) + Ok(RumaResponse(set_pushrule_enabled::v3::Response {})) } /// # `DELETE /_matrix/client/r0/pushrules/{scope}/{kind}/{ruleId}` @@ -366,7 +366,7 @@ pub(crate) async fn set_pushrule_enabled_route( /// Deletes a single specified push rule for this user. pub(crate) async fn delete_pushrule_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if body.scope != RuleScope::Global { @@ -418,7 +418,7 @@ pub(crate) async fn delete_pushrule_route( .expect("to json value always works"), )?; - Ok(delete_pushrule::v3::Response {}) + Ok(RumaResponse(delete_pushrule::v3::Response {})) } /// # `GET /_matrix/client/r0/pushers` @@ -426,12 +426,12 @@ pub(crate) async fn delete_pushrule_route( /// Gets all currently active pushers for the sender user. pub(crate) async fn get_pushers_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); - Ok(get_pushers::v3::Response { + Ok(RumaResponse(get_pushers::v3::Response { pushers: services().pusher.get_pushers(sender_user)?, - }) + })) } /// # `POST /_matrix/client/r0/pushers/set` @@ -441,10 +441,10 @@ pub(crate) async fn get_pushers_route( /// - TODO: Handle `append` pub(crate) async fn set_pushers_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); services().pusher.set_pusher(sender_user, body.action.clone())?; - Ok(set_pusher::v3::Response::default()) + Ok(RumaResponse(set_pusher::v3::Response::default())) } diff --git a/src/api/client_server/read_marker.rs b/src/api/client_server/read_marker.rs index 5fdfc7de..9d0643e8 100644 --- a/src/api/client_server/read_marker.rs +++ b/src/api/client_server/read_marker.rs @@ -13,6 +13,7 @@ use ruma::{ use crate::{ service::rooms::timeline::PduCount, services, Error, Result, Ruma, + RumaResponse, }; /// # `POST /_matrix/client/r0/rooms/{roomId}/read_markers` @@ -24,7 +25,7 @@ use crate::{ /// EDU pub(crate) async fn set_read_marker_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if let Some(fully_read) = &body.fully_read { @@ -97,7 +98,7 @@ pub(crate) async fn set_read_marker_route( )?; } - Ok(set_read_marker::v3::Response {}) + Ok(RumaResponse(set_read_marker::v3::Response {})) } /// # `POST /_matrix/client/r0/rooms/{roomId}/receipt/{receiptType}/{eventId}` @@ -105,7 +106,7 @@ pub(crate) async fn set_read_marker_route( /// Sets private read marker and public read receipt EDU. pub(crate) async fn create_receipt_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if matches!( @@ -187,5 +188,5 @@ pub(crate) async fn create_receipt_route( _ => return Err(Error::bad_database("Unsupported receipt type")), } - Ok(create_receipt::v3::Response {}) + Ok(RumaResponse(create_receipt::v3::Response {})) } diff --git a/src/api/client_server/redact.rs b/src/api/client_server/redact.rs index 65683d81..0114aa29 100644 --- a/src/api/client_server/redact.rs +++ b/src/api/client_server/redact.rs @@ -6,7 +6,7 @@ use ruma::{ }; use serde_json::value::to_raw_value; -use crate::{service::pdu::PduBuilder, services, Result, Ruma}; +use crate::{service::pdu::PduBuilder, services, Result, Ruma, RumaResponse}; /// # `PUT /_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId}` /// @@ -15,7 +15,7 @@ use crate::{service::pdu::PduBuilder, services, Result, Ruma}; /// - TODO: Handle txn id pub(crate) async fn redact_event_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let body = body.body; @@ -54,7 +54,7 @@ pub(crate) async fn redact_event_route( drop(state_lock); let event_id = (*event_id).to_owned(); - Ok(redact_event::v3::Response { + Ok(RumaResponse(redact_event::v3::Response { event_id, - }) + })) } diff --git a/src/api/client_server/relations.rs b/src/api/client_server/relations.rs index 475adb9a..d2068bc2 100644 --- a/src/api/client_server/relations.rs +++ b/src/api/client_server/relations.rs @@ -6,12 +6,18 @@ use ruma::{ uint, }; -use crate::{service::rooms::timeline::PduCount, services, Result, Ruma}; +use crate::{ + service::rooms::timeline::PduCount, services, Result, Ruma, RumaResponse, +}; /// # `GET /_matrix/client/r0/rooms/{roomId}/relations/{eventId}/{relType}/{eventType}` pub(crate) async fn get_relating_events_with_rel_type_and_event_type_route( body: Ruma, -) -> Result { +) -> Result< + RumaResponse< + get_relating_events_with_rel_type_and_event_type::v1::Response, + >, +> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let from = match body.from.clone() { @@ -44,17 +50,19 @@ pub(crate) async fn get_relating_events_with_rel_type_and_event_type_route( limit, )?; - Ok(get_relating_events_with_rel_type_and_event_type::v1::Response { - chunk: res.chunk, - next_batch: res.next_batch, - prev_batch: res.prev_batch, - }) + Ok(RumaResponse( + get_relating_events_with_rel_type_and_event_type::v1::Response { + chunk: res.chunk, + next_batch: res.next_batch, + prev_batch: res.prev_batch, + }, + )) } /// # `GET /_matrix/client/r0/rooms/{roomId}/relations/{eventId}/{relType}` pub(crate) async fn get_relating_events_with_rel_type_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let from = match body.from.clone() { @@ -87,17 +95,17 @@ pub(crate) async fn get_relating_events_with_rel_type_route( limit, )?; - Ok(get_relating_events_with_rel_type::v1::Response { + Ok(RumaResponse(get_relating_events_with_rel_type::v1::Response { chunk: res.chunk, next_batch: res.next_batch, prev_batch: res.prev_batch, - }) + })) } /// # `GET /_matrix/client/r0/rooms/{roomId}/relations/{eventId}` pub(crate) async fn get_relating_events_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let from = match body.from.clone() { @@ -119,14 +127,18 @@ pub(crate) async fn get_relating_events_route( .try_into() .expect("0-100 should fit in usize"); - services().rooms.pdu_metadata.paginate_relations_with_filter( - sender_user, - &body.room_id, - &body.event_id, - None, - None, - from, - to, - limit, - ) + services() + .rooms + .pdu_metadata + .paginate_relations_with_filter( + sender_user, + &body.room_id, + &body.event_id, + None, + None, + from, + to, + limit, + ) + .map(RumaResponse) } diff --git a/src/api/client_server/report.rs b/src/api/client_server/report.rs index d9242f62..9dfd1a31 100644 --- a/src/api/client_server/report.rs +++ b/src/api/client_server/report.rs @@ -4,14 +4,14 @@ use ruma::{ int, }; -use crate::{services, Error, Result, Ruma}; +use crate::{services, Error, Result, Ruma, RumaResponse}; /// # `POST /_matrix/client/r0/rooms/{roomId}/report/{eventId}` /// /// Reports an inappropriate event to homeserver admins pub(crate) async fn report_event_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let Some(pdu) = services().rooms.timeline.get_pdu(&body.event_id)? else { @@ -91,5 +91,5 @@ pub(crate) async fn report_event_route( ), )); - Ok(report_content::v3::Response {}) + Ok(RumaResponse(report_content::v3::Response {})) } diff --git a/src/api/client_server/room.rs b/src/api/client_server/room.rs index b0a17e6a..f177bebf 100644 --- a/src/api/client_server/room.rs +++ b/src/api/client_server/room.rs @@ -31,7 +31,7 @@ use tracing::{info, warn}; use crate::{ api::client_server::invite_helper, service::pdu::PduBuilder, services, - Error, Result, Ruma, + Error, Result, Ruma, RumaResponse, }; /// # `POST /_matrix/client/r0/createRoom` @@ -53,7 +53,7 @@ use crate::{ #[allow(clippy::too_many_lines)] pub(crate) async fn create_room_route( body: Ruma, -) -> Result { +) -> Result> { use create_room::v3::RoomPreset; let sender_user = body.sender_user.as_ref().expect("user is authenticated"); @@ -538,7 +538,7 @@ pub(crate) async fn create_room_route( info!("{} created a room", sender_user); - Ok(create_room::v3::Response::new(room_id)) + Ok(RumaResponse(create_room::v3::Response::new(room_id))) } /// # `GET /_matrix/client/r0/rooms/{roomId}/event/{eventId}` @@ -549,7 +549,7 @@ pub(crate) async fn create_room_route( /// visibility) pub(crate) async fn get_room_event_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let event = services().rooms.timeline.get_pdu(&body.event_id)?.ok_or_else( @@ -573,9 +573,9 @@ pub(crate) async fn get_room_event_route( let mut event = (*event).clone(); event.add_age()?; - Ok(get_room_event::v3::Response { + Ok(RumaResponse(get_room_event::v3::Response { event: event.to_room_event(), - }) + })) } /// # `GET /_matrix/client/r0/rooms/{roomId}/aliases` @@ -586,7 +586,7 @@ pub(crate) async fn get_room_event_route( /// user to call it if `history_visibility` is world readable pub(crate) async fn get_room_aliases_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if !services().rooms.state_cache.is_joined(sender_user, &body.room_id)? { @@ -596,14 +596,14 @@ pub(crate) async fn get_room_aliases_route( )); } - Ok(aliases::v3::Response { + Ok(RumaResponse(aliases::v3::Response { aliases: services() .rooms .alias .local_aliases_for_room(&body.room_id) .filter_map(Result::ok) .collect(), - }) + })) } /// # `POST /_matrix/client/r0/rooms/{roomId}/upgrade` @@ -619,7 +619,7 @@ pub(crate) async fn get_room_aliases_route( #[allow(clippy::too_many_lines)] pub(crate) async fn upgrade_room_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if !services().globals.supported_room_versions().contains(&body.new_version) @@ -914,7 +914,7 @@ pub(crate) async fn upgrade_room_route( drop(state_lock); // Return the replacement room id - Ok(upgrade_room::v3::Response { + Ok(RumaResponse(upgrade_room::v3::Response { replacement_room, - }) + })) } diff --git a/src/api/client_server/search.rs b/src/api/client_server/search.rs index 340eb839..afe44597 100644 --- a/src/api/client_server/search.rs +++ b/src/api/client_server/search.rs @@ -14,7 +14,7 @@ use ruma::{ uint, }; -use crate::{services, Error, Result, Ruma}; +use crate::{services, Error, Result, Ruma, RumaResponse}; /// # `POST /_matrix/client/r0/search` /// @@ -25,7 +25,7 @@ use crate::{services, Error, Result, Ruma}; #[allow(clippy::too_many_lines)] pub(crate) async fn search_events_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let search_criteria = body.search_categories.room_events.as_ref().unwrap(); @@ -136,7 +136,7 @@ pub(crate) async fn search_events_route( Some((skip + limit).to_string()) }; - Ok(search_events::v3::Response::new(ResultCategories { + Ok(RumaResponse(search_events::v3::Response::new(ResultCategories { room_events: ResultRoomEvents { count: None, // TODO @@ -151,5 +151,5 @@ pub(crate) async fn search_events_route( .map(str::to_lowercase) .collect(), }, - })) + }))) } diff --git a/src/api/client_server/session.rs b/src/api/client_server/session.rs index 3894a05f..13c86896 100644 --- a/src/api/client_server/session.rs +++ b/src/api/client_server/session.rs @@ -16,7 +16,7 @@ use serde::Deserialize; use tracing::{info, warn}; use super::{DEVICE_ID_LENGTH, TOKEN_LENGTH}; -use crate::{services, utils, Error, Result, Ruma}; +use crate::{services, utils, Error, Result, Ruma, RumaResponse}; #[derive(Debug, Deserialize)] struct Claims { @@ -29,13 +29,13 @@ struct Claims { /// the `type` field when logging in. pub(crate) async fn get_login_types_route( _body: Ruma, -) -> Result { - Ok(get_login_types::v3::Response::new(vec![ +) -> Result> { + Ok(RumaResponse(get_login_types::v3::Response::new(vec![ get_login_types::v3::LoginType::Password(PasswordLoginType::default()), get_login_types::v3::LoginType::ApplicationService( ApplicationServiceLoginType::default(), ), - ])) + ]))) } /// # `POST /_matrix/client/r0/login` @@ -54,7 +54,7 @@ pub(crate) async fn get_login_types_route( #[allow(clippy::too_many_lines)] pub(crate) async fn login_route( body: Ruma, -) -> Result { +) -> Result> { // To allow deprecated login methods #![allow(deprecated)] // Validate login method @@ -256,7 +256,7 @@ pub(crate) async fn login_route( // Homeservers are still required to send the `home_server` field #[allow(deprecated)] - Ok(login::v3::Response { + Ok(RumaResponse(login::v3::Response { user_id, access_token: token, home_server: Some(services().globals.server_name().to_owned()), @@ -264,7 +264,7 @@ pub(crate) async fn login_route( well_known: None, refresh_token: None, expires_in: None, - }) + })) } /// # `POST /_matrix/client/r0/logout` @@ -278,7 +278,7 @@ pub(crate) async fn login_route( /// - Triggers device list updates pub(crate) async fn logout_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated"); @@ -294,7 +294,7 @@ pub(crate) async fn logout_route( services().users.remove_device(sender_user, sender_device)?; - Ok(logout::v3::Response::new()) + Ok(RumaResponse(logout::v3::Response::new())) } /// # `POST /_matrix/client/r0/logout/all` @@ -311,7 +311,7 @@ pub(crate) async fn logout_route( /// /_matrix/client/r0/logout`](logout_route) from each device of this user. pub(crate) async fn logout_all_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if let Some(info) = &body.appservice_info { @@ -332,5 +332,5 @@ pub(crate) async fn logout_all_route( services().users.remove_device(sender_user, &device_id)?; } - Ok(logout_all::v3::Response::new()) + Ok(RumaResponse(logout_all::v3::Response::new())) } diff --git a/src/api/client_server/space.rs b/src/api/client_server/space.rs index a9b1af35..77396ed6 100644 --- a/src/api/client_server/space.rs +++ b/src/api/client_server/space.rs @@ -1,6 +1,6 @@ use ruma::{api::client::space::get_hierarchy, uint}; -use crate::{services, Result, Ruma}; +use crate::{services, Result, Ruma, RumaResponse}; /// # `GET /_matrix/client/v1/rooms/{room_id}/hierarchy` /// @@ -8,7 +8,7 @@ use crate::{services, Result, Ruma}; /// of a given space. pub(crate) async fn get_hierarchy_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let skip = @@ -40,4 +40,5 @@ pub(crate) async fn get_hierarchy_route( body.suggested_only, ) .await + .map(RumaResponse) } diff --git a/src/api/client_server/state.rs b/src/api/client_server/state.rs index ace26880..31b770d5 100644 --- a/src/api/client_server/state.rs +++ b/src/api/client_server/state.rs @@ -28,7 +28,7 @@ use crate::{ /// - If event is new `canonical_alias`: Rejects if alias is incorrect pub(crate) async fn send_state_event_for_key_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let event_id = send_state_event_for_key_helper( @@ -42,9 +42,9 @@ pub(crate) async fn send_state_event_for_key_route( .await?; let event_id = (*event_id).to_owned(); - Ok(send_state_event::v3::Response { + Ok(RumaResponse(send_state_event::v3::Response { event_id, - }) + })) } /// # `PUT /_matrix/client/r0/rooms/{roomId}/state/{eventType}` @@ -94,7 +94,7 @@ pub(crate) async fn send_state_event_for_empty_key_route( /// readable pub(crate) async fn get_state_events_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if !services() @@ -108,7 +108,7 @@ pub(crate) async fn get_state_events_route( )); } - Ok(get_state_events::v3::Response { + Ok(RumaResponse(get_state_events::v3::Response { room_state: services() .rooms .state_accessor @@ -117,7 +117,7 @@ pub(crate) async fn get_state_events_route( .values() .map(|pdu| pdu.to_state_event()) .collect(), - }) + })) } /// # `GET /_matrix/client/r0/rooms/{roomid}/state/{eventType}/{stateKey}` @@ -128,7 +128,7 @@ pub(crate) async fn get_state_events_route( /// readable pub(crate) async fn get_state_events_for_key_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); if !services() @@ -154,11 +154,11 @@ pub(crate) async fn get_state_events_for_key_route( Error::BadRequest(ErrorKind::NotFound, "State event not found.") })?; - Ok(get_state_events_for_key::v3::Response { + Ok(RumaResponse(get_state_events_for_key::v3::Response { content: serde_json::from_str(event.content.get()).map_err(|_| { Error::bad_database("Invalid event content in database") })?, - }) + })) } /// # `GET /_matrix/client/r0/rooms/{roomid}/state/{eventType}` diff --git a/src/api/client_server/sync.rs b/src/api/client_server/sync.rs index bad7e304..a7176a3d 100644 --- a/src/api/client_server/sync.rs +++ b/src/api/client_server/sync.rs @@ -74,7 +74,8 @@ use crate::{ #[allow(clippy::too_many_lines)] pub(crate) async fn sync_events_route( body: Ruma, -) -> Result> { +) -> Result, RumaResponse> +{ let sender_user = body.sender_user.expect("user is authenticated"); let sender_device = body.sender_device.expect("user is authenticated"); let body = body.body; @@ -457,10 +458,10 @@ pub(crate) async fn sync_events_route( Ok(x) => x.expect("watcher should succeed"), Err(error) => debug!(%error, "timed out"), }; - Ok(response) + Ok(RumaResponse(response)) } else { // Only cache if we made progress - Ok(response) + Ok(RumaResponse(response)) } } @@ -1127,7 +1128,8 @@ fn share_encrypted_room( #[allow(clippy::too_many_lines)] pub(crate) async fn sync_events_v4_route( body: Ruma, -) -> Result> { +) -> Result, RumaResponse> +{ let sender_user = body.sender_user.expect("user is authenticated"); let sender_device = body.sender_device.expect("user is authenticated"); let mut body = body.body; @@ -1700,7 +1702,7 @@ pub(crate) async fn sync_events_v4_route( }; } - Ok(sync_events::v4::Response { + Ok(RumaResponse(sync_events::v4::Response { initial: globalsince == 0, txn_id: body.txn_id.clone(), pos: next_batch.to_string(), @@ -1763,5 +1765,5 @@ pub(crate) async fn sync_events_v4_route( }, }, delta_token: None, - }) + })) } diff --git a/src/api/client_server/tag.rs b/src/api/client_server/tag.rs index 22738af8..81ba5f9a 100644 --- a/src/api/client_server/tag.rs +++ b/src/api/client_server/tag.rs @@ -8,7 +8,7 @@ use ruma::{ }, }; -use crate::{services, Error, Result, Ruma}; +use crate::{services, Error, Result, Ruma, RumaResponse}; /// # `PUT /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags/{tag}` /// @@ -17,7 +17,7 @@ use crate::{services, Error, Result, Ruma}; /// - Inserts the tag into the tag event of the room account data. pub(crate) async fn update_tag_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let event = services().account_data.get( @@ -53,7 +53,7 @@ pub(crate) async fn update_tag_route( &serde_json::to_value(tags_event).expect("to json value always works"), )?; - Ok(create_tag::v3::Response {}) + Ok(RumaResponse(create_tag::v3::Response {})) } /// # `DELETE /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags/{tag}` @@ -63,7 +63,7 @@ pub(crate) async fn update_tag_route( /// - Removes the tag from the tag event of the room account data. pub(crate) async fn delete_tag_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let event = services().account_data.get( @@ -96,7 +96,7 @@ pub(crate) async fn delete_tag_route( &serde_json::to_value(tags_event).expect("to json value always works"), )?; - Ok(delete_tag::v3::Response {}) + Ok(RumaResponse(delete_tag::v3::Response {})) } /// # `GET /_matrix/client/r0/user/{userId}/rooms/{roomId}/tags` @@ -106,7 +106,7 @@ pub(crate) async fn delete_tag_route( /// - Gets the tag event of the room account data. pub(crate) async fn get_tags_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let event = services().account_data.get( @@ -130,7 +130,7 @@ pub(crate) async fn get_tags_route( }, )?; - Ok(get_tags::v3::Response { + Ok(RumaResponse(get_tags::v3::Response { tags: tags_event.content.tags, - }) + })) } diff --git a/src/api/client_server/thirdparty.rs b/src/api/client_server/thirdparty.rs index 8cb77e4b..f93fc1f1 100644 --- a/src/api/client_server/thirdparty.rs +++ b/src/api/client_server/thirdparty.rs @@ -2,16 +2,16 @@ use std::collections::BTreeMap; use ruma::api::client::thirdparty::get_protocols; -use crate::{Result, Ruma}; +use crate::{Result, Ruma, RumaResponse}; /// # `GET /_matrix/client/r0/thirdparty/protocols` /// /// TODO: Fetches all metadata about protocols supported by the homeserver. pub(crate) async fn get_protocols_route( _body: Ruma, -) -> Result { +) -> Result> { // TODO - Ok(get_protocols::v3::Response { + Ok(RumaResponse(get_protocols::v3::Response { protocols: BTreeMap::new(), - }) + })) } diff --git a/src/api/client_server/threads.rs b/src/api/client_server/threads.rs index 9beb8aa2..fdc1de01 100644 --- a/src/api/client_server/threads.rs +++ b/src/api/client_server/threads.rs @@ -1,11 +1,11 @@ use ruma::api::client::{error::ErrorKind, threads::get_threads}; -use crate::{services, Error, Result, Ruma}; +use crate::{services, Error, Result, Ruma, RumaResponse}; /// # `GET /_matrix/client/r0/rooms/{roomId}/threads` pub(crate) async fn get_threads_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); // Use limit or else 10, with maximum 100 @@ -36,11 +36,11 @@ pub(crate) async fn get_threads_route( let next_batch = threads.last().map(|(count, _)| count.to_string()); - Ok(get_threads::v1::Response { + Ok(RumaResponse(get_threads::v1::Response { chunk: threads .into_iter() .map(|(_, pdu)| pdu.to_room_event()) .collect(), next_batch, - }) + })) } diff --git a/src/api/client_server/to_device.rs b/src/api/client_server/to_device.rs index c29c41de..0fb36c59 100644 --- a/src/api/client_server/to_device.rs +++ b/src/api/client_server/to_device.rs @@ -8,14 +8,14 @@ use ruma::{ to_device::DeviceIdOrAllDevices, }; -use crate::{services, Error, Result, Ruma}; +use crate::{services, Error, Result, Ruma, RumaResponse}; /// # `PUT /_matrix/client/r0/sendToDevice/{eventType}/{txnId}` /// /// Send a to-device event to a set of client devices. pub(crate) async fn send_event_to_device_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_deref(); @@ -25,7 +25,7 @@ pub(crate) async fn send_event_to_device_route( .existing_txnid(sender_user, sender_device, &body.txn_id)? .is_some() { - return Ok(send_event_to_device::v3::Response {}); + return Ok(RumaResponse(send_event_to_device::v3::Response {})); } for (target_user_id, map) in &body.messages { @@ -103,5 +103,5 @@ pub(crate) async fn send_event_to_device_route( &[], )?; - Ok(send_event_to_device::v3::Response {}) + Ok(RumaResponse(send_event_to_device::v3::Response {})) } diff --git a/src/api/client_server/typing.rs b/src/api/client_server/typing.rs index 1e7b1b68..eddcfa48 100644 --- a/src/api/client_server/typing.rs +++ b/src/api/client_server/typing.rs @@ -1,13 +1,13 @@ use ruma::api::client::{error::ErrorKind, typing::create_typing_event}; -use crate::{services, utils, Error, Result, Ruma}; +use crate::{services, utils, Error, Result, Ruma, RumaResponse}; /// # `PUT /_matrix/client/r0/rooms/{roomId}/typing/{userId}` /// /// Sets the typing state of the sender user. pub(crate) async fn create_typing_event_route( body: Ruma, -) -> Result { +) -> Result> { use create_typing_event::v3::Typing; let sender_user = body.sender_user.as_ref().expect("user is authenticated"); @@ -40,5 +40,5 @@ pub(crate) async fn create_typing_event_route( .await?; } - Ok(create_typing_event::v3::Response {}) + Ok(RumaResponse(create_typing_event::v3::Response {})) } diff --git a/src/api/client_server/unversioned.rs b/src/api/client_server/unversioned.rs index 9d3dab29..0ad806a6 100644 --- a/src/api/client_server/unversioned.rs +++ b/src/api/client_server/unversioned.rs @@ -2,7 +2,7 @@ use std::{collections::BTreeMap, iter::FromIterator}; use ruma::api::client::discovery::get_supported_versions; -use crate::{Result, Ruma}; +use crate::{Result, Ruma, RumaResponse}; /// # `GET /_matrix/client/versions` /// @@ -18,7 +18,7 @@ use crate::{Result, Ruma}; /// should avoid using unstable features in their stable releases pub(crate) async fn get_supported_versions_route( _body: Ruma, -) -> Result { +) -> Result> { let resp = get_supported_versions::Response { versions: vec![ "r0.5.0".to_owned(), @@ -35,5 +35,5 @@ pub(crate) async fn get_supported_versions_route( )]), }; - Ok(resp) + Ok(RumaResponse(resp)) } diff --git a/src/api/client_server/user_directory.rs b/src/api/client_server/user_directory.rs index fdac5c1b..2667e927 100644 --- a/src/api/client_server/user_directory.rs +++ b/src/api/client_server/user_directory.rs @@ -6,7 +6,7 @@ use ruma::{ }, }; -use crate::{services, Result, Ruma}; +use crate::{services, Result, Ruma, RumaResponse}; /// # `POST /_matrix/client/r0/user_directory/search` /// @@ -17,7 +17,7 @@ use crate::{services, Result, Ruma}; /// and don't share a room with the sender pub(crate) async fn search_users_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let limit = body.limit.try_into().unwrap_or(usize::MAX); @@ -99,8 +99,8 @@ pub(crate) async fn search_users_route( let results = users.by_ref().take(limit).collect(); let limited = users.next().is_some(); - Ok(search_users::v3::Response { + Ok(RumaResponse(search_users::v3::Response { results, limited, - }) + })) } diff --git a/src/api/client_server/voip.rs b/src/api/client_server/voip.rs index 1cd80d27..9c29c2d1 100644 --- a/src/api/client_server/voip.rs +++ b/src/api/client_server/voip.rs @@ -5,7 +5,7 @@ use hmac::{Hmac, Mac}; use ruma::{api::client::voip::get_turn_server_info, SecondsSinceUnixEpoch}; use sha1::Sha1; -use crate::{services, Result, Ruma}; +use crate::{services, Result, Ruma, RumaResponse}; type HmacSha1 = Hmac; @@ -14,7 +14,7 @@ type HmacSha1 = Hmac; /// TODO: Returns information about the recommended turn server. pub(crate) async fn turn_server_route( body: Ruma, -) -> Result { +) -> Result> { let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let turn_secret = services().globals.turn_secret().clone(); @@ -43,10 +43,10 @@ pub(crate) async fn turn_server_route( (username, password) }; - Ok(get_turn_server_info::v3::Response { + Ok(RumaResponse(get_turn_server_info::v3::Response { username, password, uris: services().globals.turn_uris().to_vec(), ttl: Duration::from_secs(services().globals.turn_ttl()), - }) + })) } diff --git a/src/api/server_server.rs b/src/api/server_server.rs index 64fe253d..e66d9ab2 100644 --- a/src/api/server_server.rs +++ b/src/api/server_server.rs @@ -66,7 +66,7 @@ use tracing::{debug, error, warn}; use crate::{ api::client_server::{self, claim_keys_helper, get_keys_helper}, service::pdu::{gen_event_id_canonical_json, PduBuilder}, - services, utils, Error, PduEvent, Result, Ruma, + services, utils, Error, PduEvent, Result, Ruma, RumaResponse, }; /// Wraps either an literal IP address plus port, or a hostname plus complement @@ -554,13 +554,13 @@ async fn request_well_known(destination: &str) -> Option { /// Get version information on this server. pub(crate) async fn get_server_version_route( _body: Ruma, -) -> Result { - Ok(get_server_version::v1::Response { +) -> Result> { + Ok(RumaResponse(get_server_version::v1::Response { server: Some(get_server_version::v1::Server { name: Some(env!("CARGO_PKG_NAME").to_owned()), version: Some(crate::version()), }), - }) + })) } /// # `GET /_matrix/key/v2/server` @@ -631,7 +631,7 @@ pub(crate) async fn get_server_keys_deprecated_route() -> impl IntoResponse { /// Lists the public rooms on this server. pub(crate) async fn get_public_rooms_filtered_route( body: Ruma, -) -> Result { +) -> Result> { let response = client_server::get_public_rooms_filtered_helper( None, body.limit, @@ -641,12 +641,12 @@ pub(crate) async fn get_public_rooms_filtered_route( ) .await?; - Ok(get_public_rooms_filtered::v1::Response { + Ok(RumaResponse(get_public_rooms_filtered::v1::Response { chunk: response.chunk, prev_batch: response.prev_batch, next_batch: response.next_batch, total_room_count_estimate: response.total_room_count_estimate, - }) + })) } /// # `GET /_matrix/federation/v1/publicRooms` @@ -654,7 +654,7 @@ pub(crate) async fn get_public_rooms_filtered_route( /// Lists the public rooms on this server. pub(crate) async fn get_public_rooms_route( body: Ruma, -) -> Result { +) -> Result> { let response = client_server::get_public_rooms_filtered_helper( None, body.limit, @@ -664,12 +664,12 @@ pub(crate) async fn get_public_rooms_route( ) .await?; - Ok(get_public_rooms::v1::Response { + Ok(RumaResponse(get_public_rooms::v1::Response { chunk: response.chunk, prev_batch: response.prev_batch, next_batch: response.next_batch, total_room_count_estimate: response.total_room_count_estimate, - }) + })) } pub(crate) fn parse_incoming_pdu( @@ -709,7 +709,7 @@ pub(crate) fn parse_incoming_pdu( #[allow(clippy::too_many_lines)] pub(crate) async fn send_transaction_message_route( body: Ruma, -) -> Result { +) -> Result> { let sender_servername = body.sender_servername.as_ref().expect("server is authenticated"); @@ -976,12 +976,12 @@ pub(crate) async fn send_transaction_message_route( } } - Ok(send_transaction_message::v1::Response { + Ok(RumaResponse(send_transaction_message::v1::Response { pdus: resolved_map .into_iter() .map(|(e, r)| (e, r.map_err(|e| e.sanitized_error()))) .collect(), - }) + })) } /// # `GET /_matrix/federation/v1/event/{eventId}` @@ -992,7 +992,7 @@ pub(crate) async fn send_transaction_message_route( /// room pub(crate) async fn get_event_route( body: Ruma, -) -> Result { +) -> Result> { let sender_servername = body.sender_servername.as_ref().expect("server is authenticated"); @@ -1035,11 +1035,11 @@ pub(crate) async fn get_event_route( )); } - Ok(get_event::v1::Response { + Ok(RumaResponse(get_event::v1::Response { origin: services().globals.server_name().to_owned(), origin_server_ts: MilliSecondsSinceUnixEpoch::now(), pdu: PduEvent::convert_to_outgoing_federation_event(event), - }) + })) } /// # `GET /_matrix/federation/v1/backfill/` @@ -1048,7 +1048,7 @@ pub(crate) async fn get_event_route( /// history visibility allows. pub(crate) async fn get_backfill_route( body: Ruma, -) -> Result { +) -> Result> { let sender_servername = body.sender_servername.as_ref().expect("server is authenticated"); @@ -1106,11 +1106,11 @@ pub(crate) async fn get_backfill_route( .map(PduEvent::convert_to_outgoing_federation_event) .collect(); - Ok(get_backfill::v1::Response { + Ok(RumaResponse(get_backfill::v1::Response { origin: services().globals.server_name().to_owned(), origin_server_ts: MilliSecondsSinceUnixEpoch::now(), pdus: events, - }) + })) } /// # `POST /_matrix/federation/v1/get_missing_events/{roomId}` @@ -1118,7 +1118,7 @@ pub(crate) async fn get_backfill_route( /// Retrieves events that the sender is missing. pub(crate) async fn get_missing_events_route( body: Ruma, -) -> Result { +) -> Result> { let sender_servername = body.sender_servername.as_ref().expect("server is authenticated"); @@ -1208,9 +1208,9 @@ pub(crate) async fn get_missing_events_route( i += 1; } - Ok(get_missing_events::v1::Response { + Ok(RumaResponse(get_missing_events::v1::Response { events, - }) + })) } /// # `GET /_matrix/federation/v1/event_auth/{roomId}/{eventId}` @@ -1220,7 +1220,7 @@ pub(crate) async fn get_missing_events_route( /// - This does not include the event itself pub(crate) async fn get_event_authorization_route( body: Ruma, -) -> Result { +) -> Result> { let sender_servername = body.sender_servername.as_ref().expect("server is authenticated"); @@ -1263,14 +1263,14 @@ pub(crate) async fn get_event_authorization_route( .get_auth_chain(room_id, vec![Arc::from(&*body.event_id)]) .await?; - Ok(get_event_authorization::v1::Response { + Ok(RumaResponse(get_event_authorization::v1::Response { auth_chain: auth_chain_ids .filter_map(|id| { services().rooms.timeline.get_pdu_json(&id).ok()? }) .map(PduEvent::convert_to_outgoing_federation_event) .collect(), - }) + })) } /// # `GET /_matrix/federation/v1/state/{roomId}` @@ -1278,7 +1278,7 @@ pub(crate) async fn get_event_authorization_route( /// Retrieves the current state of the room. pub(crate) async fn get_room_state_route( body: Ruma, -) -> Result { +) -> Result> { let sender_servername = body.sender_servername.as_ref().expect("server is authenticated"); @@ -1326,7 +1326,7 @@ pub(crate) async fn get_room_state_route( .get_auth_chain(&body.room_id, vec![Arc::from(&*body.event_id)]) .await?; - Ok(get_room_state::v1::Response { + Ok(RumaResponse(get_room_state::v1::Response { auth_chain: auth_chain_ids .filter_map(|id| { if let Some(json) = @@ -1340,7 +1340,7 @@ pub(crate) async fn get_room_state_route( }) .collect(), pdus, - }) + })) } /// # `GET /_matrix/federation/v1/state_ids/{roomId}` @@ -1348,7 +1348,7 @@ pub(crate) async fn get_room_state_route( /// Retrieves the current state of the room. pub(crate) async fn get_room_state_ids_route( body: Ruma, -) -> Result { +) -> Result> { let sender_servername = body.sender_servername.as_ref().expect("server is authenticated"); @@ -1392,10 +1392,10 @@ pub(crate) async fn get_room_state_ids_route( .get_auth_chain(&body.room_id, vec![Arc::from(&*body.event_id)]) .await?; - Ok(get_room_state_ids::v1::Response { + Ok(RumaResponse(get_room_state_ids::v1::Response { auth_chain_ids: auth_chain_ids.map(|id| (*id).to_owned()).collect(), pdu_ids, - }) + })) } /// # `GET /_matrix/federation/v1/make_join/{roomId}/{userId}` @@ -1403,7 +1403,7 @@ pub(crate) async fn get_room_state_ids_route( /// Creates a join template. pub(crate) async fn create_join_event_template_route( body: Ruma, -) -> Result { +) -> Result> { if !services().rooms.metadata.exists(&body.room_id)? { return Err(Error::BadRequest( ErrorKind::NotFound, @@ -1504,11 +1504,11 @@ pub(crate) async fn create_join_event_template_route( pdu_json.remove("event_id"); - Ok(prepare_join_event::v1::Response { + Ok(RumaResponse(prepare_join_event::v1::Response { room_version: Some(room_version_id), event: to_raw_value(&pdu_json) .expect("CanonicalJson can be serialized to JSON"), - }) + })) } #[allow(clippy::too_many_lines)] @@ -1661,16 +1661,16 @@ async fn create_join_event( /// Submits a signed join event. pub(crate) async fn create_join_event_v1_route( body: Ruma, -) -> Result { +) -> Result> { let sender_servername = body.sender_servername.as_ref().expect("server is authenticated"); let room_state = create_join_event(sender_servername, &body.room_id, &body.pdu).await?; - Ok(create_join_event::v1::Response { + Ok(RumaResponse(create_join_event::v1::Response { room_state, - }) + })) } /// # `PUT /_matrix/federation/v2/send_join/{roomId}/{eventId}` @@ -1678,7 +1678,7 @@ pub(crate) async fn create_join_event_v1_route( /// Submits a signed join event. pub(crate) async fn create_join_event_v2_route( body: Ruma, -) -> Result { +) -> Result> { let sender_servername = body.sender_servername.as_ref().expect("server is authenticated"); @@ -1695,9 +1695,9 @@ pub(crate) async fn create_join_event_v2_route( servers_in_room: None, }; - Ok(create_join_event::v2::Response { + Ok(RumaResponse(create_join_event::v2::Response { room_state, - }) + })) } /// # `PUT /_matrix/federation/v2/invite/{roomId}/{eventId}` @@ -1706,7 +1706,7 @@ pub(crate) async fn create_join_event_v2_route( #[allow(clippy::too_many_lines)] pub(crate) async fn create_invite_route( body: Ruma, -) -> Result { +) -> Result> { let sender_servername = body.sender_servername.as_ref().expect("server is authenticated"); @@ -1827,9 +1827,9 @@ pub(crate) async fn create_invite_route( )?; } - Ok(create_invite::v2::Response { + Ok(RumaResponse(create_invite::v2::Response { event: PduEvent::convert_to_outgoing_federation_event(signed_event), - }) + })) } /// # `GET /_matrix/federation/v1/user/devices/{userId}` @@ -1837,7 +1837,7 @@ pub(crate) async fn create_invite_route( /// Gets information on all devices of the user. pub(crate) async fn get_devices_route( body: Ruma, -) -> Result { +) -> Result> { if body.user_id.server_name() != services().globals.server_name() { return Err(Error::BadRequest( ErrorKind::InvalidParam, @@ -1848,7 +1848,7 @@ pub(crate) async fn get_devices_route( let sender_servername = body.sender_servername.as_ref().expect("server is authenticated"); - Ok(get_devices::v1::Response { + Ok(RumaResponse(get_devices::v1::Response { user_id: body.user_id.clone(), stream_id: services() .users @@ -1881,7 +1881,7 @@ pub(crate) async fn get_devices_route( &body.user_id, &|u| u.server_name() == sender_servername, )?, - }) + })) } /// # `GET /_matrix/federation/v1/query/directory` @@ -1889,16 +1889,16 @@ pub(crate) async fn get_devices_route( /// Resolve a room alias to a room id. pub(crate) async fn get_room_information_route( body: Ruma, -) -> Result { +) -> Result> { let room_id = services().rooms.alias.resolve_local_alias(&body.room_alias)?.ok_or( Error::BadRequest(ErrorKind::NotFound, "Room alias not found."), )?; - Ok(get_room_information::v1::Response { + Ok(RumaResponse(get_room_information::v1::Response { room_id, servers: vec![services().globals.server_name().to_owned()], - }) + })) } /// # `GET /_matrix/federation/v1/query/profile` @@ -1906,7 +1906,7 @@ pub(crate) async fn get_room_information_route( /// Gets information on a profile. pub(crate) async fn get_profile_information_route( body: Ruma, -) -> Result { +) -> Result> { if body.user_id.server_name() != services().globals.server_name() { return Err(Error::BadRequest( ErrorKind::InvalidParam, @@ -1935,11 +1935,11 @@ pub(crate) async fn get_profile_information_route( } } - Ok(get_profile_information::v1::Response { + Ok(RumaResponse(get_profile_information::v1::Response { displayname, avatar_url, blurhash, - }) + })) } /// # `POST /_matrix/federation/v1/user/keys/query` @@ -1947,7 +1947,7 @@ pub(crate) async fn get_profile_information_route( /// Gets devices and identity keys for the given users. pub(crate) async fn get_keys_route( body: Ruma, -) -> Result { +) -> Result> { if body .device_keys .iter() @@ -1964,11 +1964,11 @@ pub(crate) async fn get_keys_route( }) .await?; - Ok(get_keys::v1::Response { + Ok(RumaResponse(get_keys::v1::Response { device_keys: result.device_keys, master_keys: result.master_keys, self_signing_keys: result.self_signing_keys, - }) + })) } /// # `POST /_matrix/federation/v1/user/keys/claim` @@ -1976,7 +1976,7 @@ pub(crate) async fn get_keys_route( /// Claims one-time keys. pub(crate) async fn claim_keys_route( body: Ruma, -) -> Result { +) -> Result> { if body .one_time_keys .iter() @@ -1990,9 +1990,9 @@ pub(crate) async fn claim_keys_route( let result = claim_keys_helper(&body.one_time_keys).await?; - Ok(claim_keys::v1::Response { + Ok(RumaResponse(claim_keys::v1::Response { one_time_keys: result.one_time_keys, - }) + })) } #[cfg(test)] diff --git a/src/main.rs b/src/main.rs index cccc7ba4..10eda195 100644 --- a/src/main.rs +++ b/src/main.rs @@ -579,11 +579,13 @@ macro_rules! impl_ruma_handler { ( $($ty:ident),* $(,)? ) => { #[axum::async_trait] #[allow(non_snake_case)] - impl RumaHandler<($($ty,)* Ruma,)> for F + impl + RumaHandler<($($ty,)* Ruma,)> for F where Req: IncomingRequest + Send + 'static, + Resp: IntoResponse, F: FnOnce($($ty,)* Ruma) -> Fut + Clone + Send + 'static, - Fut: Future> + Fut: Future> + Send, E: IntoResponse, $( $ty: FromRequestParts<()> + Send + 'static, )* @@ -600,7 +602,7 @@ macro_rules! impl_ruma_handler { on( method_filter, |$( $ty: $ty, )* req| async move { - handler($($ty,)* req).await.map(RumaResponse) + handler($($ty,)* req).await } ) )