From 42e0102a2a1de4b08afd52cb7ed26b692047733e Mon Sep 17 00:00:00 2001 From: Devin Ragotzy Date: Sat, 25 Jul 2020 23:08:00 -0400 Subject: [PATCH] Conditionally emit rocket::http_verb attr macros if lib/bin --- Cargo.lock | 2 - Cargo.toml | 44 ++-- src/client_server.rs | 491 ++++++++++++++++++++++++++++++++----------- src/error.rs | 23 +- src/lib.rs | 24 +++ src/main.rs | 1 + src/push_rules.rs | 5 +- src/ruma_wrapper.rs | 33 ++- 8 files changed, 453 insertions(+), 170 deletions(-) create mode 100644 src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index ab5b5513..45a5edd9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -261,7 +261,6 @@ checksum = "0dbbb57365263e881e805dc77d94697c9118fd94d8da011240555aa7b23445bd" name = "conduit" version = "0.1.0" dependencies = [ - "base64 0.12.3", "directories", "http", "image", @@ -276,7 +275,6 @@ dependencies = [ "serde_json", "sled", "thiserror", - "tokio", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index ec1ee630..02a90cdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,19 +12,31 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -rocket = { git = "https://github.com/SergioBenitez/Rocket.git", rev = "8d779caa22c63b15a6c3ceb75d8f6d4971b2eb67", features = ["tls"] } -http = "0.2.1" -log = "0.4.8" -sled = "0.32.0" -directories = "2.0.2" -js_int = "0.1.5" -serde_json = { version = "1.0.53", features = ["raw_value"] } -serde = "1.0.111" -tokio = { version = "0.2.21", features = ["macros"] } -rand = "0.7.3" -rust-argon2 = "0.8.2" -reqwest = "0.10.6" -base64 = "0.12.1" -thiserror = "1.0.19" -image = { version = "0.23.4", default-features = false, features = ["jpeg", "png", "gif"] } -ruma = { git = "https://github.com/ruma/ruma", features = ["rand", "client-api", "federation-api", "unstable-pre-spec", "unstable-synapse-quirks"], rev = "e047c647ddcb368e7eb1e05ae8823a9494273457" } +# TODO: This can become optional as soon as proper configs are supported +rocket = { git = "https://github.com/SergioBenitez/Rocket.git", rev = "8d779caa22c63b15a6c3ceb75d8f6d4971b2eb67", features = ["tls"], optional = false } # Used to handle requests +ruma = { git = "https://github.com/ruma/ruma", features = ["rand", "client-api", "federation-api", "unstable-pre-spec", "unstable-synapse-quirks"], rev = "e047c647ddcb368e7eb1e05ae8823a9494273457" } # Used for matrix spec type definitions and helpers +sled = "0.32.0" # Used for storing data permanently +log = "0.4.8" # Used for emitting log entries +http = "0.2.1" # Used for rocket<->ruma conversions +directories = "2.0.2" # Used to find data directory for default db path +js_int = "0.1.5" # Used for number types for ruma +serde_json = { version = "1.0.53", features = ["raw_value"] } # Used for ruma wrapper +serde = "1.0.111" # Used for pdu definition +rand = "0.7.3" # Used for secure identifiers +rust-argon2 = "0.8.2" # Used to hash passwords +reqwest = "0.10.6" # Used to send requests +thiserror = "1.0.19" # Used for conduit::Error type +image = { version = "0.23.4", default-features = false, features = ["jpeg", "png", "gif"] } # Used to generate thumbnails for images + +[features] +default = ["conduit_bin"] +conduit_bin = [] # TODO: add rocket to this when it is optional + +[[bin]] +name = "conduit" +path = "src/main.rs" +required-features = ["conduit_bin"] + +[lib] +name = "conduit" +path = "src/lib.rs" diff --git a/src/client_server.rs b/src/client_server.rs index c63e8b9c..d1addc89 100644 --- a/src/client_server.rs +++ b/src/client_server.rs @@ -7,7 +7,12 @@ use std::{ use crate::{utils, ConduitResult, Database, Error, Ruma}; use keys::{upload_signatures, upload_signing_keys}; use log::warn; + +#[cfg(not(feature = "conduit_bin"))] +use super::State; +#[cfg(feature = "conduit_bin")] use rocket::{delete, get, options, post, put, State}; + use ruma::{ api::client::{ error::ErrorKind, @@ -77,7 +82,7 @@ const TOKEN_LENGTH: usize = 256; const MXC_LENGTH: usize = 256; const SESSION_ID_LENGTH: usize = 256; -#[get("/_matrix/client/versions")] +#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/versions"))] pub fn get_supported_versions_route() -> ConduitResult { let mut unstable_features = BTreeMap::new(); @@ -90,7 +95,10 @@ pub fn get_supported_versions_route() -> ConduitResult, body: Ruma, @@ -120,7 +128,10 @@ pub fn get_register_available_route( Ok(get_username_availability::Response { available: true }.into()) } -#[post("/_matrix/client/r0/register", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/register", data = "") +)] pub fn register_route( db: State<'_, Database>, body: Ruma, @@ -223,7 +234,7 @@ pub fn register_route( .into()) } -#[get("/_matrix/client/r0/login")] +#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/login"))] pub fn get_login_route() -> ConduitResult { Ok(get_login_types::Response { flows: vec![get_login_types::LoginType::Password], @@ -231,7 +242,10 @@ pub fn get_login_route() -> ConduitResult { .into()) } -#[post("/_matrix/client/r0/login", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/login", data = "") +)] pub fn login_route( db: State<'_, Database>, body: Ruma, @@ -289,7 +303,10 @@ pub fn login_route( .into()) } -#[post("/_matrix/client/r0/logout", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/logout", data = "") +)] pub fn logout_route( db: State<'_, Database>, body: Ruma, @@ -302,7 +319,10 @@ pub fn logout_route( Ok(logout::Response.into()) } -#[post("/_matrix/client/r0/logout/all", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/logout/all", data = "") +)] pub fn logout_all_route( db: State<'_, Database>, body: Ruma, @@ -318,7 +338,10 @@ pub fn logout_all_route( Ok(logout_all::Response.into()) } -#[post("/_matrix/client/r0/account/password", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/account/password", data = "") +)] pub fn change_password_route( db: State<'_, Database>, body: Ruma, @@ -367,7 +390,10 @@ pub fn change_password_route( Ok(change_password::Response.into()) } -#[post("/_matrix/client/r0/account/deactivate", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/account/deactivate", data = "") +)] pub fn deactivate_route( db: State<'_, Database>, body: Ruma, @@ -440,7 +466,7 @@ pub fn deactivate_route( .into()) } -#[get("/_matrix/client/r0/capabilities")] +#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/capabilities"))] pub fn get_capabilities_route() -> ConduitResult { let mut available = BTreeMap::new(); available.insert( @@ -465,7 +491,10 @@ pub fn get_capabilities_route() -> ConduitResult { .into()) } -#[get("/_matrix/client/r0/pushrules", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/pushrules", data = "") +)] pub fn get_pushrules_all_route( db: State<'_, Database>, body: Ruma, @@ -486,10 +515,10 @@ pub fn get_pushrules_all_route( .into()) } -#[put( +#[cfg_attr(feature = "conduit_bin", put( "/_matrix/client/r0/pushrules/<_scope>/<_kind>/<_rule_id>", //data = "" -)] +))] pub fn set_pushrule_route( //db: State<'_, Database>, //body: Ruma, @@ -502,7 +531,10 @@ pub fn set_pushrule_route( Ok(set_pushrule::Response.into()) } -#[put("/_matrix/client/r0/pushrules/<_scope>/<_kind>/<_rule_id>/enabled")] +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/pushrules/<_scope>/<_kind>/<_rule_id>/enabled") +)] pub fn set_pushrule_enabled_route( _scope: String, _kind: String, @@ -513,7 +545,10 @@ pub fn set_pushrule_enabled_route( Ok(set_pushrule_enabled::Response.into()) } -#[get("/_matrix/client/r0/user/<_user_id>/filter/<_filter_id>")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/user/<_user_id>/filter/<_filter_id>") +)] pub fn get_filter_route( _user_id: String, _filter_id: String, @@ -531,7 +566,10 @@ pub fn get_filter_route( .into()) } -#[post("/_matrix/client/r0/user/<_user_id>/filter")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/user/<_user_id>/filter") +)] pub fn create_filter_route(_user_id: String) -> ConduitResult { // TODO Ok(create_filter::Response { @@ -540,9 +578,12 @@ pub fn create_filter_route(_user_id: String) -> ConduitResult/account_data/<_type>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + put( + "/_matrix/client/r0/user/<_user_id>/account_data/<_type>", + data = "" + ) )] pub fn set_global_account_data_route( db: State<'_, Database>, @@ -568,9 +609,12 @@ pub fn set_global_account_data_route( Ok(set_global_account_data::Response.into()) } -#[get( - "/_matrix/client/r0/user/<_user_id>/account_data/<_type>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + get( + "/_matrix/client/r0/user/<_user_id>/account_data/<_type>", + data = "" + ) )] pub fn get_global_account_data_route( db: State<'_, Database>, @@ -595,7 +639,10 @@ pub fn get_global_account_data_route( .into()) } -#[put("/_matrix/client/r0/profile/<_user_id>/displayname", data = "")] +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/profile/<_user_id>/displayname", data = "") +)] pub fn set_displayname_route( db: State<'_, Database>, body: Ruma, @@ -661,7 +708,10 @@ pub fn set_displayname_route( Ok(set_display_name::Response.into()) } -#[get("/_matrix/client/r0/profile/<_user_id>/displayname", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/profile/<_user_id>/displayname", data = "") +)] pub fn get_displayname_route( db: State<'_, Database>, body: Ruma, @@ -674,7 +724,10 @@ pub fn get_displayname_route( .into()) } -#[put("/_matrix/client/r0/profile/<_user_id>/avatar_url", data = "")] +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/profile/<_user_id>/avatar_url", data = "") +)] pub fn set_avatar_url_route( db: State<'_, Database>, body: Ruma, @@ -751,7 +804,10 @@ pub fn set_avatar_url_route( Ok(set_avatar_url::Response.into()) } -#[get("/_matrix/client/r0/profile/<_user_id>/avatar_url", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/profile/<_user_id>/avatar_url", data = "") +)] pub fn get_avatar_url_route( db: State<'_, Database>, body: Ruma, @@ -764,7 +820,10 @@ pub fn get_avatar_url_route( .into()) } -#[get("/_matrix/client/r0/profile/<_user_id>", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/profile/<_user_id>", data = "") +)] pub fn get_profile_route( db: State<'_, Database>, body: Ruma, @@ -789,7 +848,10 @@ pub fn get_profile_route( .into()) } -#[put("/_matrix/client/r0/presence/<_user_id>/status", data = "")] +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/presence/<_user_id>/status", data = "") +)] pub fn set_presence_route( db: State<'_, Database>, body: Ruma, @@ -819,7 +881,10 @@ pub fn set_presence_route( Ok(set_presence::Response.into()) } -#[post("/_matrix/client/r0/keys/upload", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/keys/upload", data = "") +)] pub fn upload_keys_route( db: State<'_, Database>, body: Ruma, @@ -848,7 +913,10 @@ pub fn upload_keys_route( .into()) } -#[post("/_matrix/client/r0/keys/query", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/keys/query", data = "") +)] pub fn get_keys_route( db: State<'_, Database>, body: Ruma, @@ -925,7 +993,10 @@ pub fn get_keys_route( .into()) } -#[post("/_matrix/client/r0/keys/claim", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/keys/claim", data = "") +)] pub fn claim_keys_route( db: State<'_, Database>, body: Ruma, @@ -953,7 +1024,10 @@ pub fn claim_keys_route( .into()) } -#[post("/_matrix/client/unstable/room_keys/version", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/unstable/room_keys/version", data = "") +)] pub fn create_backup_route( db: State<'_, Database>, body: Ruma, @@ -966,9 +1040,12 @@ pub fn create_backup_route( Ok(create_backup::Response { version }.into()) } -#[put( - "/_matrix/client/unstable/room_keys/version/<_version>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + put( + "/_matrix/client/unstable/room_keys/version/<_version>", + data = "" + ) )] pub fn update_backup_route( db: State<'_, Database>, @@ -982,7 +1059,10 @@ pub fn update_backup_route( Ok(update_backup::Response.into()) } -#[get("/_matrix/client/unstable/room_keys/version", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/unstable/room_keys/version", data = "") +)] pub fn get_latest_backup_route( db: State<'_, Database>, body: Ruma, @@ -1006,9 +1086,12 @@ pub fn get_latest_backup_route( .into()) } -#[get( - "/_matrix/client/unstable/room_keys/version/<_version>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + get( + "/_matrix/client/unstable/room_keys/version/<_version>", + data = "" + ) )] pub fn get_backup_route( db: State<'_, Database>, @@ -1033,7 +1116,11 @@ pub fn get_backup_route( .into()) } -#[put("/_matrix/client/unstable/room_keys/keys", data = "")] +/// Add the received backup_keys to the database. +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/unstable/room_keys/keys", data = "") +)] pub fn add_backup_keys_route( db: State<'_, Database>, body: Ruma, @@ -1060,7 +1147,10 @@ pub fn add_backup_keys_route( .into()) } -#[get("/_matrix/client/unstable/room_keys/keys", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/unstable/room_keys/keys", data = "") +)] pub fn get_backup_keys_route( db: State<'_, Database>, body: Ruma, @@ -1072,7 +1162,10 @@ pub fn get_backup_keys_route( Ok(get_backup_keys::Response { rooms }.into()) } -#[post("/_matrix/client/r0/rooms/<_room_id>/read_markers", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_room_id>/read_markers", data = "") +)] pub fn set_read_marker_route( db: State<'_, Database>, body: Ruma, @@ -1134,9 +1227,12 @@ pub fn set_read_marker_route( Ok(set_read_marker::Response.into()) } -#[put( - "/_matrix/client/r0/rooms/<_room_id>/typing/<_user_id>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + put( + "/_matrix/client/r0/rooms/<_room_id>/typing/<_user_id>", + data = "" + ) )] pub fn create_typing_event_route( db: State<'_, Database>, @@ -1163,7 +1259,10 @@ pub fn create_typing_event_route( Ok(create_typing_event::Response.into()) } -#[post("/_matrix/client/r0/createRoom", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/createRoom", data = "") +)] pub fn create_room_route( db: State<'_, Database>, body: Ruma, @@ -1428,7 +1527,10 @@ pub fn create_room_route( Ok(create_room::Response { room_id }.into()) } -#[get("/_matrix/client/r0/joined_rooms", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/joined_rooms", data = "") +)] pub fn joined_rooms_route( db: State<'_, Database>, body: Ruma, @@ -1445,9 +1547,12 @@ pub fn joined_rooms_route( .into()) } -#[put( - "/_matrix/client/r0/rooms/<_room_id>/redact/<_event_id>/<_txn_id>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + put( + "/_matrix/client/r0/rooms/<_room_id>/redact/<_event_id>/<_txn_id>", + data = "" + ) )] pub fn redact_event_route( db: State<'_, Database>, @@ -1475,7 +1580,10 @@ pub fn redact_event_route( Ok(redact_event::Response { event_id }.into()) } -#[put("/_matrix/client/r0/directory/room/<_room_alias>", data = "")] +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/directory/room/<_room_alias>", data = "") +)] pub fn create_alias_route( db: State<'_, Database>, body: Ruma, @@ -1491,7 +1599,10 @@ pub fn create_alias_route( Ok(create_alias::Response.into()) } -#[delete("/_matrix/client/r0/directory/room/<_room_alias>", data = "")] +#[cfg_attr( + feature = "conduit_bin", + delete("/_matrix/client/r0/directory/room/<_room_alias>", data = "") +)] pub fn delete_alias_route( db: State<'_, Database>, body: Ruma, @@ -1502,7 +1613,10 @@ pub fn delete_alias_route( Ok(delete_alias::Response.into()) } -#[get("/_matrix/client/r0/directory/room/<_room_alias>", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/directory/room/<_room_alias>", data = "") +)] pub fn get_alias_route( db: State<'_, Database>, body: Ruma, @@ -1527,7 +1641,10 @@ pub fn get_alias_route( .into()) } -#[post("/_matrix/client/r0/rooms/<_room_id>/join", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_room_id>/join", data = "") +)] pub fn join_room_by_id_route( db: State<'_, Database>, body: Ruma, @@ -1562,7 +1679,10 @@ pub fn join_room_by_id_route( .into()) } -#[post("/_matrix/client/r0/join/<_room_id_or_alias>", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/join/<_room_id_or_alias>", data = "") +)] pub fn join_room_by_id_or_alias_route( db: State<'_, Database>, body: Ruma, @@ -1591,7 +1711,10 @@ pub fn join_room_by_id_or_alias_route( .into()) } -#[post("/_matrix/client/r0/rooms/<_room_id>/leave", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_room_id>/leave", data = "") +)] pub fn leave_room_route( db: State<'_, Database>, body: Ruma, @@ -1629,7 +1752,10 @@ pub fn leave_room_route( Ok(leave_room::Response.into()) } -#[post("/_matrix/client/r0/rooms/<_room_id>/kick", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_room_id>/kick", data = "") +)] pub fn kick_user_route( db: State<'_, Database>, body: Ruma, @@ -1668,7 +1794,10 @@ pub fn kick_user_route( Ok(kick_user::Response.into()) } -#[get("/_matrix/client/r0/rooms/<_room_id>/joined_members", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/rooms/<_room_id>/joined_members", data = "") +)] pub fn joined_members_route( db: State<'_, Database>, body: Ruma, @@ -1700,7 +1829,10 @@ pub fn joined_members_route( Ok(joined_members::Response { joined }.into()) } -#[post("/_matrix/client/r0/rooms/<_room_id>/ban", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_room_id>/ban", data = "") +)] pub fn ban_user_route( db: State<'_, Database>, body: Ruma, @@ -1747,7 +1879,10 @@ pub fn ban_user_route( Ok(ban_user::Response.into()) } -#[post("/_matrix/client/r0/rooms/<_room_id>/unban", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_room_id>/unban", data = "") +)] pub fn unban_user_route( db: State<'_, Database>, body: Ruma, @@ -1785,7 +1920,10 @@ pub fn unban_user_route( Ok(unban_user::Response.into()) } -#[post("/_matrix/client/r0/rooms/<_room_id>/forget", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_room_id>/forget", data = "") +)] pub fn forget_room_route( db: State<'_, Database>, body: Ruma, @@ -1798,7 +1936,10 @@ pub fn forget_room_route( Ok(forget_room::Response.into()) } -#[post("/_matrix/client/r0/rooms/<_room_id>/invite", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_room_id>/invite", data = "") +)] pub fn invite_user_route( db: State<'_, Database>, body: Ruma, @@ -1829,7 +1970,10 @@ pub fn invite_user_route( } } -#[put("/_matrix/client/r0/directory/list/room/<_room_id>", data = "")] +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/directory/list/room/<_room_id>", data = "") +)] pub async fn set_room_visibility_route( db: State<'_, Database>, body: Ruma, @@ -1843,7 +1987,10 @@ pub async fn set_room_visibility_route( Ok(set_room_visibility::Response.into()) } -#[get("/_matrix/client/r0/directory/list/room/<_room_id>", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/directory/list/room/<_room_id>", data = "") +)] pub async fn get_room_visibility_route( db: State<'_, Database>, body: Ruma, @@ -1859,7 +2006,10 @@ pub async fn get_room_visibility_route( .into()) } -#[get("/_matrix/client/r0/publicRooms", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/publicRooms", data = "") +)] pub async fn get_public_rooms_route( db: State<'_, Database>, body: Ruma, @@ -1908,7 +2058,10 @@ pub async fn get_public_rooms_route( .into()) } -#[post("/_matrix/client/r0/publicRooms", data = "<_body>")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/publicRooms", data = "<_body>") +)] pub async fn get_public_rooms_filtered_route( db: State<'_, Database>, _body: Ruma, @@ -2020,7 +2173,10 @@ pub async fn get_public_rooms_filtered_route( .into()) } -#[post("/_matrix/client/r0/user_directory/search", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/user_directory/search", data = "") +)] pub fn search_users_route( db: State<'_, Database>, body: Ruma, @@ -2060,7 +2216,10 @@ pub fn search_users_route( .into()) } -#[get("/_matrix/client/r0/rooms/<_room_id>/members", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/rooms/<_room_id>/members", data = "") +)] pub fn get_member_events_route( db: State<'_, Database>, body: Ruma, @@ -2086,7 +2245,10 @@ pub fn get_member_events_route( .into()) } -#[get("/_matrix/client/r0/thirdparty/protocols")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/thirdparty/protocols") +)] pub fn get_protocols_route() -> ConduitResult { warn!("TODO: get_protocols_route"); Ok(get_protocols::Response { @@ -2095,9 +2257,12 @@ pub fn get_protocols_route() -> ConduitResult { .into()) } -#[get( - "/_matrix/client/r0/rooms/<_room_id>/event/<_event_id>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + get( + "/_matrix/client/r0/rooms/<_room_id>/event/<_event_id>", + data = "" + ) )] pub fn get_room_event_route( db: State<'_, Database>, @@ -2124,9 +2289,12 @@ pub fn get_room_event_route( .into()) } -#[put( - "/_matrix/client/r0/rooms/<_room_id>/send/<_event_type>/<_txn_id>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + put( + "/_matrix/client/r0/rooms/<_room_id>/send/<_event_type>/<_txn_id>", + data = "" + ) )] pub fn create_message_event_route( db: State<'_, Database>, @@ -2159,9 +2327,12 @@ pub fn create_message_event_route( Ok(create_message_event::Response { event_id }.into()) } -#[put( - "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>/<_state_key>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + put( + "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>/<_state_key>", + data = "" + ) )] pub fn create_state_event_for_key_route( db: State<'_, Database>, @@ -2221,9 +2392,12 @@ pub fn create_state_event_for_key_route( Ok(create_state_event_for_key::Response { event_id }.into()) } -#[put( - "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + put( + "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>", + data = "" + ) )] pub fn create_state_event_for_empty_key_route( db: State<'_, Database>, @@ -2268,7 +2442,10 @@ pub fn create_state_event_for_empty_key_route( .into()) } -#[get("/_matrix/client/r0/rooms/<_room_id>/state", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/rooms/<_room_id>/state", data = "") +)] pub fn get_state_events_route( db: State<'_, Database>, body: Ruma, @@ -2294,9 +2471,12 @@ pub fn get_state_events_route( .into()) } -#[get( - "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>/<_state_key>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + get( + "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>/<_state_key>", + data = "" + ) )] pub fn get_state_events_for_key_route( db: State<'_, Database>, @@ -2329,9 +2509,12 @@ pub fn get_state_events_for_key_route( .into()) } -#[get( - "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + get( + "/_matrix/client/r0/rooms/<_room_id>/state/<_event_type>", + data = "" + ) )] pub fn get_state_events_for_empty_key_route( db: State<'_, Database>, @@ -2363,7 +2546,10 @@ pub fn get_state_events_for_empty_key_route( .into()) } -#[get("/_matrix/client/r0/sync", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/sync", data = "") +)] pub fn sync_route( db: State<'_, Database>, body: Ruma, @@ -2703,10 +2889,10 @@ pub fn sync_route( .deserialize() .map_err(|_| Error::bad_database("EDU in database is invalid."))?; if let Some(timestamp) = edu.content.last_active_ago { - let last_active_ago = - js_int::UInt::try_from(utils::millis_since_unix_epoch()) - .expect("time is valid") - - timestamp; + let mut last_active_ago = utils::millis_since_unix_epoch() + .try_into() + .expect("time is valid"); + last_active_ago -= timestamp; edu.content.last_active_ago = Some(last_active_ago); } Ok::<_, Error>(edu.into()) @@ -2745,9 +2931,12 @@ pub fn sync_route( .into()) } -#[get( - "/_matrix/client/r0/rooms/<_room_id>/context/<_event_id>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + get( + "/_matrix/client/r0/rooms/<_room_id>/context/<_event_id>", + data = "" + ) )] pub fn get_context_route( db: State<'_, Database>, @@ -2847,7 +3036,10 @@ pub fn get_context_route( .into()) } -#[get("/_matrix/client/r0/rooms/<_room_id>/messages", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/rooms/<_room_id>/messages", data = "") +)] pub fn get_message_events_route( db: State<'_, Database>, body: Ruma, @@ -2944,7 +3136,7 @@ pub fn get_message_events_route( } } -#[get("/_matrix/client/r0/voip/turnServer")] +#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/voip/turnServer"))] pub fn turn_server_route() -> ConduitResult { Err(Error::BadRequest( ErrorKind::NotFound, @@ -2952,7 +3144,7 @@ pub fn turn_server_route() -> ConduitResult { )) } -#[post("/_matrix/client/r0/publicised_groups")] +#[cfg_attr(feature = "conduit_bin", post("/_matrix/client/r0/publicised_groups"))] pub fn publicised_groups_route() -> ConduitResult { Err(Error::BadRequest( ErrorKind::NotFound, @@ -2960,9 +3152,12 @@ pub fn publicised_groups_route() -> ConduitResult/<_txn_id>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + put( + "/_matrix/client/r0/sendToDevice/<_event_type>/<_txn_id>", + data = "" + ) )] pub fn send_event_to_device_route( db: State<'_, Database>, @@ -3009,7 +3204,7 @@ pub fn send_event_to_device_route( Ok(send_event_to_device::Response.into()) } -#[get("/_matrix/media/r0/config")] +#[cfg_attr(feature = "conduit_bin", get("/_matrix/media/r0/config"))] pub fn get_media_config_route( db: State<'_, Database>, ) -> ConduitResult { @@ -3019,7 +3214,10 @@ pub fn get_media_config_route( .into()) } -#[post("/_matrix/media/r0/upload", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/media/r0/upload", data = "") +)] pub fn create_content_route( db: State<'_, Database>, body: Ruma, @@ -3039,9 +3237,12 @@ pub fn create_content_route( Ok(create_content::Response { content_uri: mxc }.into()) } -#[get( - "/_matrix/media/r0/download/<_server_name>/<_media_id>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + get( + "/_matrix/media/r0/download/<_server_name>/<_media_id>", + data = "" + ) )] pub fn get_content_route( db: State<'_, Database>, @@ -3064,9 +3265,12 @@ pub fn get_content_route( } } -#[get( - "/_matrix/media/r0/thumbnail/<_server_name>/<_media_id>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + get( + "/_matrix/media/r0/thumbnail/<_server_name>/<_media_id>", + data = "" + ) )] pub fn get_content_thumbnail_route( db: State<'_, Database>, @@ -3089,7 +3293,10 @@ pub fn get_content_thumbnail_route( } } -#[get("/_matrix/client/r0/devices", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/devices", data = "") +)] pub fn get_devices_route( db: State<'_, Database>, body: Ruma, @@ -3105,7 +3312,10 @@ pub fn get_devices_route( Ok(get_devices::Response { devices }.into()) } -#[get("/_matrix/client/r0/devices/<_device_id>", data = "")] +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/devices/<_device_id>", data = "") +)] pub fn get_device_route( db: State<'_, Database>, body: Ruma, @@ -3121,7 +3331,10 @@ pub fn get_device_route( Ok(get_device::Response { device }.into()) } -#[put("/_matrix/client/r0/devices/<_device_id>", data = "")] +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/devices/<_device_id>", data = "") +)] pub fn update_device_route( db: State<'_, Database>, body: Ruma, @@ -3142,7 +3355,10 @@ pub fn update_device_route( Ok(update_device::Response.into()) } -#[delete("/_matrix/client/r0/devices/<_device_id>", data = "")] +#[cfg_attr( + feature = "conduit_bin", + delete("/_matrix/client/r0/devices/<_device_id>", data = "") +)] pub fn delete_device_route( db: State<'_, Database>, body: Ruma, @@ -3186,7 +3402,10 @@ pub fn delete_device_route( Ok(delete_device::Response.into()) } -#[post("/_matrix/client/r0/delete_devices", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/delete_devices", data = "") +)] pub fn delete_devices_route( db: State<'_, Database>, body: Ruma, @@ -3231,7 +3450,10 @@ pub fn delete_devices_route( Ok(delete_devices::Response.into()) } -#[post("/_matrix/client/unstable/keys/device_signing/upload", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/unstable/keys/device_signing/upload", data = "") +)] pub fn upload_signing_keys_route( db: State<'_, Database>, body: Ruma, @@ -3282,7 +3504,10 @@ pub fn upload_signing_keys_route( Ok(upload_signing_keys::Response.into()) } -#[post("/_matrix/client/unstable/keys/signatures/upload", data = "")] +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/unstable/keys/signatures/upload", data = "") +)] pub fn upload_signatures_route( db: State<'_, Database>, body: Ruma, @@ -3331,7 +3556,7 @@ pub fn upload_signatures_route( Ok(upload_signatures::Response.into()) } -#[get("/_matrix/client/r0/pushers")] +#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/pushers"))] pub fn pushers_route() -> ConduitResult { Ok(get_pushers::Response { pushers: Vec::new(), @@ -3339,7 +3564,7 @@ pub fn pushers_route() -> ConduitResult { .into()) } -#[post("/_matrix/client/r0/pushers/set")] +#[cfg_attr(feature = "conduit_bin", post("/_matrix/client/r0/pushers/set"))] pub fn set_pushers_route() -> ConduitResult { Ok(get_pushers::Response { pushers: Vec::new(), @@ -3347,9 +3572,12 @@ pub fn set_pushers_route() -> ConduitResult { .into()) } -#[put( - "/_matrix/client/r0/user/<_user_id>/rooms/<_room_id>/tags/<_tag>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + put( + "/_matrix/client/r0/user/<_user_id>/rooms/<_room_id>/tags/<_tag>", + data = "" + ) )] pub fn update_tag_route( db: State<'_, Database>, @@ -3384,9 +3612,12 @@ pub fn update_tag_route( Ok(create_tag::Response.into()) } -#[delete( - "/_matrix/client/r0/user/<_user_id>/rooms/<_room_id>/tags/<_tag>", - data = "" +#[cfg_attr( + feature = "conduit_bin", + delete( + "/_matrix/client/r0/user/<_user_id>/rooms/<_room_id>/tags/<_tag>", + data = "" + ) )] pub fn delete_tag_route( db: State<'_, Database>, @@ -3418,9 +3649,12 @@ pub fn delete_tag_route( Ok(delete_tag::Response.into()) } -#[get( - "/_matrix/client/r0/user/<_user_id>/rooms/<_room_id>/tags", - data = "" +#[cfg_attr( + feature = "conduit_bin", + get( + "/_matrix/client/r0/user/<_user_id>/rooms/<_room_id>/tags", + data = "" + ) )] pub fn get_tags_route( db: State<'_, Database>, @@ -3445,6 +3679,7 @@ pub fn get_tags_route( .into()) } +#[cfg(feature = "conduit_bin")] #[options("/<_segments..>")] pub fn options_route( _segments: rocket::http::uri::Segments<'_>, diff --git a/src/error.rs b/src/error.rs index 7305073c..af5405c2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,16 +1,18 @@ -use crate::RumaResponse; -use http::StatusCode; use log::error; -use rocket::{ - response::{self, Responder}, - Request, -}; -use ruma::api::client::{ - error::{Error as RumaError, ErrorKind}, - r0::uiaa::{UiaaInfo, UiaaResponse}, -}; +use ruma::api::client::{error::ErrorKind, r0::uiaa::UiaaInfo}; use thiserror::Error; +#[cfg(feature = "conduit_bin")] +use { + crate::RumaResponse, + http::StatusCode, + rocket::{ + response::{self, Responder}, + Request, + }, + ruma::api::client::{error::Error as RumaError, r0::uiaa::UiaaResponse}, +}; + pub type Result = std::result::Result; #[derive(Error, Debug)] @@ -46,6 +48,7 @@ impl Error { } } +#[cfg(feature = "conduit_bin")] impl<'r, 'o> Responder<'r, 'o> for Error where 'o: 'r, diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..cd5029c1 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,24 @@ +pub mod client_server; +mod database; +mod error; +mod pdu; +pub mod push_rules; +mod ruma_wrapper; +mod utils; + +pub use database::Database; +pub use error::{Error, Result}; +pub use pdu::PduEvent; +pub use ruma_wrapper::{ConduitResult, Ruma, RumaResponse}; +use std::ops::Deref; + +pub struct State<'r, T: Send + Sync + 'static>(&'r T); + +impl<'r, T: Send + Sync + 'static> Deref for State<'r, T> { + type Target = T; + + #[inline(always)] + fn deref(&self) -> &T { + self.0 + } +} diff --git a/src/main.rs b/src/main.rs index ba22b645..1feee4d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ mod utils; pub use database::Database; pub use error::{Error, Result}; pub use pdu::PduEvent; +pub use rocket::State; pub use ruma_wrapper::{ConduitResult, Ruma, RumaResponse}; use rocket::{fairing::AdHoc, routes}; diff --git a/src/push_rules.rs b/src/push_rules.rs index 43afbca0..32c709e6 100644 --- a/src/push_rules.rs +++ b/src/push_rules.rs @@ -1,4 +1,3 @@ -use js_int::uint; use ruma::{ push::{ Action, ConditionalPushRule, ConditionalPushRuleInit, PatternedPushRule, @@ -185,7 +184,7 @@ pub fn encrypted_room_one_to_one_rule() -> ConditionalPushRule { rule_id: ".m.rule.encrypted_room_one_to_one".to_owned(), conditions: vec![ PushCondition::RoomMemberCount { - is: RoomMemberCountIs::from(uint!(2)..), + is: RoomMemberCountIs::from(2_u32.into()..), }, PushCondition::EventMatch { key: "type".to_owned(), @@ -208,7 +207,7 @@ pub fn room_one_to_one_rule() -> ConditionalPushRule { rule_id: ".m.rule.room_one_to_one".to_owned(), conditions: vec![ PushCondition::RoomMemberCount { - is: RoomMemberCountIs::from(uint!(2)..), + is: RoomMemberCountIs::from(2_u32.into()..), }, PushCondition::EventMatch { key: "type".to_owned(), diff --git a/src/ruma_wrapper.rs b/src/ruma_wrapper.rs index 5b380b37..2a82b10f 100644 --- a/src/ruma_wrapper.rs +++ b/src/ruma_wrapper.rs @@ -1,15 +1,24 @@ -use crate::{utils, Error}; -use log::warn; -use rocket::{ - data::{Data, FromDataFuture, FromTransformedData, Transform, TransformFuture, Transformed}, - http::Status, - response::{self, Responder}, - Outcome::*, - Request, State, +use crate::Error; +use ruma::identifiers::{DeviceId, UserId}; +use std::{convert::TryInto, ops::Deref}; + +#[cfg(feature = "conduit_bin")] +use { + crate::utils, + log::warn, + rocket::{ + data::{ + Data, FromDataFuture, FromTransformedData, Transform, TransformFuture, Transformed, + }, + http::Status, + response::{self, Responder}, + tokio::io::AsyncReadExt, + Outcome::*, + Request, State, + }, + ruma::api::Endpoint, + std::io::Cursor, }; -use ruma::{api::Endpoint, DeviceId, UserId}; -use std::{convert::TryInto, io::Cursor, ops::Deref}; -use tokio::io::AsyncReadExt; /// This struct converts rocket requests into ruma structs by converting them into http requests /// first. @@ -20,6 +29,7 @@ pub struct Ruma { pub json_body: Option>, // This is None when body is not a valid string } +#[cfg(feature = "conduit_bin")] impl<'a, T: Endpoint> FromTransformedData<'a> for Ruma { type Error = (); // TODO: Better error handling type Owned = Data; @@ -119,6 +129,7 @@ impl>>> From for RumaResponse { } } +#[cfg(feature = "conduit_bin")] impl<'r, 'o, T> Responder<'r, 'o> for RumaResponse where T: Send + TryInto>>,