From c1c62b7eb45a147f7f9b269bd784ba51cbb1b357 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Thu, 30 Jul 2020 18:14:47 +0200 Subject: [PATCH] refactor: put endpoints into modules --- src/client_server.rs | 3844 --------------------------- src/client_server/account.rs | 308 +++ src/client_server/alias.rs | 67 + src/client_server/backup.rs | 142 + src/client_server/capabilities.rs | 31 + src/client_server/config.rs | 67 + src/client_server/context.rs | 92 + src/client_server/device.rs | 170 ++ src/client_server/directory.rs | 258 ++ src/client_server/filter.rs | 29 + src/client_server/keys.rs | 323 +++ src/client_server/media.rs | 107 + src/client_server/membership.rs | 422 +++ src/client_server/message.rs | 128 + src/client_server/mod.rs | 78 + src/client_server/presence.rs | 45 + src/client_server/profile.rs | 236 ++ src/client_server/push.rs | 75 + src/client_server/read_marker.rs | 74 + src/client_server/redact.rs | 39 + src/client_server/room.rs | 345 +++ src/client_server/session.rs | 128 + src/client_server/state.rs | 216 ++ src/client_server/sync.rs | 477 ++++ src/client_server/tag.rs | 100 + src/client_server/thirdparty.rs | 19 + src/client_server/to_device.rs | 56 + src/client_server/typing.rs | 33 + src/client_server/unversioned.rs | 19 + src/client_server/user_directory.rs | 52 + src/client_server/voip.rs | 13 + src/lib.rs | 5 +- src/main.rs | 3 +- 33 files changed, 4153 insertions(+), 3848 deletions(-) delete mode 100644 src/client_server.rs create mode 100644 src/client_server/account.rs create mode 100644 src/client_server/alias.rs create mode 100644 src/client_server/backup.rs create mode 100644 src/client_server/capabilities.rs create mode 100644 src/client_server/config.rs create mode 100644 src/client_server/context.rs create mode 100644 src/client_server/device.rs create mode 100644 src/client_server/directory.rs create mode 100644 src/client_server/filter.rs create mode 100644 src/client_server/keys.rs create mode 100644 src/client_server/media.rs create mode 100644 src/client_server/membership.rs create mode 100644 src/client_server/message.rs create mode 100644 src/client_server/mod.rs create mode 100644 src/client_server/presence.rs create mode 100644 src/client_server/profile.rs create mode 100644 src/client_server/push.rs create mode 100644 src/client_server/read_marker.rs create mode 100644 src/client_server/redact.rs create mode 100644 src/client_server/room.rs create mode 100644 src/client_server/session.rs create mode 100644 src/client_server/state.rs create mode 100644 src/client_server/sync.rs create mode 100644 src/client_server/tag.rs create mode 100644 src/client_server/thirdparty.rs create mode 100644 src/client_server/to_device.rs create mode 100644 src/client_server/typing.rs create mode 100644 src/client_server/unversioned.rs create mode 100644 src/client_server/user_directory.rs create mode 100644 src/client_server/voip.rs diff --git a/src/client_server.rs b/src/client_server.rs deleted file mode 100644 index cf3f67c..0000000 --- a/src/client_server.rs +++ /dev/null @@ -1,3844 +0,0 @@ -use std::{ - collections::{hash_map, BTreeMap, HashMap, HashSet}, - convert::{TryFrom, TryInto}, - time::{Duration, SystemTime}, -}; - -use crate::{ - database::media::FileMeta, pdu::PduBuilder, 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, tokio, State}; - -use ruma::{ - api::client::{ - error::ErrorKind, - r0::{ - account::{ - change_password, deactivate, get_username_availability, register, whoami, - ThirdPartyIdRemovalStatus, - }, - alias::{create_alias, delete_alias, get_alias}, - backup::{ - add_backup_keys, create_backup, get_backup, get_backup_keys, get_latest_backup, - update_backup, - }, - capabilities::get_capabilities, - config::{get_global_account_data, set_global_account_data}, - context::get_context, - device::{self, delete_device, delete_devices, get_device, get_devices, update_device}, - directory::{ - self, get_public_rooms, get_public_rooms_filtered, get_room_visibility, - set_room_visibility, - }, - filter::{self, create_filter, get_filter}, - keys::{self, claim_keys, get_key_changes, get_keys, upload_keys}, - media::{create_content, get_content, get_content_thumbnail, get_media_config}, - membership::{ - ban_user, forget_room, get_member_events, invite_user, join_room_by_id, - join_room_by_id_or_alias, joined_members, joined_rooms, kick_user, leave_room, - unban_user, - }, - message::{create_message_event, get_message_events}, - presence::set_presence, - profile::{ - get_avatar_url, get_display_name, get_profile, set_avatar_url, set_display_name, - }, - push::{get_pushers, get_pushrules_all, set_pushrule, set_pushrule_enabled}, - read_marker::set_read_marker, - redact::redact_event, - room::{self, create_room, get_room_event}, - session::{get_login_types, login, logout, logout_all}, - state::{ - create_state_event_for_empty_key, create_state_event_for_key, get_state_events, - get_state_events_for_empty_key, get_state_events_for_key, - }, - sync::sync_events, - tag::{create_tag, delete_tag, get_tags}, - thirdparty::get_protocols, - to_device::{self, send_event_to_device}, - typing::create_typing_event, - uiaa::{AuthFlow, UiaaInfo}, - user_directory::search_users, - }, - unversioned::get_supported_versions, - }, - events::{ - custom::CustomEventContent, - room::{ - canonical_alias, guest_access, history_visibility, join_rules, member, name, redaction, - topic, - }, - AnyEphemeralRoomEvent, AnyEvent, AnySyncEphemeralRoomEvent, BasicEvent, EventType, - }, - Raw, RoomAliasId, RoomId, RoomVersionId, UserId, -}; - -const GUEST_NAME_LENGTH: usize = 10; -const DEVICE_ID_LENGTH: usize = 10; -const TOKEN_LENGTH: usize = 256; -const MXC_LENGTH: usize = 256; -const SESSION_ID_LENGTH: usize = 256; - -#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/versions"))] -pub fn get_supported_versions_route() -> ConduitResult { - let mut unstable_features = BTreeMap::new(); - - unstable_features.insert("org.matrix.e2e_cross_signing".to_owned(), true); - - Ok(get_supported_versions::Response { - versions: vec!["r0.5.0".to_owned(), "r0.6.0".to_owned()], - unstable_features, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/register/available", data = "") -)] -pub fn get_register_available_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - // Validate user id - let user_id = UserId::parse_with_server_name(body.username.clone(), db.globals.server_name()) - .ok() - .filter(|user_id| { - !user_id.is_historical() && user_id.server_name() == db.globals.server_name() - }) - .ok_or(Error::BadRequest( - ErrorKind::InvalidUsername, - "Username is invalid.", - ))?; - - // Check if username is creative enough - if db.users.exists(&user_id)? { - return Err(Error::BadRequest( - ErrorKind::UserInUse, - "Desired user ID is already taken.", - )); - } - - // 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::Response { available: true }.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/register", data = "") -)] -pub fn register_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - if db.globals.registration_disabled() { - return Err(Error::BadRequest( - ErrorKind::Forbidden, - "Registration has been disabled.", - )); - } - - // Validate user id - let user_id = UserId::parse_with_server_name( - body.username - .clone() - .unwrap_or_else(|| utils::random_string(GUEST_NAME_LENGTH)) - .to_lowercase(), - db.globals.server_name(), - ) - .ok() - .filter(|user_id| !user_id.is_historical() && user_id.server_name() == db.globals.server_name()) - .ok_or(Error::BadRequest( - ErrorKind::InvalidUsername, - "Username is invalid.", - ))?; - - // Check if username is creative enough - if db.users.exists(&user_id)? { - return Err(Error::BadRequest( - ErrorKind::UserInUse, - "Desired user ID is already taken.", - )); - } - - // UIAA - let mut uiaainfo = UiaaInfo { - flows: vec![AuthFlow { - stages: vec!["m.login.dummy".to_owned()], - }], - completed: Vec::new(), - params: Default::default(), - session: None, - auth_error: None, - }; - - if let Some(auth) = &body.auth { - let (worked, uiaainfo) = - db.uiaa - .try_auth(&user_id, "".into(), auth, &uiaainfo, &db.users, &db.globals)?; - if !worked { - return Err(Error::Uiaa(uiaainfo)); - } - // Success! - } else { - uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); - db.uiaa.create(&user_id, "".into(), &uiaainfo)?; - return Err(Error::Uiaa(uiaainfo)); - } - - let password = body.password.clone().unwrap_or_default(); - - // Create user - db.users.create(&user_id, &password)?; - - // Generate new device id if the user didn't specify one - let device_id = body - .device_id - .clone() - .unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into()); - - // Generate new token for the device - let token = utils::random_string(TOKEN_LENGTH); - - // Add device - db.users.create_device( - &user_id, - &device_id, - &token, - body.initial_device_display_name.clone(), - )?; - - // Initial data - db.account_data.update( - None, - &user_id, - EventType::PushRules, - &ruma::events::push_rules::PushRulesEvent { - content: ruma::events::push_rules::PushRulesEventContent { - global: crate::push_rules::default_pushrules(&user_id), - }, - }, - &db.globals, - )?; - - Ok(register::Response { - access_token: Some(token), - user_id, - device_id: Some(device_id), - } - .into()) -} - -#[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], - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/login", data = "") -)] -pub fn login_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - // Validate login method - let user_id = - // TODO: Other login methods - if let (login::UserInfo::MatrixId(username), login::LoginInfo::Password { password }) = - (body.user.clone(), body.login_info.clone()) - { - let user_id = UserId::parse_with_server_name(username, db.globals.server_name()) - .map_err(|_| Error::BadRequest( - ErrorKind::InvalidUsername, - "Username is invalid." - ))?; - let hash = db.users.password_hash(&user_id)? - .ok_or(Error::BadRequest( - ErrorKind::Forbidden, - "Wrong username or password." - ))?; - - if hash.is_empty() { - return Err(Error::BadRequest( - ErrorKind::UserDeactivated, - "The user has been deactivated" - )); - } - - let hash_matches = - argon2::verify_encoded(&hash, password.as_bytes()).unwrap_or(false); - - if !hash_matches { - return Err(Error::BadRequest(ErrorKind::Forbidden, "Wrong username or password.")); - } - - user_id - } else { - return Err(Error::BadRequest(ErrorKind::Forbidden, "Bad login type.")); - }; - - // Generate new device id if the user didn't specify one - let device_id = body - .body - .device_id - .clone() - .unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into()); - - // Generate a new token for the device - let token = utils::random_string(TOKEN_LENGTH); - - // Add device - db.users.create_device( - &user_id, - &device_id, - &token, - body.initial_device_display_name.clone(), - )?; - - Ok(login::Response { - user_id, - access_token: token, - home_server: Some(db.globals.server_name().to_owned()), - device_id, - well_known: None, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/account/whoami", data = "") -)] -pub fn whoami_route(body: Ruma) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - Ok(whoami::Response { - user_id: sender_id.clone(), - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/logout", data = "") -)] -pub fn logout_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - let device_id = body.device_id.as_ref().expect("user is authenticated"); - - db.users.remove_device(&sender_id, device_id)?; - - Ok(logout::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/logout/all", data = "") -)] -pub fn logout_all_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - for device_id in db.users.all_device_ids(sender_id) { - if let Ok(device_id) = device_id { - db.users.remove_device(&sender_id, &device_id)?; - } - } - - Ok(logout_all::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/account/password", data = "") -)] -pub fn change_password_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - let device_id = body.device_id.as_ref().expect("user is authenticated"); - - let mut uiaainfo = UiaaInfo { - flows: vec![AuthFlow { - stages: vec!["m.login.password".to_owned()], - }], - completed: Vec::new(), - params: Default::default(), - session: None, - auth_error: None, - }; - - if let Some(auth) = &body.auth { - let (worked, uiaainfo) = db.uiaa.try_auth( - &sender_id, - device_id, - auth, - &uiaainfo, - &db.users, - &db.globals, - )?; - if !worked { - return Err(Error::Uiaa(uiaainfo)); - } - // Success! - } else { - uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); - db.uiaa.create(&sender_id, &device_id, &uiaainfo)?; - return Err(Error::Uiaa(uiaainfo)); - } - - db.users.set_password(&sender_id, &body.new_password)?; - - // TODO: Read logout_devices field when it's available and respect that, currently not supported in Ruma - // See: https://github.com/ruma/ruma/issues/107 - // Logout all devices except the current one - for id in db - .users - .all_device_ids(&sender_id) - .filter_map(|id| id.ok()) - .filter(|id| id != device_id) - { - db.users.remove_device(&sender_id, &id)?; - } - - Ok(change_password::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/account/deactivate", data = "") -)] -pub fn deactivate_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - let device_id = body.device_id.as_ref().expect("user is authenticated"); - - let mut uiaainfo = UiaaInfo { - flows: vec![AuthFlow { - stages: vec!["m.login.password".to_owned()], - }], - completed: Vec::new(), - params: Default::default(), - session: None, - auth_error: None, - }; - - if let Some(auth) = &body.auth { - let (worked, uiaainfo) = db.uiaa.try_auth( - &sender_id, - &device_id, - auth, - &uiaainfo, - &db.users, - &db.globals, - )?; - if !worked { - return Err(Error::Uiaa(uiaainfo)); - } - // Success! - } else { - uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); - db.uiaa.create(&sender_id, &device_id, &uiaainfo)?; - return Err(Error::Uiaa(uiaainfo)); - } - - // Leave all joined rooms and reject all invitations - for room_id in db - .rooms - .rooms_joined(&sender_id) - .chain(db.rooms.rooms_invited(&sender_id)) - { - let room_id = room_id?; - let event = member::MemberEventContent { - membership: member::MembershipState::Leave, - displayname: None, - avatar_url: None, - is_direct: None, - third_party_invite: None, - }; - - db.rooms.append_pdu( - PduBuilder { - room_id: room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomMember, - content: serde_json::to_value(event).expect("event is valid, we just created it"), - unsigned: None, - state_key: Some(sender_id.to_string()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - } - - // Remove devices and mark account as deactivated - db.users.deactivate_account(&sender_id)?; - - Ok(deactivate::Response { - id_server_unbind_result: ThirdPartyIdRemovalStatus::NoSupport, - } - .into()) -} - -#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/capabilities"))] -pub fn get_capabilities_route() -> ConduitResult { - let mut available = BTreeMap::new(); - available.insert( - RoomVersionId::Version5, - get_capabilities::RoomVersionStability::Stable, - ); - available.insert( - RoomVersionId::Version6, - get_capabilities::RoomVersionStability::Stable, - ); - - Ok(get_capabilities::Response { - capabilities: get_capabilities::Capabilities { - change_password: None, // None means it is possible - room_versions: Some(get_capabilities::RoomVersionsCapability { - default: "6".to_owned(), - available, - }), - custom_capabilities: BTreeMap::new(), - }, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/pushrules", data = "") -)] -pub fn get_pushrules_all_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let event = db - .account_data - .get::(None, &sender_id, EventType::PushRules)? - .ok_or(Error::BadRequest( - ErrorKind::NotFound, - "PushRules event not found.", - ))?; - - Ok(get_pushrules_all::Response { - global: event.content.global, - } - .into()) -} - -#[cfg_attr(feature = "conduit_bin", put( - "/_matrix/client/r0/pushrules/<_>/<_>/<_>", - //data = "" -))] -pub fn set_pushrule_route(//db: State<'_, Database>, - //body: Ruma, -) -> ConduitResult { - // TODO - warn!("TODO: set_pushrule_route"); - Ok(set_pushrule::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/pushrules/<_>/<_>/<_>/enabled") -)] -pub fn set_pushrule_enabled_route() -> ConduitResult { - // TODO - warn!("TODO: set_pushrule_enabled_route"); - Ok(set_pushrule_enabled::Response.into()) -} - -#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/user/<_>/filter/<_>"))] -pub fn get_filter_route() -> ConduitResult { - // TODO - Ok(get_filter::Response { - filter: filter::FilterDefinition { - event_fields: None, - event_format: None, - account_data: None, - room: None, - presence: None, - }, - } - .into()) -} - -#[cfg_attr(feature = "conduit_bin", post("/_matrix/client/r0/user/<_>/filter"))] -pub fn create_filter_route() -> ConduitResult { - // TODO - Ok(create_filter::Response { - filter_id: utils::random_string(10), - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/user/<_>/account_data/<_>", data = "") -)] -pub fn set_global_account_data_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let content = serde_json::from_str::(body.data.get()) - .map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?; - - let event_type = body.event_type.to_string(); - - db.account_data.update( - None, - sender_id, - event_type.clone().into(), - &BasicEvent { - content: CustomEventContent { - event_type, - json: content, - }, - }, - &db.globals, - )?; - - Ok(set_global_account_data::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/user/<_>/account_data/<_>", data = "") -)] -pub fn get_global_account_data_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let data = db - .account_data - .get::>( - None, - sender_id, - EventType::try_from(&body.event_type).expect("EventType::try_from can never fail"), - )? - .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?; - - Ok(get_global_account_data::Response { account_data: data }.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/profile/<_>/displayname", data = "") -)] -pub fn set_displayname_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - db.users - .set_displayname(&sender_id, body.displayname.clone())?; - - // Send a new membership event and presence update into all joined rooms - for room_id in db.rooms.rooms_joined(&sender_id) { - let room_id = room_id?; - db.rooms.append_pdu( - PduBuilder { - room_id: room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomMember, - content: serde_json::to_value(ruma::events::room::member::MemberEventContent { - displayname: body.displayname.clone(), - ..serde_json::from_value::>( - db.rooms - .room_state_get( - &room_id, - &EventType::RoomMember, - &sender_id.to_string(), - )? - .ok_or_else(|| { - Error::bad_database( - "Tried to send displayname update for user not in the room.", - ) - })? - .content - .clone(), - ) - .expect("from_value::> can never fail") - .deserialize() - .map_err(|_| Error::bad_database("Database contains invalid PDU."))? - }) - .expect("event is valid, we just created it"), - unsigned: None, - state_key: Some(sender_id.to_string()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - // Presence update - db.rooms.edus.update_presence( - &sender_id, - &room_id, - ruma::events::presence::PresenceEvent { - content: ruma::events::presence::PresenceEventContent { - avatar_url: db.users.avatar_url(&sender_id)?, - currently_active: None, - displayname: db.users.displayname(&sender_id)?, - last_active_ago: Some( - utils::millis_since_unix_epoch() - .try_into() - .expect("time is valid"), - ), - presence: ruma::presence::PresenceState::Online, - status_msg: None, - }, - sender: sender_id.clone(), - }, - &db.globals, - )?; - } - - Ok(set_display_name::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/profile/<_>/displayname", data = "") -)] -pub fn get_displayname_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - Ok(get_display_name::Response { - displayname: db.users.displayname(&body.user_id)?, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/profile/<_>/avatar_url", data = "") -)] -pub fn set_avatar_url_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - if let Some(avatar_url) = &body.avatar_url { - if !avatar_url.starts_with("mxc://") { - return Err(Error::BadRequest( - ErrorKind::InvalidParam, - "avatar_url has to start with mxc://.", - )); - } - - // TODO in the future when we can handle media uploads make sure that this url is our own server - // TODO also make sure this is valid mxc:// format (not only starting with it) - } - - db.users - .set_avatar_url(&sender_id, body.avatar_url.clone())?; - - // Send a new membership event and presence update into all joined rooms - for room_id in db.rooms.rooms_joined(&sender_id) { - let room_id = room_id?; - db.rooms.append_pdu( - PduBuilder { - room_id: room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomMember, - content: serde_json::to_value(ruma::events::room::member::MemberEventContent { - avatar_url: body.avatar_url.clone(), - ..serde_json::from_value::>( - db.rooms - .room_state_get( - &room_id, - &EventType::RoomMember, - &sender_id.to_string(), - )? - .ok_or_else(|| { - Error::bad_database( - "Tried to send avatar url update for user not in the room.", - ) - })? - .content - .clone(), - ) - .expect("from_value::> can never fail") - .deserialize() - .map_err(|_| Error::bad_database("Database contains invalid PDU."))? - }) - .expect("event is valid, we just created it"), - unsigned: None, - state_key: Some(sender_id.to_string()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - // Presence update - db.rooms.edus.update_presence( - &sender_id, - &room_id, - ruma::events::presence::PresenceEvent { - content: ruma::events::presence::PresenceEventContent { - avatar_url: db.users.avatar_url(&sender_id)?, - currently_active: None, - displayname: db.users.displayname(&sender_id)?, - last_active_ago: Some( - utils::millis_since_unix_epoch() - .try_into() - .expect("time is valid"), - ), - presence: ruma::presence::PresenceState::Online, - status_msg: None, - }, - sender: sender_id.clone(), - }, - &db.globals, - )?; - } - - Ok(set_avatar_url::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/profile/<_>/avatar_url", data = "") -)] -pub fn get_avatar_url_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - Ok(get_avatar_url::Response { - avatar_url: db.users.avatar_url(&body.user_id)?, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/profile/<_>", data = "") -)] -pub fn get_profile_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let avatar_url = db.users.avatar_url(&body.user_id)?; - let displayname = db.users.displayname(&body.user_id)?; - - if avatar_url.is_none() && displayname.is_none() { - // Return 404 if we don't have a profile for this id - return Err(Error::BadRequest( - ErrorKind::NotFound, - "Profile was not found.", - )); - } - - Ok(get_profile::Response { - avatar_url, - displayname, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/presence/<_>/status", data = "") -)] -pub fn set_presence_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - for room_id in db.rooms.rooms_joined(&sender_id) { - let room_id = room_id?; - - db.rooms.edus.update_presence( - &sender_id, - &room_id, - ruma::events::presence::PresenceEvent { - content: ruma::events::presence::PresenceEventContent { - avatar_url: db.users.avatar_url(&sender_id)?, - currently_active: None, - displayname: db.users.displayname(&sender_id)?, - last_active_ago: Some( - utils::millis_since_unix_epoch() - .try_into() - .expect("time is valid"), - ), - presence: body.presence, - status_msg: body.status_msg.clone(), - }, - sender: sender_id.clone(), - }, - &db.globals, - )?; - } - - Ok(set_presence::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/keys/upload", data = "") -)] -pub fn upload_keys_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - let device_id = body.device_id.as_ref().expect("user is authenticated"); - - if let Some(one_time_keys) = &body.one_time_keys { - for (key_key, key_value) in one_time_keys { - db.users - .add_one_time_key(sender_id, device_id, key_key, key_value, &db.globals)?; - } - } - - if let Some(device_keys) = &body.device_keys { - // This check is needed to assure that signatures are kept - if db.users.get_device_keys(sender_id, device_id)?.is_none() { - db.users - .add_device_keys(sender_id, device_id, device_keys, &db.rooms, &db.globals)?; - } - } - - Ok(upload_keys::Response { - one_time_key_counts: db.users.count_one_time_keys(sender_id, device_id)?, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/keys/query", data = "") -)] -pub fn get_keys_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let mut master_keys = BTreeMap::new(); - let mut self_signing_keys = BTreeMap::new(); - let mut user_signing_keys = BTreeMap::new(); - let mut device_keys = BTreeMap::new(); - - for (user_id, device_ids) in &body.device_keys { - if device_ids.is_empty() { - let mut container = BTreeMap::new(); - for device_id in db.users.all_device_ids(user_id) { - let device_id = device_id?; - if let Some(mut keys) = db.users.get_device_keys(user_id, &device_id)? { - let metadata = db - .users - .get_device_metadata(user_id, &device_id)? - .ok_or_else(|| { - Error::bad_database("all_device_keys contained nonexistent device.") - })?; - - keys.unsigned = Some(keys::UnsignedDeviceInfo { - device_display_name: metadata.display_name, - }); - - container.insert(device_id, keys); - } - } - device_keys.insert(user_id.clone(), container); - } else { - for device_id in device_ids { - let mut container = BTreeMap::new(); - if let Some(mut keys) = db.users.get_device_keys(&user_id.clone(), &device_id)? { - let metadata = db.users.get_device_metadata(user_id, &device_id)?.ok_or( - Error::BadRequest( - ErrorKind::InvalidParam, - "Tried to get keys for nonexistent device.", - ), - )?; - - keys.unsigned = Some(keys::UnsignedDeviceInfo { - device_display_name: metadata.display_name, - }); - - container.insert(device_id.clone(), keys); - } - device_keys.insert(user_id.clone(), container); - } - } - - if let Some(master_key) = db.users.get_master_key(user_id, sender_id)? { - master_keys.insert(user_id.clone(), master_key); - } - if let Some(self_signing_key) = db.users.get_self_signing_key(user_id, sender_id)? { - self_signing_keys.insert(user_id.clone(), self_signing_key); - } - if user_id == sender_id { - if let Some(user_signing_key) = db.users.get_user_signing_key(sender_id)? { - user_signing_keys.insert(user_id.clone(), user_signing_key); - } - } - } - - Ok(get_keys::Response { - master_keys, - self_signing_keys, - user_signing_keys, - device_keys, - failures: BTreeMap::new(), - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/keys/claim", data = "") -)] -pub fn claim_keys_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let mut one_time_keys = BTreeMap::new(); - for (user_id, map) in &body.one_time_keys { - let mut container = BTreeMap::new(); - for (device_id, key_algorithm) in map { - if let Some(one_time_keys) = - db.users - .take_one_time_key(user_id, device_id, key_algorithm, &db.globals)? - { - let mut c = BTreeMap::new(); - c.insert(one_time_keys.0, one_time_keys.1); - container.insert(device_id.clone(), c); - } - } - one_time_keys.insert(user_id.clone(), container); - } - - Ok(claim_keys::Response { - failures: BTreeMap::new(), - one_time_keys, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/unstable/room_keys/version", data = "") -)] -pub fn create_backup_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - let version = db - .key_backups - .create_backup(&sender_id, &body.algorithm, &db.globals)?; - - Ok(create_backup::Response { version }.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/unstable/room_keys/version/<_>", data = "") -)] -pub fn update_backup_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - db.key_backups - .update_backup(&sender_id, &body.version, &body.algorithm, &db.globals)?; - - Ok(update_backup::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/unstable/room_keys/version", data = "") -)] -pub fn get_latest_backup_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let (version, algorithm) = - db.key_backups - .get_latest_backup(&sender_id)? - .ok_or(Error::BadRequest( - ErrorKind::NotFound, - "Key backup does not exist.", - ))?; - - Ok(get_latest_backup::Response { - algorithm, - count: (db.key_backups.count_keys(sender_id, &version)? as u32).into(), - etag: db.key_backups.get_etag(sender_id, &version)?, - version, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/unstable/room_keys/version/<_>", data = "") -)] -pub fn get_backup_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - let algorithm = db - .key_backups - .get_backup(&sender_id, &body.version)? - .ok_or(Error::BadRequest( - ErrorKind::NotFound, - "Key backup does not exist.", - ))?; - - Ok(get_backup::Response { - algorithm, - count: (db.key_backups.count_keys(sender_id, &body.version)? as u32).into(), - etag: db.key_backups.get_etag(sender_id, &body.version)?, - version: body.version.clone(), - } - .into()) -} - -/// 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, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - for (room_id, room) in &body.rooms { - for (session_id, key_data) in &room.sessions { - db.key_backups.add_key( - &sender_id, - &body.version, - &room_id, - &session_id, - &key_data, - &db.globals, - )? - } - } - - Ok(add_backup_keys::Response { - count: (db.key_backups.count_keys(sender_id, &body.version)? as u32).into(), - etag: db.key_backups.get_etag(sender_id, &body.version)?, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/unstable/room_keys/keys", data = "") -)] -pub fn get_backup_keys_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let rooms = db.key_backups.get_all(&sender_id, &body.version)?; - - Ok(get_backup_keys::Response { rooms }.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/rooms/<_>/read_markers", data = "") -)] -pub fn set_read_marker_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let fully_read_event = ruma::events::fully_read::FullyReadEvent { - content: ruma::events::fully_read::FullyReadEventContent { - event_id: body.fully_read.clone(), - }, - room_id: body.room_id.clone(), - }; - db.account_data.update( - Some(&body.room_id), - &sender_id, - EventType::FullyRead, - &fully_read_event, - &db.globals, - )?; - - if let Some(event) = &body.read_receipt { - db.rooms.edus.room_read_set( - &body.room_id, - &sender_id, - db.rooms.get_pdu_count(event)?.ok_or(Error::BadRequest( - ErrorKind::InvalidParam, - "Event does not exist.", - ))?, - )?; - - let mut user_receipts = BTreeMap::new(); - user_receipts.insert( - sender_id.clone(), - ruma::events::receipt::Receipt { - ts: Some(SystemTime::now()), - }, - ); - let mut receipt_content = BTreeMap::new(); - receipt_content.insert( - event.clone(), - ruma::events::receipt::Receipts { - read: Some(user_receipts), - }, - ); - - db.rooms.edus.roomlatest_update( - &sender_id, - &body.room_id, - AnyEvent::Ephemeral(AnyEphemeralRoomEvent::Receipt( - ruma::events::receipt::ReceiptEvent { - content: ruma::events::receipt::ReceiptEventContent(receipt_content), - room_id: body.room_id.clone(), - }, - )), - &db.globals, - )?; - } - Ok(set_read_marker::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/rooms/<_>/typing/<_>", data = "") -)] -pub fn create_typing_event_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - if body.typing { - db.rooms.edus.roomactive_add( - &sender_id, - &body.room_id, - body.timeout.map(|d| d.as_millis() as u64).unwrap_or(30000) - + utils::millis_since_unix_epoch(), - &db.globals, - )?; - } else { - db.rooms - .edus - .roomactive_remove(&sender_id, &body.room_id, &db.globals)?; - } - - Ok(create_typing_event::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/createRoom", data = "") -)] -pub fn create_room_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let room_id = RoomId::new(db.globals.server_name()); - - let alias = body - .room_alias_name - .as_ref() - .map_or(Ok(None), |localpart| { - // TODO: Check for invalid characters and maximum length - let alias = - RoomAliasId::try_from(format!("#{}:{}", localpart, db.globals.server_name())) - .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?; - - if db.rooms.id_from_alias(&alias)?.is_some() { - Err(Error::BadRequest( - ErrorKind::RoomInUse, - "Room alias already exists.", - )) - } else { - Ok(Some(alias)) - } - })?; - - let mut content = ruma::events::room::create::CreateEventContent::new(sender_id.clone()); - content.federate = body.creation_content.as_ref().map_or(true, |c| c.federate); - content.predecessor = body - .creation_content - .as_ref() - .and_then(|c| c.predecessor.clone()); - content.room_version = RoomVersionId::Version6; - - // 1. The room create event - db.rooms.append_pdu( - PduBuilder { - room_id: room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomCreate, - content: serde_json::to_value(content).expect("event is valid, we just created it"), - unsigned: None, - state_key: Some("".to_owned()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - // 2. Let the room creator join - db.rooms.append_pdu( - PduBuilder { - room_id: room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomMember, - content: serde_json::to_value(member::MemberEventContent { - membership: member::MembershipState::Join, - displayname: db.users.displayname(&sender_id)?, - avatar_url: db.users.avatar_url(&sender_id)?, - is_direct: body.is_direct, - third_party_invite: None, - }) - .expect("event is valid, we just created it"), - unsigned: None, - state_key: Some(sender_id.to_string()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - // Figure out preset. We need it for power levels and preset specific events - let visibility = body.visibility.unwrap_or(room::Visibility::Private); - let preset = body.preset.unwrap_or_else(|| match visibility { - room::Visibility::Private => create_room::RoomPreset::PrivateChat, - room::Visibility::Public => create_room::RoomPreset::PublicChat, - }); - - // 3. Power levels - let mut users = BTreeMap::new(); - users.insert(sender_id.clone(), 100.into()); - for invite_ in &body.invite { - users.insert(invite_.clone(), 100.into()); - } - - let power_levels_content = if let Some(power_levels) = &body.power_level_content_override { - serde_json::from_str(power_levels.json().get()).map_err(|_| { - Error::BadRequest(ErrorKind::BadJson, "Invalid power_level_content_override.") - })? - } else { - serde_json::to_value(ruma::events::room::power_levels::PowerLevelsEventContent { - ban: 50.into(), - events: BTreeMap::new(), - events_default: 0.into(), - invite: 50.into(), - kick: 50.into(), - redact: 50.into(), - state_default: 50.into(), - users, - users_default: 0.into(), - notifications: ruma::events::room::power_levels::NotificationPowerLevels { - room: 50.into(), - }, - }) - .expect("event is valid, we just created it") - }; - db.rooms.append_pdu( - PduBuilder { - room_id: room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomPowerLevels, - content: power_levels_content, - unsigned: None, - state_key: Some("".to_owned()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - // 4. Events set by preset - // 4.1 Join Rules - db.rooms.append_pdu( - PduBuilder { - room_id: room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomJoinRules, - content: match preset { - create_room::RoomPreset::PublicChat => serde_json::to_value( - join_rules::JoinRulesEventContent::new(join_rules::JoinRule::Public), - ) - .expect("event is valid, we just created it"), - // according to spec "invite" is the default - _ => serde_json::to_value(join_rules::JoinRulesEventContent::new( - join_rules::JoinRule::Invite, - )) - .expect("event is valid, we just created it"), - }, - unsigned: None, - state_key: Some("".to_owned()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - // 4.2 History Visibility - db.rooms.append_pdu( - PduBuilder { - room_id: room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomHistoryVisibility, - content: serde_json::to_value(history_visibility::HistoryVisibilityEventContent::new( - history_visibility::HistoryVisibility::Shared, - )) - .expect("event is valid, we just created it"), - unsigned: None, - state_key: Some("".to_owned()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - // 4.3 Guest Access - db.rooms.append_pdu( - PduBuilder { - room_id: room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomGuestAccess, - content: match preset { - create_room::RoomPreset::PublicChat => { - serde_json::to_value(guest_access::GuestAccessEventContent::new( - guest_access::GuestAccess::Forbidden, - )) - .expect("event is valid, we just created it") - } - _ => serde_json::to_value(guest_access::GuestAccessEventContent::new( - guest_access::GuestAccess::CanJoin, - )) - .expect("event is valid, we just created it"), - }, - unsigned: None, - state_key: Some("".to_owned()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - // 5. Events listed in initial_state - for create_room::InitialStateEvent { - event_type, - state_key, - content, - } in &body.initial_state - { - // Silently skip encryption events if they are not allowed - if event_type == &EventType::RoomEncryption && db.globals.encryption_disabled() { - continue; - } - - db.rooms.append_pdu( - PduBuilder { - room_id: room_id.clone(), - sender: sender_id.clone(), - event_type: event_type.clone(), - content: serde_json::from_str(content.get()).map_err(|_| { - Error::BadRequest(ErrorKind::BadJson, "Invalid initial_state content.") - })?, - unsigned: None, - state_key: state_key.clone(), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - } - - // 6. Events implied by name and topic - if let Some(name) = &body.name { - db.rooms.append_pdu( - PduBuilder { - room_id: room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomName, - content: serde_json::to_value( - name::NameEventContent::new(name.clone()).map_err(|_| { - Error::BadRequest(ErrorKind::InvalidParam, "Name is invalid.") - })?, - ) - .expect("event is valid, we just created it"), - unsigned: None, - state_key: Some("".to_owned()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - } - - if let Some(topic) = &body.topic { - db.rooms.append_pdu( - PduBuilder { - room_id: room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomTopic, - content: serde_json::to_value(topic::TopicEventContent { - topic: topic.clone(), - }) - .expect("event is valid, we just created it"), - unsigned: None, - state_key: Some("".to_owned()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - } - - // 7. Events implied by invite (and TODO: invite_3pid) - for user in &body.invite { - db.rooms.append_pdu( - PduBuilder { - room_id: room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomMember, - content: serde_json::to_value(member::MemberEventContent { - membership: member::MembershipState::Invite, - displayname: db.users.displayname(&user)?, - avatar_url: db.users.avatar_url(&user)?, - is_direct: body.is_direct, - third_party_invite: None, - }) - .expect("event is valid, we just created it"), - unsigned: None, - state_key: Some(user.to_string()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - } - - // Homeserver specific stuff - if let Some(alias) = alias { - db.rooms.set_alias(&alias, Some(&room_id), &db.globals)?; - } - - if let Some(room::Visibility::Public) = body.visibility { - db.rooms.set_public(&room_id, true)?; - } - - Ok(create_room::Response { room_id }.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/joined_rooms", data = "") -)] -pub fn joined_rooms_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - Ok(joined_rooms::Response { - joined_rooms: db - .rooms - .rooms_joined(&sender_id) - .filter_map(|r| r.ok()) - .collect(), - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/rooms/<_>/redact/<_>/<_>", data = "") -)] -pub fn redact_event_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let event_id = db.rooms.append_pdu( - PduBuilder { - room_id: body.room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomRedaction, - content: serde_json::to_value(redaction::RedactionEventContent { - reason: body.reason.clone(), - }) - .expect("event is valid, we just created it"), - unsigned: None, - state_key: None, - redacts: Some(body.event_id.clone()), - }, - &db.globals, - &db.account_data, - )?; - - Ok(redact_event::Response { event_id }.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/directory/room/<_>", data = "") -)] -pub fn create_alias_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - if db.rooms.id_from_alias(&body.room_alias)?.is_some() { - return Err(Error::Conflict("Alias already exists.")); - } - - db.rooms - .set_alias(&body.room_alias, Some(&body.room_id), &db.globals)?; - - Ok(create_alias::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - delete("/_matrix/client/r0/directory/room/<_>", data = "") -)] -pub fn delete_alias_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - db.rooms.set_alias(&body.room_alias, None, &db.globals)?; - - Ok(delete_alias::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/directory/room/<_>", data = "") -)] -pub fn get_alias_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - if body.room_alias.server_name() != db.globals.server_name() { - todo!("ask remote server"); - } - - let room_id = db - .rooms - .id_from_alias(&body.room_alias)? - .ok_or(Error::BadRequest( - ErrorKind::NotFound, - "Room with alias not found.", - ))?; - - Ok(get_alias::Response { - room_id, - servers: vec![db.globals.server_name().to_string()], - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/rooms/<_>/join", data = "") -)] -pub fn join_room_by_id_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - // TODO: Ask a remote server if we don't have this room - - let event = member::MemberEventContent { - membership: member::MembershipState::Join, - displayname: db.users.displayname(&sender_id)?, - avatar_url: db.users.avatar_url(&sender_id)?, - is_direct: None, - third_party_invite: None, - }; - - db.rooms.append_pdu( - PduBuilder { - room_id: body.room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomMember, - content: serde_json::to_value(event).expect("event is valid, we just created it"), - unsigned: None, - state_key: Some(sender_id.to_string()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - Ok(join_room_by_id::Response { - room_id: body.room_id.clone(), - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/join/<_>", data = "") -)] -pub fn join_room_by_id_or_alias_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let room_id = RoomId::try_from(body.room_id_or_alias.clone()).or_else(|alias| { - Ok::<_, Error>(db.rooms.id_from_alias(&alias)?.ok_or(Error::BadRequest( - ErrorKind::NotFound, - "Room not found (TODO: Federation).", - ))?) - })?; - - let body = Ruma { - sender_id: body.sender_id.clone(), - device_id: body.device_id.clone(), - json_body: None, - body: join_room_by_id::Request { - room_id, - third_party_signed: body.third_party_signed.clone(), - }, - }; - - Ok(join_room_by_id_or_alias::Response { - room_id: join_room_by_id_route(db, body)?.0.room_id, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/rooms/<_>/leave", data = "") -)] -pub fn leave_room_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let mut event = serde_json::from_value::>( - db.rooms - .room_state_get( - &body.room_id, - &EventType::RoomMember, - &sender_id.to_string(), - )? - .ok_or(Error::BadRequest( - ErrorKind::BadState, - "Cannot leave a room you are not a member of.", - ))? - .content, - ) - .expect("from_value::> can never fail") - .deserialize() - .map_err(|_| Error::bad_database("Invalid member event in database."))?; - - event.membership = member::MembershipState::Leave; - - db.rooms.append_pdu( - PduBuilder { - room_id: body.room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomMember, - content: serde_json::to_value(event).expect("event is valid, we just created it"), - unsigned: None, - state_key: Some(sender_id.to_string()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - Ok(leave_room::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/rooms/<_>/kick", data = "") -)] -pub fn kick_user_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let mut event = serde_json::from_value::>( - db.rooms - .room_state_get( - &body.room_id, - &EventType::RoomMember, - &body.user_id.to_string(), - )? - .ok_or(Error::BadRequest( - ErrorKind::BadState, - "Cannot kick member that's not in the room.", - ))? - .content, - ) - .expect("Raw::from_value always works") - .deserialize() - .map_err(|_| Error::bad_database("Invalid member event in database."))?; - - event.membership = ruma::events::room::member::MembershipState::Leave; - // TODO: reason - - db.rooms.append_pdu( - PduBuilder { - room_id: body.room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomMember, - content: serde_json::to_value(event).expect("event is valid, we just created it"), - unsigned: None, - state_key: Some(body.user_id.to_string()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - Ok(kick_user::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/rooms/<_>/joined_members", data = "") -)] -pub fn joined_members_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - if !db - .rooms - .is_joined(&sender_id, &body.room_id) - .unwrap_or(false) - { - return Err(Error::BadRequest( - ErrorKind::Forbidden, - "You aren't a member of the room.", - )); - } - - let mut joined = BTreeMap::new(); - for user_id in db.rooms.room_members(&body.room_id).filter_map(|r| r.ok()) { - let display_name = db.users.displayname(&user_id)?; - let avatar_url = db.users.avatar_url(&user_id)?; - - joined.insert( - user_id, - joined_members::RoomMember { - display_name, - avatar_url, - }, - ); - } - - Ok(joined_members::Response { joined }.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/rooms/<_>/ban", data = "") -)] -pub fn ban_user_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - // TODO: reason - - let event = db - .rooms - .room_state_get( - &body.room_id, - &EventType::RoomMember, - &body.user_id.to_string(), - )? - .map_or( - Ok::<_, Error>(member::MemberEventContent { - membership: member::MembershipState::Ban, - displayname: db.users.displayname(&body.user_id)?, - avatar_url: db.users.avatar_url(&body.user_id)?, - is_direct: None, - third_party_invite: None, - }), - |event| { - let mut event = - serde_json::from_value::>(event.content) - .expect("Raw::from_value always works") - .deserialize() - .map_err(|_| Error::bad_database("Invalid member event in database."))?; - event.membership = ruma::events::room::member::MembershipState::Ban; - Ok(event) - }, - )?; - - db.rooms.append_pdu( - PduBuilder { - room_id: body.room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomMember, - content: serde_json::to_value(event).expect("event is valid, we just created it"), - unsigned: None, - state_key: Some(body.user_id.to_string()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - Ok(ban_user::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/rooms/<_>/unban", data = "") -)] -pub fn unban_user_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let mut event = serde_json::from_value::>( - db.rooms - .room_state_get( - &body.room_id, - &EventType::RoomMember, - &body.user_id.to_string(), - )? - .ok_or(Error::BadRequest( - ErrorKind::BadState, - "Cannot unban a user who is not banned.", - ))? - .content, - ) - .expect("from_value::> can never fail") - .deserialize() - .map_err(|_| Error::bad_database("Invalid member event in database."))?; - - event.membership = ruma::events::room::member::MembershipState::Leave; - - db.rooms.append_pdu( - PduBuilder { - room_id: body.room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomMember, - content: serde_json::to_value(event).expect("event is valid, we just created it"), - unsigned: None, - state_key: Some(body.user_id.to_string()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - Ok(unban_user::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/rooms/<_>/forget", data = "") -)] -pub fn forget_room_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - db.rooms.forget(&body.room_id, &sender_id)?; - - Ok(forget_room::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/rooms/<_>/invite", data = "") -)] -pub fn invite_user_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - if let invite_user::InvitationRecipient::UserId { user_id } = &body.recipient { - db.rooms.append_pdu( - PduBuilder { - room_id: body.room_id.clone(), - sender: sender_id.clone(), - event_type: EventType::RoomMember, - content: serde_json::to_value(member::MemberEventContent { - membership: member::MembershipState::Invite, - displayname: db.users.displayname(&user_id)?, - avatar_url: db.users.avatar_url(&user_id)?, - is_direct: None, - third_party_invite: None, - }) - .expect("event is valid, we just created it"), - unsigned: None, - state_key: Some(user_id.to_string()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - Ok(invite_user::Response.into()) - } else { - Err(Error::BadRequest(ErrorKind::NotFound, "User not found.")) - } -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/directory/list/room/<_>", data = "") -)] -pub async fn set_room_visibility_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - match body.visibility { - room::Visibility::Public => db.rooms.set_public(&body.room_id, true)?, - room::Visibility::Private => db.rooms.set_public(&body.room_id, false)?, - } - - Ok(set_room_visibility::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/directory/list/room/<_>", data = "") -)] -pub async fn get_room_visibility_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - Ok(get_room_visibility::Response { - visibility: if db.rooms.is_public_room(&body.room_id)? { - room::Visibility::Public - } else { - room::Visibility::Private - }, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/publicRooms", data = "") -)] -pub async fn get_public_rooms_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let Ruma { - body: - get_public_rooms::Request { - limit, - server, - since, - }, - sender_id, - device_id, - json_body, - } = body; - - let get_public_rooms_filtered::Response { - chunk, - prev_batch, - next_batch, - total_room_count_estimate, - } = get_public_rooms_filtered_route( - db, - Ruma { - body: get_public_rooms_filtered::Request { - filter: None, - limit, - room_network: get_public_rooms_filtered::RoomNetwork::Matrix, - server, - since, - }, - sender_id, - device_id, - json_body, - }, - ) - .await? - .0; - - Ok(get_public_rooms::Response { - chunk, - prev_batch, - next_batch, - total_room_count_estimate, - } - .into()) -} - -#[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, -) -> ConduitResult { - use ruma::events::room; - - let mut chunk = db - .rooms - .public_rooms() - .map(|room_id| { - let room_id = room_id?; - - // TODO: Do not load full state? - let state = db.rooms.room_state_full(&room_id)?; - - let chunk = directory::PublicRoomsChunk { - aliases: Vec::new(), - canonical_alias: state - .get(&(EventType::RoomCanonicalAlias, "".to_owned())) - .map_or(Ok::<_, Error>(None), |s| { - Ok(serde_json::from_value::< - Raw, - >(s.content.clone()) - .expect("from_value::> can never fail") - .deserialize() - .map_err(|_| { - Error::bad_database("Invalid canonical alias event in database.") - })? - .alias) - })?, - name: state.get(&(EventType::RoomName, "".to_owned())).map_or( - Ok::<_, Error>(None), - |s| { - Ok(serde_json::from_value::>( - s.content.clone(), - ) - .expect("from_value::> can never fail") - .deserialize() - .map_err(|_| Error::bad_database("Invalid room name event in database."))? - .name() - .map(|n| n.to_owned())) - }, - )?, - num_joined_members: (db.rooms.room_members(&room_id).count() as u32).into(), - room_id, - topic: state.get(&(EventType::RoomTopic, "".to_owned())).map_or( - Ok::<_, Error>(None), - |s| { - Ok(Some( - serde_json::from_value::>( - s.content.clone(), - ) - .expect("from_value::> can never fail") - .deserialize() - .map_err(|_| { - Error::bad_database("Invalid room topic event in database.") - })? - .topic, - )) - }, - )?, - world_readable: state - .get(&(EventType::RoomHistoryVisibility, "".to_owned())) - .map_or(Ok::<_, Error>(false), |s| { - Ok(serde_json::from_value::< - Raw, - >(s.content.clone()) - .expect("from_value::> can never fail") - .deserialize() - .map_err(|_| { - Error::bad_database( - "Invalid room history visibility event in database.", - ) - })? - .history_visibility - == history_visibility::HistoryVisibility::WorldReadable) - })?, - guest_can_join: state - .get(&(EventType::RoomGuestAccess, "".to_owned())) - .map_or(Ok::<_, Error>(false), |s| { - Ok( - serde_json::from_value::< - Raw, - >(s.content.clone()) - .expect("from_value::> can never fail") - .deserialize() - .map_err(|_| { - Error::bad_database("Invalid room guest access event in database.") - })? - .guest_access - == guest_access::GuestAccess::CanJoin, - ) - })?, - avatar_url: state.get(&(EventType::RoomAvatar, "".to_owned())).map_or( - Ok::<_, Error>(None), - |s| { - Ok(Some( - serde_json::from_value::>( - s.content.clone(), - ) - .expect("from_value::> can never fail") - .deserialize() - .map_err(|_| { - Error::bad_database("Invalid room avatar event in database.") - })? - .url, - )) - }, - )?, - }; - Ok::<_, Error>(chunk) - }) - .filter_map(|r| r.ok()) // Filter out buggy rooms - // We need to collect all, so we can sort by member count - .collect::>(); - - chunk.sort_by(|l, r| r.num_joined_members.cmp(&l.num_joined_members)); - - /* - chunk.extend_from_slice( - &server_server::send_request( - &db, - "privacytools.io".to_owned(), - ruma::api::federation::v1::get_public_rooms::Request { - limit: Some(20_u32.into()), - since: None, - room_network: ruma::api::federation::v1::get_public_rooms::RoomNetwork::Matrix, - }, - ) - .await - ? - .chunk - .into_iter() - .map(|c| serde_json::from_str(&serde_json::to_string(&c)?)?) - .collect::>(), - ); - */ - - let total_room_count_estimate = (chunk.len() as u32).into(); - - Ok(get_public_rooms_filtered::Response { - chunk, - prev_batch: None, - next_batch: None, - total_room_count_estimate: Some(total_room_count_estimate), - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/user_directory/search", data = "") -)] -pub fn search_users_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let limit = if let Some(limit) = body.limit { - u64::from(limit) - } else { - 10 - } as usize; - - let mut users = db.users.iter().filter_map(|user_id| { - // Filter out buggy users (they should not exist, but you never know...) - let user_id = user_id.ok()?; - if db.users.is_deactivated(&user_id).ok()? { - return None; - } - - let user = search_users::User { - user_id: user_id.clone(), - display_name: db.users.displayname(&user_id).ok()?, - avatar_url: db.users.avatar_url(&user_id).ok()?, - }; - - if !user.user_id.to_string().contains(&body.search_term) - && user - .display_name - .as_ref() - .filter(|name| name.contains(&body.search_term)) - .is_none() - { - return None; - } - - Some(user) - }); - - let results = users.by_ref().take(limit).collect(); - let limited = users.next().is_some(); - - Ok(search_users::Response { results, limited }.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/rooms/<_>/members", data = "") -)] -pub fn get_member_events_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - if !db.rooms.is_joined(sender_id, &body.room_id)? { - return Err(Error::BadRequest( - ErrorKind::Forbidden, - "You don't have permission to view this room.", - )); - } - - Ok(get_member_events::Response { - chunk: db - .rooms - .room_state_type(&body.room_id, &EventType::RoomMember)? - .values() - .map(|pdu| pdu.to_member_event()) - .collect(), - } - .into()) -} - -#[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 { - protocols: BTreeMap::new(), - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/rooms/<_>/event/<_>", data = "") -)] -pub fn get_room_event_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - if !db.rooms.is_joined(sender_id, &body.room_id)? { - return Err(Error::BadRequest( - ErrorKind::Forbidden, - "You don't have permission to view this room.", - )); - } - - Ok(get_room_event::Response { - event: db - .rooms - .get_pdu(&body.event_id)? - .ok_or(Error::BadRequest(ErrorKind::NotFound, "Event not found."))? - .to_room_event(), - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/rooms/<_>/send/<_>/<_>", data = "") -)] -pub fn create_message_event_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let mut unsigned = serde_json::Map::new(); - unsigned.insert("transaction_id".to_owned(), body.txn_id.clone().into()); - - let event_id = db.rooms.append_pdu( - PduBuilder { - room_id: body.room_id.clone(), - sender: sender_id.clone(), - event_type: body.event_type.clone(), - content: serde_json::from_str( - body.json_body - .ok_or(Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))? - .get(), - ) - .map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?, - unsigned: Some(unsigned), - state_key: None, - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - Ok(create_message_event::Response { event_id }.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/rooms/<_>/state/<_>/<_>", data = "") -)] -pub fn create_state_event_for_key_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let content = serde_json::from_str::( - body.json_body - .as_ref() - .ok_or(Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))? - .get(), - ) - .map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?; - - if body.event_type == EventType::RoomCanonicalAlias { - let canonical_alias = serde_json::from_value::< - Raw, - >(content.clone()) - .expect("from_value::> can never fail") - .deserialize() - .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid canonical alias."))?; - - let mut aliases = canonical_alias.alt_aliases; - - if let Some(alias) = canonical_alias.alias { - aliases.push(alias); - } - - for alias in aliases { - if alias.server_name() != db.globals.server_name() - || db - .rooms - .id_from_alias(&alias)? - .filter(|room| room == &body.room_id) // Make sure it's the right room - .is_none() - { - return Err(Error::BadRequest( - ErrorKind::Forbidden, - "You are only allowed to send canonical_alias \ - events when it's aliases already exists", - )); - } - } - } - - let event_id = db.rooms.append_pdu( - PduBuilder { - room_id: body.room_id.clone(), - sender: sender_id.clone(), - event_type: body.event_type.clone(), - content, - unsigned: None, - state_key: Some(body.state_key.clone()), - redacts: None, - }, - &db.globals, - &db.account_data, - )?; - - Ok(create_state_event_for_key::Response { event_id }.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/rooms/<_>/state/<_>", data = "") -)] -pub fn create_state_event_for_empty_key_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - // This just calls create_state_event_for_key_route - let Ruma { - body: - create_state_event_for_empty_key::Request { - room_id, - event_type, - data, - }, - sender_id, - device_id, - json_body, - } = body; - - Ok(create_state_event_for_empty_key::Response { - event_id: create_state_event_for_key_route( - db, - Ruma { - body: create_state_event_for_key::Request { - room_id, - event_type, - data, - state_key: "".to_owned(), - }, - sender_id, - device_id, - json_body, - }, - )? - .0 - .event_id, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/rooms/<_>/state", data = "") -)] -pub fn get_state_events_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - if !db.rooms.is_joined(sender_id, &body.room_id)? { - return Err(Error::BadRequest( - ErrorKind::Forbidden, - "You don't have permission to view the room state.", - )); - } - - Ok(get_state_events::Response { - room_state: db - .rooms - .room_state_full(&body.room_id)? - .values() - .map(|pdu| pdu.to_state_event()) - .collect(), - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/rooms/<_>/state/<_>/<_>", data = "") -)] -pub fn get_state_events_for_key_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - if !db.rooms.is_joined(sender_id, &body.room_id)? { - return Err(Error::BadRequest( - ErrorKind::Forbidden, - "You don't have permission to view the room state.", - )); - } - - let event = db - .rooms - .room_state_get(&body.room_id, &body.event_type, &body.state_key)? - .ok_or(Error::BadRequest( - ErrorKind::NotFound, - "State event not found.", - ))?; - - Ok(get_state_events_for_key::Response { - content: serde_json::value::to_raw_value(&event.content) - .map_err(|_| Error::bad_database("Invalid event content in database"))?, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/rooms/<_>/state/<_>", data = "") -)] -pub fn get_state_events_for_empty_key_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - if !db.rooms.is_joined(sender_id, &body.room_id)? { - return Err(Error::BadRequest( - ErrorKind::Forbidden, - "You don't have permission to view the room state.", - )); - } - - let event = db - .rooms - .room_state_get(&body.room_id, &body.event_type, "")? - .ok_or(Error::BadRequest( - ErrorKind::NotFound, - "State event not found.", - ))?; - - Ok(get_state_events_for_empty_key::Response { - content: serde_json::value::to_raw_value(&event) - .map_err(|_| Error::bad_database("Invalid event content in database"))?, - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/sync", data = "") -)] -pub async fn sync_events_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - let device_id = body.device_id.as_ref().expect("user is authenticated"); - - // TODO: match body.set_presence { - db.rooms.edus.ping_presence(&sender_id)?; - - // Setup watchers, so if there's no response, we can wait for them - let watcher = db.watch(sender_id, device_id); - - let next_batch = db.globals.current_count()?.to_string(); - - let mut joined_rooms = BTreeMap::new(); - let since = body - .since - .clone() - .and_then(|string| string.parse().ok()) - .unwrap_or(0); - - let mut presence_updates = HashMap::new(); - let mut device_list_updates = HashSet::new(); - - // Look for device list updates of this account - device_list_updates.extend( - db.users - .keys_changed(&sender_id.to_string(), since, None) - .filter_map(|r| r.ok()), - ); - - for room_id in db.rooms.rooms_joined(&sender_id) { - let room_id = room_id?; - - let mut non_timeline_pdus = db - .rooms - .pdus_since(&sender_id, &room_id, since)? - .filter_map(|r| r.ok()); // Filter out buggy events - - // Take the last 10 events for the timeline - let timeline_pdus = non_timeline_pdus - .by_ref() - .rev() - .take(10) - .collect::>() - .into_iter() - .rev() - .collect::>(); - - // They /sync response doesn't always return all messages, so we say the output is - // limited unless there are events in non_timeline_pdus - //let mut limited = false; - - let mut state_pdus = Vec::new(); - for pdu in non_timeline_pdus { - if pdu.state_key.is_some() { - state_pdus.push(pdu); - } - } - - let mut send_member_count = false; - let mut joined_since_last_sync = false; - let mut send_notification_counts = false; - for pdu in db - .rooms - .pdus_since(&sender_id, &room_id, since)? - .filter_map(|r| r.ok()) - { - send_notification_counts = true; - if pdu.kind == EventType::RoomMember { - send_member_count = true; - if !joined_since_last_sync && pdu.state_key == Some(sender_id.to_string()) { - let content = serde_json::from_value::< - Raw, - >(pdu.content.clone()) - .expect("Raw::from_value always works") - .deserialize() - .map_err(|_| Error::bad_database("Invalid PDU in database."))?; - if content.membership == ruma::events::room::member::MembershipState::Join { - joined_since_last_sync = true; - // Both send_member_count and joined_since_last_sync are set. There's - // nothing more to do - break; - } - } - } - } - - let members = db.rooms.room_state_type(&room_id, &EventType::RoomMember)?; - - let (joined_member_count, invited_member_count, heroes) = if send_member_count { - let joined_member_count = db.rooms.room_members(&room_id).count(); - let invited_member_count = db.rooms.room_members_invited(&room_id).count(); - - // Recalculate heroes (first 5 members) - let mut heroes = Vec::new(); - - if joined_member_count + invited_member_count <= 5 { - // Go through all PDUs and for each member event, check if the user is still joined or - // invited until we have 5 or we reach the end - - for hero in db - .rooms - .all_pdus(&sender_id, &room_id)? - .filter_map(|pdu| pdu.ok()) // Ignore all broken pdus - .filter(|pdu| pdu.kind == EventType::RoomMember) - .map(|pdu| { - let content = serde_json::from_value::< - Raw, - >(pdu.content.clone()) - .expect("Raw::from_value always works") - .deserialize() - .map_err(|_| Error::bad_database("Invalid member event in database."))?; - - if let Some(state_key) = &pdu.state_key { - let current_content = serde_json::from_value::< - Raw, - >( - members - .get(state_key) - .ok_or_else(|| { - Error::bad_database( - "A user that joined once has no member event anymore.", - ) - })? - .content - .clone(), - ) - .expect("Raw::from_value always works") - .deserialize() - .map_err(|_| { - Error::bad_database("Invalid member event in database.") - })?; - - // The membership was and still is invite or join - if matches!( - content.membership, - ruma::events::room::member::MembershipState::Join - | ruma::events::room::member::MembershipState::Invite - ) && matches!( - current_content.membership, - ruma::events::room::member::MembershipState::Join - | ruma::events::room::member::MembershipState::Invite - ) { - Ok::<_, Error>(Some(state_key.clone())) - } else { - Ok(None) - } - } else { - Ok(None) - } - }) - .filter_map(|u| u.ok()) // Filter out buggy users - // Filter for possible heroes - .filter_map(|u| u) - { - if heroes.contains(&hero) || hero == sender_id.as_str() { - continue; - } - - heroes.push(hero); - } - } - - ( - Some(joined_member_count), - Some(invited_member_count), - heroes, - ) - } else { - (None, None, Vec::new()) - }; - - let notification_count = if send_notification_counts { - if let Some(last_read) = db.rooms.edus.room_read_get(&room_id, &sender_id)? { - Some( - (db.rooms - .pdus_since(&sender_id, &room_id, last_read)? - .filter_map(|pdu| pdu.ok()) // Filter out buggy events - .filter(|pdu| { - matches!( - pdu.kind.clone(), - EventType::RoomMessage | EventType::RoomEncrypted - ) - }) - .count() as u32) - .into(), - ) - } else { - None - } - } else { - None - }; - - let prev_batch = timeline_pdus.first().map_or(Ok::<_, Error>(None), |e| { - Ok(Some( - db.rooms - .get_pdu_count(&e.event_id)? - .ok_or_else(|| Error::bad_database("Can't find count from event in db."))? - .to_string(), - )) - })?; - - let room_events = timeline_pdus - .into_iter() - .map(|pdu| pdu.to_sync_room_event()) - .collect::>(); - - let mut edus = db - .rooms - .edus - .roomlatests_since(&room_id, since)? - .filter_map(|r| r.ok()) // Filter out buggy events - .collect::>(); - - if db - .rooms - .edus - .last_roomactive_update(&room_id, &db.globals)? - > since - { - edus.push( - serde_json::from_str( - &serde_json::to_string(&AnySyncEphemeralRoomEvent::Typing( - db.rooms.edus.roomactives_all(&room_id)?, - )) - .expect("event is valid, we just created it"), - ) - .expect("event is valid, we just created it"), - ); - } - - let joined_room = sync_events::JoinedRoom { - account_data: sync_events::AccountData { - events: db - .account_data - .changes_since(Some(&room_id), &sender_id, since)? - .into_iter() - .filter_map(|(_, v)| { - serde_json::from_str(v.json().get()) - .map_err(|_| Error::bad_database("Invalid account event in database.")) - .ok() - }) - .collect::>(), - }, - summary: sync_events::RoomSummary { - heroes, - joined_member_count: joined_member_count.map(|n| (n as u32).into()), - invited_member_count: invited_member_count.map(|n| (n as u32).into()), - }, - unread_notifications: sync_events::UnreadNotificationsCount { - highlight_count: None, - notification_count, - }, - timeline: sync_events::Timeline { - limited: joined_since_last_sync, - prev_batch, - events: room_events, - }, - // TODO: state before timeline - state: sync_events::State { - events: if joined_since_last_sync { - db.rooms - .room_state_full(&room_id)? - .into_iter() - .map(|(_, pdu)| pdu.to_sync_state_event()) - .collect() - } else { - Vec::new() - }, - }, - ephemeral: sync_events::Ephemeral { events: edus }, - }; - - if !joined_room.is_empty() { - joined_rooms.insert(room_id.clone(), joined_room); - } - - // Look for device list updates in this room - device_list_updates.extend( - db.users - .keys_changed(&room_id.to_string(), since, None) - .filter_map(|r| r.ok()), - ); - - // Take presence updates from this room - for (user_id, presence) in - db.rooms - .edus - .presence_since(&room_id, since, &db.rooms, &db.globals)? - { - match presence_updates.entry(user_id) { - hash_map::Entry::Vacant(v) => { - v.insert(presence); - } - hash_map::Entry::Occupied(mut o) => { - let p = o.get_mut(); - - // Update existing presence event with more info - p.content.presence = presence.content.presence; - if let Some(status_msg) = presence.content.status_msg { - p.content.status_msg = Some(status_msg); - } - if let Some(last_active_ago) = presence.content.last_active_ago { - p.content.last_active_ago = Some(last_active_ago); - } - if let Some(displayname) = presence.content.displayname { - p.content.displayname = Some(displayname); - } - if let Some(avatar_url) = presence.content.avatar_url { - p.content.avatar_url = Some(avatar_url); - } - if let Some(currently_active) = presence.content.currently_active { - p.content.currently_active = Some(currently_active); - } - } - } - } - } - - let mut left_rooms = BTreeMap::new(); - for room_id in db.rooms.rooms_left(&sender_id) { - let room_id = room_id?; - let pdus = db.rooms.pdus_since(&sender_id, &room_id, since)?; - let room_events = pdus - .filter_map(|pdu| pdu.ok()) // Filter out buggy events - .map(|pdu| pdu.to_sync_room_event()) - .collect(); - - // TODO: Only until leave point - let mut edus = db - .rooms - .edus - .roomlatests_since(&room_id, since)? - .filter_map(|r| r.ok()) // Filter out buggy events - .collect::>(); - - if db - .rooms - .edus - .last_roomactive_update(&room_id, &db.globals)? - > since - { - edus.push( - serde_json::from_str( - &serde_json::to_string(&AnySyncEphemeralRoomEvent::Typing( - db.rooms.edus.roomactives_all(&room_id)?, - )) - .expect("event is valid, we just created it"), - ) - .expect("event is valid, we just created it"), - ); - } - - let left_room = sync_events::LeftRoom { - account_data: sync_events::AccountData { events: Vec::new() }, - timeline: sync_events::Timeline { - limited: false, - prev_batch: Some(next_batch.clone()), - events: room_events, - }, - state: sync_events::State { events: Vec::new() }, - }; - - if !left_room.is_empty() { - left_rooms.insert(room_id.clone(), left_room); - } - } - - let mut invited_rooms = BTreeMap::new(); - for room_id in db.rooms.rooms_invited(&sender_id) { - let room_id = room_id?; - - let invited_room = sync_events::InvitedRoom { - invite_state: sync_events::InviteState { - events: db - .rooms - .room_state_full(&room_id)? - .into_iter() - .map(|(_, pdu)| pdu.to_stripped_state_event()) - .collect(), - }, - }; - - if !invited_room.is_empty() { - invited_rooms.insert(room_id.clone(), invited_room); - } - } - - // Remove all to-device events the device received *last time* - db.users - .remove_to_device_events(sender_id, device_id, since)?; - - let response = sync_events::Response { - next_batch, - rooms: sync_events::Rooms { - leave: left_rooms, - join: joined_rooms, - invite: invited_rooms, - }, - presence: sync_events::Presence { - events: presence_updates - .into_iter() - .map(|(_, v)| Raw::from(v)) - .collect(), - }, - account_data: sync_events::AccountData { - events: db - .account_data - .changes_since(None, &sender_id, since)? - .into_iter() - .filter_map(|(_, v)| { - serde_json::from_str(v.json().get()) - .map_err(|_| Error::bad_database("Invalid account event in database.")) - .ok() - }) - .collect::>(), - }, - device_lists: sync_events::DeviceLists { - changed: device_list_updates.into_iter().collect(), - left: Vec::new(), // TODO - }, - device_one_time_keys_count: if db.users.last_one_time_keys_update(sender_id)? > since { - db.users.count_one_time_keys(sender_id, device_id)? - } else { - BTreeMap::new() - }, - to_device: sync_events::ToDevice { - events: db.users.get_to_device_events(sender_id, device_id)?, - }, - }; - - // TODO: Retry the endpoint instead of returning (waiting for #118) - if !body.full_state - && response.rooms.is_empty() - && response.presence.is_empty() - && response.account_data.is_empty() - && response.device_lists.is_empty() - && response.device_one_time_keys_count.is_empty() - && response.to_device.is_empty() - { - // Hang a few seconds so requests are not spammed - // Stop hanging if new info arrives - let mut duration = body.timeout.unwrap_or_default(); - if duration.as_secs() > 30 { - duration = Duration::from_secs(30); - } - let mut delay = tokio::time::delay_for(duration); - tokio::select! { - _ = &mut delay => {} - _ = watcher => {} - } - } - - Ok(response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/rooms/<_>/context/<_>", data = "") -)] -pub fn get_context_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - if !db.rooms.is_joined(sender_id, &body.room_id)? { - return Err(Error::BadRequest( - ErrorKind::Forbidden, - "You don't have permission to view this room.", - )); - } - - let base_event = db - .rooms - .get_pdu(&body.event_id)? - .ok_or(Error::BadRequest( - ErrorKind::NotFound, - "Base event not found.", - ))? - .to_room_event(); - - let base_token = db - .rooms - .get_pdu_count(&body.event_id)? - .expect("event still exists"); - - let events_before = db - .rooms - .pdus_until(&sender_id, &body.room_id, base_token) - .take( - u32::try_from(body.limit).map_err(|_| { - Error::BadRequest(ErrorKind::InvalidParam, "Limit value is invalid.") - })? as usize - / 2, - ) - .filter_map(|r| r.ok()) // Remove buggy events - .collect::>(); - - let start_token = events_before.last().map(|(count, _)| count.to_string()); - - let events_before = events_before - .into_iter() - .map(|(_, pdu)| pdu.to_room_event()) - .collect::>(); - - let events_after = db - .rooms - .pdus_after(&sender_id, &body.room_id, base_token) - .take( - u32::try_from(body.limit).map_err(|_| { - Error::BadRequest(ErrorKind::InvalidParam, "Limit value is invalid.") - })? as usize - / 2, - ) - .filter_map(|r| r.ok()) // Remove buggy events - .collect::>(); - - let end_token = events_after.last().map(|(count, _)| count.to_string()); - - let events_after = events_after - .into_iter() - .map(|(_, pdu)| pdu.to_room_event()) - .collect::>(); - - Ok(get_context::Response { - start: start_token, - end: end_token, - events_before, - event: Some(base_event), - events_after, - state: db // TODO: State at event - .rooms - .room_state_full(&body.room_id)? - .values() - .map(|pdu| pdu.to_state_event()) - .collect(), - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/rooms/<_>/messages", data = "") -)] -pub fn get_message_events_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - if !db.rooms.is_joined(sender_id, &body.room_id)? { - return Err(Error::BadRequest( - ErrorKind::Forbidden, - "You don't have permission to view this room.", - )); - } - - let from = body - .from - .clone() - .parse() - .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid `from` value."))?; - - let to = body.to.as_ref().map(|t| t.parse()); - - // Use limit or else 10 - let limit = body - .limit - .try_into() - .map_or(Ok::<_, Error>(10_usize), |l: u32| Ok(l as usize))?; - - match body.dir { - get_message_events::Direction::Forward => { - let events_after = db - .rooms - .pdus_after(&sender_id, &body.room_id, from) - .take(limit) - .filter_map(|r| r.ok()) // Filter out buggy events - .take_while(|&(k, _)| Some(Ok(k)) != to) // Stop at `to` - .collect::>(); - - let end_token = events_after.last().map(|(count, _)| count.to_string()); - - let events_after = events_after - .into_iter() - .map(|(_, pdu)| pdu.to_room_event()) - .collect::>(); - - Ok(get_message_events::Response { - start: Some(body.from.clone()), - end: end_token, - chunk: events_after, - state: Vec::new(), - } - .into()) - } - get_message_events::Direction::Backward => { - let events_before = db - .rooms - .pdus_until(&sender_id, &body.room_id, from) - .take(limit) - .filter_map(|r| r.ok()) // Filter out buggy events - .take_while(|&(k, _)| Some(Ok(k)) != to) // Stop at `to` - .collect::>(); - - let start_token = events_before.last().map(|(count, _)| count.to_string()); - - let events_before = events_before - .into_iter() - .map(|(_, pdu)| pdu.to_room_event()) - .collect::>(); - - Ok(get_message_events::Response { - start: Some(body.from.clone()), - end: start_token, - chunk: events_before, - state: Vec::new(), - } - .into()) - } - } -} - -#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/voip/turnServer"))] -pub fn turn_server_route() -> ConduitResult { - Err(Error::BadRequest( - ErrorKind::NotFound, - "There is no turn server yet.", - )) -} - -#[cfg_attr(feature = "conduit_bin", post("/_matrix/client/r0/publicised_groups"))] -pub fn publicised_groups_route() -> ConduitResult { - Err(Error::BadRequest( - ErrorKind::NotFound, - "There are not publicised groups yet.", - )) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/sendToDevice/<_>/<_>", data = "") -)] -pub fn send_event_to_device_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - for (target_user_id, map) in &body.messages { - for (target_device_id_maybe, event) in map { - match target_device_id_maybe { - to_device::DeviceIdOrAllDevices::DeviceId(target_device_id) => { - db.users.add_to_device_event( - sender_id, - &target_user_id, - &target_device_id, - &body.event_type, - serde_json::from_str(event.get()).map_err(|_| { - Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid") - })?, - &db.globals, - )? - } - - to_device::DeviceIdOrAllDevices::AllDevices => { - for target_device_id in db.users.all_device_ids(&target_user_id) { - db.users.add_to_device_event( - sender_id, - &target_user_id, - &target_device_id?, - &body.event_type, - serde_json::from_str(event.get()).map_err(|_| { - Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid") - })?, - &db.globals, - )?; - } - } - } - } - } - - Ok(send_event_to_device::Response.into()) -} - -#[cfg_attr(feature = "conduit_bin", get("/_matrix/media/r0/config"))] -pub fn get_media_config_route( - db: State<'_, Database>, -) -> ConduitResult { - Ok(get_media_config::Response { - upload_size: db.globals.max_request_size().into(), - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/media/r0/upload", data = "") -)] -pub fn create_content_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let mxc = format!( - "mxc://{}/{}", - db.globals.server_name(), - utils::random_string(MXC_LENGTH) - ); - db.media.create( - mxc.clone(), - body.filename.as_ref(), - &body.content_type, - &body.file, - )?; - - Ok(create_content::Response { content_uri: mxc }.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get( - "/_matrix/media/r0/download/<_server_name>/<_media_id>", - data = "" - ) -)] -pub fn get_content_route( - db: State<'_, Database>, - body: Ruma, - _server_name: String, - _media_id: String, -) -> ConduitResult { - if let Some(FileMeta { - filename, - content_type, - file, - }) = db - .media - .get(format!("mxc://{}/{}", body.server_name, body.media_id))? - { - Ok(get_content::Response { - file, - content_type, - content_disposition: filename.unwrap_or_default(), // TODO: Spec says this should be optional - } - .into()) - } else { - Err(Error::BadRequest(ErrorKind::NotFound, "Media not found.")) - } -} - -#[cfg_attr( - feature = "conduit_bin", - get( - "/_matrix/media/r0/thumbnail/<_server_name>/<_media_id>", - data = "" - ) -)] -pub fn get_content_thumbnail_route( - db: State<'_, Database>, - body: Ruma, - _server_name: String, - _media_id: String, -) -> ConduitResult { - if let Some(FileMeta { - content_type, file, .. - }) = db.media.get_thumbnail( - format!("mxc://{}/{}", body.server_name, body.media_id), - body.width - .try_into() - .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?, - body.height - .try_into() - .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?, - )? { - Ok(get_content_thumbnail::Response { file, content_type }.into()) - } else { - Err(Error::BadRequest(ErrorKind::NotFound, "Media not found.")) - } -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/devices", data = "") -)] -pub fn get_devices_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let devices = db - .users - .all_devices_metadata(sender_id) - .filter_map(|r| r.ok()) // Filter out buggy devices - .collect::>(); - - Ok(get_devices::Response { devices }.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/devices/<_device_id>", data = "") -)] -pub fn get_device_route( - db: State<'_, Database>, - body: Ruma, - _device_id: String, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let device = db - .users - .get_device_metadata(&sender_id, &body.body.device_id)? - .ok_or(Error::BadRequest(ErrorKind::NotFound, "Device not found."))?; - - Ok(get_device::Response { device }.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/devices/<_device_id>", data = "") -)] -pub fn update_device_route( - db: State<'_, Database>, - body: Ruma, - _device_id: String, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let mut device = db - .users - .get_device_metadata(&sender_id, &body.body.device_id)? - .ok_or(Error::BadRequest(ErrorKind::NotFound, "Device not found."))?; - - device.display_name = body.display_name.clone(); - - db.users - .update_device_metadata(&sender_id, &body.body.device_id, &device)?; - - Ok(update_device::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - delete("/_matrix/client/r0/devices/<_device_id>", data = "") -)] -pub fn delete_device_route( - db: State<'_, Database>, - body: Ruma, - _device_id: String, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - let device_id = body.device_id.as_ref().expect("user is authenticated"); - - // UIAA - let mut uiaainfo = UiaaInfo { - flows: vec![AuthFlow { - stages: vec!["m.login.password".to_owned()], - }], - completed: Vec::new(), - params: Default::default(), - session: None, - auth_error: None, - }; - - if let Some(auth) = &body.auth { - let (worked, uiaainfo) = db.uiaa.try_auth( - &sender_id, - &device_id, - auth, - &uiaainfo, - &db.users, - &db.globals, - )?; - if !worked { - return Err(Error::Uiaa(uiaainfo)); - } - // Success! - } else { - uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); - db.uiaa.create(&sender_id, &device_id, &uiaainfo)?; - return Err(Error::Uiaa(uiaainfo)); - } - - db.users.remove_device(&sender_id, &body.body.device_id)?; - - Ok(delete_device::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/r0/delete_devices", data = "") -)] -pub fn delete_devices_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - let device_id = body.device_id.as_ref().expect("user is authenticated"); - - // UIAA - let mut uiaainfo = UiaaInfo { - flows: vec![AuthFlow { - stages: vec!["m.login.password".to_owned()], - }], - completed: Vec::new(), - params: Default::default(), - session: None, - auth_error: None, - }; - - if let Some(auth) = &body.auth { - let (worked, uiaainfo) = db.uiaa.try_auth( - &sender_id, - &device_id, - auth, - &uiaainfo, - &db.users, - &db.globals, - )?; - if !worked { - return Err(Error::Uiaa(uiaainfo)); - } - // Success! - } else { - uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); - db.uiaa.create(&sender_id, &device_id, &uiaainfo)?; - return Err(Error::Uiaa(uiaainfo)); - } - - for device_id in &body.devices { - db.users.remove_device(&sender_id, &device_id)? - } - - Ok(delete_devices::Response.into()) -} - -#[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, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - let device_id = body.device_id.as_ref().expect("user is authenticated"); - - // UIAA - let mut uiaainfo = UiaaInfo { - flows: vec![AuthFlow { - stages: vec!["m.login.password".to_owned()], - }], - completed: Vec::new(), - params: Default::default(), - session: None, - auth_error: None, - }; - - if let Some(auth) = &body.auth { - let (worked, uiaainfo) = db.uiaa.try_auth( - &sender_id, - &device_id, - auth, - &uiaainfo, - &db.users, - &db.globals, - )?; - if !worked { - return Err(Error::Uiaa(uiaainfo)); - } - // Success! - } else { - uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); - db.uiaa.create(&sender_id, &device_id, &uiaainfo)?; - return Err(Error::Uiaa(uiaainfo)); - } - - if let Some(master_key) = &body.master_key { - db.users.add_cross_signing_keys( - sender_id, - &master_key, - &body.self_signing_key, - &body.user_signing_key, - &db.rooms, - &db.globals, - )?; - } - - Ok(upload_signing_keys::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - post("/_matrix/client/unstable/keys/signatures/upload", data = "") -)] -pub fn upload_signatures_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - for (user_id, signed_keys) in &body.signed_keys { - for (key_id, signed_key) in signed_keys { - for signature in signed_key - .get("signatures") - .ok_or(Error::BadRequest( - ErrorKind::InvalidParam, - "Missing signatures field.", - ))? - .get(sender_id.to_string()) - .ok_or(Error::BadRequest( - ErrorKind::InvalidParam, - "Invalid user in signatures field.", - ))? - .as_object() - .ok_or(Error::BadRequest( - ErrorKind::InvalidParam, - "Invalid signature.", - ))? - .clone() - .into_iter() - { - // Signature validation? - let signature = ( - signature.0, - signature - .1 - .as_str() - .ok_or(Error::BadRequest( - ErrorKind::InvalidParam, - "Invalid signature value.", - ))? - .to_owned(), - ); - db.users.sign_key( - &user_id, - &key_id, - signature, - &sender_id, - &db.rooms, - &db.globals, - )?; - } - } - } - - Ok(upload_signatures::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/keys/changes", data = "") -)] -pub fn get_key_changes_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let mut device_list_updates = HashSet::new(); - - device_list_updates.extend( - db.users - .keys_changed( - &sender_id.to_string(), - body.from - .parse() - .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid `from`."))?, - Some( - body.to - .parse() - .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid `to`."))?, - ), - ) - .filter_map(|r| r.ok()), - ); - - for room_id in db.rooms.rooms_joined(sender_id).filter_map(|r| r.ok()) { - device_list_updates.extend( - db.users - .keys_changed( - &room_id.to_string(), - body.from.parse().map_err(|_| { - Error::BadRequest(ErrorKind::InvalidParam, "Invalid `from`.") - })?, - Some(body.to.parse().map_err(|_| { - Error::BadRequest(ErrorKind::InvalidParam, "Invalid `to`.") - })?), - ) - .filter_map(|r| r.ok()), - ); - } - Ok(get_key_changes::Response { - changed: device_list_updates.into_iter().collect(), - left: Vec::new(), // TODO - } - .into()) -} - -#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/pushers"))] -pub fn pushers_route() -> ConduitResult { - Ok(get_pushers::Response { - pushers: Vec::new(), - } - .into()) -} - -#[cfg_attr(feature = "conduit_bin", post("/_matrix/client/r0/pushers/set"))] -pub fn set_pushers_route() -> ConduitResult { - Ok(get_pushers::Response { - pushers: Vec::new(), - } - .into()) -} - -#[cfg_attr( - feature = "conduit_bin", - put("/_matrix/client/r0/user/<_>/rooms/<_>/tags/<_>", data = "") -)] -pub fn update_tag_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let mut tags_event = db - .account_data - .get::(Some(&body.room_id), sender_id, EventType::Tag)? - .unwrap_or_else(|| ruma::events::tag::TagEvent { - content: ruma::events::tag::TagEventContent { - tags: BTreeMap::new(), - }, - }); - tags_event - .content - .tags - .insert(body.tag.to_string(), body.tag_info.clone()); - - db.account_data.update( - Some(&body.room_id), - sender_id, - EventType::Tag, - &tags_event, - &db.globals, - )?; - - Ok(create_tag::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - delete("/_matrix/client/r0/user/<_>/rooms/<_>/tags/<_>", data = "") -)] -pub fn delete_tag_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - let mut tags_event = db - .account_data - .get::(Some(&body.room_id), sender_id, EventType::Tag)? - .unwrap_or_else(|| ruma::events::tag::TagEvent { - content: ruma::events::tag::TagEventContent { - tags: BTreeMap::new(), - }, - }); - tags_event.content.tags.remove(&body.tag); - - db.account_data.update( - Some(&body.room_id), - sender_id, - EventType::Tag, - &tags_event, - &db.globals, - )?; - - Ok(delete_tag::Response.into()) -} - -#[cfg_attr( - feature = "conduit_bin", - get("/_matrix/client/r0/user/<_>/rooms/<_>/tags", data = "") -)] -pub fn get_tags_route( - db: State<'_, Database>, - body: Ruma, -) -> ConduitResult { - let sender_id = body.sender_id.as_ref().expect("user is authenticated"); - - Ok(get_tags::Response { - tags: db - .account_data - .get::(Some(&body.room_id), sender_id, EventType::Tag)? - .unwrap_or_else(|| ruma::events::tag::TagEvent { - content: ruma::events::tag::TagEventContent { - tags: BTreeMap::new(), - }, - }) - .content - .tags, - } - .into()) -} - -#[cfg(feature = "conduit_bin")] -#[options("/<_..>")] -pub fn options_route() -> ConduitResult { - Ok(send_event_to_device::Response.into()) -} diff --git a/src/client_server/account.rs b/src/client_server/account.rs new file mode 100644 index 0000000..bfb768a --- /dev/null +++ b/src/client_server/account.rs @@ -0,0 +1,308 @@ +use super::{State, DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH}; +use crate::{pdu::PduBuilder, utils, ConduitResult, Database, Error, Ruma}; +use ruma::{ + api::client::{ + error::ErrorKind, + r0::{ + account::{ + change_password, deactivate, get_username_availability, register, whoami, + ThirdPartyIdRemovalStatus, + }, + uiaa::{AuthFlow, UiaaInfo}, + }, + }, + events::{room::member, EventType}, + UserId, +}; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, post}; + +const GUEST_NAME_LENGTH: usize = 10; + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/register/available", data = "") +)] +pub fn get_register_available_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + // Validate user id + let user_id = UserId::parse_with_server_name(body.username.clone(), db.globals.server_name()) + .ok() + .filter(|user_id| { + !user_id.is_historical() && user_id.server_name() == db.globals.server_name() + }) + .ok_or(Error::BadRequest( + ErrorKind::InvalidUsername, + "Username is invalid.", + ))?; + + // Check if username is creative enough + if db.users.exists(&user_id)? { + return Err(Error::BadRequest( + ErrorKind::UserInUse, + "Desired user ID is already taken.", + )); + } + + // 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::Response { available: true }.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/register", data = "") +)] +pub fn register_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + if db.globals.registration_disabled() { + return Err(Error::BadRequest( + ErrorKind::Forbidden, + "Registration has been disabled.", + )); + } + + // Validate user id + let user_id = UserId::parse_with_server_name( + body.username + .clone() + .unwrap_or_else(|| utils::random_string(GUEST_NAME_LENGTH)) + .to_lowercase(), + db.globals.server_name(), + ) + .ok() + .filter(|user_id| !user_id.is_historical() && user_id.server_name() == db.globals.server_name()) + .ok_or(Error::BadRequest( + ErrorKind::InvalidUsername, + "Username is invalid.", + ))?; + + // Check if username is creative enough + if db.users.exists(&user_id)? { + return Err(Error::BadRequest( + ErrorKind::UserInUse, + "Desired user ID is already taken.", + )); + } + + // UIAA + let mut uiaainfo = UiaaInfo { + flows: vec![AuthFlow { + stages: vec!["m.login.dummy".to_owned()], + }], + completed: Vec::new(), + params: Default::default(), + session: None, + auth_error: None, + }; + + if let Some(auth) = &body.auth { + let (worked, uiaainfo) = + db.uiaa + .try_auth(&user_id, "".into(), auth, &uiaainfo, &db.users, &db.globals)?; + if !worked { + return Err(Error::Uiaa(uiaainfo)); + } + // Success! + } else { + uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); + db.uiaa.create(&user_id, "".into(), &uiaainfo)?; + return Err(Error::Uiaa(uiaainfo)); + } + + let password = body.password.clone().unwrap_or_default(); + + // Create user + db.users.create(&user_id, &password)?; + + // Generate new device id if the user didn't specify one + let device_id = body + .device_id + .clone() + .unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into()); + + // Generate new token for the device + let token = utils::random_string(TOKEN_LENGTH); + + // Add device + db.users.create_device( + &user_id, + &device_id, + &token, + body.initial_device_display_name.clone(), + )?; + + // Initial data + db.account_data.update( + None, + &user_id, + EventType::PushRules, + &ruma::events::push_rules::PushRulesEvent { + content: ruma::events::push_rules::PushRulesEventContent { + global: crate::push_rules::default_pushrules(&user_id), + }, + }, + &db.globals, + )?; + + Ok(register::Response { + access_token: Some(token), + user_id, + device_id: Some(device_id), + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/account/password", data = "") +)] +pub fn change_password_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + let device_id = body.device_id.as_ref().expect("user is authenticated"); + + let mut uiaainfo = UiaaInfo { + flows: vec![AuthFlow { + stages: vec!["m.login.password".to_owned()], + }], + completed: Vec::new(), + params: Default::default(), + session: None, + auth_error: None, + }; + + if let Some(auth) = &body.auth { + let (worked, uiaainfo) = db.uiaa.try_auth( + &sender_id, + device_id, + auth, + &uiaainfo, + &db.users, + &db.globals, + )?; + if !worked { + return Err(Error::Uiaa(uiaainfo)); + } + // Success! + } else { + uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); + db.uiaa.create(&sender_id, &device_id, &uiaainfo)?; + return Err(Error::Uiaa(uiaainfo)); + } + + db.users.set_password(&sender_id, &body.new_password)?; + + // TODO: Read logout_devices field when it's available and respect that, currently not supported in Ruma + // See: https://github.com/ruma/ruma/issues/107 + // Logout all devices except the current one + for id in db + .users + .all_device_ids(&sender_id) + .filter_map(|id| id.ok()) + .filter(|id| id != device_id) + { + db.users.remove_device(&sender_id, &id)?; + } + + Ok(change_password::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/account/whoami", data = "") +)] +pub fn whoami_route(body: Ruma) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + Ok(whoami::Response { + user_id: sender_id.clone(), + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/account/deactivate", data = "") +)] +pub fn deactivate_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + let device_id = body.device_id.as_ref().expect("user is authenticated"); + + let mut uiaainfo = UiaaInfo { + flows: vec![AuthFlow { + stages: vec!["m.login.password".to_owned()], + }], + completed: Vec::new(), + params: Default::default(), + session: None, + auth_error: None, + }; + + if let Some(auth) = &body.auth { + let (worked, uiaainfo) = db.uiaa.try_auth( + &sender_id, + &device_id, + auth, + &uiaainfo, + &db.users, + &db.globals, + )?; + if !worked { + return Err(Error::Uiaa(uiaainfo)); + } + // Success! + } else { + uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); + db.uiaa.create(&sender_id, &device_id, &uiaainfo)?; + return Err(Error::Uiaa(uiaainfo)); + } + + // Leave all joined rooms and reject all invitations + for room_id in db + .rooms + .rooms_joined(&sender_id) + .chain(db.rooms.rooms_invited(&sender_id)) + { + let room_id = room_id?; + let event = member::MemberEventContent { + membership: member::MembershipState::Leave, + displayname: None, + avatar_url: None, + is_direct: None, + third_party_invite: None, + }; + + db.rooms.append_pdu( + PduBuilder { + room_id: room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomMember, + content: serde_json::to_value(event).expect("event is valid, we just created it"), + unsigned: None, + state_key: Some(sender_id.to_string()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + } + + // Remove devices and mark account as deactivated + db.users.deactivate_account(&sender_id)?; + + Ok(deactivate::Response { + id_server_unbind_result: ThirdPartyIdRemovalStatus::NoSupport, + } + .into()) +} diff --git a/src/client_server/alias.rs b/src/client_server/alias.rs new file mode 100644 index 0000000..4399cb5 --- /dev/null +++ b/src/client_server/alias.rs @@ -0,0 +1,67 @@ +use super::State; +use crate::{ConduitResult, Database, Error, Ruma}; +use ruma::api::client::{ + error::ErrorKind, + r0::alias::{create_alias, delete_alias, get_alias}, +}; + +#[cfg(feature = "conduit_bin")] +use rocket::{delete, get, put}; + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/directory/room/<_>", data = "") +)] +pub fn create_alias_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + if db.rooms.id_from_alias(&body.room_alias)?.is_some() { + return Err(Error::Conflict("Alias already exists.")); + } + + db.rooms + .set_alias(&body.room_alias, Some(&body.room_id), &db.globals)?; + + Ok(create_alias::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + delete("/_matrix/client/r0/directory/room/<_>", data = "") +)] +pub fn delete_alias_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + db.rooms.set_alias(&body.room_alias, None, &db.globals)?; + + Ok(delete_alias::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/directory/room/<_>", data = "") +)] +pub fn get_alias_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + if body.room_alias.server_name() != db.globals.server_name() { + todo!("ask remote server"); + } + + let room_id = db + .rooms + .id_from_alias(&body.room_alias)? + .ok_or(Error::BadRequest( + ErrorKind::NotFound, + "Room with alias not found.", + ))?; + + Ok(get_alias::Response { + room_id, + servers: vec![db.globals.server_name().to_string()], + } + .into()) +} diff --git a/src/client_server/backup.rs b/src/client_server/backup.rs new file mode 100644 index 0000000..a104964 --- /dev/null +++ b/src/client_server/backup.rs @@ -0,0 +1,142 @@ +use super::State; +use crate::{ConduitResult, Database, Error, Ruma}; +use ruma::api::client::{ + error::ErrorKind, + r0::backup::{ + add_backup_keys, create_backup, get_backup, get_backup_keys, get_latest_backup, + update_backup, + }, +}; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, post, put}; + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/unstable/room_keys/version", data = "") +)] +pub fn create_backup_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + let version = db + .key_backups + .create_backup(&sender_id, &body.algorithm, &db.globals)?; + + Ok(create_backup::Response { version }.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/unstable/room_keys/version/<_>", data = "") +)] +pub fn update_backup_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + db.key_backups + .update_backup(&sender_id, &body.version, &body.algorithm, &db.globals)?; + + Ok(update_backup::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/unstable/room_keys/version", data = "") +)] +pub fn get_latest_backup_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let (version, algorithm) = + db.key_backups + .get_latest_backup(&sender_id)? + .ok_or(Error::BadRequest( + ErrorKind::NotFound, + "Key backup does not exist.", + ))?; + + Ok(get_latest_backup::Response { + algorithm, + count: (db.key_backups.count_keys(sender_id, &version)? as u32).into(), + etag: db.key_backups.get_etag(sender_id, &version)?, + version, + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/unstable/room_keys/version/<_>", data = "") +)] +pub fn get_backup_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + let algorithm = db + .key_backups + .get_backup(&sender_id, &body.version)? + .ok_or(Error::BadRequest( + ErrorKind::NotFound, + "Key backup does not exist.", + ))?; + + Ok(get_backup::Response { + algorithm, + count: (db.key_backups.count_keys(sender_id, &body.version)? as u32).into(), + etag: db.key_backups.get_etag(sender_id, &body.version)?, + version: body.version.clone(), + } + .into()) +} + +/// 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, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + for (room_id, room) in &body.rooms { + for (session_id, key_data) in &room.sessions { + db.key_backups.add_key( + &sender_id, + &body.version, + &room_id, + &session_id, + &key_data, + &db.globals, + )? + } + } + + Ok(add_backup_keys::Response { + count: (db.key_backups.count_keys(sender_id, &body.version)? as u32).into(), + etag: db.key_backups.get_etag(sender_id, &body.version)?, + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/unstable/room_keys/keys", data = "") +)] +pub fn get_backup_keys_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let rooms = db.key_backups.get_all(&sender_id, &body.version)?; + + Ok(get_backup_keys::Response { rooms }.into()) +} diff --git a/src/client_server/capabilities.rs b/src/client_server/capabilities.rs new file mode 100644 index 0000000..afa0604 --- /dev/null +++ b/src/client_server/capabilities.rs @@ -0,0 +1,31 @@ +use crate::ConduitResult; +use ruma::{api::client::r0::capabilities::get_capabilities, RoomVersionId}; +use std::collections::BTreeMap; + +#[cfg(feature = "conduit_bin")] +use rocket::get; + +#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/capabilities"))] +pub fn get_capabilities_route() -> ConduitResult { + let mut available = BTreeMap::new(); + available.insert( + RoomVersionId::Version5, + get_capabilities::RoomVersionStability::Stable, + ); + available.insert( + RoomVersionId::Version6, + get_capabilities::RoomVersionStability::Stable, + ); + + Ok(get_capabilities::Response { + capabilities: get_capabilities::Capabilities { + change_password: None, // None means it is possible + room_versions: Some(get_capabilities::RoomVersionsCapability { + default: "6".to_owned(), + available, + }), + custom_capabilities: BTreeMap::new(), + }, + } + .into()) +} diff --git a/src/client_server/config.rs b/src/client_server/config.rs new file mode 100644 index 0000000..8cb6a0d --- /dev/null +++ b/src/client_server/config.rs @@ -0,0 +1,67 @@ +use super::State; +use crate::{ConduitResult, Database, Error, Ruma}; +use ruma::{ + api::client::{ + error::ErrorKind, + r0::config::{get_global_account_data, set_global_account_data}, + }, + events::{custom::CustomEventContent, BasicEvent, EventType}, + Raw, +}; +use std::convert::TryFrom; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, put}; + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/user/<_>/account_data/<_>", data = "") +)] +pub fn set_global_account_data_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let content = serde_json::from_str::(body.data.get()) + .map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Data is invalid."))?; + + let event_type = body.event_type.to_string(); + + db.account_data.update( + None, + sender_id, + event_type.clone().into(), + &BasicEvent { + content: CustomEventContent { + event_type, + json: content, + }, + }, + &db.globals, + )?; + + Ok(set_global_account_data::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/user/<_>/account_data/<_>", data = "") +)] +pub fn get_global_account_data_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let data = db + .account_data + .get::>( + None, + sender_id, + EventType::try_from(&body.event_type).expect("EventType::try_from can never fail"), + )? + .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?; + + Ok(get_global_account_data::Response { account_data: data }.into()) +} diff --git a/src/client_server/context.rs b/src/client_server/context.rs new file mode 100644 index 0000000..7a6cbce --- /dev/null +++ b/src/client_server/context.rs @@ -0,0 +1,92 @@ +use super::State; +use crate::{ConduitResult, Database, Error, Ruma}; +use ruma::api::client::{error::ErrorKind, r0::context::get_context}; +use std::convert::TryFrom; + +#[cfg(feature = "conduit_bin")] +use rocket::get; + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/rooms/<_>/context/<_>", data = "") +)] +pub fn get_context_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + if !db.rooms.is_joined(sender_id, &body.room_id)? { + return Err(Error::BadRequest( + ErrorKind::Forbidden, + "You don't have permission to view this room.", + )); + } + + let base_event = db + .rooms + .get_pdu(&body.event_id)? + .ok_or(Error::BadRequest( + ErrorKind::NotFound, + "Base event not found.", + ))? + .to_room_event(); + + let base_token = db + .rooms + .get_pdu_count(&body.event_id)? + .expect("event still exists"); + + let events_before = db + .rooms + .pdus_until(&sender_id, &body.room_id, base_token) + .take( + u32::try_from(body.limit).map_err(|_| { + Error::BadRequest(ErrorKind::InvalidParam, "Limit value is invalid.") + })? as usize + / 2, + ) + .filter_map(|r| r.ok()) // Remove buggy events + .collect::>(); + + let start_token = events_before.last().map(|(count, _)| count.to_string()); + + let events_before = events_before + .into_iter() + .map(|(_, pdu)| pdu.to_room_event()) + .collect::>(); + + let events_after = db + .rooms + .pdus_after(&sender_id, &body.room_id, base_token) + .take( + u32::try_from(body.limit).map_err(|_| { + Error::BadRequest(ErrorKind::InvalidParam, "Limit value is invalid.") + })? as usize + / 2, + ) + .filter_map(|r| r.ok()) // Remove buggy events + .collect::>(); + + let end_token = events_after.last().map(|(count, _)| count.to_string()); + + let events_after = events_after + .into_iter() + .map(|(_, pdu)| pdu.to_room_event()) + .collect::>(); + + Ok(get_context::Response { + start: start_token, + end: end_token, + events_before, + event: Some(base_event), + events_after, + state: db // TODO: State at event + .rooms + .room_state_full(&body.room_id)? + .values() + .map(|pdu| pdu.to_state_event()) + .collect(), + } + .into()) +} diff --git a/src/client_server/device.rs b/src/client_server/device.rs new file mode 100644 index 0000000..379f827 --- /dev/null +++ b/src/client_server/device.rs @@ -0,0 +1,170 @@ +use super::State; +use crate::{utils, ConduitResult, Database, Error, Ruma}; +use ruma::api::client::{ + error::ErrorKind, + r0::{ + device::{self, delete_device, delete_devices, get_device, get_devices, update_device}, + uiaa::{AuthFlow, UiaaInfo}, + }, +}; + +use super::SESSION_ID_LENGTH; +#[cfg(feature = "conduit_bin")] +use rocket::{delete, get, post, put}; + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/devices", data = "") +)] +pub fn get_devices_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let devices = db + .users + .all_devices_metadata(sender_id) + .filter_map(|r| r.ok()) // Filter out buggy devices + .collect::>(); + + Ok(get_devices::Response { devices }.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/devices/<_device_id>", data = "") +)] +pub fn get_device_route( + db: State<'_, Database>, + body: Ruma, + _device_id: String, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let device = db + .users + .get_device_metadata(&sender_id, &body.body.device_id)? + .ok_or(Error::BadRequest(ErrorKind::NotFound, "Device not found."))?; + + Ok(get_device::Response { device }.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/devices/<_device_id>", data = "") +)] +pub fn update_device_route( + db: State<'_, Database>, + body: Ruma, + _device_id: String, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let mut device = db + .users + .get_device_metadata(&sender_id, &body.body.device_id)? + .ok_or(Error::BadRequest(ErrorKind::NotFound, "Device not found."))?; + + device.display_name = body.display_name.clone(); + + db.users + .update_device_metadata(&sender_id, &body.body.device_id, &device)?; + + Ok(update_device::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + delete("/_matrix/client/r0/devices/<_device_id>", data = "") +)] +pub fn delete_device_route( + db: State<'_, Database>, + body: Ruma, + _device_id: String, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + let device_id = body.device_id.as_ref().expect("user is authenticated"); + + // UIAA + let mut uiaainfo = UiaaInfo { + flows: vec![AuthFlow { + stages: vec!["m.login.password".to_owned()], + }], + completed: Vec::new(), + params: Default::default(), + session: None, + auth_error: None, + }; + + if let Some(auth) = &body.auth { + let (worked, uiaainfo) = db.uiaa.try_auth( + &sender_id, + &device_id, + auth, + &uiaainfo, + &db.users, + &db.globals, + )?; + if !worked { + return Err(Error::Uiaa(uiaainfo)); + } + // Success! + } else { + uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); + db.uiaa.create(&sender_id, &device_id, &uiaainfo)?; + return Err(Error::Uiaa(uiaainfo)); + } + + db.users.remove_device(&sender_id, &body.body.device_id)?; + + Ok(delete_device::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/delete_devices", data = "") +)] +pub fn delete_devices_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + let device_id = body.device_id.as_ref().expect("user is authenticated"); + + // UIAA + let mut uiaainfo = UiaaInfo { + flows: vec![AuthFlow { + stages: vec!["m.login.password".to_owned()], + }], + completed: Vec::new(), + params: Default::default(), + session: None, + auth_error: None, + }; + + if let Some(auth) = &body.auth { + let (worked, uiaainfo) = db.uiaa.try_auth( + &sender_id, + &device_id, + auth, + &uiaainfo, + &db.users, + &db.globals, + )?; + if !worked { + return Err(Error::Uiaa(uiaainfo)); + } + // Success! + } else { + uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); + db.uiaa.create(&sender_id, &device_id, &uiaainfo)?; + return Err(Error::Uiaa(uiaainfo)); + } + + for device_id in &body.devices { + db.users.remove_device(&sender_id, &device_id)? + } + + Ok(delete_devices::Response.into()) +} diff --git a/src/client_server/directory.rs b/src/client_server/directory.rs new file mode 100644 index 0000000..510511c --- /dev/null +++ b/src/client_server/directory.rs @@ -0,0 +1,258 @@ +use super::State; +use crate::{ConduitResult, Database, Error, Result, Ruma}; +use ruma::{ + api::client::r0::{ + directory::{ + self, get_public_rooms, get_public_rooms_filtered, get_room_visibility, + set_room_visibility, + }, + room, + }, + events::{ + room::{avatar, canonical_alias, guest_access, history_visibility, name, topic}, + EventType, + }, + Raw, +}; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, post, put}; + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/publicRooms", data = "") +)] +pub async fn get_public_rooms_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let Ruma { + body: + get_public_rooms::Request { + limit, + server, + since, + }, + sender_id, + device_id, + json_body, + } = body; + + let get_public_rooms_filtered::Response { + chunk, + prev_batch, + next_batch, + total_room_count_estimate, + } = get_public_rooms_filtered_route( + db, + Ruma { + body: get_public_rooms_filtered::Request { + filter: None, + limit, + room_network: get_public_rooms_filtered::RoomNetwork::Matrix, + server, + since, + }, + sender_id, + device_id, + json_body, + }, + ) + .await? + .0; + + Ok(get_public_rooms::Response { + chunk, + prev_batch, + next_batch, + total_room_count_estimate, + } + .into()) +} + +#[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, +) -> ConduitResult { + let mut chunk = + db.rooms + .public_rooms() + .map(|room_id| { + let room_id = room_id?; + + // TODO: Do not load full state? + let state = db.rooms.room_state_full(&room_id)?; + + let chunk = directory::PublicRoomsChunk { + aliases: Vec::new(), + canonical_alias: state + .get(&(EventType::RoomCanonicalAlias, "".to_owned())) + .map_or(Ok::<_, Error>(None), |s| { + Ok(serde_json::from_value::< + Raw, + >(s.content.clone()) + .expect("from_value::> can never fail") + .deserialize() + .map_err(|_| { + Error::bad_database("Invalid canonical alias event in database.") + })? + .alias) + })?, + name: state.get(&(EventType::RoomName, "".to_owned())).map_or( + Ok::<_, Error>(None), + |s| { + Ok(serde_json::from_value::>( + s.content.clone(), + ) + .expect("from_value::> can never fail") + .deserialize() + .map_err(|_| { + Error::bad_database("Invalid room name event in database.") + })? + .name() + .map(|n| n.to_owned())) + }, + )?, + num_joined_members: (db.rooms.room_members(&room_id).count() as u32).into(), + room_id, + topic: state.get(&(EventType::RoomTopic, "".to_owned())).map_or( + Ok::<_, Error>(None), + |s| { + Ok(Some( + serde_json::from_value::>( + s.content.clone(), + ) + .expect("from_value::> can never fail") + .deserialize() + .map_err(|_| { + Error::bad_database("Invalid room topic event in database.") + })? + .topic, + )) + }, + )?, + world_readable: state + .get(&(EventType::RoomHistoryVisibility, "".to_owned())) + .map_or(Ok::<_, Error>(false), |s| { + Ok(serde_json::from_value::< + Raw, + >(s.content.clone()) + .expect("from_value::> can never fail") + .deserialize() + .map_err(|_| { + Error::bad_database( + "Invalid room history visibility event in database.", + ) + })? + .history_visibility + == history_visibility::HistoryVisibility::WorldReadable) + })?, + guest_can_join: state + .get(&(EventType::RoomGuestAccess, "".to_owned())) + .map_or(Ok::<_, Error>(false), |s| { + Ok( + serde_json::from_value::>( + s.content.clone(), + ) + .expect("from_value::> can never fail") + .deserialize() + .map_err(|_| { + Error::bad_database("Invalid room guest access event in database.") + })? + .guest_access + == guest_access::GuestAccess::CanJoin, + ) + })?, + avatar_url: state + .get(&(EventType::RoomAvatar, "".to_owned())) + .map(|s| { + Ok::<_, Error>( + serde_json::from_value::>( + s.content.clone(), + ) + .expect("from_value::> can never fail") + .deserialize() + .map_err(|_| { + Error::bad_database("Invalid room avatar event in database.") + })? + .url, + ) + }) + .transpose()?, + }; + Ok(chunk) + }) + .filter_map(|r: Result<_>| r.ok()) // Filter out buggy rooms + // We need to collect all, so we can sort by member count + .collect::>(); + + chunk.sort_by(|l, r| r.num_joined_members.cmp(&l.num_joined_members)); + + /* + chunk.extend_from_slice( + &server_server::send_request( + &db, + "privacytools.io".to_owned(), + ruma::api::federation::v1::get_public_rooms::Request { + limit: Some(20_u32.into()), + since: None, + room_network: ruma::api::federation::v1::get_public_rooms::RoomNetwork::Matrix, + }, + ) + .await + ? + .chunk + .into_iter() + .map(|c| serde_json::from_str(&serde_json::to_string(&c)?)?) + .collect::>(), + ); + */ + + let total_room_count_estimate = (chunk.len() as u32).into(); + + Ok(get_public_rooms_filtered::Response { + chunk, + prev_batch: None, + next_batch: None, + total_room_count_estimate: Some(total_room_count_estimate), + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/directory/list/room/<_>", data = "") +)] +pub async fn set_room_visibility_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + match body.visibility { + room::Visibility::Public => db.rooms.set_public(&body.room_id, true)?, + room::Visibility::Private => db.rooms.set_public(&body.room_id, false)?, + } + + Ok(set_room_visibility::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/directory/list/room/<_>", data = "") +)] +pub async fn get_room_visibility_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + Ok(get_room_visibility::Response { + visibility: if db.rooms.is_public_room(&body.room_id)? { + room::Visibility::Public + } else { + room::Visibility::Private + }, + } + .into()) +} diff --git a/src/client_server/filter.rs b/src/client_server/filter.rs new file mode 100644 index 0000000..165419a --- /dev/null +++ b/src/client_server/filter.rs @@ -0,0 +1,29 @@ +use crate::{utils, ConduitResult}; +use ruma::api::client::r0::filter::{self, create_filter, get_filter}; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, post}; + +#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/user/<_>/filter/<_>"))] +pub fn get_filter_route() -> ConduitResult { + // TODO + Ok(get_filter::Response { + filter: filter::FilterDefinition { + event_fields: None, + event_format: None, + account_data: None, + room: None, + presence: None, + }, + } + .into()) +} + +#[cfg_attr(feature = "conduit_bin", post("/_matrix/client/r0/user/<_>/filter"))] +pub fn create_filter_route() -> ConduitResult { + // TODO + Ok(create_filter::Response { + filter_id: utils::random_string(10), + } + .into()) +} diff --git a/src/client_server/keys.rs b/src/client_server/keys.rs new file mode 100644 index 0000000..4067210 --- /dev/null +++ b/src/client_server/keys.rs @@ -0,0 +1,323 @@ +use super::State; +use super::SESSION_ID_LENGTH; +use crate::{utils, ConduitResult, Database, Error, Ruma}; +use ruma::api::client::{ + error::ErrorKind, + r0::{ + keys::{ + self, claim_keys, get_key_changes, get_keys, upload_keys, upload_signatures, + upload_signing_keys, + }, + uiaa::{AuthFlow, UiaaInfo}, + }, +}; +use std::collections::{BTreeMap, HashSet}; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, post}; + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/keys/upload", data = "") +)] +pub fn upload_keys_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + let device_id = body.device_id.as_ref().expect("user is authenticated"); + + if let Some(one_time_keys) = &body.one_time_keys { + for (key_key, key_value) in one_time_keys { + db.users + .add_one_time_key(sender_id, device_id, key_key, key_value, &db.globals)?; + } + } + + if let Some(device_keys) = &body.device_keys { + // This check is needed to assure that signatures are kept + if db.users.get_device_keys(sender_id, device_id)?.is_none() { + db.users + .add_device_keys(sender_id, device_id, device_keys, &db.rooms, &db.globals)?; + } + } + + Ok(upload_keys::Response { + one_time_key_counts: db.users.count_one_time_keys(sender_id, device_id)?, + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/keys/query", data = "") +)] +pub fn get_keys_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let mut master_keys = BTreeMap::new(); + let mut self_signing_keys = BTreeMap::new(); + let mut user_signing_keys = BTreeMap::new(); + let mut device_keys = BTreeMap::new(); + + for (user_id, device_ids) in &body.device_keys { + if device_ids.is_empty() { + let mut container = BTreeMap::new(); + for device_id in db.users.all_device_ids(user_id) { + let device_id = device_id?; + if let Some(mut keys) = db.users.get_device_keys(user_id, &device_id)? { + let metadata = db + .users + .get_device_metadata(user_id, &device_id)? + .ok_or_else(|| { + Error::bad_database("all_device_keys contained nonexistent device.") + })?; + + keys.unsigned = Some(keys::UnsignedDeviceInfo { + device_display_name: metadata.display_name, + }); + + container.insert(device_id, keys); + } + } + device_keys.insert(user_id.clone(), container); + } else { + for device_id in device_ids { + let mut container = BTreeMap::new(); + if let Some(mut keys) = db.users.get_device_keys(&user_id.clone(), &device_id)? { + let metadata = db.users.get_device_metadata(user_id, &device_id)?.ok_or( + Error::BadRequest( + ErrorKind::InvalidParam, + "Tried to get keys for nonexistent device.", + ), + )?; + + keys.unsigned = Some(keys::UnsignedDeviceInfo { + device_display_name: metadata.display_name, + }); + + container.insert(device_id.clone(), keys); + } + device_keys.insert(user_id.clone(), container); + } + } + + if let Some(master_key) = db.users.get_master_key(user_id, sender_id)? { + master_keys.insert(user_id.clone(), master_key); + } + if let Some(self_signing_key) = db.users.get_self_signing_key(user_id, sender_id)? { + self_signing_keys.insert(user_id.clone(), self_signing_key); + } + if user_id == sender_id { + if let Some(user_signing_key) = db.users.get_user_signing_key(sender_id)? { + user_signing_keys.insert(user_id.clone(), user_signing_key); + } + } + } + + Ok(get_keys::Response { + master_keys, + self_signing_keys, + user_signing_keys, + device_keys, + failures: BTreeMap::new(), + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/keys/claim", data = "") +)] +pub fn claim_keys_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let mut one_time_keys = BTreeMap::new(); + for (user_id, map) in &body.one_time_keys { + let mut container = BTreeMap::new(); + for (device_id, key_algorithm) in map { + if let Some(one_time_keys) = + db.users + .take_one_time_key(user_id, device_id, key_algorithm, &db.globals)? + { + let mut c = BTreeMap::new(); + c.insert(one_time_keys.0, one_time_keys.1); + container.insert(device_id.clone(), c); + } + } + one_time_keys.insert(user_id.clone(), container); + } + + Ok(claim_keys::Response { + failures: BTreeMap::new(), + one_time_keys, + } + .into()) +} + +#[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, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + let device_id = body.device_id.as_ref().expect("user is authenticated"); + + // UIAA + let mut uiaainfo = UiaaInfo { + flows: vec![AuthFlow { + stages: vec!["m.login.password".to_owned()], + }], + completed: Vec::new(), + params: Default::default(), + session: None, + auth_error: None, + }; + + if let Some(auth) = &body.auth { + let (worked, uiaainfo) = db.uiaa.try_auth( + &sender_id, + &device_id, + auth, + &uiaainfo, + &db.users, + &db.globals, + )?; + if !worked { + return Err(Error::Uiaa(uiaainfo)); + } + // Success! + } else { + uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); + db.uiaa.create(&sender_id, &device_id, &uiaainfo)?; + return Err(Error::Uiaa(uiaainfo)); + } + + if let Some(master_key) = &body.master_key { + db.users.add_cross_signing_keys( + sender_id, + &master_key, + &body.self_signing_key, + &body.user_signing_key, + &db.rooms, + &db.globals, + )?; + } + + Ok(upload_signing_keys::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/unstable/keys/signatures/upload", data = "") +)] +pub fn upload_signatures_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + for (user_id, signed_keys) in &body.signed_keys { + for (key_id, signed_key) in signed_keys { + for signature in signed_key + .get("signatures") + .ok_or(Error::BadRequest( + ErrorKind::InvalidParam, + "Missing signatures field.", + ))? + .get(sender_id.to_string()) + .ok_or(Error::BadRequest( + ErrorKind::InvalidParam, + "Invalid user in signatures field.", + ))? + .as_object() + .ok_or(Error::BadRequest( + ErrorKind::InvalidParam, + "Invalid signature.", + ))? + .clone() + .into_iter() + { + // Signature validation? + let signature = ( + signature.0, + signature + .1 + .as_str() + .ok_or(Error::BadRequest( + ErrorKind::InvalidParam, + "Invalid signature value.", + ))? + .to_owned(), + ); + db.users.sign_key( + &user_id, + &key_id, + signature, + &sender_id, + &db.rooms, + &db.globals, + )?; + } + } + } + + Ok(upload_signatures::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/keys/changes", data = "") +)] +pub fn get_key_changes_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let mut device_list_updates = HashSet::new(); + + device_list_updates.extend( + db.users + .keys_changed( + &sender_id.to_string(), + body.from + .parse() + .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid `from`."))?, + Some( + body.to + .parse() + .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid `to`."))?, + ), + ) + .filter_map(|r| r.ok()), + ); + + for room_id in db.rooms.rooms_joined(sender_id).filter_map(|r| r.ok()) { + device_list_updates.extend( + db.users + .keys_changed( + &room_id.to_string(), + body.from.parse().map_err(|_| { + Error::BadRequest(ErrorKind::InvalidParam, "Invalid `from`.") + })?, + Some(body.to.parse().map_err(|_| { + Error::BadRequest(ErrorKind::InvalidParam, "Invalid `to`.") + })?), + ) + .filter_map(|r| r.ok()), + ); + } + Ok(get_key_changes::Response { + changed: device_list_updates.into_iter().collect(), + left: Vec::new(), // TODO + } + .into()) +} diff --git a/src/client_server/media.rs b/src/client_server/media.rs new file mode 100644 index 0000000..efcb3a6 --- /dev/null +++ b/src/client_server/media.rs @@ -0,0 +1,107 @@ +use super::State; +use crate::{database::media::FileMeta, utils, ConduitResult, Database, Error, Ruma}; +use ruma::api::client::{ + error::ErrorKind, + r0::media::{create_content, get_content, get_content_thumbnail, get_media_config}, +}; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, post}; +use std::convert::TryInto; + +const MXC_LENGTH: usize = 256; + +#[cfg_attr(feature = "conduit_bin", get("/_matrix/media/r0/config"))] +pub fn get_media_config_route( + db: State<'_, Database>, +) -> ConduitResult { + Ok(get_media_config::Response { + upload_size: db.globals.max_request_size().into(), + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/media/r0/upload", data = "") +)] +pub fn create_content_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let mxc = format!( + "mxc://{}/{}", + db.globals.server_name(), + utils::random_string(MXC_LENGTH) + ); + db.media.create( + mxc.clone(), + body.filename.as_ref(), + &body.content_type, + &body.file, + )?; + + Ok(create_content::Response { content_uri: mxc }.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get( + "/_matrix/media/r0/download/<_server_name>/<_media_id>", + data = "" + ) +)] +pub fn get_content_route( + db: State<'_, Database>, + body: Ruma, + _server_name: String, + _media_id: String, +) -> ConduitResult { + if let Some(FileMeta { + filename, + content_type, + file, + }) = db + .media + .get(format!("mxc://{}/{}", body.server_name, body.media_id))? + { + Ok(get_content::Response { + file, + content_type, + content_disposition: filename.unwrap_or_default(), // TODO: Spec says this should be optional + } + .into()) + } else { + Err(Error::BadRequest(ErrorKind::NotFound, "Media not found.")) + } +} + +#[cfg_attr( + feature = "conduit_bin", + get( + "/_matrix/media/r0/thumbnail/<_server_name>/<_media_id>", + data = "" + ) +)] +pub fn get_content_thumbnail_route( + db: State<'_, Database>, + body: Ruma, + _server_name: String, + _media_id: String, +) -> ConduitResult { + if let Some(FileMeta { + content_type, file, .. + }) = db.media.get_thumbnail( + format!("mxc://{}/{}", body.server_name, body.media_id), + body.width + .try_into() + .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?, + body.height + .try_into() + .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Width is invalid."))?, + )? { + Ok(get_content_thumbnail::Response { file, content_type }.into()) + } else { + Err(Error::BadRequest(ErrorKind::NotFound, "Media not found.")) + } +} diff --git a/src/client_server/membership.rs b/src/client_server/membership.rs new file mode 100644 index 0000000..716d5e4 --- /dev/null +++ b/src/client_server/membership.rs @@ -0,0 +1,422 @@ +use super::State; +use crate::{pdu::PduBuilder, ConduitResult, Database, Error, Ruma}; +use ruma::{ + api::client::{ + error::ErrorKind, + r0::membership::{ + ban_user, forget_room, get_member_events, invite_user, join_room_by_id, + join_room_by_id_or_alias, joined_members, joined_rooms, kick_user, leave_room, + unban_user, + }, + }, + events::{room::member, EventType}, + Raw, RoomId, +}; +use std::{collections::BTreeMap, convert::TryFrom}; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, post}; + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_>/join", data = "") +)] +pub fn join_room_by_id_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + // TODO: Ask a remote server if we don't have this room + + let event = member::MemberEventContent { + membership: member::MembershipState::Join, + displayname: db.users.displayname(&sender_id)?, + avatar_url: db.users.avatar_url(&sender_id)?, + is_direct: None, + third_party_invite: None, + }; + + db.rooms.append_pdu( + PduBuilder { + room_id: body.room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomMember, + content: serde_json::to_value(event).expect("event is valid, we just created it"), + unsigned: None, + state_key: Some(sender_id.to_string()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + Ok(join_room_by_id::Response { + room_id: body.room_id.clone(), + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/join/<_>", data = "") +)] +pub fn join_room_by_id_or_alias_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let room_id = RoomId::try_from(body.room_id_or_alias.clone()).or_else(|alias| { + Ok::<_, Error>(db.rooms.id_from_alias(&alias)?.ok_or(Error::BadRequest( + ErrorKind::NotFound, + "Room not found (TODO: Federation).", + ))?) + })?; + + let body = Ruma { + sender_id: body.sender_id.clone(), + device_id: body.device_id.clone(), + json_body: None, + body: join_room_by_id::Request { + room_id, + third_party_signed: body.third_party_signed.clone(), + }, + }; + + Ok(join_room_by_id_or_alias::Response { + room_id: join_room_by_id_route(db, body)?.0.room_id, + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_>/leave", data = "") +)] +pub fn leave_room_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let mut event = serde_json::from_value::>( + db.rooms + .room_state_get( + &body.room_id, + &EventType::RoomMember, + &sender_id.to_string(), + )? + .ok_or(Error::BadRequest( + ErrorKind::BadState, + "Cannot leave a room you are not a member of.", + ))? + .content, + ) + .expect("from_value::> can never fail") + .deserialize() + .map_err(|_| Error::bad_database("Invalid member event in database."))?; + + event.membership = member::MembershipState::Leave; + + db.rooms.append_pdu( + PduBuilder { + room_id: body.room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomMember, + content: serde_json::to_value(event).expect("event is valid, we just created it"), + unsigned: None, + state_key: Some(sender_id.to_string()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + Ok(leave_room::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_>/invite", data = "") +)] +pub fn invite_user_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + if let invite_user::InvitationRecipient::UserId { user_id } = &body.recipient { + db.rooms.append_pdu( + PduBuilder { + room_id: body.room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomMember, + content: serde_json::to_value(member::MemberEventContent { + membership: member::MembershipState::Invite, + displayname: db.users.displayname(&user_id)?, + avatar_url: db.users.avatar_url(&user_id)?, + is_direct: None, + third_party_invite: None, + }) + .expect("event is valid, we just created it"), + unsigned: None, + state_key: Some(user_id.to_string()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + Ok(invite_user::Response.into()) + } else { + Err(Error::BadRequest(ErrorKind::NotFound, "User not found.")) + } +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_>/kick", data = "") +)] +pub fn kick_user_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let mut event = serde_json::from_value::>( + db.rooms + .room_state_get( + &body.room_id, + &EventType::RoomMember, + &body.user_id.to_string(), + )? + .ok_or(Error::BadRequest( + ErrorKind::BadState, + "Cannot kick member that's not in the room.", + ))? + .content, + ) + .expect("Raw::from_value always works") + .deserialize() + .map_err(|_| Error::bad_database("Invalid member event in database."))?; + + event.membership = ruma::events::room::member::MembershipState::Leave; + // TODO: reason + + db.rooms.append_pdu( + PduBuilder { + room_id: body.room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomMember, + content: serde_json::to_value(event).expect("event is valid, we just created it"), + unsigned: None, + state_key: Some(body.user_id.to_string()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + Ok(kick_user::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_>/ban", data = "") +)] +pub fn ban_user_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + // TODO: reason + + let event = db + .rooms + .room_state_get( + &body.room_id, + &EventType::RoomMember, + &body.user_id.to_string(), + )? + .map_or( + Ok::<_, Error>(member::MemberEventContent { + membership: member::MembershipState::Ban, + displayname: db.users.displayname(&body.user_id)?, + avatar_url: db.users.avatar_url(&body.user_id)?, + is_direct: None, + third_party_invite: None, + }), + |event| { + let mut event = + serde_json::from_value::>(event.content) + .expect("Raw::from_value always works") + .deserialize() + .map_err(|_| Error::bad_database("Invalid member event in database."))?; + event.membership = ruma::events::room::member::MembershipState::Ban; + Ok(event) + }, + )?; + + db.rooms.append_pdu( + PduBuilder { + room_id: body.room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomMember, + content: serde_json::to_value(event).expect("event is valid, we just created it"), + unsigned: None, + state_key: Some(body.user_id.to_string()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + Ok(ban_user::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_>/unban", data = "") +)] +pub fn unban_user_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let mut event = serde_json::from_value::>( + db.rooms + .room_state_get( + &body.room_id, + &EventType::RoomMember, + &body.user_id.to_string(), + )? + .ok_or(Error::BadRequest( + ErrorKind::BadState, + "Cannot unban a user who is not banned.", + ))? + .content, + ) + .expect("from_value::> can never fail") + .deserialize() + .map_err(|_| Error::bad_database("Invalid member event in database."))?; + + event.membership = ruma::events::room::member::MembershipState::Leave; + + db.rooms.append_pdu( + PduBuilder { + room_id: body.room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomMember, + content: serde_json::to_value(event).expect("event is valid, we just created it"), + unsigned: None, + state_key: Some(body.user_id.to_string()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + Ok(unban_user::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_>/forget", data = "") +)] +pub fn forget_room_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + db.rooms.forget(&body.room_id, &sender_id)?; + + Ok(forget_room::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/joined_rooms", data = "") +)] +pub fn joined_rooms_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + Ok(joined_rooms::Response { + joined_rooms: db + .rooms + .rooms_joined(&sender_id) + .filter_map(|r| r.ok()) + .collect(), + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/rooms/<_>/members", data = "") +)] +pub fn get_member_events_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + if !db.rooms.is_joined(sender_id, &body.room_id)? { + return Err(Error::BadRequest( + ErrorKind::Forbidden, + "You don't have permission to view this room.", + )); + } + + Ok(get_member_events::Response { + chunk: db + .rooms + .room_state_type(&body.room_id, &EventType::RoomMember)? + .values() + .map(|pdu| pdu.to_member_event()) + .collect(), + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/rooms/<_>/joined_members", data = "") +)] +pub fn joined_members_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + if !db + .rooms + .is_joined(&sender_id, &body.room_id) + .unwrap_or(false) + { + return Err(Error::BadRequest( + ErrorKind::Forbidden, + "You aren't a member of the room.", + )); + } + + let mut joined = BTreeMap::new(); + for user_id in db.rooms.room_members(&body.room_id).filter_map(|r| r.ok()) { + let display_name = db.users.displayname(&user_id)?; + let avatar_url = db.users.avatar_url(&user_id)?; + + joined.insert( + user_id, + joined_members::RoomMember { + display_name, + avatar_url, + }, + ); + } + + Ok(joined_members::Response { joined }.into()) +} diff --git a/src/client_server/message.rs b/src/client_server/message.rs new file mode 100644 index 0000000..04d965d --- /dev/null +++ b/src/client_server/message.rs @@ -0,0 +1,128 @@ +use super::State; +use crate::{pdu::PduBuilder, ConduitResult, Database, Error, Ruma}; +use ruma::api::client::{ + error::ErrorKind, + r0::message::{create_message_event, get_message_events}, +}; +use std::convert::TryInto; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, put}; + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/rooms/<_>/send/<_>/<_>", data = "") +)] +pub fn create_message_event_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let mut unsigned = serde_json::Map::new(); + unsigned.insert("transaction_id".to_owned(), body.txn_id.clone().into()); + + let event_id = db.rooms.append_pdu( + PduBuilder { + room_id: body.room_id.clone(), + sender: sender_id.clone(), + event_type: body.event_type.clone(), + content: serde_json::from_str( + body.json_body + .ok_or(Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))? + .get(), + ) + .map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?, + unsigned: Some(unsigned), + state_key: None, + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + Ok(create_message_event::Response { event_id }.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/rooms/<_>/messages", data = "") +)] +pub fn get_message_events_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + if !db.rooms.is_joined(sender_id, &body.room_id)? { + return Err(Error::BadRequest( + ErrorKind::Forbidden, + "You don't have permission to view this room.", + )); + } + + let from = body + .from + .clone() + .parse() + .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid `from` value."))?; + + let to = body.to.as_ref().map(|t| t.parse()); + + // Use limit or else 10 + let limit = body + .limit + .try_into() + .map_or(Ok::<_, Error>(10_usize), |l: u32| Ok(l as usize))?; + + match body.dir { + get_message_events::Direction::Forward => { + let events_after = db + .rooms + .pdus_after(&sender_id, &body.room_id, from) + .take(limit) + .filter_map(|r| r.ok()) // Filter out buggy events + .take_while(|&(k, _)| Some(Ok(k)) != to) // Stop at `to` + .collect::>(); + + let end_token = events_after.last().map(|(count, _)| count.to_string()); + + let events_after = events_after + .into_iter() + .map(|(_, pdu)| pdu.to_room_event()) + .collect::>(); + + Ok(get_message_events::Response { + start: Some(body.from.clone()), + end: end_token, + chunk: events_after, + state: Vec::new(), + } + .into()) + } + get_message_events::Direction::Backward => { + let events_before = db + .rooms + .pdus_until(&sender_id, &body.room_id, from) + .take(limit) + .filter_map(|r| r.ok()) // Filter out buggy events + .take_while(|&(k, _)| Some(Ok(k)) != to) // Stop at `to` + .collect::>(); + + let start_token = events_before.last().map(|(count, _)| count.to_string()); + + let events_before = events_before + .into_iter() + .map(|(_, pdu)| pdu.to_room_event()) + .collect::>(); + + Ok(get_message_events::Response { + start: Some(body.from.clone()), + end: start_token, + chunk: events_before, + state: Vec::new(), + } + .into()) + } + } +} diff --git a/src/client_server/mod.rs b/src/client_server/mod.rs new file mode 100644 index 0000000..7703198 --- /dev/null +++ b/src/client_server/mod.rs @@ -0,0 +1,78 @@ +mod account; +mod alias; +mod backup; +mod capabilities; +mod config; +mod context; +mod device; +mod directory; +mod filter; +mod keys; +mod media; +mod membership; +mod message; +mod presence; +mod profile; +mod push; +mod read_marker; +mod redact; +mod room; +mod session; +mod state; +mod sync; +mod tag; +mod thirdparty; +mod to_device; +mod typing; +mod unversioned; +mod user_directory; +mod voip; + +pub use account::*; +pub use alias::*; +pub use backup::*; +pub use capabilities::*; +pub use config::*; +pub use context::*; +pub use device::*; +pub use directory::*; +pub use filter::*; +pub use keys::*; +pub use media::*; +pub use membership::*; +pub use message::*; +pub use presence::*; +pub use profile::*; +pub use push::*; +pub use read_marker::*; +pub use redact::*; +pub use room::*; +pub use session::*; +pub use state::*; +pub use sync::*; +pub use tag::*; +pub use thirdparty::*; +pub use to_device::*; +pub use typing::*; +pub use unversioned::*; +pub use user_directory::*; +pub use voip::*; + +#[cfg(not(feature = "conduit_bin"))] +use super::State; +#[cfg(feature = "conduit_bin")] +use { + crate::ConduitResult, + rocket::{options, State}, + ruma::api::client::r0::to_device::send_event_to_device, +}; + +const DEVICE_ID_LENGTH: usize = 10; +const TOKEN_LENGTH: usize = 256; +const SESSION_ID_LENGTH: usize = 256; + +#[cfg(feature = "conduit_bin")] +#[options("/<_..>")] +pub fn options_route() -> ConduitResult { + Ok(send_event_to_device::Response.into()) +} diff --git a/src/client_server/presence.rs b/src/client_server/presence.rs new file mode 100644 index 0000000..0b6a51f --- /dev/null +++ b/src/client_server/presence.rs @@ -0,0 +1,45 @@ +use super::State; +use crate::{utils, ConduitResult, Database, Ruma}; +use ruma::api::client::r0::presence::set_presence; +use std::convert::TryInto; + +#[cfg(feature = "conduit_bin")] +use rocket::put; + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/presence/<_>/status", data = "") +)] +pub fn set_presence_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + for room_id in db.rooms.rooms_joined(&sender_id) { + let room_id = room_id?; + + db.rooms.edus.update_presence( + &sender_id, + &room_id, + ruma::events::presence::PresenceEvent { + content: ruma::events::presence::PresenceEventContent { + avatar_url: db.users.avatar_url(&sender_id)?, + currently_active: None, + displayname: db.users.displayname(&sender_id)?, + last_active_ago: Some( + utils::millis_since_unix_epoch() + .try_into() + .expect("time is valid"), + ), + presence: body.presence, + status_msg: body.status_msg.clone(), + }, + sender: sender_id.clone(), + }, + &db.globals, + )?; + } + + Ok(set_presence::Response.into()) +} diff --git a/src/client_server/profile.rs b/src/client_server/profile.rs new file mode 100644 index 0000000..1313db7 --- /dev/null +++ b/src/client_server/profile.rs @@ -0,0 +1,236 @@ +use super::State; +use crate::{pdu::PduBuilder, utils, ConduitResult, Database, Error, Ruma}; +use ruma::{ + api::client::{ + error::ErrorKind, + r0::profile::{ + get_avatar_url, get_display_name, get_profile, set_avatar_url, set_display_name, + }, + }, + events::EventType, + Raw, +}; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, put}; +use std::convert::TryInto; + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/profile/<_>/displayname", data = "") +)] +pub fn set_displayname_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + db.users + .set_displayname(&sender_id, body.displayname.clone())?; + + // Send a new membership event and presence update into all joined rooms + for room_id in db.rooms.rooms_joined(&sender_id) { + let room_id = room_id?; + db.rooms.append_pdu( + PduBuilder { + room_id: room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomMember, + content: serde_json::to_value(ruma::events::room::member::MemberEventContent { + displayname: body.displayname.clone(), + ..serde_json::from_value::>( + db.rooms + .room_state_get( + &room_id, + &EventType::RoomMember, + &sender_id.to_string(), + )? + .ok_or_else(|| { + Error::bad_database( + "Tried to send displayname update for user not in the room.", + ) + })? + .content + .clone(), + ) + .expect("from_value::> can never fail") + .deserialize() + .map_err(|_| Error::bad_database("Database contains invalid PDU."))? + }) + .expect("event is valid, we just created it"), + unsigned: None, + state_key: Some(sender_id.to_string()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + // Presence update + db.rooms.edus.update_presence( + &sender_id, + &room_id, + ruma::events::presence::PresenceEvent { + content: ruma::events::presence::PresenceEventContent { + avatar_url: db.users.avatar_url(&sender_id)?, + currently_active: None, + displayname: db.users.displayname(&sender_id)?, + last_active_ago: Some( + utils::millis_since_unix_epoch() + .try_into() + .expect("time is valid"), + ), + presence: ruma::presence::PresenceState::Online, + status_msg: None, + }, + sender: sender_id.clone(), + }, + &db.globals, + )?; + } + + Ok(set_display_name::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/profile/<_>/displayname", data = "") +)] +pub fn get_displayname_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + Ok(get_display_name::Response { + displayname: db.users.displayname(&body.user_id)?, + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/profile/<_>/avatar_url", data = "") +)] +pub fn set_avatar_url_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + if let Some(avatar_url) = &body.avatar_url { + if !avatar_url.starts_with("mxc://") { + return Err(Error::BadRequest( + ErrorKind::InvalidParam, + "avatar_url has to start with mxc://.", + )); + } + + // TODO in the future when we can handle media uploads make sure that this url is our own server + // TODO also make sure this is valid mxc:// format (not only starting with it) + } + + db.users + .set_avatar_url(&sender_id, body.avatar_url.clone())?; + + // Send a new membership event and presence update into all joined rooms + for room_id in db.rooms.rooms_joined(&sender_id) { + let room_id = room_id?; + db.rooms.append_pdu( + PduBuilder { + room_id: room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomMember, + content: serde_json::to_value(ruma::events::room::member::MemberEventContent { + avatar_url: body.avatar_url.clone(), + ..serde_json::from_value::>( + db.rooms + .room_state_get( + &room_id, + &EventType::RoomMember, + &sender_id.to_string(), + )? + .ok_or_else(|| { + Error::bad_database( + "Tried to send avatar url update for user not in the room.", + ) + })? + .content + .clone(), + ) + .expect("from_value::> can never fail") + .deserialize() + .map_err(|_| Error::bad_database("Database contains invalid PDU."))? + }) + .expect("event is valid, we just created it"), + unsigned: None, + state_key: Some(sender_id.to_string()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + // Presence update + db.rooms.edus.update_presence( + &sender_id, + &room_id, + ruma::events::presence::PresenceEvent { + content: ruma::events::presence::PresenceEventContent { + avatar_url: db.users.avatar_url(&sender_id)?, + currently_active: None, + displayname: db.users.displayname(&sender_id)?, + last_active_ago: Some( + utils::millis_since_unix_epoch() + .try_into() + .expect("time is valid"), + ), + presence: ruma::presence::PresenceState::Online, + status_msg: None, + }, + sender: sender_id.clone(), + }, + &db.globals, + )?; + } + + Ok(set_avatar_url::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/profile/<_>/avatar_url", data = "") +)] +pub fn get_avatar_url_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + Ok(get_avatar_url::Response { + avatar_url: db.users.avatar_url(&body.user_id)?, + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/profile/<_>", data = "") +)] +pub fn get_profile_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let avatar_url = db.users.avatar_url(&body.user_id)?; + let displayname = db.users.displayname(&body.user_id)?; + + if avatar_url.is_none() && displayname.is_none() { + // Return 404 if we don't have a profile for this id + return Err(Error::BadRequest( + ErrorKind::NotFound, + "Profile was not found.", + )); + } + + Ok(get_profile::Response { + avatar_url, + displayname, + } + .into()) +} diff --git a/src/client_server/push.rs b/src/client_server/push.rs new file mode 100644 index 0000000..906f4a7 --- /dev/null +++ b/src/client_server/push.rs @@ -0,0 +1,75 @@ +use super::State; +use crate::{ConduitResult, Database, Error, Ruma}; +use log::warn; +use ruma::{ + api::client::{ + error::ErrorKind, + r0::push::{get_pushers, get_pushrules_all, set_pushrule, set_pushrule_enabled}, + }, + events::EventType, +}; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, post, put}; + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/pushrules", data = "") +)] +pub fn get_pushrules_all_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let event = db + .account_data + .get::(None, &sender_id, EventType::PushRules)? + .ok_or(Error::BadRequest( + ErrorKind::NotFound, + "PushRules event not found.", + ))?; + + Ok(get_pushrules_all::Response { + global: event.content.global, + } + .into()) +} + +#[cfg_attr(feature = "conduit_bin", put( + "/_matrix/client/r0/pushrules/<_>/<_>/<_>", + //data = "" +))] +pub fn set_pushrule_route(//db: State<'_, Database>, + //body: Ruma, +) -> ConduitResult { + // TODO + warn!("TODO: set_pushrule_route"); + Ok(set_pushrule::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/pushrules/<_>/<_>/<_>/enabled") +)] +pub fn set_pushrule_enabled_route() -> ConduitResult { + // TODO + warn!("TODO: set_pushrule_enabled_route"); + Ok(set_pushrule_enabled::Response.into()) +} + +#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/pushers"))] +pub fn get_pushers_route() -> ConduitResult { + Ok(get_pushers::Response { + pushers: Vec::new(), + } + .into()) +} + +#[cfg_attr(feature = "conduit_bin", post("/_matrix/client/r0/pushers/set"))] +pub fn set_pushers_route() -> ConduitResult { + Ok(get_pushers::Response { + pushers: Vec::new(), + } + .into()) +} diff --git a/src/client_server/read_marker.rs b/src/client_server/read_marker.rs new file mode 100644 index 0000000..ff72765 --- /dev/null +++ b/src/client_server/read_marker.rs @@ -0,0 +1,74 @@ +use super::State; +use crate::{ConduitResult, Database, Error, Ruma}; +use ruma::{ + api::client::{error::ErrorKind, r0::read_marker::set_read_marker}, + events::{AnyEphemeralRoomEvent, AnyEvent, EventType}, +}; + +#[cfg(feature = "conduit_bin")] +use rocket::post; +use std::{collections::BTreeMap, time::SystemTime}; + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/rooms/<_>/read_markers", data = "") +)] +pub fn set_read_marker_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let fully_read_event = ruma::events::fully_read::FullyReadEvent { + content: ruma::events::fully_read::FullyReadEventContent { + event_id: body.fully_read.clone(), + }, + room_id: body.room_id.clone(), + }; + db.account_data.update( + Some(&body.room_id), + &sender_id, + EventType::FullyRead, + &fully_read_event, + &db.globals, + )?; + + if let Some(event) = &body.read_receipt { + db.rooms.edus.room_read_set( + &body.room_id, + &sender_id, + db.rooms.get_pdu_count(event)?.ok_or(Error::BadRequest( + ErrorKind::InvalidParam, + "Event does not exist.", + ))?, + )?; + + let mut user_receipts = BTreeMap::new(); + user_receipts.insert( + sender_id.clone(), + ruma::events::receipt::Receipt { + ts: Some(SystemTime::now()), + }, + ); + let mut receipt_content = BTreeMap::new(); + receipt_content.insert( + event.clone(), + ruma::events::receipt::Receipts { + read: Some(user_receipts), + }, + ); + + db.rooms.edus.roomlatest_update( + &sender_id, + &body.room_id, + AnyEvent::Ephemeral(AnyEphemeralRoomEvent::Receipt( + ruma::events::receipt::ReceiptEvent { + content: ruma::events::receipt::ReceiptEventContent(receipt_content), + room_id: body.room_id.clone(), + }, + )), + &db.globals, + )?; + } + Ok(set_read_marker::Response.into()) +} diff --git a/src/client_server/redact.rs b/src/client_server/redact.rs new file mode 100644 index 0000000..fc65c23 --- /dev/null +++ b/src/client_server/redact.rs @@ -0,0 +1,39 @@ +use super::State; +use crate::{pdu::PduBuilder, ConduitResult, Database, Ruma}; +use ruma::{ + api::client::r0::redact::redact_event, + events::{room::redaction, EventType}, +}; + +#[cfg(feature = "conduit_bin")] +use rocket::put; + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/rooms/<_>/redact/<_>/<_>", data = "") +)] +pub fn redact_event_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let event_id = db.rooms.append_pdu( + PduBuilder { + room_id: body.room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomRedaction, + content: serde_json::to_value(redaction::RedactionEventContent { + reason: body.reason.clone(), + }) + .expect("event is valid, we just created it"), + unsigned: None, + state_key: None, + redacts: Some(body.event_id.clone()), + }, + &db.globals, + &db.account_data, + )?; + + Ok(redact_event::Response { event_id }.into()) +} diff --git a/src/client_server/room.rs b/src/client_server/room.rs new file mode 100644 index 0000000..54e57fd --- /dev/null +++ b/src/client_server/room.rs @@ -0,0 +1,345 @@ +use super::State; +use crate::{pdu::PduBuilder, ConduitResult, Database, Error, Ruma}; +use ruma::{ + api::client::{ + error::ErrorKind, + r0::room::{self, create_room, get_room_event}, + }, + events::{ + room::{guest_access, history_visibility, join_rules, member, name, topic}, + EventType, + }, + RoomAliasId, RoomId, RoomVersionId, +}; +use std::{collections::BTreeMap, convert::TryFrom}; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, post}; + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/createRoom", data = "") +)] +pub fn create_room_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let room_id = RoomId::new(db.globals.server_name()); + + let alias = body + .room_alias_name + .as_ref() + .map_or(Ok(None), |localpart| { + // TODO: Check for invalid characters and maximum length + let alias = + RoomAliasId::try_from(format!("#{}:{}", localpart, db.globals.server_name())) + .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?; + + if db.rooms.id_from_alias(&alias)?.is_some() { + Err(Error::BadRequest( + ErrorKind::RoomInUse, + "Room alias already exists.", + )) + } else { + Ok(Some(alias)) + } + })?; + + let mut content = ruma::events::room::create::CreateEventContent::new(sender_id.clone()); + content.federate = body.creation_content.as_ref().map_or(true, |c| c.federate); + content.predecessor = body + .creation_content + .as_ref() + .and_then(|c| c.predecessor.clone()); + content.room_version = RoomVersionId::Version6; + + // 1. The room create event + db.rooms.append_pdu( + PduBuilder { + room_id: room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomCreate, + content: serde_json::to_value(content).expect("event is valid, we just created it"), + unsigned: None, + state_key: Some("".to_owned()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + // 2. Let the room creator join + db.rooms.append_pdu( + PduBuilder { + room_id: room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomMember, + content: serde_json::to_value(member::MemberEventContent { + membership: member::MembershipState::Join, + displayname: db.users.displayname(&sender_id)?, + avatar_url: db.users.avatar_url(&sender_id)?, + is_direct: body.is_direct, + third_party_invite: None, + }) + .expect("event is valid, we just created it"), + unsigned: None, + state_key: Some(sender_id.to_string()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + // Figure out preset. We need it for power levels and preset specific events + let visibility = body.visibility.unwrap_or(room::Visibility::Private); + let preset = body.preset.unwrap_or_else(|| match visibility { + room::Visibility::Private => create_room::RoomPreset::PrivateChat, + room::Visibility::Public => create_room::RoomPreset::PublicChat, + }); + + // 3. Power levels + let mut users = BTreeMap::new(); + users.insert(sender_id.clone(), 100.into()); + for invite_ in &body.invite { + users.insert(invite_.clone(), 100.into()); + } + + let power_levels_content = if let Some(power_levels) = &body.power_level_content_override { + serde_json::from_str(power_levels.json().get()).map_err(|_| { + Error::BadRequest(ErrorKind::BadJson, "Invalid power_level_content_override.") + })? + } else { + serde_json::to_value(ruma::events::room::power_levels::PowerLevelsEventContent { + ban: 50.into(), + events: BTreeMap::new(), + events_default: 0.into(), + invite: 50.into(), + kick: 50.into(), + redact: 50.into(), + state_default: 50.into(), + users, + users_default: 0.into(), + notifications: ruma::events::room::power_levels::NotificationPowerLevels { + room: 50.into(), + }, + }) + .expect("event is valid, we just created it") + }; + db.rooms.append_pdu( + PduBuilder { + room_id: room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomPowerLevels, + content: power_levels_content, + unsigned: None, + state_key: Some("".to_owned()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + // 4. Events set by preset + // 4.1 Join Rules + db.rooms.append_pdu( + PduBuilder { + room_id: room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomJoinRules, + content: match preset { + create_room::RoomPreset::PublicChat => serde_json::to_value( + join_rules::JoinRulesEventContent::new(join_rules::JoinRule::Public), + ) + .expect("event is valid, we just created it"), + // according to spec "invite" is the default + _ => serde_json::to_value(join_rules::JoinRulesEventContent::new( + join_rules::JoinRule::Invite, + )) + .expect("event is valid, we just created it"), + }, + unsigned: None, + state_key: Some("".to_owned()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + // 4.2 History Visibility + db.rooms.append_pdu( + PduBuilder { + room_id: room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomHistoryVisibility, + content: serde_json::to_value(history_visibility::HistoryVisibilityEventContent::new( + history_visibility::HistoryVisibility::Shared, + )) + .expect("event is valid, we just created it"), + unsigned: None, + state_key: Some("".to_owned()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + // 4.3 Guest Access + db.rooms.append_pdu( + PduBuilder { + room_id: room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomGuestAccess, + content: match preset { + create_room::RoomPreset::PublicChat => { + serde_json::to_value(guest_access::GuestAccessEventContent::new( + guest_access::GuestAccess::Forbidden, + )) + .expect("event is valid, we just created it") + } + _ => serde_json::to_value(guest_access::GuestAccessEventContent::new( + guest_access::GuestAccess::CanJoin, + )) + .expect("event is valid, we just created it"), + }, + unsigned: None, + state_key: Some("".to_owned()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + // 5. Events listed in initial_state + for create_room::InitialStateEvent { + event_type, + state_key, + content, + } in &body.initial_state + { + // Silently skip encryption events if they are not allowed + if event_type == &EventType::RoomEncryption && db.globals.encryption_disabled() { + continue; + } + + db.rooms.append_pdu( + PduBuilder { + room_id: room_id.clone(), + sender: sender_id.clone(), + event_type: event_type.clone(), + content: serde_json::from_str(content.get()).map_err(|_| { + Error::BadRequest(ErrorKind::BadJson, "Invalid initial_state content.") + })?, + unsigned: None, + state_key: state_key.clone(), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + } + + // 6. Events implied by name and topic + if let Some(name) = &body.name { + db.rooms.append_pdu( + PduBuilder { + room_id: room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomName, + content: serde_json::to_value( + name::NameEventContent::new(name.clone()).map_err(|_| { + Error::BadRequest(ErrorKind::InvalidParam, "Name is invalid.") + })?, + ) + .expect("event is valid, we just created it"), + unsigned: None, + state_key: Some("".to_owned()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + } + + if let Some(topic) = &body.topic { + db.rooms.append_pdu( + PduBuilder { + room_id: room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomTopic, + content: serde_json::to_value(topic::TopicEventContent { + topic: topic.clone(), + }) + .expect("event is valid, we just created it"), + unsigned: None, + state_key: Some("".to_owned()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + } + + // 7. Events implied by invite (and TODO: invite_3pid) + for user in &body.invite { + db.rooms.append_pdu( + PduBuilder { + room_id: room_id.clone(), + sender: sender_id.clone(), + event_type: EventType::RoomMember, + content: serde_json::to_value(member::MemberEventContent { + membership: member::MembershipState::Invite, + displayname: db.users.displayname(&user)?, + avatar_url: db.users.avatar_url(&user)?, + is_direct: body.is_direct, + third_party_invite: None, + }) + .expect("event is valid, we just created it"), + unsigned: None, + state_key: Some(user.to_string()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + } + + // Homeserver specific stuff + if let Some(alias) = alias { + db.rooms.set_alias(&alias, Some(&room_id), &db.globals)?; + } + + if let Some(room::Visibility::Public) = body.visibility { + db.rooms.set_public(&room_id, true)?; + } + + Ok(create_room::Response { room_id }.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/rooms/<_>/event/<_>", data = "") +)] +pub fn get_room_event_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + if !db.rooms.is_joined(sender_id, &body.room_id)? { + return Err(Error::BadRequest( + ErrorKind::Forbidden, + "You don't have permission to view this room.", + )); + } + + Ok(get_room_event::Response { + event: db + .rooms + .get_pdu(&body.event_id)? + .ok_or(Error::BadRequest(ErrorKind::NotFound, "Event not found."))? + .to_room_event(), + } + .into()) +} diff --git a/src/client_server/session.rs b/src/client_server/session.rs new file mode 100644 index 0000000..a431d23 --- /dev/null +++ b/src/client_server/session.rs @@ -0,0 +1,128 @@ +use super::State; +use super::{DEVICE_ID_LENGTH, TOKEN_LENGTH}; +use crate::{utils, ConduitResult, Database, Error, Ruma}; +use ruma::{ + api::client::{ + error::ErrorKind, + r0::session::{get_login_types, login, logout, logout_all}, + }, + UserId, +}; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, post}; + +#[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], + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/login", data = "") +)] +pub fn login_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + // Validate login method + let user_id = + // TODO: Other login methods + if let (login::UserInfo::MatrixId(username), login::LoginInfo::Password { password }) = + (body.user.clone(), body.login_info.clone()) + { + let user_id = UserId::parse_with_server_name(username, db.globals.server_name()) + .map_err(|_| Error::BadRequest( + ErrorKind::InvalidUsername, + "Username is invalid." + ))?; + let hash = db.users.password_hash(&user_id)? + .ok_or(Error::BadRequest( + ErrorKind::Forbidden, + "Wrong username or password." + ))?; + + if hash.is_empty() { + return Err(Error::BadRequest( + ErrorKind::UserDeactivated, + "The user has been deactivated" + )); + } + + let hash_matches = + argon2::verify_encoded(&hash, password.as_bytes()).unwrap_or(false); + + if !hash_matches { + return Err(Error::BadRequest(ErrorKind::Forbidden, "Wrong username or password.")); + } + + user_id + } else { + return Err(Error::BadRequest(ErrorKind::Forbidden, "Bad login type.")); + }; + + // Generate new device id if the user didn't specify one + let device_id = body + .body + .device_id + .clone() + .unwrap_or_else(|| utils::random_string(DEVICE_ID_LENGTH).into()); + + // Generate a new token for the device + let token = utils::random_string(TOKEN_LENGTH); + + // Add device + db.users.create_device( + &user_id, + &device_id, + &token, + body.initial_device_display_name.clone(), + )?; + + Ok(login::Response { + user_id, + access_token: token, + home_server: Some(db.globals.server_name().to_owned()), + device_id, + well_known: None, + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/logout", data = "") +)] +pub fn logout_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + let device_id = body.device_id.as_ref().expect("user is authenticated"); + + db.users.remove_device(&sender_id, device_id)?; + + Ok(logout::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/logout/all", data = "") +)] +pub fn logout_all_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + for device_id in db.users.all_device_ids(sender_id) { + if let Ok(device_id) = device_id { + db.users.remove_device(&sender_id, &device_id)?; + } + } + + Ok(logout_all::Response.into()) +} diff --git a/src/client_server/state.rs b/src/client_server/state.rs new file mode 100644 index 0000000..814a246 --- /dev/null +++ b/src/client_server/state.rs @@ -0,0 +1,216 @@ +use super::State; +use crate::{pdu::PduBuilder, ConduitResult, Database, Error, Ruma}; +use ruma::{ + api::client::{ + error::ErrorKind, + r0::state::{ + create_state_event_for_empty_key, create_state_event_for_key, get_state_events, + get_state_events_for_empty_key, get_state_events_for_key, + }, + }, + events::{room::canonical_alias, EventType}, + Raw, +}; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, put}; + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/rooms/<_>/state/<_>/<_>", data = "") +)] +pub fn create_state_event_for_key_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let content = serde_json::from_str::( + body.json_body + .as_ref() + .ok_or(Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))? + .get(), + ) + .map_err(|_| Error::BadRequest(ErrorKind::BadJson, "Invalid JSON body."))?; + + if body.event_type == EventType::RoomCanonicalAlias { + let canonical_alias = serde_json::from_value::< + Raw, + >(content.clone()) + .expect("from_value::> can never fail") + .deserialize() + .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid canonical alias."))?; + + let mut aliases = canonical_alias.alt_aliases; + + if let Some(alias) = canonical_alias.alias { + aliases.push(alias); + } + + for alias in aliases { + if alias.server_name() != db.globals.server_name() + || db + .rooms + .id_from_alias(&alias)? + .filter(|room| room == &body.room_id) // Make sure it's the right room + .is_none() + { + return Err(Error::BadRequest( + ErrorKind::Forbidden, + "You are only allowed to send canonical_alias \ + events when it's aliases already exists", + )); + } + } + } + + let event_id = db.rooms.append_pdu( + PduBuilder { + room_id: body.room_id.clone(), + sender: sender_id.clone(), + event_type: body.event_type.clone(), + content, + unsigned: None, + state_key: Some(body.state_key.clone()), + redacts: None, + }, + &db.globals, + &db.account_data, + )?; + + Ok(create_state_event_for_key::Response { event_id }.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/rooms/<_>/state/<_>", data = "") +)] +pub fn create_state_event_for_empty_key_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + // This just calls create_state_event_for_key_route + let Ruma { + body: + create_state_event_for_empty_key::Request { + room_id, + event_type, + data, + }, + sender_id, + device_id, + json_body, + } = body; + + Ok(create_state_event_for_empty_key::Response { + event_id: create_state_event_for_key_route( + db, + Ruma { + body: create_state_event_for_key::Request { + room_id, + event_type, + data, + state_key: "".to_owned(), + }, + sender_id, + device_id, + json_body, + }, + )? + .0 + .event_id, + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/rooms/<_>/state", data = "") +)] +pub fn get_state_events_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + if !db.rooms.is_joined(sender_id, &body.room_id)? { + return Err(Error::BadRequest( + ErrorKind::Forbidden, + "You don't have permission to view the room state.", + )); + } + + Ok(get_state_events::Response { + room_state: db + .rooms + .room_state_full(&body.room_id)? + .values() + .map(|pdu| pdu.to_state_event()) + .collect(), + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/rooms/<_>/state/<_>/<_>", data = "") +)] +pub fn get_state_events_for_key_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + if !db.rooms.is_joined(sender_id, &body.room_id)? { + return Err(Error::BadRequest( + ErrorKind::Forbidden, + "You don't have permission to view the room state.", + )); + } + + let event = db + .rooms + .room_state_get(&body.room_id, &body.event_type, &body.state_key)? + .ok_or(Error::BadRequest( + ErrorKind::NotFound, + "State event not found.", + ))?; + + Ok(get_state_events_for_key::Response { + content: serde_json::value::to_raw_value(&event.content) + .map_err(|_| Error::bad_database("Invalid event content in database"))?, + } + .into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/rooms/<_>/state/<_>", data = "") +)] +pub fn get_state_events_for_empty_key_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + if !db.rooms.is_joined(sender_id, &body.room_id)? { + return Err(Error::BadRequest( + ErrorKind::Forbidden, + "You don't have permission to view the room state.", + )); + } + + let event = db + .rooms + .room_state_get(&body.room_id, &body.event_type, "")? + .ok_or(Error::BadRequest( + ErrorKind::NotFound, + "State event not found.", + ))?; + + Ok(get_state_events_for_empty_key::Response { + content: serde_json::value::to_raw_value(&event) + .map_err(|_| Error::bad_database("Invalid event content in database"))?, + } + .into()) +} diff --git a/src/client_server/sync.rs b/src/client_server/sync.rs new file mode 100644 index 0000000..71e3422 --- /dev/null +++ b/src/client_server/sync.rs @@ -0,0 +1,477 @@ +use super::State; +use crate::{ConduitResult, Database, Error, Ruma}; +use ruma::{ + api::client::r0::sync::sync_events, + events::{AnySyncEphemeralRoomEvent, EventType}, + Raw, +}; + +#[cfg(feature = "conduit_bin")] +use rocket::{get, tokio}; +use std::{ + collections::{hash_map, BTreeMap, HashMap, HashSet}, + time::Duration, +}; + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/sync", data = "") +)] +pub async fn sync_events_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + let device_id = body.device_id.as_ref().expect("user is authenticated"); + + // TODO: match body.set_presence { + db.rooms.edus.ping_presence(&sender_id)?; + + // Setup watchers, so if there's no response, we can wait for them + let watcher = db.watch(sender_id, device_id); + + let next_batch = db.globals.current_count()?.to_string(); + + let mut joined_rooms = BTreeMap::new(); + let since = body + .since + .clone() + .and_then(|string| string.parse().ok()) + .unwrap_or(0); + + let mut presence_updates = HashMap::new(); + let mut device_list_updates = HashSet::new(); + + // Look for device list updates of this account + device_list_updates.extend( + db.users + .keys_changed(&sender_id.to_string(), since, None) + .filter_map(|r| r.ok()), + ); + + for room_id in db.rooms.rooms_joined(&sender_id) { + let room_id = room_id?; + + let mut non_timeline_pdus = db + .rooms + .pdus_since(&sender_id, &room_id, since)? + .filter_map(|r| r.ok()); // Filter out buggy events + + // Take the last 10 events for the timeline + let timeline_pdus = non_timeline_pdus + .by_ref() + .rev() + .take(10) + .collect::>() + .into_iter() + .rev() + .collect::>(); + + // They /sync response doesn't always return all messages, so we say the output is + // limited unless there are events in non_timeline_pdus + //let mut limited = false; + + let mut state_pdus = Vec::new(); + for pdu in non_timeline_pdus { + if pdu.state_key.is_some() { + state_pdus.push(pdu); + } + } + + let mut send_member_count = false; + let mut joined_since_last_sync = false; + let mut send_notification_counts = false; + for pdu in db + .rooms + .pdus_since(&sender_id, &room_id, since)? + .filter_map(|r| r.ok()) + { + send_notification_counts = true; + if pdu.kind == EventType::RoomMember { + send_member_count = true; + if !joined_since_last_sync && pdu.state_key == Some(sender_id.to_string()) { + let content = serde_json::from_value::< + Raw, + >(pdu.content.clone()) + .expect("Raw::from_value always works") + .deserialize() + .map_err(|_| Error::bad_database("Invalid PDU in database."))?; + if content.membership == ruma::events::room::member::MembershipState::Join { + joined_since_last_sync = true; + // Both send_member_count and joined_since_last_sync are set. There's + // nothing more to do + break; + } + } + } + } + + let members = db.rooms.room_state_type(&room_id, &EventType::RoomMember)?; + + let (joined_member_count, invited_member_count, heroes) = if send_member_count { + let joined_member_count = db.rooms.room_members(&room_id).count(); + let invited_member_count = db.rooms.room_members_invited(&room_id).count(); + + // Recalculate heroes (first 5 members) + let mut heroes = Vec::new(); + + if joined_member_count + invited_member_count <= 5 { + // Go through all PDUs and for each member event, check if the user is still joined or + // invited until we have 5 or we reach the end + + for hero in db + .rooms + .all_pdus(&sender_id, &room_id)? + .filter_map(|pdu| pdu.ok()) // Ignore all broken pdus + .filter(|pdu| pdu.kind == EventType::RoomMember) + .map(|pdu| { + let content = serde_json::from_value::< + Raw, + >(pdu.content.clone()) + .expect("Raw::from_value always works") + .deserialize() + .map_err(|_| Error::bad_database("Invalid member event in database."))?; + + if let Some(state_key) = &pdu.state_key { + let current_content = serde_json::from_value::< + Raw, + >( + members + .get(state_key) + .ok_or_else(|| { + Error::bad_database( + "A user that joined once has no member event anymore.", + ) + })? + .content + .clone(), + ) + .expect("Raw::from_value always works") + .deserialize() + .map_err(|_| { + Error::bad_database("Invalid member event in database.") + })?; + + // The membership was and still is invite or join + if matches!( + content.membership, + ruma::events::room::member::MembershipState::Join + | ruma::events::room::member::MembershipState::Invite + ) && matches!( + current_content.membership, + ruma::events::room::member::MembershipState::Join + | ruma::events::room::member::MembershipState::Invite + ) { + Ok::<_, Error>(Some(state_key.clone())) + } else { + Ok(None) + } + } else { + Ok(None) + } + }) + .filter_map(|u| u.ok()) // Filter out buggy users + // Filter for possible heroes + .filter_map(|u| u) + { + if heroes.contains(&hero) || hero == sender_id.as_str() { + continue; + } + + heroes.push(hero); + } + } + + ( + Some(joined_member_count), + Some(invited_member_count), + heroes, + ) + } else { + (None, None, Vec::new()) + }; + + let notification_count = if send_notification_counts { + if let Some(last_read) = db.rooms.edus.room_read_get(&room_id, &sender_id)? { + Some( + (db.rooms + .pdus_since(&sender_id, &room_id, last_read)? + .filter_map(|pdu| pdu.ok()) // Filter out buggy events + .filter(|pdu| { + matches!( + pdu.kind.clone(), + EventType::RoomMessage | EventType::RoomEncrypted + ) + }) + .count() as u32) + .into(), + ) + } else { + None + } + } else { + None + }; + + let prev_batch = timeline_pdus.first().map_or(Ok::<_, Error>(None), |e| { + Ok(Some( + db.rooms + .get_pdu_count(&e.event_id)? + .ok_or_else(|| Error::bad_database("Can't find count from event in db."))? + .to_string(), + )) + })?; + + let room_events = timeline_pdus + .into_iter() + .map(|pdu| pdu.to_sync_room_event()) + .collect::>(); + + let mut edus = db + .rooms + .edus + .roomlatests_since(&room_id, since)? + .filter_map(|r| r.ok()) // Filter out buggy events + .collect::>(); + + if db + .rooms + .edus + .last_roomactive_update(&room_id, &db.globals)? + > since + { + edus.push( + serde_json::from_str( + &serde_json::to_string(&AnySyncEphemeralRoomEvent::Typing( + db.rooms.edus.roomactives_all(&room_id)?, + )) + .expect("event is valid, we just created it"), + ) + .expect("event is valid, we just created it"), + ); + } + + let joined_room = sync_events::JoinedRoom { + account_data: sync_events::AccountData { + events: db + .account_data + .changes_since(Some(&room_id), &sender_id, since)? + .into_iter() + .filter_map(|(_, v)| { + serde_json::from_str(v.json().get()) + .map_err(|_| Error::bad_database("Invalid account event in database.")) + .ok() + }) + .collect::>(), + }, + summary: sync_events::RoomSummary { + heroes, + joined_member_count: joined_member_count.map(|n| (n as u32).into()), + invited_member_count: invited_member_count.map(|n| (n as u32).into()), + }, + unread_notifications: sync_events::UnreadNotificationsCount { + highlight_count: None, + notification_count, + }, + timeline: sync_events::Timeline { + limited: joined_since_last_sync, + prev_batch, + events: room_events, + }, + // TODO: state before timeline + state: sync_events::State { + events: if joined_since_last_sync { + db.rooms + .room_state_full(&room_id)? + .into_iter() + .map(|(_, pdu)| pdu.to_sync_state_event()) + .collect() + } else { + Vec::new() + }, + }, + ephemeral: sync_events::Ephemeral { events: edus }, + }; + + if !joined_room.is_empty() { + joined_rooms.insert(room_id.clone(), joined_room); + } + + // Look for device list updates in this room + device_list_updates.extend( + db.users + .keys_changed(&room_id.to_string(), since, None) + .filter_map(|r| r.ok()), + ); + + // Take presence updates from this room + for (user_id, presence) in + db.rooms + .edus + .presence_since(&room_id, since, &db.rooms, &db.globals)? + { + match presence_updates.entry(user_id) { + hash_map::Entry::Vacant(v) => { + v.insert(presence); + } + hash_map::Entry::Occupied(mut o) => { + let p = o.get_mut(); + + // Update existing presence event with more info + p.content.presence = presence.content.presence; + if let Some(status_msg) = presence.content.status_msg { + p.content.status_msg = Some(status_msg); + } + if let Some(last_active_ago) = presence.content.last_active_ago { + p.content.last_active_ago = Some(last_active_ago); + } + if let Some(displayname) = presence.content.displayname { + p.content.displayname = Some(displayname); + } + if let Some(avatar_url) = presence.content.avatar_url { + p.content.avatar_url = Some(avatar_url); + } + if let Some(currently_active) = presence.content.currently_active { + p.content.currently_active = Some(currently_active); + } + } + } + } + } + + let mut left_rooms = BTreeMap::new(); + for room_id in db.rooms.rooms_left(&sender_id) { + let room_id = room_id?; + let pdus = db.rooms.pdus_since(&sender_id, &room_id, since)?; + let room_events = pdus + .filter_map(|pdu| pdu.ok()) // Filter out buggy events + .map(|pdu| pdu.to_sync_room_event()) + .collect(); + + // TODO: Only until leave point + let mut edus = db + .rooms + .edus + .roomlatests_since(&room_id, since)? + .filter_map(|r| r.ok()) // Filter out buggy events + .collect::>(); + + if db + .rooms + .edus + .last_roomactive_update(&room_id, &db.globals)? + > since + { + edus.push( + serde_json::from_str( + &serde_json::to_string(&AnySyncEphemeralRoomEvent::Typing( + db.rooms.edus.roomactives_all(&room_id)?, + )) + .expect("event is valid, we just created it"), + ) + .expect("event is valid, we just created it"), + ); + } + + let left_room = sync_events::LeftRoom { + account_data: sync_events::AccountData { events: Vec::new() }, + timeline: sync_events::Timeline { + limited: false, + prev_batch: Some(next_batch.clone()), + events: room_events, + }, + state: sync_events::State { events: Vec::new() }, + }; + + if !left_room.is_empty() { + left_rooms.insert(room_id.clone(), left_room); + } + } + + let mut invited_rooms = BTreeMap::new(); + for room_id in db.rooms.rooms_invited(&sender_id) { + let room_id = room_id?; + + let invited_room = sync_events::InvitedRoom { + invite_state: sync_events::InviteState { + events: db + .rooms + .room_state_full(&room_id)? + .into_iter() + .map(|(_, pdu)| pdu.to_stripped_state_event()) + .collect(), + }, + }; + + if !invited_room.is_empty() { + invited_rooms.insert(room_id.clone(), invited_room); + } + } + + // Remove all to-device events the device received *last time* + db.users + .remove_to_device_events(sender_id, device_id, since)?; + + let response = sync_events::Response { + next_batch, + rooms: sync_events::Rooms { + leave: left_rooms, + join: joined_rooms, + invite: invited_rooms, + }, + presence: sync_events::Presence { + events: presence_updates + .into_iter() + .map(|(_, v)| Raw::from(v)) + .collect(), + }, + account_data: sync_events::AccountData { + events: db + .account_data + .changes_since(None, &sender_id, since)? + .into_iter() + .filter_map(|(_, v)| { + serde_json::from_str(v.json().get()) + .map_err(|_| Error::bad_database("Invalid account event in database.")) + .ok() + }) + .collect::>(), + }, + device_lists: sync_events::DeviceLists { + changed: device_list_updates.into_iter().collect(), + left: Vec::new(), // TODO + }, + device_one_time_keys_count: if db.users.last_one_time_keys_update(sender_id)? > since { + db.users.count_one_time_keys(sender_id, device_id)? + } else { + BTreeMap::new() + }, + to_device: sync_events::ToDevice { + events: db.users.get_to_device_events(sender_id, device_id)?, + }, + }; + + // TODO: Retry the endpoint instead of returning (waiting for #118) + if !body.full_state + && response.rooms.is_empty() + && response.presence.is_empty() + && response.account_data.is_empty() + && response.device_lists.is_empty() + && response.device_one_time_keys_count.is_empty() + && response.to_device.is_empty() + { + // Hang a few seconds so requests are not spammed + // Stop hanging if new info arrives + let mut duration = body.timeout.unwrap_or_default(); + if duration.as_secs() > 30 { + duration = Duration::from_secs(30); + } + let mut delay = tokio::time::delay_for(duration); + tokio::select! { + _ = &mut delay => {} + _ = watcher => {} + } + } + + Ok(response.into()) +} diff --git a/src/client_server/tag.rs b/src/client_server/tag.rs new file mode 100644 index 0000000..99ee6e3 --- /dev/null +++ b/src/client_server/tag.rs @@ -0,0 +1,100 @@ +use super::State; +use crate::{ConduitResult, Database, Ruma}; +use ruma::{ + api::client::r0::tag::{create_tag, delete_tag, get_tags}, + events::EventType, +}; +use std::collections::BTreeMap; + +#[cfg(feature = "conduit_bin")] +use rocket::{delete, get, put}; + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/user/<_>/rooms/<_>/tags/<_>", data = "") +)] +pub fn update_tag_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let mut tags_event = db + .account_data + .get::(Some(&body.room_id), sender_id, EventType::Tag)? + .unwrap_or_else(|| ruma::events::tag::TagEvent { + content: ruma::events::tag::TagEventContent { + tags: BTreeMap::new(), + }, + }); + tags_event + .content + .tags + .insert(body.tag.to_string(), body.tag_info.clone()); + + db.account_data.update( + Some(&body.room_id), + sender_id, + EventType::Tag, + &tags_event, + &db.globals, + )?; + + Ok(create_tag::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + delete("/_matrix/client/r0/user/<_>/rooms/<_>/tags/<_>", data = "") +)] +pub fn delete_tag_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + let mut tags_event = db + .account_data + .get::(Some(&body.room_id), sender_id, EventType::Tag)? + .unwrap_or_else(|| ruma::events::tag::TagEvent { + content: ruma::events::tag::TagEventContent { + tags: BTreeMap::new(), + }, + }); + tags_event.content.tags.remove(&body.tag); + + db.account_data.update( + Some(&body.room_id), + sender_id, + EventType::Tag, + &tags_event, + &db.globals, + )?; + + Ok(delete_tag::Response.into()) +} + +#[cfg_attr( + feature = "conduit_bin", + get("/_matrix/client/r0/user/<_>/rooms/<_>/tags", data = "") +)] +pub fn get_tags_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + Ok(get_tags::Response { + tags: db + .account_data + .get::(Some(&body.room_id), sender_id, EventType::Tag)? + .unwrap_or_else(|| ruma::events::tag::TagEvent { + content: ruma::events::tag::TagEventContent { + tags: BTreeMap::new(), + }, + }) + .content + .tags, + } + .into()) +} diff --git a/src/client_server/thirdparty.rs b/src/client_server/thirdparty.rs new file mode 100644 index 0000000..d9b540b --- /dev/null +++ b/src/client_server/thirdparty.rs @@ -0,0 +1,19 @@ +use crate::ConduitResult; +use ruma::api::client::r0::thirdparty::get_protocols; + +use log::warn; +#[cfg(feature = "conduit_bin")] +use rocket::get; +use std::collections::BTreeMap; + +#[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 { + protocols: BTreeMap::new(), + } + .into()) +} diff --git a/src/client_server/to_device.rs b/src/client_server/to_device.rs new file mode 100644 index 0000000..db4f36e --- /dev/null +++ b/src/client_server/to_device.rs @@ -0,0 +1,56 @@ +use super::State; +use crate::{ConduitResult, Database, Error, Ruma}; +use ruma::api::client::{ + error::ErrorKind, + r0::to_device::{self, send_event_to_device}, +}; + +#[cfg(feature = "conduit_bin")] +use rocket::put; + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/sendToDevice/<_>/<_>", data = "") +)] +pub fn send_event_to_device_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + for (target_user_id, map) in &body.messages { + for (target_device_id_maybe, event) in map { + match target_device_id_maybe { + to_device::DeviceIdOrAllDevices::DeviceId(target_device_id) => { + db.users.add_to_device_event( + sender_id, + &target_user_id, + &target_device_id, + &body.event_type, + serde_json::from_str(event.get()).map_err(|_| { + Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid") + })?, + &db.globals, + )? + } + + to_device::DeviceIdOrAllDevices::AllDevices => { + for target_device_id in db.users.all_device_ids(&target_user_id) { + db.users.add_to_device_event( + sender_id, + &target_user_id, + &target_device_id?, + &body.event_type, + serde_json::from_str(event.get()).map_err(|_| { + Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid") + })?, + &db.globals, + )?; + } + } + } + } + } + + Ok(send_event_to_device::Response.into()) +} diff --git a/src/client_server/typing.rs b/src/client_server/typing.rs new file mode 100644 index 0000000..7eba13e --- /dev/null +++ b/src/client_server/typing.rs @@ -0,0 +1,33 @@ +use super::State; +use crate::{utils, ConduitResult, Database, Ruma}; +use ruma::api::client::r0::typing::create_typing_event; + +#[cfg(feature = "conduit_bin")] +use rocket::put; + +#[cfg_attr( + feature = "conduit_bin", + put("/_matrix/client/r0/rooms/<_>/typing/<_>", data = "") +)] +pub fn create_typing_event_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let sender_id = body.sender_id.as_ref().expect("user is authenticated"); + + if body.typing { + db.rooms.edus.roomactive_add( + &sender_id, + &body.room_id, + body.timeout.map(|d| d.as_millis() as u64).unwrap_or(30000) + + utils::millis_since_unix_epoch(), + &db.globals, + )?; + } else { + db.rooms + .edus + .roomactive_remove(&sender_id, &body.room_id, &db.globals)?; + } + + Ok(create_typing_event::Response.into()) +} diff --git a/src/client_server/unversioned.rs b/src/client_server/unversioned.rs new file mode 100644 index 0000000..e71c194 --- /dev/null +++ b/src/client_server/unversioned.rs @@ -0,0 +1,19 @@ +use crate::ConduitResult; +use ruma::api::client::unversioned::get_supported_versions; +use std::collections::BTreeMap; + +#[cfg(feature = "conduit_bin")] +use rocket::get; + +#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/versions"))] +pub fn get_supported_versions_route() -> ConduitResult { + let mut unstable_features = BTreeMap::new(); + + unstable_features.insert("org.matrix.e2e_cross_signing".to_owned(), true); + + Ok(get_supported_versions::Response { + versions: vec!["r0.5.0".to_owned(), "r0.6.0".to_owned()], + unstable_features, + } + .into()) +} diff --git a/src/client_server/user_directory.rs b/src/client_server/user_directory.rs new file mode 100644 index 0000000..746deb3 --- /dev/null +++ b/src/client_server/user_directory.rs @@ -0,0 +1,52 @@ +use super::State; +use crate::{ConduitResult, Database, Ruma}; +use ruma::api::client::r0::user_directory::search_users; + +#[cfg(feature = "conduit_bin")] +use rocket::post; + +#[cfg_attr( + feature = "conduit_bin", + post("/_matrix/client/r0/user_directory/search", data = "") +)] +pub fn search_users_route( + db: State<'_, Database>, + body: Ruma, +) -> ConduitResult { + let limit = if let Some(limit) = body.limit { + u64::from(limit) + } else { + 10 + } as usize; + + let mut users = db.users.iter().filter_map(|user_id| { + // Filter out buggy users (they should not exist, but you never know...) + let user_id = user_id.ok()?; + if db.users.is_deactivated(&user_id).ok()? { + return None; + } + + let user = search_users::User { + user_id: user_id.clone(), + display_name: db.users.displayname(&user_id).ok()?, + avatar_url: db.users.avatar_url(&user_id).ok()?, + }; + + if !user.user_id.to_string().contains(&body.search_term) + && user + .display_name + .as_ref() + .filter(|name| name.contains(&body.search_term)) + .is_none() + { + return None; + } + + Some(user) + }); + + let results = users.by_ref().take(limit).collect(); + let limited = users.next().is_some(); + + Ok(search_users::Response { results, limited }.into()) +} diff --git a/src/client_server/voip.rs b/src/client_server/voip.rs new file mode 100644 index 0000000..4671284 --- /dev/null +++ b/src/client_server/voip.rs @@ -0,0 +1,13 @@ +use crate::{ConduitResult, Error}; +use ruma::api::client::{error::ErrorKind, r0::message::create_message_event}; + +#[cfg(feature = "conduit_bin")] +use rocket::get; + +#[cfg_attr(feature = "conduit_bin", get("/_matrix/client/r0/voip/turnServer"))] +pub fn turn_server_route() -> ConduitResult { + Err(Error::BadRequest( + ErrorKind::NotFound, + "There is no turn server yet.", + )) +} diff --git a/src/lib.rs b/src/lib.rs index 5cd8d48..d6f0b55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,14 +2,15 @@ pub mod client_server; mod database; mod error; mod pdu; -pub mod push_rules; +mod push_rules; mod ruma_wrapper; mod utils; -pub use database::{media::FileMeta, Database}; +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); diff --git a/src/main.rs b/src/main.rs index 86d8446..27320b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -91,7 +91,6 @@ fn setup_rocket() -> rocket::Rocket { client_server::get_context_route, client_server::get_message_events_route, client_server::turn_server_route, - client_server::publicised_groups_route, client_server::send_event_to_device_route, client_server::get_media_config_route, client_server::create_content_route, @@ -109,7 +108,7 @@ fn setup_rocket() -> rocket::Rocket { client_server::upload_signing_keys_route, client_server::upload_signatures_route, client_server::get_key_changes_route, - client_server::pushers_route, + client_server::get_pushers_route, client_server::set_pushers_route, //server_server::well_known_server, //server_server::get_server_version,