cargo fmt

Nyaaori/refactor-next
Timo Kösters 2 years ago committed by Nyaaori
parent 33a2b2b772
commit a4637e2ba1
No known key found for this signature in database
GPG Key ID: E7819C3ED4D1F82E

@ -1,4 +1,4 @@
use crate::{utils, Error, Result, services}; use crate::{services, utils, Error, Result};
use bytes::BytesMut; use bytes::BytesMut;
use ruma::api::{IncomingResponse, MatrixVersion, OutgoingRequest, SendAccessToken}; use ruma::api::{IncomingResponse, MatrixVersion, OutgoingRequest, SendAccessToken};
use std::{fmt::Debug, mem, time::Duration}; use std::{fmt::Debug, mem, time::Duration};
@ -45,7 +45,11 @@ where
*reqwest_request.timeout_mut() = Some(Duration::from_secs(30)); *reqwest_request.timeout_mut() = Some(Duration::from_secs(30));
let url = reqwest_request.url().clone(); let url = reqwest_request.url().clone();
let mut response = services().globals.default_client().execute(reqwest_request).await?; let mut response = services()
.globals
.default_client()
.execute(reqwest_request)
.await?;
// reqwest::Response -> http::Response conversion // reqwest::Response -> http::Response conversion
let status = response.status(); let status = response.status();

@ -1,9 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH}; use super::{DEVICE_ID_LENGTH, SESSION_ID_LENGTH, TOKEN_LENGTH};
use crate::{ use crate::{api::client_server, services, utils, Error, Result, Ruma};
utils, Error, Result, Ruma, services, api::client_server,
};
use ruma::{ use ruma::{
api::client::{ api::client::{
account::{ account::{
@ -43,8 +41,10 @@ pub async fn get_register_available_route(
body: Ruma<get_username_availability::v3::IncomingRequest>, body: Ruma<get_username_availability::v3::IncomingRequest>,
) -> Result<get_username_availability::v3::Response> { ) -> Result<get_username_availability::v3::Response> {
// Validate user id // Validate user id
let user_id = let user_id = UserId::parse_with_server_name(
UserId::parse_with_server_name(body.username.to_lowercase(), services().globals.server_name()) body.username.to_lowercase(),
services().globals.server_name(),
)
.ok() .ok()
.filter(|user_id| { .filter(|user_id| {
!user_id.is_historical() && user_id.server_name() == services().globals.server_name() !user_id.is_historical() && user_id.server_name() == services().globals.server_name()
@ -95,8 +95,10 @@ pub async fn register_route(
let user_id = match (&body.username, is_guest) { let user_id = match (&body.username, is_guest) {
(Some(username), false) => { (Some(username), false) => {
let proposed_user_id = let proposed_user_id = UserId::parse_with_server_name(
UserId::parse_with_server_name(username.to_lowercase(), services().globals.server_name()) username.to_lowercase(),
services().globals.server_name(),
)
.ok() .ok()
.filter(|user_id| { .filter(|user_id| {
!user_id.is_historical() !user_id.is_historical()
@ -176,7 +178,8 @@ pub async fn register_route(
// Default to pretty displayname // Default to pretty displayname
let displayname = format!("{} ⚡️", user_id.localpart()); let displayname = format!("{} ⚡️", user_id.localpart());
services().users services()
.users
.set_displayname(&user_id, Some(displayname.clone()))?; .set_displayname(&user_id, Some(displayname.clone()))?;
// Initial account data // Initial account data
@ -188,7 +191,8 @@ pub async fn register_route(
content: ruma::events::push_rules::PushRulesEventContent { content: ruma::events::push_rules::PushRulesEventContent {
global: push::Ruleset::server_default(&user_id), global: push::Ruleset::server_default(&user_id),
}, },
}).expect("to json always works"), })
.expect("to json always works"),
)?; )?;
// Inhibit login does not work for guests // Inhibit login does not work for guests
@ -220,7 +224,8 @@ pub async fn register_route(
)?; )?;
info!("New user {} registered on this server.", user_id); info!("New user {} registered on this server.", user_id);
services().admin services()
.admin
.send_message(RoomMessageEventContent::notice_plain(format!( .send_message(RoomMessageEventContent::notice_plain(format!(
"New user {} registered on this server.", "New user {} registered on this server.",
user_id user_id
@ -229,7 +234,10 @@ pub async fn register_route(
// If this is the first real user, grant them admin privileges // If this is the first real user, grant them admin privileges
// Note: the server user, @conduit:servername, is generated first // Note: the server user, @conduit:servername, is generated first
if services().users.count()? == 2 { if services().users.count()? == 2 {
services().admin.make_user_admin(&user_id, displayname).await?; services()
.admin
.make_user_admin(&user_id, displayname)
.await?;
warn!("Granting {} admin privileges as the first user", user_id); warn!("Granting {} admin privileges as the first user", user_id);
} }
@ -272,26 +280,26 @@ pub async fn change_password_route(
}; };
if let Some(auth) = &body.auth { if let Some(auth) = &body.auth {
let (worked, uiaainfo) = services().uiaa.try_auth( let (worked, uiaainfo) =
sender_user, services()
sender_device, .uiaa
auth, .try_auth(sender_user, sender_device, auth, &uiaainfo)?;
&uiaainfo,
)?;
if !worked { if !worked {
return Err(Error::Uiaa(uiaainfo)); return Err(Error::Uiaa(uiaainfo));
} }
// Success! // Success!
} else if let Some(json) = body.json_body { } else if let Some(json) = body.json_body {
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
services().uiaa services()
.uiaa
.create(sender_user, sender_device, &uiaainfo, &json)?; .create(sender_user, sender_device, &uiaainfo, &json)?;
return Err(Error::Uiaa(uiaainfo)); return Err(Error::Uiaa(uiaainfo));
} else { } else {
return Err(Error::BadRequest(ErrorKind::NotJson, "Not json.")); return Err(Error::BadRequest(ErrorKind::NotJson, "Not json."));
} }
services().users services()
.users
.set_password(sender_user, Some(&body.new_password))?; .set_password(sender_user, Some(&body.new_password))?;
if body.logout_devices { if body.logout_devices {
@ -307,7 +315,8 @@ pub async fn change_password_route(
} }
info!("User {} changed their password.", sender_user); info!("User {} changed their password.", sender_user);
services().admin services()
.admin
.send_message(RoomMessageEventContent::notice_plain(format!( .send_message(RoomMessageEventContent::notice_plain(format!(
"User {} changed their password.", "User {} changed their password.",
sender_user sender_user
@ -321,9 +330,7 @@ pub async fn change_password_route(
/// Get user_id of the sender user. /// Get user_id of the sender user.
/// ///
/// Note: Also works for Application Services /// Note: Also works for Application Services
pub async fn whoami_route( pub async fn whoami_route(body: Ruma<whoami::v3::Request>) -> Result<whoami::v3::Response> {
body: Ruma<whoami::v3::Request>,
) -> Result<whoami::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let device_id = body.sender_device.as_ref().cloned(); let device_id = body.sender_device.as_ref().cloned();
@ -361,19 +368,18 @@ pub async fn deactivate_route(
}; };
if let Some(auth) = &body.auth { if let Some(auth) = &body.auth {
let (worked, uiaainfo) = services().uiaa.try_auth( let (worked, uiaainfo) =
sender_user, services()
sender_device, .uiaa
auth, .try_auth(sender_user, sender_device, auth, &uiaainfo)?;
&uiaainfo,
)?;
if !worked { if !worked {
return Err(Error::Uiaa(uiaainfo)); return Err(Error::Uiaa(uiaainfo));
} }
// Success! // Success!
} else if let Some(json) = body.json_body { } else if let Some(json) = body.json_body {
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
services().uiaa services()
.uiaa
.create(sender_user, sender_device, &uiaainfo, &json)?; .create(sender_user, sender_device, &uiaainfo, &json)?;
return Err(Error::Uiaa(uiaainfo)); return Err(Error::Uiaa(uiaainfo));
} else { } else {
@ -387,7 +393,8 @@ pub async fn deactivate_route(
services().users.deactivate_account(sender_user)?; services().users.deactivate_account(sender_user)?;
info!("User {} deactivated their account.", sender_user); info!("User {} deactivated their account.", sender_user);
services().admin services()
.admin
.send_message(RoomMessageEventContent::notice_plain(format!( .send_message(RoomMessageEventContent::notice_plain(format!(
"User {} deactivated their account.", "User {} deactivated their account.",
sender_user sender_user

@ -1,4 +1,4 @@
use crate::{Error, Result, Ruma, services}; use crate::{services, Error, Result, Ruma};
use regex::Regex; use regex::Regex;
use ruma::{ use ruma::{
api::{ api::{
@ -25,11 +25,18 @@ pub async fn create_alias_route(
)); ));
} }
if services().rooms.alias.resolve_local_alias(&body.room_alias)?.is_some() { if services()
.rooms
.alias
.resolve_local_alias(&body.room_alias)?
.is_some()
{
return Err(Error::Conflict("Alias already exists.")); return Err(Error::Conflict("Alias already exists."));
} }
services().rooms.alias services()
.rooms
.alias
.set_alias(&body.room_alias, &body.room_id)?; .set_alias(&body.room_alias, &body.room_id)?;
Ok(create_alias::v3::Response::new()) Ok(create_alias::v3::Response::new())
@ -69,9 +76,7 @@ pub async fn get_alias_route(
get_alias_helper(&body.room_alias).await get_alias_helper(&body.room_alias).await
} }
pub(crate) async fn get_alias_helper( pub(crate) async fn get_alias_helper(room_alias: &RoomAliasId) -> Result<get_alias::v3::Response> {
room_alias: &RoomAliasId,
) -> Result<get_alias::v3::Response> {
if room_alias.server_name() != services().globals.server_name() { if room_alias.server_name() != services().globals.server_name() {
let response = services() let response = services()
.sending .sending
@ -115,9 +120,15 @@ pub(crate) async fn get_alias_helper(
.await .await
.is_ok() .is_ok()
{ {
room_id = Some(services().rooms.alias.resolve_local_alias(room_alias)?.ok_or_else(|| { room_id = Some(
services()
.rooms
.alias
.resolve_local_alias(room_alias)?
.ok_or_else(|| {
Error::bad_config("Appservice lied to us. Room does not exist.") Error::bad_config("Appservice lied to us. Room does not exist.")
})?); })?,
);
break; break;
} }
} }

@ -1,4 +1,4 @@
use crate::{Error, Result, Ruma, services}; use crate::{services, Error, Result, Ruma};
use ruma::api::client::{ use ruma::api::client::{
backup::{ backup::{
add_backup_keys, add_backup_keys_for_room, add_backup_keys_for_session, add_backup_keys, add_backup_keys_for_room, add_backup_keys_for_session,
@ -31,7 +31,8 @@ pub async fn update_backup_version_route(
body: Ruma<update_backup_version::v3::IncomingRequest>, body: Ruma<update_backup_version::v3::IncomingRequest>,
) -> Result<update_backup_version::v3::Response> { ) -> Result<update_backup_version::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services().key_backups services()
.key_backups
.update_backup(sender_user, &body.version, &body.algorithm)?; .update_backup(sender_user, &body.version, &body.algorithm)?;
Ok(update_backup_version::v3::Response {}) Ok(update_backup_version::v3::Response {})
@ -45,8 +46,8 @@ pub async fn get_latest_backup_info_route(
) -> Result<get_latest_backup_info::v3::Response> { ) -> Result<get_latest_backup_info::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let (version, algorithm) = let (version, algorithm) = services()
services().key_backups .key_backups
.get_latest_backup(sender_user)? .get_latest_backup(sender_user)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
@ -78,8 +79,13 @@ pub async fn get_backup_info_route(
Ok(get_backup_info::v3::Response { Ok(get_backup_info::v3::Response {
algorithm, algorithm,
count: (services().key_backups.count_keys(sender_user, &body.version)? as u32).into(), count: (services()
etag: services().key_backups.get_etag(sender_user, &body.version)?, .key_backups
.count_keys(sender_user, &body.version)? as u32)
.into(),
etag: services()
.key_backups
.get_etag(sender_user, &body.version)?,
version: body.version.to_owned(), version: body.version.to_owned(),
}) })
} }
@ -94,7 +100,9 @@ pub async fn delete_backup_version_route(
) -> Result<delete_backup_version::v3::Response> { ) -> Result<delete_backup_version::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services().key_backups.delete_backup(sender_user, &body.version)?; services()
.key_backups
.delete_backup(sender_user, &body.version)?;
Ok(delete_backup_version::v3::Response {}) Ok(delete_backup_version::v3::Response {})
} }
@ -136,8 +144,13 @@ pub async fn add_backup_keys_route(
} }
Ok(add_backup_keys::v3::Response { Ok(add_backup_keys::v3::Response {
count: (services().key_backups.count_keys(sender_user, &body.version)? as u32).into(), count: (services()
etag: services().key_backups.get_etag(sender_user, &body.version)?, .key_backups
.count_keys(sender_user, &body.version)? as u32)
.into(),
etag: services()
.key_backups
.get_etag(sender_user, &body.version)?,
}) })
} }
@ -176,8 +189,13 @@ pub async fn add_backup_keys_for_room_route(
} }
Ok(add_backup_keys_for_room::v3::Response { Ok(add_backup_keys_for_room::v3::Response {
count: (services().key_backups.count_keys(sender_user, &body.version)? as u32).into(), count: (services()
etag: services().key_backups.get_etag(sender_user, &body.version)?, .key_backups
.count_keys(sender_user, &body.version)? as u32)
.into(),
etag: services()
.key_backups
.get_etag(sender_user, &body.version)?,
}) })
} }
@ -214,8 +232,13 @@ pub async fn add_backup_keys_for_session_route(
)?; )?;
Ok(add_backup_keys_for_session::v3::Response { Ok(add_backup_keys_for_session::v3::Response {
count: (services().key_backups.count_keys(sender_user, &body.version)? as u32).into(), count: (services()
etag: services().key_backups.get_etag(sender_user, &body.version)?, .key_backups
.count_keys(sender_user, &body.version)? as u32)
.into(),
etag: services()
.key_backups
.get_etag(sender_user, &body.version)?,
}) })
} }
@ -274,11 +297,18 @@ pub async fn delete_backup_keys_route(
) -> Result<delete_backup_keys::v3::Response> { ) -> Result<delete_backup_keys::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services().key_backups.delete_all_keys(sender_user, &body.version)?; services()
.key_backups
.delete_all_keys(sender_user, &body.version)?;
Ok(delete_backup_keys::v3::Response { Ok(delete_backup_keys::v3::Response {
count: (services().key_backups.count_keys(sender_user, &body.version)? as u32).into(), count: (services()
etag: services().key_backups.get_etag(sender_user, &body.version)?, .key_backups
.count_keys(sender_user, &body.version)? as u32)
.into(),
etag: services()
.key_backups
.get_etag(sender_user, &body.version)?,
}) })
} }
@ -290,12 +320,18 @@ pub async fn delete_backup_keys_for_room_route(
) -> Result<delete_backup_keys_for_room::v3::Response> { ) -> Result<delete_backup_keys_for_room::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services().key_backups services()
.key_backups
.delete_room_keys(sender_user, &body.version, &body.room_id)?; .delete_room_keys(sender_user, &body.version, &body.room_id)?;
Ok(delete_backup_keys_for_room::v3::Response { Ok(delete_backup_keys_for_room::v3::Response {
count: (services().key_backups.count_keys(sender_user, &body.version)? as u32).into(), count: (services()
etag: services().key_backups.get_etag(sender_user, &body.version)?, .key_backups
.count_keys(sender_user, &body.version)? as u32)
.into(),
etag: services()
.key_backups
.get_etag(sender_user, &body.version)?,
}) })
} }
@ -307,11 +343,20 @@ pub async fn delete_backup_keys_for_session_route(
) -> Result<delete_backup_keys_for_session::v3::Response> { ) -> Result<delete_backup_keys_for_session::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services().key_backups services().key_backups.delete_room_key(
.delete_room_key(sender_user, &body.version, &body.room_id, &body.session_id)?; sender_user,
&body.version,
&body.room_id,
&body.session_id,
)?;
Ok(delete_backup_keys_for_session::v3::Response { Ok(delete_backup_keys_for_session::v3::Response {
count: (services().key_backups.count_keys(sender_user, &body.version)? as u32).into(), count: (services()
etag: services().key_backups.get_etag(sender_user, &body.version)?, .key_backups
.count_keys(sender_user, &body.version)? as u32)
.into(),
etag: services()
.key_backups
.get_etag(sender_user, &body.version)?,
}) })
} }

@ -1,4 +1,4 @@
use crate::{Result, Ruma, services}; use crate::{services, Result, Ruma};
use ruma::api::client::discovery::get_capabilities::{ use ruma::api::client::discovery::get_capabilities::{
self, Capabilities, RoomVersionStability, RoomVersionsCapability, self, Capabilities, RoomVersionStability, RoomVersionsCapability,
}; };

@ -1,4 +1,4 @@
use crate::{Error, Result, Ruma, services}; use crate::{services, Error, Result, Ruma};
use ruma::{ use ruma::{
api::client::{ api::client::{
config::{ config::{

@ -1,4 +1,4 @@
use crate::{Error, Result, Ruma, services}; use crate::{services, Error, Result, Ruma};
use ruma::{ use ruma::{
api::client::{context::get_context, error::ErrorKind, filter::LazyLoadOptions}, api::client::{context::get_context, error::ErrorKind, filter::LazyLoadOptions},
events::StateEventType, events::StateEventType,
@ -49,7 +49,11 @@ pub async fn get_context_route(
let room_id = base_event.room_id.clone(); let room_id = base_event.room_id.clone();
if !services().rooms.state_cache.is_joined(sender_user, &room_id)? { if !services()
.rooms
.state_cache
.is_joined(sender_user, &room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"You don't have permission to view this room.", "You don't have permission to view this room.",
@ -141,7 +145,11 @@ pub async fn get_context_route(
.expect("All rooms have state"), .expect("All rooms have state"),
}; };
let state_ids = services().rooms.state_accessor.state_full_ids(shortstatehash).await?; let state_ids = services()
.rooms
.state_accessor
.state_full_ids(shortstatehash)
.await?;
let end_token = events_after let end_token = events_after
.last() .last()
@ -156,7 +164,10 @@ pub async fn get_context_route(
let mut state = Vec::new(); let mut state = Vec::new();
for (shortstatekey, id) in state_ids { for (shortstatekey, id) in state_ids {
let (event_type, state_key) = services().rooms.short.get_statekey_from_short(shortstatekey)?; let (event_type, state_key) = services()
.rooms
.short
.get_statekey_from_short(shortstatekey)?;
if event_type != StateEventType::RoomMember { if event_type != StateEventType::RoomMember {
let pdu = match services().rooms.timeline.get_pdu(&id)? { let pdu = match services().rooms.timeline.get_pdu(&id)? {

@ -1,4 +1,4 @@
use crate::{utils, Error, Result, Ruma, services}; use crate::{services, utils, Error, Result, Ruma};
use ruma::api::client::{ use ruma::api::client::{
device::{self, delete_device, delete_devices, get_device, get_devices, update_device}, device::{self, delete_device, delete_devices, get_device, get_devices, update_device},
error::ErrorKind, error::ErrorKind,
@ -55,7 +55,8 @@ pub async fn update_device_route(
device.display_name = body.display_name.clone(); device.display_name = body.display_name.clone();
services().users services()
.users
.update_device_metadata(sender_user, &body.device_id, &device)?; .update_device_metadata(sender_user, &body.device_id, &device)?;
Ok(update_device::v3::Response {}) Ok(update_device::v3::Response {})
@ -88,26 +89,27 @@ pub async fn delete_device_route(
}; };
if let Some(auth) = &body.auth { if let Some(auth) = &body.auth {
let (worked, uiaainfo) = services().uiaa.try_auth( let (worked, uiaainfo) =
sender_user, services()
sender_device, .uiaa
auth, .try_auth(sender_user, sender_device, auth, &uiaainfo)?;
&uiaainfo,
)?;
if !worked { if !worked {
return Err(Error::Uiaa(uiaainfo)); return Err(Error::Uiaa(uiaainfo));
} }
// Success! // Success!
} else if let Some(json) = body.json_body { } else if let Some(json) = body.json_body {
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
services().uiaa services()
.uiaa
.create(sender_user, sender_device, &uiaainfo, &json)?; .create(sender_user, sender_device, &uiaainfo, &json)?;
return Err(Error::Uiaa(uiaainfo)); return Err(Error::Uiaa(uiaainfo));
} else { } else {
return Err(Error::BadRequest(ErrorKind::NotJson, "Not json.")); return Err(Error::BadRequest(ErrorKind::NotJson, "Not json."));
} }
services().users.remove_device(sender_user, &body.device_id)?; services()
.users
.remove_device(sender_user, &body.device_id)?;
Ok(delete_device::v3::Response {}) Ok(delete_device::v3::Response {})
} }
@ -141,19 +143,18 @@ pub async fn delete_devices_route(
}; };
if let Some(auth) = &body.auth { if let Some(auth) = &body.auth {
let (worked, uiaainfo) = services().uiaa.try_auth( let (worked, uiaainfo) =
sender_user, services()
sender_device, .uiaa
auth, .try_auth(sender_user, sender_device, auth, &uiaainfo)?;
&uiaainfo,
)?;
if !worked { if !worked {
return Err(Error::Uiaa(uiaainfo)); return Err(Error::Uiaa(uiaainfo));
} }
// Success! // Success!
} else if let Some(json) = body.json_body { } else if let Some(json) = body.json_body {
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
services().uiaa services()
.uiaa
.create(sender_user, sender_device, &uiaainfo, &json)?; .create(sender_user, sender_device, &uiaainfo, &json)?;
return Err(Error::Uiaa(uiaainfo)); return Err(Error::Uiaa(uiaainfo));
} else { } else {

@ -1,4 +1,4 @@
use crate::{Error, Result, Ruma, services}; use crate::{services, Error, Result, Ruma};
use ruma::{ use ruma::{
api::{ api::{
client::{ client::{
@ -123,7 +123,8 @@ pub(crate) async fn get_public_rooms_filtered_helper(
filter: &IncomingFilter, filter: &IncomingFilter,
_network: &IncomingRoomNetwork, _network: &IncomingRoomNetwork,
) -> Result<get_public_rooms_filtered::v3::Response> { ) -> Result<get_public_rooms_filtered::v3::Response> {
if let Some(other_server) = server.filter(|server| *server != services().globals.server_name().as_str()) if let Some(other_server) =
server.filter(|server| *server != services().globals.server_name().as_str())
{ {
let response = services() let response = services()
.sending .sending

@ -1,4 +1,4 @@
use crate::{Error, Result, Ruma, services}; use crate::{services, Error, Result, Ruma};
use ruma::api::client::{ use ruma::api::client::{
error::ErrorKind, error::ErrorKind,
filter::{create_filter, get_filter}, filter::{create_filter, get_filter},

@ -1,5 +1,5 @@
use super::SESSION_ID_LENGTH; use super::SESSION_ID_LENGTH;
use crate::{utils, Error, Result, Ruma, services}; use crate::{services, utils, Error, Result, Ruma};
use futures_util::{stream::FuturesUnordered, StreamExt}; use futures_util::{stream::FuturesUnordered, StreamExt};
use ruma::{ use ruma::{
api::{ api::{
@ -32,7 +32,8 @@ pub async fn upload_keys_route(
let sender_device = body.sender_device.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated");
for (key_key, key_value) in &body.one_time_keys { for (key_key, key_value) in &body.one_time_keys {
services().users services()
.users
.add_one_time_key(sender_user, sender_device, key_key, key_value)?; .add_one_time_key(sender_user, sender_device, key_key, key_value)?;
} }
@ -44,16 +45,16 @@ pub async fn upload_keys_route(
.get_device_keys(sender_user, sender_device)? .get_device_keys(sender_user, sender_device)?
.is_none() .is_none()
{ {
services().users.add_device_keys( services()
sender_user, .users
sender_device, .add_device_keys(sender_user, sender_device, device_keys)?;
device_keys,
)?;
} }
} }
Ok(upload_keys::v3::Response { Ok(upload_keys::v3::Response {
one_time_key_counts: services().users.count_one_time_keys(sender_user, sender_device)?, one_time_key_counts: services()
.users
.count_one_time_keys(sender_user, sender_device)?,
}) })
} }
@ -69,12 +70,8 @@ pub async fn get_keys_route(
) -> Result<get_keys::v3::Response> { ) -> Result<get_keys::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let response = get_keys_helper( let response =
Some(sender_user), get_keys_helper(Some(sender_user), &body.device_keys, |u| u == sender_user).await?;
&body.device_keys,
|u| u == sender_user,
)
.await?;
Ok(response) Ok(response)
} }
@ -113,19 +110,18 @@ pub async fn upload_signing_keys_route(
}; };
if let Some(auth) = &body.auth { if let Some(auth) = &body.auth {
let (worked, uiaainfo) = services().uiaa.try_auth( let (worked, uiaainfo) =
sender_user, services()
sender_device, .uiaa
auth, .try_auth(sender_user, sender_device, auth, &uiaainfo)?;
&uiaainfo,
)?;
if !worked { if !worked {
return Err(Error::Uiaa(uiaainfo)); return Err(Error::Uiaa(uiaainfo));
} }
// Success! // Success!
} else if let Some(json) = body.json_body { } else if let Some(json) = body.json_body {
uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH)); uiaainfo.session = Some(utils::random_string(SESSION_ID_LENGTH));
services().uiaa services()
.uiaa
.create(sender_user, sender_device, &uiaainfo, &json)?; .create(sender_user, sender_device, &uiaainfo, &json)?;
return Err(Error::Uiaa(uiaainfo)); return Err(Error::Uiaa(uiaainfo));
} else { } else {
@ -187,12 +183,9 @@ pub async fn upload_signatures_route(
))? ))?
.to_owned(), .to_owned(),
); );
services().users.sign_key( services()
user_id, .users
key_id, .sign_key(user_id, key_id, signature, sender_user)?;
signature,
sender_user,
)?;
} }
} }
} }
@ -215,7 +208,8 @@ pub async fn get_key_changes_route(
let mut device_list_updates = HashSet::new(); let mut device_list_updates = HashSet::new();
device_list_updates.extend( device_list_updates.extend(
services().users services()
.users
.keys_changed( .keys_changed(
sender_user.as_str(), sender_user.as_str(),
body.from body.from
@ -230,9 +224,15 @@ pub async fn get_key_changes_route(
.filter_map(|r| r.ok()), .filter_map(|r| r.ok()),
); );
for room_id in services().rooms.state_cache.rooms_joined(sender_user).filter_map(|r| r.ok()) { for room_id in services()
.rooms
.state_cache
.rooms_joined(sender_user)
.filter_map(|r| r.ok())
{
device_list_updates.extend( device_list_updates.extend(
services().users services()
.users
.keys_changed( .keys_changed(
&room_id.to_string(), &room_id.to_string(),
body.from.parse().map_err(|_| { body.from.parse().map_err(|_| {
@ -296,12 +296,13 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
for device_id in device_ids { for device_id in device_ids {
let mut container = BTreeMap::new(); let mut container = BTreeMap::new();
if let Some(mut keys) = services().users.get_device_keys(user_id, device_id)? { if let Some(mut keys) = services().users.get_device_keys(user_id, device_id)? {
let metadata = services().users.get_device_metadata(user_id, device_id)?.ok_or( let metadata = services()
Error::BadRequest( .users
.get_device_metadata(user_id, device_id)?
.ok_or(Error::BadRequest(
ErrorKind::InvalidParam, ErrorKind::InvalidParam,
"Tried to get keys for nonexistent device.", "Tried to get keys for nonexistent device.",
), ))?;
)?;
add_unsigned_device_display_name(&mut keys, metadata) add_unsigned_device_display_name(&mut keys, metadata)
.map_err(|_| Error::bad_database("invalid device keys in database"))?; .map_err(|_| Error::bad_database("invalid device keys in database"))?;
@ -311,7 +312,10 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
} }
} }
if let Some(master_key) = services().users.get_master_key(user_id, &allowed_signatures)? { if let Some(master_key) = services()
.users
.get_master_key(user_id, &allowed_signatures)?
{
master_keys.insert(user_id.to_owned(), master_key); master_keys.insert(user_id.to_owned(), master_key);
} }
if let Some(self_signing_key) = services() if let Some(self_signing_key) = services()
@ -338,7 +342,8 @@ pub(crate) async fn get_keys_helper<F: Fn(&UserId) -> bool>(
} }
( (
server, server,
services().sending services()
.sending
.send_federation_request( .send_federation_request(
server, server,
federation::keys::get_keys::v1::Request { federation::keys::get_keys::v1::Request {
@ -408,7 +413,8 @@ pub(crate) async fn claim_keys_helper(
let mut container = BTreeMap::new(); let mut container = BTreeMap::new();
for (device_id, key_algorithm) in map { for (device_id, key_algorithm) in map {
if let Some(one_time_keys) = if let Some(one_time_keys) =
services().users services()
.users
.take_one_time_key(user_id, device_id, key_algorithm)? .take_one_time_key(user_id, device_id, key_algorithm)?
{ {
let mut c = BTreeMap::new(); let mut c = BTreeMap::new();

@ -1,6 +1,4 @@
use crate::{ use crate::{service::media::FileMeta, services, utils, Error, Result, Ruma};
utils, Error, Result, Ruma, services, service::media::FileMeta,
};
use ruma::api::client::{ use ruma::api::client::{
error::ErrorKind, error::ErrorKind,
media::{ media::{
@ -37,11 +35,11 @@ pub async fn create_content_route(
utils::random_string(MXC_LENGTH) utils::random_string(MXC_LENGTH)
); );
services().media services()
.media
.create( .create(
mxc.clone(), mxc.clone(),
body body.filename
.filename
.as_ref() .as_ref()
.map(|filename| "inline; filename=".to_owned() + filename) .map(|filename| "inline; filename=".to_owned() + filename)
.as_deref(), .as_deref(),
@ -73,7 +71,8 @@ pub async fn get_remote_content(
) )
.await?; .await?;
services().media services()
.media
.create( .create(
mxc.to_string(), mxc.to_string(),
content_response.content_disposition.as_deref(), content_response.content_disposition.as_deref(),
@ -192,7 +191,8 @@ pub async fn get_content_thumbnail_route(
) )
.await?; .await?;
services().media services()
.media
.upload_thumbnail( .upload_thumbnail(
mxc, mxc,
None, None,

@ -30,7 +30,11 @@ use std::{
}; };
use tracing::{debug, error, warn}; use tracing::{debug, error, warn};
use crate::{Result, services, PduEvent, service::pdu::{gen_event_id_canonical_json, PduBuilder}, Error, api::{server_server, client_server}, utils, Ruma}; use crate::{
api::{client_server, server_server},
service::pdu::{gen_event_id_canonical_json, PduBuilder},
services, utils, Error, PduEvent, Result, Ruma,
};
use super::get_alias_helper; use super::get_alias_helper;
@ -47,7 +51,8 @@ pub async fn join_room_by_id_route(
let mut servers = Vec::new(); // There is no body.server_name for /roomId/join let mut servers = Vec::new(); // There is no body.server_name for /roomId/join
servers.extend( servers.extend(
services().rooms services()
.rooms
.state_cache .state_cache
.invite_state(sender_user, &body.room_id)? .invite_state(sender_user, &body.room_id)?
.unwrap_or_default() .unwrap_or_default()
@ -88,7 +93,8 @@ pub async fn join_room_by_id_or_alias_route(
Ok(room_id) => { Ok(room_id) => {
let mut servers = body.server_name.clone(); let mut servers = body.server_name.clone();
servers.extend( servers.extend(
services().rooms services()
.rooms
.state_cache .state_cache
.invite_state(sender_user, &room_id)? .invite_state(sender_user, &room_id)?
.unwrap_or_default() .unwrap_or_default()
@ -163,7 +169,8 @@ pub async fn kick_user_route(
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut event: RoomMemberEventContent = serde_json::from_str( let mut event: RoomMemberEventContent = serde_json::from_str(
services().rooms services()
.rooms
.state_accessor .state_accessor
.room_state_get( .room_state_get(
&body.room_id, &body.room_id,
@ -183,7 +190,8 @@ pub async fn kick_user_route(
// TODO: reason // TODO: reason
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -250,7 +258,8 @@ pub async fn ban_user_route(
)?; )?;
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -286,7 +295,8 @@ pub async fn unban_user_route(
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut event: RoomMemberEventContent = serde_json::from_str( let mut event: RoomMemberEventContent = serde_json::from_str(
services().rooms services()
.rooms
.state_accessor .state_accessor
.room_state_get( .room_state_get(
&body.room_id, &body.room_id,
@ -305,7 +315,8 @@ pub async fn unban_user_route(
event.membership = MembershipState::Leave; event.membership = MembershipState::Leave;
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -345,7 +356,10 @@ pub async fn forget_room_route(
) -> Result<forget_room::v3::Response> { ) -> Result<forget_room::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services().rooms.state_cache.forget(&body.room_id, sender_user)?; services()
.rooms
.state_cache
.forget(&body.room_id, sender_user)?;
Ok(forget_room::v3::Response::new()) Ok(forget_room::v3::Response::new())
} }
@ -379,7 +393,11 @@ pub async fn get_member_events_route(
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
// TODO: check history visibility? // TODO: check history visibility?
if !services().rooms.state_cache.is_joined(sender_user, &body.room_id)? { if !services()
.rooms
.state_cache
.is_joined(sender_user, &body.room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"You don't have permission to view this room.", "You don't have permission to view this room.",
@ -410,7 +428,11 @@ pub async fn joined_members_route(
) -> Result<joined_members::v3::Response> { ) -> Result<joined_members::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
if !services().rooms.state_cache.is_joined(sender_user, &body.room_id)? { if !services()
.rooms
.state_cache
.is_joined(sender_user, &body.room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"You aren't a member of the room.", "You aren't a member of the room.",
@ -418,7 +440,12 @@ pub async fn joined_members_route(
} }
let mut joined = BTreeMap::new(); let mut joined = BTreeMap::new();
for user_id in services().rooms.state_cache.room_members(&body.room_id).filter_map(|r| r.ok()) { for user_id in services()
.rooms
.state_cache
.room_members(&body.room_id)
.filter_map(|r| r.ok())
{
let display_name = services().users.displayname(&user_id)?; let display_name = services().users.displayname(&user_id)?;
let avatar_url = services().users.avatar_url(&user_id)?; let avatar_url = services().users.avatar_url(&user_id)?;
@ -443,7 +470,8 @@ async fn join_room_by_id_helper(
let sender_user = sender_user.expect("user is authenticated"); let sender_user = sender_user.expect("user is authenticated");
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -481,7 +509,14 @@ async fn join_room_by_id_helper(
let (make_join_response, remote_server) = make_join_response_and_server?; let (make_join_response, remote_server) = make_join_response_and_server?;
let room_version = match make_join_response.room_version { let room_version = match make_join_response.room_version {
Some(room_version) if services().globals.supported_room_versions().contains(&room_version) => room_version, Some(room_version)
if services()
.globals
.supported_room_versions()
.contains(&room_version) =>
{
room_version
}
_ => return Err(Error::BadServerResponse("Room version is not supported")), _ => return Err(Error::BadServerResponse("Room version is not supported")),
}; };
@ -568,11 +603,10 @@ async fn join_room_by_id_helper(
let mut state = HashMap::new(); let mut state = HashMap::new();
let pub_key_map = RwLock::new(BTreeMap::new()); let pub_key_map = RwLock::new(BTreeMap::new());
services().rooms.event_handler.fetch_join_signing_keys( services()
&send_join_response, .rooms
&room_version, .event_handler
&pub_key_map, .fetch_join_signing_keys(&send_join_response, &room_version, &pub_key_map)
)
.await?; .await?;
for result in send_join_response for result in send_join_response
@ -591,12 +625,15 @@ async fn join_room_by_id_helper(
Error::BadServerResponse("Invalid PDU in send_join response.") Error::BadServerResponse("Invalid PDU in send_join response.")
})?; })?;
services().rooms.outlier.add_pdu_outlier(&event_id, &value)?; services()
.rooms
.outlier
.add_pdu_outlier(&event_id, &value)?;
if let Some(state_key) = &pdu.state_key { if let Some(state_key) = &pdu.state_key {
let shortstatekey = services().rooms.short.get_or_create_shortstatekey( let shortstatekey = services()
&pdu.kind.to_string().into(), .rooms
state_key, .short
)?; .get_or_create_shortstatekey(&pdu.kind.to_string().into(), state_key)?;
state.insert(shortstatekey, pdu.event_id.clone()); state.insert(shortstatekey, pdu.event_id.clone());
} }
} }
@ -632,7 +669,10 @@ async fn join_room_by_id_helper(
Err(_) => continue, Err(_) => continue,
}; };
services().rooms.outlier.add_pdu_outlier(&event_id, &value)?; services()
.rooms
.outlier
.add_pdu_outlier(&event_id, &value)?;
} }
let shortstatehash = services().rooms.state.set_event_state( let shortstatehash = services().rooms.state.set_event_state(
@ -640,7 +680,12 @@ async fn join_room_by_id_helper(
room_id, room_id,
state state
.into_iter() .into_iter()
.map(|(k, id)| services().rooms.state_compressor.compress_state_event(k, &id)) .map(|(k, id)| {
services()
.rooms
.state_compressor
.compress_state_event(k, &id)
})
.collect::<Result<_>>()?, .collect::<Result<_>>()?,
)?; )?;
@ -650,12 +695,15 @@ async fn join_room_by_id_helper(
&parsed_pdu, &parsed_pdu,
join_event, join_event,
vec![(*parsed_pdu.event_id).to_owned()], vec![(*parsed_pdu.event_id).to_owned()],
&state_lock &state_lock,
)?; )?;
// We set the room state after inserting the pdu, so that we never have a moment in time // We set the room state after inserting the pdu, so that we never have a moment in time
// where events in the current room state do not exist // where events in the current room state do not exist
services().rooms.state.set_room_state(room_id, shortstatehash, &state_lock)?; services()
.rooms
.state
.set_room_state(room_id, shortstatehash, &state_lock)?;
let statehashid = services().rooms.state.append_to_state(&parsed_pdu)?; let statehashid = services().rooms.state.append_to_state(&parsed_pdu)?;
} else { } else {
@ -705,7 +753,13 @@ fn validate_and_add_event_id(
)) ))
.expect("ruma's reference hashes are valid event ids"); .expect("ruma's reference hashes are valid event ids");
let back_off = |id| match services().globals.bad_event_ratelimiter.write().unwrap().entry(id) { let back_off = |id| match services()
.globals
.bad_event_ratelimiter
.write()
.unwrap()
.entry(id)
{
Entry::Vacant(e) => { Entry::Vacant(e) => {
e.insert((Instant::now(), 1)); e.insert((Instant::now(), 1));
} }
@ -760,7 +814,8 @@ pub(crate) async fn invite_helper<'a>(
if user_id.server_name() != services().globals.server_name() { if user_id.server_name() != services().globals.server_name() {
let (pdu_json, invite_room_state) = { let (pdu_json, invite_room_state) = {
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -781,13 +836,18 @@ pub(crate) async fn invite_helper<'a>(
}) })
.expect("member event is valid value"); .expect("member event is valid value");
let (pdu, pdu_json) = services().rooms.timeline.create_hash_and_sign_event(PduBuilder { let (pdu, pdu_json) = services().rooms.timeline.create_hash_and_sign_event(
PduBuilder {
event_type: RoomEventType::RoomMember, event_type: RoomEventType::RoomMember,
content, content,
unsigned: None, unsigned: None,
state_key: Some(user_id.to_string()), state_key: Some(user_id.to_string()),
redacts: None, redacts: None,
}, sender_user, room_id, &state_lock)?; },
sender_user,
room_id,
&state_lock,
)?;
let invite_room_state = services().rooms.state.calculate_invite_state(&pdu)?; let invite_room_state = services().rooms.state.calculate_invite_state(&pdu)?;
@ -799,7 +859,10 @@ pub(crate) async fn invite_helper<'a>(
// Generate event id // Generate event id
let expected_event_id = format!( let expected_event_id = format!(
"${}", "${}",
ruma::signatures::reference_hash(&pdu_json, &services().rooms.state.get_room_version(&room_id)?) ruma::signatures::reference_hash(
&pdu_json,
&services().rooms.state.get_room_version(&room_id)?
)
.expect("ruma can calculate reference hashes") .expect("ruma can calculate reference hashes")
); );
let expected_event_id = <&EventId>::try_from(expected_event_id.as_str()) let expected_event_id = <&EventId>::try_from(expected_event_id.as_str())
@ -822,8 +885,7 @@ pub(crate) async fn invite_helper<'a>(
let pub_key_map = RwLock::new(BTreeMap::new()); let pub_key_map = RwLock::new(BTreeMap::new());
// We do not add the event_id field to the pdu here because of signature and hashes checks // We do not add the event_id field to the pdu here because of signature and hashes checks
let (event_id, value) = match gen_event_id_canonical_json(&response.event) let (event_id, value) = match gen_event_id_canonical_json(&response.event) {
{
Ok(t) => t, Ok(t) => t,
Err(_) => { Err(_) => {
// Event could not be converted to canonical json // Event could not be converted to canonical json
@ -847,14 +909,10 @@ pub(crate) async fn invite_helper<'a>(
) )
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Origin field is invalid."))?; .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Origin field is invalid."))?;
let pdu_id: Vec<u8> = services().rooms.event_handler.handle_incoming_pdu( let pdu_id: Vec<u8> = services()
&origin, .rooms
&event_id, .event_handler
room_id, .handle_incoming_pdu(&origin, &event_id, room_id, value, true, &pub_key_map)
value,
true,
&pub_key_map,
)
.await? .await?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::InvalidParam, ErrorKind::InvalidParam,
@ -862,7 +920,9 @@ pub(crate) async fn invite_helper<'a>(
))?; ))?;
// Bind to variable because of lifetimes // Bind to variable because of lifetimes
let servers = services().rooms.state_cache let servers = services()
.rooms
.state_cache
.room_servers(room_id) .room_servers(room_id)
.filter_map(|r| r.ok()) .filter_map(|r| r.ok())
.filter(|server| &**server != services().globals.server_name()); .filter(|server| &**server != services().globals.server_name());
@ -872,7 +932,11 @@ pub(crate) async fn invite_helper<'a>(
return Ok(()); return Ok(());
} }
if !services().rooms.state_cache.is_joined(sender_user, &room_id)? { if !services()
.rooms
.state_cache
.is_joined(sender_user, &room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"You don't have permission to view this room.", "You don't have permission to view this room.",
@ -880,7 +944,8 @@ pub(crate) async fn invite_helper<'a>(
} }
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -923,7 +988,13 @@ pub async fn leave_all_rooms(user_id: &UserId) -> Result<()> {
.rooms .rooms
.state_cache .state_cache
.rooms_joined(user_id) .rooms_joined(user_id)
.chain(services().rooms.state_cache.rooms_invited(user_id).map(|t| t.map(|(r, _)| r))) .chain(
services()
.rooms
.state_cache
.rooms_invited(user_id)
.map(|t| t.map(|(r, _)| r)),
)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
for room_id in all_rooms { for room_id in all_rooms {
@ -938,20 +1009,24 @@ pub async fn leave_all_rooms(user_id: &UserId) -> Result<()> {
Ok(()) Ok(())
} }
pub async fn leave_room( pub async fn leave_room(user_id: &UserId, room_id: &RoomId) -> Result<()> {
user_id: &UserId,
room_id: &RoomId,
) -> Result<()> {
// Ask a remote server if we don't have this room // Ask a remote server if we don't have this room
if !services().rooms.metadata.exists(room_id)? && room_id.server_name() != services().globals.server_name() { if !services().rooms.metadata.exists(room_id)?
&& room_id.server_name() != services().globals.server_name()
{
if let Err(e) = remote_leave_room(user_id, room_id).await { if let Err(e) = remote_leave_room(user_id, room_id).await {
warn!("Failed to leave room {} remotely: {}", user_id, e); warn!("Failed to leave room {} remotely: {}", user_id, e);
// Don't tell the client about this error // Don't tell the client about this error
} }
let last_state = services().rooms.state_cache let last_state = services()
.rooms
.state_cache
.invite_state(user_id, room_id)? .invite_state(user_id, room_id)?
.map_or_else(|| services().rooms.state_cache.left_state(user_id, room_id), |s| Ok(Some(s)))?; .map_or_else(
|| services().rooms.state_cache.left_state(user_id, room_id),
|s| Ok(Some(s)),
)?;
// We always drop the invite, we can't rely on other servers // We always drop the invite, we can't rely on other servers
services().rooms.state_cache.update_membership( services().rooms.state_cache.update_membership(
@ -964,7 +1039,8 @@ pub async fn leave_room(
)?; )?;
} else { } else {
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -974,7 +1050,10 @@ pub async fn leave_room(
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
let mut event: RoomMemberEventContent = serde_json::from_str( let mut event: RoomMemberEventContent = serde_json::from_str(
services().rooms.state_accessor.room_state_get(room_id, &StateEventType::RoomMember, user_id.as_str())? services()
.rooms
.state_accessor
.room_state_get(room_id, &StateEventType::RoomMember, user_id.as_str())?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::BadState, ErrorKind::BadState,
"Cannot leave a room you are not a member of.", "Cannot leave a room you are not a member of.",
@ -1003,10 +1082,7 @@ pub async fn leave_room(
Ok(()) Ok(())
} }
async fn remote_leave_room( async fn remote_leave_room(user_id: &UserId, room_id: &RoomId) -> Result<()> {
user_id: &UserId,
room_id: &RoomId,
) -> Result<()> {
let mut make_leave_response_and_server = Err(Error::BadServerResponse( let mut make_leave_response_and_server = Err(Error::BadServerResponse(
"No server available to assist in leaving.", "No server available to assist in leaving.",
)); ));
@ -1048,14 +1124,21 @@ async fn remote_leave_room(
let (make_leave_response, remote_server) = make_leave_response_and_server?; let (make_leave_response, remote_server) = make_leave_response_and_server?;
let room_version_id = match make_leave_response.room_version { let room_version_id = match make_leave_response.room_version {
Some(version) if services().globals.supported_room_versions().contains(&version) => version, Some(version)
if services()
.globals
.supported_room_versions()
.contains(&version) =>
{
version
}
_ => return Err(Error::BadServerResponse("Room version is not supported")), _ => return Err(Error::BadServerResponse("Room version is not supported")),
}; };
let mut leave_event_stub = let mut leave_event_stub = serde_json::from_str::<CanonicalJsonObject>(
serde_json::from_str::<CanonicalJsonObject>(make_leave_response.event.get()).map_err( make_leave_response.event.get(),
|_| Error::BadServerResponse("Invalid make_leave event json received from server."), )
)?; .map_err(|_| Error::BadServerResponse("Invalid make_leave event json received from server."))?;
// TODO: Is origin needed? // TODO: Is origin needed?
leave_event_stub.insert( leave_event_stub.insert(
@ -1099,7 +1182,8 @@ async fn remote_leave_room(
// It has enough fields to be called a proper event now // It has enough fields to be called a proper event now
let leave_event = leave_event_stub; let leave_event = leave_event_stub;
services().sending services()
.sending
.send_federation_request( .send_federation_request(
&remote_server, &remote_server,
federation::membership::create_leave_event::v2::Request { federation::membership::create_leave_event::v2::Request {

@ -1,4 +1,4 @@
use crate::{utils, Error, Result, Ruma, services, service::pdu::PduBuilder}; use crate::{service::pdu::PduBuilder, services, utils, Error, Result, Ruma};
use ruma::{ use ruma::{
api::client::{ api::client::{
error::ErrorKind, error::ErrorKind,
@ -25,7 +25,8 @@ pub async fn send_message_event_route(
let sender_device = body.sender_device.as_deref(); let sender_device = body.sender_device.as_deref();
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -46,7 +47,8 @@ pub async fn send_message_event_route(
// Check if this is a new transaction id // Check if this is a new transaction id
if let Some(response) = if let Some(response) =
services().transaction_ids services()
.transaction_ids
.existing_txnid(sender_user, sender_device, &body.txn_id)? .existing_txnid(sender_user, sender_device, &body.txn_id)?
{ {
// The client might have sent a txnid of the /sendToDevice endpoint // The client might have sent a txnid of the /sendToDevice endpoint
@ -108,7 +110,11 @@ pub async fn get_message_events_route(
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sender_device = body.sender_device.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated");
if !services().rooms.state_cache.is_joined(sender_user, &body.room_id)? { if !services()
.rooms
.state_cache
.is_joined(sender_user, &body.room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"You don't have permission to view this room.", "You don't have permission to view this room.",
@ -128,8 +134,12 @@ pub async fn get_message_events_route(
let to = body.to.as_ref().map(|t| t.parse()); let to = body.to.as_ref().map(|t| t.parse());
services().rooms services().rooms.lazy_loading.lazy_load_confirm_delivery(
.lazy_loading.lazy_load_confirm_delivery(sender_user, sender_device, &body.room_id, from)?; sender_user,
sender_device,
&body.room_id,
from,
)?;
// Use limit or else 10 // Use limit or else 10
let limit = body.limit.try_into().map_or(10_usize, |l: u32| l as usize); let limit = body.limit.try_into().map_or(10_usize, |l: u32| l as usize);
@ -149,8 +159,10 @@ pub async fn get_message_events_route(
.take(limit) .take(limit)
.filter_map(|r| r.ok()) // Filter out buggy events .filter_map(|r| r.ok()) // Filter out buggy events
.filter_map(|(pdu_id, pdu)| { .filter_map(|(pdu_id, pdu)| {
services().rooms services()
.timeline.pdu_count(&pdu_id) .rooms
.timeline
.pdu_count(&pdu_id)
.map(|pdu_count| (pdu_count, pdu)) .map(|pdu_count| (pdu_count, pdu))
.ok() .ok()
}) })
@ -187,7 +199,8 @@ pub async fn get_message_events_route(
.take(limit) .take(limit)
.filter_map(|r| r.ok()) // Filter out buggy events .filter_map(|r| r.ok()) // Filter out buggy events
.filter_map(|(pdu_id, pdu)| { .filter_map(|(pdu_id, pdu)| {
services().rooms services()
.rooms
.timeline .timeline
.pdu_count(&pdu_id) .pdu_count(&pdu_id)
.map(|pdu_count| (pdu_count, pdu)) .map(|pdu_count| (pdu_count, pdu))
@ -222,10 +235,11 @@ pub async fn get_message_events_route(
resp.state = Vec::new(); resp.state = Vec::new();
for ll_id in &lazy_loaded { for ll_id in &lazy_loaded {
if let Some(member_event) = if let Some(member_event) = services().rooms.state_accessor.room_state_get(
services().rooms.state_accessor &body.room_id,
.room_state_get(&body.room_id, &StateEventType::RoomMember, ll_id.as_str())? &StateEventType::RoomMember,
{ ll_id.as_str(),
)? {
resp.state.push(member_event.to_state_event()); resp.state.push(member_event.to_state_event());
} }
} }

@ -1,4 +1,4 @@
use crate::{utils, Result, Ruma, services}; use crate::{services, utils, Result, Ruma};
use ruma::api::client::presence::{get_presence, set_presence}; use ruma::api::client::presence::{get_presence, set_presence};
use std::time::Duration; use std::time::Duration;
@ -51,7 +51,8 @@ pub async fn get_presence_route(
for room_id in services() for room_id in services()
.rooms .rooms
.user.get_shared_rooms(vec![sender_user.clone(), body.user_id.clone()])? .user
.get_shared_rooms(vec![sender_user.clone(), body.user_id.clone()])?
{ {
let room_id = room_id?; let room_id = room_id?;

@ -1,4 +1,4 @@
use crate::{utils, Error, Result, Ruma, services, service::pdu::PduBuilder}; use crate::{service::pdu::PduBuilder, services, utils, Error, Result, Ruma};
use ruma::{ use ruma::{
api::{ api::{
client::{ client::{
@ -24,7 +24,8 @@ pub async fn set_displayname_route(
) -> Result<set_display_name::v3::Response> { ) -> Result<set_display_name::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services().users services()
.users
.set_displayname(sender_user, body.displayname.clone())?; .set_displayname(sender_user, body.displayname.clone())?;
// Send a new membership event and presence update into all joined rooms // Send a new membership event and presence update into all joined rooms
@ -40,7 +41,8 @@ pub async fn set_displayname_route(
content: to_raw_value(&RoomMemberEventContent { content: to_raw_value(&RoomMemberEventContent {
displayname: body.displayname.clone(), displayname: body.displayname.clone(),
..serde_json::from_str( ..serde_json::from_str(
services().rooms services()
.rooms
.state_accessor .state_accessor
.room_state_get( .room_state_get(
&room_id, &room_id,
@ -71,7 +73,8 @@ pub async fn set_displayname_route(
for (pdu_builder, room_id) in all_rooms_joined { for (pdu_builder, room_id) in all_rooms_joined {
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -80,10 +83,12 @@ pub async fn set_displayname_route(
); );
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
let _ = services() let _ = services().rooms.timeline.build_and_append_pdu(
.rooms pdu_builder,
.timeline sender_user,
.build_and_append_pdu(pdu_builder, sender_user, &room_id, &state_lock); &room_id,
&state_lock,
);
// Presence update // Presence update
services().rooms.edus.presence.update_presence( services().rooms.edus.presence.update_presence(
@ -150,10 +155,13 @@ pub async fn set_avatar_url_route(
) -> Result<set_avatar_url::v3::Response> { ) -> Result<set_avatar_url::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
services().users services()
.users
.set_avatar_url(sender_user, body.avatar_url.clone())?; .set_avatar_url(sender_user, body.avatar_url.clone())?;
services().users.set_blurhash(sender_user, body.blurhash.clone())?; services()
.users
.set_blurhash(sender_user, body.blurhash.clone())?;
// Send a new membership event and presence update into all joined rooms // Send a new membership event and presence update into all joined rooms
let all_joined_rooms: Vec<_> = services() let all_joined_rooms: Vec<_> = services()
@ -168,7 +176,8 @@ pub async fn set_avatar_url_route(
content: to_raw_value(&RoomMemberEventContent { content: to_raw_value(&RoomMemberEventContent {
avatar_url: body.avatar_url.clone(), avatar_url: body.avatar_url.clone(),
..serde_json::from_str( ..serde_json::from_str(
services().rooms services()
.rooms
.state_accessor .state_accessor
.room_state_get( .room_state_get(
&room_id, &room_id,
@ -199,7 +208,8 @@ pub async fn set_avatar_url_route(
for (pdu_builder, room_id) in all_joined_rooms { for (pdu_builder, room_id) in all_joined_rooms {
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -208,10 +218,12 @@ pub async fn set_avatar_url_route(
); );
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
let _ = services() let _ = services().rooms.timeline.build_and_append_pdu(
.rooms pdu_builder,
.timeline sender_user,
.build_and_append_pdu(pdu_builder, sender_user, &room_id, &state_lock); &room_id,
&state_lock,
);
// Presence update // Presence update
services().rooms.edus.presence.update_presence( services().rooms.edus.presence.update_presence(

@ -1,4 +1,4 @@
use crate::{Error, Result, Ruma, services}; use crate::{services, Error, Result, Ruma};
use ruma::{ use ruma::{
api::client::{ api::client::{
error::ErrorKind, error::ErrorKind,

@ -1,4 +1,4 @@
use crate::{Error, Result, Ruma, services}; use crate::{services, Error, Result, Ruma};
use ruma::{ use ruma::{
api::client::{error::ErrorKind, read_marker::set_read_marker, receipt::create_receipt}, api::client::{error::ErrorKind, read_marker::set_read_marker, receipt::create_receipt},
events::RoomAccountDataEventType, events::RoomAccountDataEventType,
@ -34,12 +34,18 @@ pub async fn set_read_marker_route(
services().rooms.edus.read_receipt.private_read_set( services().rooms.edus.read_receipt.private_read_set(
&body.room_id, &body.room_id,
sender_user, sender_user,
services().rooms.timeline.get_pdu_count(event)?.ok_or(Error::BadRequest( services()
.rooms
.timeline
.get_pdu_count(event)?
.ok_or(Error::BadRequest(
ErrorKind::InvalidParam, ErrorKind::InvalidParam,
"Event does not exist.", "Event does not exist.",
))?, ))?,
)?; )?;
services().rooms.user services()
.rooms
.user
.reset_notification_counts(sender_user, &body.room_id)?; .reset_notification_counts(sender_user, &body.room_id)?;
let mut user_receipts = BTreeMap::new(); let mut user_receipts = BTreeMap::new();
@ -80,7 +86,8 @@ pub async fn create_receipt_route(
services().rooms.edus.read_receipt.private_read_set( services().rooms.edus.read_receipt.private_read_set(
&body.room_id, &body.room_id,
sender_user, sender_user,
services().rooms services()
.rooms
.timeline .timeline
.get_pdu_count(&body.event_id)? .get_pdu_count(&body.event_id)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
@ -88,7 +95,9 @@ pub async fn create_receipt_route(
"Event does not exist.", "Event does not exist.",
))?, ))?,
)?; )?;
services().rooms.user services()
.rooms
.user
.reset_notification_counts(sender_user, &body.room_id)?; .reset_notification_counts(sender_user, &body.room_id)?;
let mut user_receipts = BTreeMap::new(); let mut user_receipts = BTreeMap::new();

@ -1,6 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use crate::{Result, Ruma, services, service::pdu::PduBuilder}; use crate::{service::pdu::PduBuilder, services, Result, Ruma};
use ruma::{ use ruma::{
api::client::redact::redact_event, api::client::redact::redact_event,
events::{room::redaction::RoomRedactionEventContent, RoomEventType}, events::{room::redaction::RoomRedactionEventContent, RoomEventType},
@ -20,7 +20,8 @@ pub async fn redact_event_route(
let body = body.body; let body = body.body;
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()

@ -1,4 +1,4 @@
use crate::{utils::HtmlEscape, Error, Result, Ruma, services}; use crate::{services, utils::HtmlEscape, Error, Result, Ruma};
use ruma::{ use ruma::{
api::client::{error::ErrorKind, room::report_content}, api::client::{error::ErrorKind, room::report_content},
events::room::message, events::room::message,

@ -1,5 +1,5 @@
use crate::{ use crate::{
Error, Result, Ruma, service::pdu::PduBuilder, services, api::client_server::invite_helper, api::client_server::invite_helper, service::pdu::PduBuilder, services, Error, Result, Ruma,
}; };
use ruma::{ use ruma::{
api::client::{ api::client::{
@ -57,7 +57,8 @@ pub async fn create_room_route(
services().rooms.short.get_or_create_shortroomid(&room_id)?; services().rooms.short.get_or_create_shortroomid(&room_id)?;
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -81,13 +82,19 @@ pub async fn create_room_route(
.as_ref() .as_ref()
.map_or(Ok(None), |localpart| { .map_or(Ok(None), |localpart| {
// TODO: Check for invalid characters and maximum length // TODO: Check for invalid characters and maximum length
let alias = let alias = RoomAliasId::parse(format!(
RoomAliasId::parse(format!("#{}:{}", localpart, services().globals.server_name())) "#{}:{}",
.map_err(|_| { localpart,
Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias.") services().globals.server_name()
})?; ))
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?;
if services().rooms.alias.resolve_local_alias(&alias)?.is_some() { if services()
.rooms
.alias
.resolve_local_alias(&alias)?
.is_some()
{
Err(Error::BadRequest( Err(Error::BadRequest(
ErrorKind::RoomInUse, ErrorKind::RoomInUse,
"Room alias already exists.", "Room alias already exists.",
@ -99,7 +106,11 @@ pub async fn create_room_route(
let room_version = match body.room_version.clone() { let room_version = match body.room_version.clone() {
Some(room_version) => { Some(room_version) => {
if services().globals.supported_room_versions().contains(&room_version) { if services()
.globals
.supported_room_versions()
.contains(&room_version)
{
room_version room_version
} else { } else {
return Err(Error::BadRequest( return Err(Error::BadRequest(
@ -338,13 +349,18 @@ pub async fn create_room_route(
pdu_builder.state_key.get_or_insert_with(|| "".to_owned()); pdu_builder.state_key.get_or_insert_with(|| "".to_owned());
// Silently skip encryption events if they are not allowed // Silently skip encryption events if they are not allowed
if pdu_builder.event_type == RoomEventType::RoomEncryption && !services().globals.allow_encryption() if pdu_builder.event_type == RoomEventType::RoomEncryption
&& !services().globals.allow_encryption()
{ {
continue; continue;
} }
services().rooms services().rooms.timeline.build_and_append_pdu(
.timeline.build_and_append_pdu(pdu_builder, sender_user, &room_id, &state_lock)?; pdu_builder,
sender_user,
&room_id,
&state_lock,
)?;
} }
// 7. Events implied by name and topic // 7. Events implied by name and topic
@ -412,7 +428,11 @@ pub async fn get_room_event_route(
) -> Result<get_room_event::v3::Response> { ) -> Result<get_room_event::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
if !services().rooms.state_cache.is_joined(sender_user, &body.room_id)? { if !services()
.rooms
.state_cache
.is_joined(sender_user, &body.room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"You don't have permission to view this room.", "You don't have permission to view this room.",
@ -439,7 +459,11 @@ pub async fn get_room_aliases_route(
) -> Result<aliases::v3::Response> { ) -> Result<aliases::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
if !services().rooms.state_cache.is_joined(sender_user, &body.room_id)? { if !services()
.rooms
.state_cache
.is_joined(sender_user, &body.room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"You don't have permission to view this room.", "You don't have permission to view this room.",
@ -449,7 +473,8 @@ pub async fn get_room_aliases_route(
Ok(aliases::v3::Response { Ok(aliases::v3::Response {
aliases: services() aliases: services()
.rooms .rooms
.alias.local_aliases_for_room(&body.room_id) .alias
.local_aliases_for_room(&body.room_id)
.filter_map(|a| a.ok()) .filter_map(|a| a.ok())
.collect(), .collect(),
}) })
@ -470,7 +495,11 @@ pub async fn upgrade_room_route(
) -> Result<upgrade_room::v3::Response> { ) -> Result<upgrade_room::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
if !services().globals.supported_room_versions().contains(&body.new_version) { if !services()
.globals
.supported_room_versions()
.contains(&body.new_version)
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::UnsupportedRoomVersion, ErrorKind::UnsupportedRoomVersion,
"This server does not support that room version.", "This server does not support that room version.",
@ -479,11 +508,14 @@ pub async fn upgrade_room_route(
// Create a replacement room // Create a replacement room
let replacement_room = RoomId::new(services().globals.server_name()); let replacement_room = RoomId::new(services().globals.server_name());
services().rooms services()
.short.get_or_create_shortroomid(&replacement_room)?; .rooms
.short
.get_or_create_shortroomid(&replacement_room)?;
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -514,7 +546,8 @@ pub async fn upgrade_room_route(
// Change lock to replacement room // Change lock to replacement room
drop(state_lock); drop(state_lock);
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -525,7 +558,8 @@ pub async fn upgrade_room_route(
// Get the old room creation event // Get the old room creation event
let mut create_event_content = serde_json::from_str::<CanonicalJsonObject>( let mut create_event_content = serde_json::from_str::<CanonicalJsonObject>(
services().rooms services()
.rooms
.state_accessor .state_accessor
.room_state_get(&body.room_id, &StateEventType::RoomCreate, "")? .room_state_get(&body.room_id, &StateEventType::RoomCreate, "")?
.ok_or_else(|| Error::bad_database("Found room without m.room.create event."))? .ok_or_else(|| Error::bad_database("Found room without m.room.create event."))?
@ -627,7 +661,12 @@ pub async fn upgrade_room_route(
// Replicate transferable state events to the new room // Replicate transferable state events to the new room
for event_type in transferable_state_events { for event_type in transferable_state_events {
let event_content = match services().rooms.state_accessor.room_state_get(&body.room_id, &event_type, "")? { let event_content =
match services()
.rooms
.state_accessor
.room_state_get(&body.room_id, &event_type, "")?
{
Some(v) => v.content.clone(), Some(v) => v.content.clone(),
None => continue, // Skipping missing events. None => continue, // Skipping missing events.
}; };
@ -647,14 +686,22 @@ pub async fn upgrade_room_route(
} }
// Moves any local aliases to the new room // Moves any local aliases to the new room
for alias in services().rooms.alias.local_aliases_for_room(&body.room_id).filter_map(|r| r.ok()) { for alias in services()
services().rooms .rooms
.alias.set_alias(&alias, &replacement_room)?; .alias
.local_aliases_for_room(&body.room_id)
.filter_map(|r| r.ok())
{
services()
.rooms
.alias
.set_alias(&alias, &replacement_room)?;
} }
// Get the old room power levels // Get the old room power levels
let mut power_levels_event_content: RoomPowerLevelsEventContent = serde_json::from_str( let mut power_levels_event_content: RoomPowerLevelsEventContent = serde_json::from_str(
services().rooms services()
.rooms
.state_accessor .state_accessor
.room_state_get(&body.room_id, &StateEventType::RoomPowerLevels, "")? .room_state_get(&body.room_id, &StateEventType::RoomPowerLevels, "")?
.ok_or_else(|| Error::bad_database("Found room without m.room.create event."))? .ok_or_else(|| Error::bad_database("Found room without m.room.create event."))?
@ -688,4 +735,3 @@ pub async fn upgrade_room_route(
// Return the replacement room id // Return the replacement room id
Ok(upgrade_room::v3::Response { replacement_room }) Ok(upgrade_room::v3::Response { replacement_room })
} }

@ -1,4 +1,4 @@
use crate::{Error, Result, Ruma, services}; use crate::{services, Error, Result, Ruma};
use ruma::api::client::{ use ruma::api::client::{
error::ErrorKind, error::ErrorKind,
search::search_events::{ search::search_events::{
@ -23,7 +23,8 @@ pub async fn search_events_route(
let filter = &search_criteria.filter; let filter = &search_criteria.filter;
let room_ids = filter.rooms.clone().unwrap_or_else(|| { let room_ids = filter.rooms.clone().unwrap_or_else(|| {
services().rooms services()
.rooms
.state_cache .state_cache
.rooms_joined(sender_user) .rooms_joined(sender_user)
.filter_map(|r| r.ok()) .filter_map(|r| r.ok())
@ -35,7 +36,11 @@ pub async fn search_events_route(
let mut searches = Vec::new(); let mut searches = Vec::new();
for room_id in room_ids { for room_id in room_ids {
if !services().rooms.state_cache.is_joined(sender_user, &room_id)? { if !services()
.rooms
.state_cache
.is_joined(sender_user, &room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"You don't have permission to view this room.", "You don't have permission to view this room.",

@ -1,5 +1,5 @@
use super::{DEVICE_ID_LENGTH, TOKEN_LENGTH}; use super::{DEVICE_ID_LENGTH, TOKEN_LENGTH};
use crate::{utils, Error, Result, Ruma, services}; use crate::{services, utils, Error, Result, Ruma};
use ruma::{ use ruma::{
api::client::{ api::client::{
error::ErrorKind, error::ErrorKind,
@ -40,9 +40,7 @@ pub async fn get_login_types_route(
/// ///
/// Note: You can use [`GET /_matrix/client/r0/login`](fn.get_supported_versions_route.html) to see /// Note: You can use [`GET /_matrix/client/r0/login`](fn.get_supported_versions_route.html) to see
/// supported login types. /// supported login types.
pub async fn login_route( pub async fn login_route(body: Ruma<login::v3::IncomingRequest>) -> Result<login::v3::Response> {
body: Ruma<login::v3::IncomingRequest>,
) -> Result<login::v3::Response> {
// Validate login method // Validate login method
// TODO: Other login methods // TODO: Other login methods
let user_id = match &body.login_info { let user_id = match &body.login_info {
@ -55,12 +53,15 @@ pub async fn login_route(
} else { } else {
return Err(Error::BadRequest(ErrorKind::Forbidden, "Bad login type.")); return Err(Error::BadRequest(ErrorKind::Forbidden, "Bad login type."));
}; };
let user_id = let user_id = UserId::parse_with_server_name(
UserId::parse_with_server_name(username.to_owned(), services().globals.server_name()) username.to_owned(),
.map_err(|_| { services().globals.server_name(),
Error::BadRequest(ErrorKind::InvalidUsername, "Username is invalid.") )
})?; .map_err(|_| Error::BadRequest(ErrorKind::InvalidUsername, "Username is invalid."))?;
let hash = services().users.password_hash(&user_id)?.ok_or(Error::BadRequest( let hash = services()
.users
.password_hash(&user_id)?
.ok_or(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"Wrong username or password.", "Wrong username or password.",
))?; ))?;
@ -121,7 +122,8 @@ pub async fn login_route(
// Determine if device_id was provided and exists in the db for this user // Determine if device_id was provided and exists in the db for this user
let device_exists = body.device_id.as_ref().map_or(false, |device_id| { let device_exists = body.device_id.as_ref().map_or(false, |device_id| {
services().users services()
.users
.all_device_ids(&user_id) .all_device_ids(&user_id)
.any(|x| x.as_ref().map_or(false, |v| v == device_id)) .any(|x| x.as_ref().map_or(false, |v| v == device_id))
}); });
@ -156,9 +158,7 @@ pub async fn login_route(
/// - Deletes device metadata (device id, device display name, last seen ip, last seen ts) /// - Deletes device metadata (device id, device display name, last seen ip, last seen ts)
/// - Forgets to-device events /// - Forgets to-device events
/// - Triggers device list updates /// - Triggers device list updates
pub async fn logout_route( pub async fn logout_route(body: Ruma<logout::v3::Request>) -> Result<logout::v3::Response> {
body: Ruma<logout::v3::Request>,
) -> Result<logout::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sender_device = body.sender_device.as_ref().expect("user is authenticated"); let sender_device = body.sender_device.as_ref().expect("user is authenticated");

@ -1,8 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use crate::{ use crate::{service::pdu::PduBuilder, services, Error, Result, Ruma, RumaResponse};
Error, Result, Ruma, RumaResponse, services, service::pdu::PduBuilder,
};
use ruma::{ use ruma::{
api::client::{ api::client::{
error::ErrorKind, error::ErrorKind,
@ -90,9 +88,13 @@ pub async fn get_state_events_route(
#[allow(clippy::blocks_in_if_conditions)] #[allow(clippy::blocks_in_if_conditions)]
// Users not in the room should not be able to access the state unless history_visibility is // Users not in the room should not be able to access the state unless history_visibility is
// WorldReadable // WorldReadable
if !services().rooms.state_cache.is_joined(sender_user, &body.room_id)? if !services()
.rooms
.state_cache
.is_joined(sender_user, &body.room_id)?
&& !matches!( && !matches!(
services().rooms services()
.rooms
.state_accessor .state_accessor
.room_state_get(&body.room_id, &StateEventType::RoomHistoryVisibility, "")? .room_state_get(&body.room_id, &StateEventType::RoomHistoryVisibility, "")?
.map(|event| { .map(|event| {
@ -138,10 +140,15 @@ pub async fn get_state_events_for_key_route(
#[allow(clippy::blocks_in_if_conditions)] #[allow(clippy::blocks_in_if_conditions)]
// Users not in the room should not be able to access the state unless history_visibility is // Users not in the room should not be able to access the state unless history_visibility is
// WorldReadable // WorldReadable
if !services().rooms.state_cache.is_joined(sender_user, &body.room_id)? if !services()
.rooms
.state_cache
.is_joined(sender_user, &body.room_id)?
&& !matches!( && !matches!(
services().rooms services()
.state_accessor.room_state_get(&body.room_id, &StateEventType::RoomHistoryVisibility, "")? .rooms
.state_accessor
.room_state_get(&body.room_id, &StateEventType::RoomHistoryVisibility, "")?
.map(|event| { .map(|event| {
serde_json::from_str(event.content.get()) serde_json::from_str(event.content.get())
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility) .map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
@ -162,7 +169,8 @@ pub async fn get_state_events_for_key_route(
let event = services() let event = services()
.rooms .rooms
.state_accessor.room_state_get(&body.room_id, &body.event_type, &body.state_key)? .state_accessor
.room_state_get(&body.room_id, &body.event_type, &body.state_key)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
"State event not found.", "State event not found.",
@ -187,10 +195,15 @@ pub async fn get_state_events_for_empty_key_route(
#[allow(clippy::blocks_in_if_conditions)] #[allow(clippy::blocks_in_if_conditions)]
// Users not in the room should not be able to access the state unless history_visibility is // Users not in the room should not be able to access the state unless history_visibility is
// WorldReadable // WorldReadable
if !services().rooms.state_cache.is_joined(sender_user, &body.room_id)? if !services()
.rooms
.state_cache
.is_joined(sender_user, &body.room_id)?
&& !matches!( && !matches!(
services().rooms services()
.state_accessor.room_state_get(&body.room_id, &StateEventType::RoomHistoryVisibility, "")? .rooms
.state_accessor
.room_state_get(&body.room_id, &StateEventType::RoomHistoryVisibility, "")?
.map(|event| { .map(|event| {
serde_json::from_str(event.content.get()) serde_json::from_str(event.content.get())
.map(|e: RoomHistoryVisibilityEventContent| e.history_visibility) .map(|e: RoomHistoryVisibilityEventContent| e.history_visibility)
@ -211,7 +224,8 @@ pub async fn get_state_events_for_empty_key_route(
let event = services() let event = services()
.rooms .rooms
.state_accessor.room_state_get(&body.room_id, &body.event_type, "")? .state_accessor
.room_state_get(&body.room_id, &body.event_type, "")?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
"State event not found.", "State event not found.",
@ -248,7 +262,8 @@ async fn send_state_event_for_key_helper(
if alias.server_name() != services().globals.server_name() if alias.server_name() != services().globals.server_name()
|| services() || services()
.rooms .rooms
.alias.resolve_local_alias(&alias)? .alias
.resolve_local_alias(&alias)?
.filter(|room| room == room_id) // Make sure it's the right room .filter(|room| room == room_id) // Make sure it's the right room
.is_none() .is_none()
{ {
@ -262,7 +277,8 @@ async fn send_state_event_for_key_helper(
} }
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()

@ -1,4 +1,4 @@
use crate::{Error, Result, Ruma, RumaResponse, services}; use crate::{services, Error, Result, Ruma, RumaResponse};
use ruma::{ use ruma::{
api::client::{ api::client::{
filter::{IncomingFilterDefinition, LazyLoadOptions}, filter::{IncomingFilterDefinition, LazyLoadOptions},
@ -129,12 +129,7 @@ async fn sync_helper_wrapper(
) { ) {
let since = body.since.clone(); let since = body.since.clone();
let r = sync_helper( let r = sync_helper(sender_user.clone(), sender_device.clone(), body).await;
sender_user.clone(),
sender_device.clone(),
body,
)
.await;
if let Ok((_, caching_allowed)) = r { if let Ok((_, caching_allowed)) = r {
if !caching_allowed { if !caching_allowed {
@ -211,12 +206,17 @@ async fn sync_helper(
// Look for device list updates of this account // Look for device list updates of this account
device_list_updates.extend( device_list_updates.extend(
services().users services()
.users
.keys_changed(&sender_user.to_string(), since, None) .keys_changed(&sender_user.to_string(), since, None)
.filter_map(|r| r.ok()), .filter_map(|r| r.ok()),
); );
let all_joined_rooms = services().rooms.state_cache.rooms_joined(&sender_user).collect::<Vec<_>>(); let all_joined_rooms = services()
.rooms
.state_cache
.rooms_joined(&sender_user)
.collect::<Vec<_>>();
for room_id in all_joined_rooms { for room_id in all_joined_rooms {
let room_id = room_id?; let room_id = room_id?;
@ -224,7 +224,8 @@ async fn sync_helper(
// Get and drop the lock to wait for remaining operations to finish // Get and drop the lock to wait for remaining operations to finish
// This will make sure the we have all events until next_batch // This will make sure the we have all events until next_batch
let mutex_insert = Arc::clone( let mutex_insert = Arc::clone(
services().globals services()
.globals
.roomid_mutex_insert .roomid_mutex_insert
.write() .write()
.unwrap() .unwrap()
@ -237,7 +238,12 @@ async fn sync_helper(
let timeline_pdus; let timeline_pdus;
let limited; let limited;
if services().rooms.timeline.last_timeline_count(&sender_user, &room_id)? > since { if services()
.rooms
.timeline
.last_timeline_count(&sender_user, &room_id)?
> since
{
let mut non_timeline_pdus = services() let mut non_timeline_pdus = services()
.rooms .rooms
.timeline .timeline
@ -250,7 +256,8 @@ async fn sync_helper(
r.ok() r.ok()
}) })
.take_while(|(pduid, _)| { .take_while(|(pduid, _)| {
services().rooms services()
.rooms
.timeline .timeline
.pdu_count(pduid) .pdu_count(pduid)
.map_or(false, |count| count > since) .map_or(false, |count| count > since)
@ -286,24 +293,40 @@ async fn sync_helper(
timeline_users.insert(event.sender.as_str().to_owned()); timeline_users.insert(event.sender.as_str().to_owned());
} }
services().rooms.lazy_loading services().rooms.lazy_loading.lazy_load_confirm_delivery(
.lazy_load_confirm_delivery(&sender_user, &sender_device, &room_id, since)?; &sender_user,
&sender_device,
&room_id,
since,
)?;
// Database queries: // Database queries:
let current_shortstatehash = if let Some(s) = services().rooms.state.get_room_shortstatehash(&room_id)? { let current_shortstatehash =
if let Some(s) = services().rooms.state.get_room_shortstatehash(&room_id)? {
s s
} else { } else {
error!("Room {} has no state", room_id); error!("Room {} has no state", room_id);
continue; continue;
}; };
let since_shortstatehash = services().rooms.user.get_token_shortstatehash(&room_id, since)?; let since_shortstatehash = services()
.rooms
.user
.get_token_shortstatehash(&room_id, since)?;
// Calculates joined_member_count, invited_member_count and heroes // Calculates joined_member_count, invited_member_count and heroes
let calculate_counts = || { let calculate_counts = || {
let joined_member_count = services().rooms.state_cache.room_joined_count(&room_id)?.unwrap_or(0); let joined_member_count = services()
let invited_member_count = services().rooms.state_cache.room_invited_count(&room_id)?.unwrap_or(0); .rooms
.state_cache
.room_joined_count(&room_id)?
.unwrap_or(0);
let invited_member_count = services()
.rooms
.state_cache
.room_invited_count(&room_id)?
.unwrap_or(0);
// Recalculate heroes (first 5 members) // Recalculate heroes (first 5 members)
let mut heroes = Vec::new(); let mut heroes = Vec::new();
@ -314,7 +337,8 @@ async fn sync_helper(
for hero in services() for hero in services()
.rooms .rooms
.timeline.all_pdus(&sender_user, &room_id)? .timeline
.all_pdus(&sender_user, &room_id)?
.filter_map(|pdu| pdu.ok()) // Ignore all broken pdus .filter_map(|pdu| pdu.ok()) // Ignore all broken pdus
.filter(|(_, pdu)| pdu.kind == RoomEventType::RoomMember) .filter(|(_, pdu)| pdu.kind == RoomEventType::RoomMember)
.map(|(_, pdu)| { .map(|(_, pdu)| {
@ -333,7 +357,10 @@ async fn sync_helper(
content.membership, content.membership,
MembershipState::Join | MembershipState::Invite MembershipState::Join | MembershipState::Invite
) && (services().rooms.state_cache.is_joined(&user_id, &room_id)? ) && (services().rooms.state_cache.is_joined(&user_id, &room_id)?
|| services().rooms.state_cache.is_invited(&user_id, &room_id)?) || services()
.rooms
.state_cache
.is_invited(&user_id, &room_id)?)
{ {
Ok::<_, Error>(Some(state_key.clone())) Ok::<_, Error>(Some(state_key.clone()))
} else { } else {
@ -374,14 +401,21 @@ async fn sync_helper(
let (joined_member_count, invited_member_count, heroes) = calculate_counts()?; let (joined_member_count, invited_member_count, heroes) = calculate_counts()?;
let current_state_ids = services().rooms.state_accessor.state_full_ids(current_shortstatehash).await?; let current_state_ids = services()
.rooms
.state_accessor
.state_full_ids(current_shortstatehash)
.await?;
let mut state_events = Vec::new(); let mut state_events = Vec::new();
let mut lazy_loaded = HashSet::new(); let mut lazy_loaded = HashSet::new();
let mut i = 0; let mut i = 0;
for (shortstatekey, id) in current_state_ids { for (shortstatekey, id) in current_state_ids {
let (event_type, state_key) = services().rooms.short.get_statekey_from_short(shortstatekey)?; let (event_type, state_key) = services()
.rooms
.short
.get_statekey_from_short(shortstatekey)?;
if event_type != StateEventType::RoomMember { if event_type != StateEventType::RoomMember {
let pdu = match services().rooms.timeline.get_pdu(&id)? { let pdu = match services().rooms.timeline.get_pdu(&id)? {
@ -423,8 +457,11 @@ async fn sync_helper(
} }
// Reset lazy loading because this is an initial sync // Reset lazy loading because this is an initial sync
services().rooms.lazy_loading services().rooms.lazy_loading.lazy_load_reset(
.lazy_load_reset(&sender_user, &sender_device, &room_id)?; &sender_user,
&sender_device,
&room_id,
)?;
// The state_events above should contain all timeline_users, let's mark them as lazy // The state_events above should contain all timeline_users, let's mark them as lazy
// loaded. // loaded.
@ -471,8 +508,16 @@ async fn sync_helper(
let mut lazy_loaded = HashSet::new(); let mut lazy_loaded = HashSet::new();
if since_shortstatehash != current_shortstatehash { if since_shortstatehash != current_shortstatehash {
let current_state_ids = services().rooms.state_accessor.state_full_ids(current_shortstatehash).await?; let current_state_ids = services()
let since_state_ids = services().rooms.state_accessor.state_full_ids(since_shortstatehash).await?; .rooms
.state_accessor
.state_full_ids(current_shortstatehash)
.await?;
let since_state_ids = services()
.rooms
.state_accessor
.state_full_ids(since_shortstatehash)
.await?;
for (key, id) in current_state_ids { for (key, id) in current_state_ids {
if body.full_state || since_state_ids.get(&key) != Some(&id) { if body.full_state || since_state_ids.get(&key) != Some(&id) {
@ -537,13 +582,15 @@ async fn sync_helper(
let encrypted_room = services() let encrypted_room = services()
.rooms .rooms
.state_accessor.state_get(current_shortstatehash, &StateEventType::RoomEncryption, "")? .state_accessor
.state_get(current_shortstatehash, &StateEventType::RoomEncryption, "")?
.is_some(); .is_some();
let since_encryption = let since_encryption = services().rooms.state_accessor.state_get(
services().rooms since_shortstatehash,
.state_accessor &StateEventType::RoomEncryption,
.state_get(since_shortstatehash, &StateEventType::RoomEncryption, "")?; "",
)?;
// Calculations: // Calculations:
let new_encrypted_room = encrypted_room && since_encryption.is_none(); let new_encrypted_room = encrypted_room && since_encryption.is_none();
@ -592,7 +639,8 @@ async fn sync_helper(
if joined_since_last_sync && encrypted_room || new_encrypted_room { if joined_since_last_sync && encrypted_room || new_encrypted_room {
// If the user is in a new encrypted room, give them all joined users // If the user is in a new encrypted room, give them all joined users
device_list_updates.extend( device_list_updates.extend(
services().rooms services()
.rooms
.state_cache .state_cache
.room_members(&room_id) .room_members(&room_id)
.flatten() .flatten()
@ -602,8 +650,7 @@ async fn sync_helper(
}) })
.filter(|user_id| { .filter(|user_id| {
// Only send keys if the sender doesn't share an encrypted room with the target already // Only send keys if the sender doesn't share an encrypted room with the target already
!share_encrypted_room(&sender_user, user_id, &room_id) !share_encrypted_room(&sender_user, user_id, &room_id).unwrap_or(false)
.unwrap_or(false)
}), }),
); );
} }
@ -625,14 +672,16 @@ async fn sync_helper(
// Look for device list updates in this room // Look for device list updates in this room
device_list_updates.extend( device_list_updates.extend(
services().users services()
.users
.keys_changed(&room_id.to_string(), since, None) .keys_changed(&room_id.to_string(), since, None)
.filter_map(|r| r.ok()), .filter_map(|r| r.ok()),
); );
let notification_count = if send_notification_counts { let notification_count = if send_notification_counts {
Some( Some(
services().rooms services()
.rooms
.user .user
.notification_count(&sender_user, &room_id)? .notification_count(&sender_user, &room_id)?
.try_into() .try_into()
@ -644,7 +693,8 @@ async fn sync_helper(
let highlight_count = if send_notification_counts { let highlight_count = if send_notification_counts {
Some( Some(
services().rooms services()
.rooms
.user .user
.highlight_count(&sender_user, &room_id)? .highlight_count(&sender_user, &room_id)?
.try_into() .try_into()
@ -657,7 +707,9 @@ async fn sync_helper(
let prev_batch = timeline_pdus let prev_batch = timeline_pdus
.first() .first()
.map_or(Ok::<_, Error>(None), |(pdu_id, _)| { .map_or(Ok::<_, Error>(None), |(pdu_id, _)| {
Ok(Some(services().rooms.timeline.pdu_count(pdu_id)?.to_string())) Ok(Some(
services().rooms.timeline.pdu_count(pdu_id)?.to_string(),
))
})?; })?;
let room_events: Vec<_> = timeline_pdus let room_events: Vec<_> = timeline_pdus
@ -685,8 +737,11 @@ async fn sync_helper(
} }
// Save the state after this sync so we can send the correct state diff next sync // Save the state after this sync so we can send the correct state diff next sync
services().rooms.user services().rooms.user.associate_token_shortstatehash(
.associate_token_shortstatehash(&room_id, next_batch, current_shortstatehash)?; &room_id,
next_batch,
current_shortstatehash,
)?;
let joined_room = JoinedRoom { let joined_room = JoinedRoom {
account_data: RoomAccountData { account_data: RoomAccountData {
@ -729,8 +784,8 @@ async fn sync_helper(
} }
// Take presence updates from this room // Take presence updates from this room
for (user_id, presence) in for (user_id, presence) in services()
services().rooms .rooms
.edus .edus
.presence .presence
.presence_since(&room_id, since)? .presence_since(&room_id, since)?
@ -765,14 +820,19 @@ async fn sync_helper(
} }
let mut left_rooms = BTreeMap::new(); let mut left_rooms = BTreeMap::new();
let all_left_rooms: Vec<_> = services().rooms.state_cache.rooms_left(&sender_user).collect(); let all_left_rooms: Vec<_> = services()
.rooms
.state_cache
.rooms_left(&sender_user)
.collect();
for result in all_left_rooms { for result in all_left_rooms {
let (room_id, left_state_events) = result?; let (room_id, left_state_events) = result?;
{ {
// Get and drop the lock to wait for remaining operations to finish // Get and drop the lock to wait for remaining operations to finish
let mutex_insert = Arc::clone( let mutex_insert = Arc::clone(
services().globals services()
.globals
.roomid_mutex_insert .roomid_mutex_insert
.write() .write()
.unwrap() .unwrap()
@ -783,7 +843,10 @@ async fn sync_helper(
drop(insert_lock); drop(insert_lock);
} }
let left_count = services().rooms.state_cache.get_left_count(&room_id, &sender_user)?; let left_count = services()
.rooms
.state_cache
.get_left_count(&room_id, &sender_user)?;
// Left before last sync // Left before last sync
if Some(since) >= left_count { if Some(since) >= left_count {
@ -807,14 +870,19 @@ async fn sync_helper(
} }
let mut invited_rooms = BTreeMap::new(); let mut invited_rooms = BTreeMap::new();
let all_invited_rooms: Vec<_> = services().rooms.state_cache.rooms_invited(&sender_user).collect(); let all_invited_rooms: Vec<_> = services()
.rooms
.state_cache
.rooms_invited(&sender_user)
.collect();
for result in all_invited_rooms { for result in all_invited_rooms {
let (room_id, invite_state_events) = result?; let (room_id, invite_state_events) = result?;
{ {
// Get and drop the lock to wait for remaining operations to finish // Get and drop the lock to wait for remaining operations to finish
let mutex_insert = Arc::clone( let mutex_insert = Arc::clone(
services().globals services()
.globals
.roomid_mutex_insert .roomid_mutex_insert
.write() .write()
.unwrap() .unwrap()
@ -825,7 +893,10 @@ async fn sync_helper(
drop(insert_lock); drop(insert_lock);
} }
let invite_count = services().rooms.state_cache.get_invite_count(&room_id, &sender_user)?; let invite_count = services()
.rooms
.state_cache
.get_invite_count(&room_id, &sender_user)?;
// Invited before last sync // Invited before last sync
if Some(since) >= invite_count { if Some(since) >= invite_count {
@ -850,8 +921,10 @@ async fn sync_helper(
.filter_map(|r| r.ok()) .filter_map(|r| r.ok())
.filter_map(|other_room_id| { .filter_map(|other_room_id| {
Some( Some(
services().rooms services()
.state_accessor.room_state_get(&other_room_id, &StateEventType::RoomEncryption, "") .rooms
.state_accessor
.room_state_get(&other_room_id, &StateEventType::RoomEncryption, "")
.ok()? .ok()?
.is_some(), .is_some(),
) )
@ -865,7 +938,8 @@ async fn sync_helper(
} }
// Remove all to-device events the device received *last time* // Remove all to-device events the device received *last time*
services().users services()
.users
.remove_to_device_events(&sender_user, &sender_device, since)?; .remove_to_device_events(&sender_user, &sender_device, since)?;
let response = sync_events::v3::Response { let response = sync_events::v3::Response {
@ -898,7 +972,9 @@ async fn sync_helper(
changed: device_list_updates.into_iter().collect(), changed: device_list_updates.into_iter().collect(),
left: device_list_left.into_iter().collect(), left: device_list_left.into_iter().collect(),
}, },
device_one_time_keys_count: services().users.count_one_time_keys(&sender_user, &sender_device)?, device_one_time_keys_count: services()
.users
.count_one_time_keys(&sender_user, &sender_device)?,
to_device: ToDevice { to_device: ToDevice {
events: services() events: services()
.users .users
@ -942,7 +1018,8 @@ fn share_encrypted_room(
.filter(|room_id| room_id != ignore_room) .filter(|room_id| room_id != ignore_room)
.filter_map(|other_room_id| { .filter_map(|other_room_id| {
Some( Some(
services().rooms services()
.rooms
.state_accessor .state_accessor
.room_state_get(&other_room_id, &StateEventType::RoomEncryption, "") .room_state_get(&other_room_id, &StateEventType::RoomEncryption, "")
.ok()? .ok()?

@ -1,4 +1,4 @@
use crate::{Result, Ruma, services, Error}; use crate::{services, Error, Result, Ruma};
use ruma::{ use ruma::{
api::client::tag::{create_tag, delete_tag, get_tags}, api::client::tag::{create_tag, delete_tag, get_tags},
events::{ events::{
@ -18,21 +18,24 @@ pub async fn update_tag_route(
) -> Result<create_tag::v3::Response> { ) -> Result<create_tag::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let event = services() let event = services().account_data.get(
.account_data
.get(
Some(&body.room_id), Some(&body.room_id),
sender_user, sender_user,
RoomAccountDataEventType::Tag, RoomAccountDataEventType::Tag,
)?; )?;
let mut tags_event = event.map(|e| serde_json::from_str(e.get()) let mut tags_event = event
.map_err(|_| Error::bad_database("Invalid account data event in db."))) .map(|e| {
.unwrap_or_else(|| Ok(TagEvent { serde_json::from_str(e.get())
.map_err(|_| Error::bad_database("Invalid account data event in db."))
})
.unwrap_or_else(|| {
Ok(TagEvent {
content: TagEventContent { content: TagEventContent {
tags: BTreeMap::new(), tags: BTreeMap::new(),
}, },
}))?; })
})?;
tags_event tags_event
.content .content
@ -59,21 +62,24 @@ pub async fn delete_tag_route(
) -> Result<delete_tag::v3::Response> { ) -> Result<delete_tag::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut event = services() let mut event = services().account_data.get(
.account_data
.get(
Some(&body.room_id), Some(&body.room_id),
sender_user, sender_user,
RoomAccountDataEventType::Tag, RoomAccountDataEventType::Tag,
)?; )?;
let mut tags_event = event.map(|e| serde_json::from_str(e.get()) let mut tags_event = event
.map_err(|_| Error::bad_database("Invalid account data event in db."))) .map(|e| {
.unwrap_or_else(|| Ok(TagEvent { serde_json::from_str(e.get())
.map_err(|_| Error::bad_database("Invalid account data event in db."))
})
.unwrap_or_else(|| {
Ok(TagEvent {
content: TagEventContent { content: TagEventContent {
tags: BTreeMap::new(), tags: BTreeMap::new(),
}, },
}))?; })
})?;
tags_event.content.tags.remove(&body.tag.clone().into()); tags_event.content.tags.remove(&body.tag.clone().into());
@ -97,21 +103,24 @@ pub async fn get_tags_route(
) -> Result<get_tags::v3::Response> { ) -> Result<get_tags::v3::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let mut event = services() let mut event = services().account_data.get(
.account_data
.get(
Some(&body.room_id), Some(&body.room_id),
sender_user, sender_user,
RoomAccountDataEventType::Tag, RoomAccountDataEventType::Tag,
)?; )?;
let mut tags_event = event.map(|e| serde_json::from_str(e.get()) let mut tags_event = event
.map_err(|_| Error::bad_database("Invalid account data event in db."))) .map(|e| {
.unwrap_or_else(|| Ok(TagEvent { serde_json::from_str(e.get())
.map_err(|_| Error::bad_database("Invalid account data event in db."))
})
.unwrap_or_else(|| {
Ok(TagEvent {
content: TagEventContent { content: TagEventContent {
tags: BTreeMap::new(), tags: BTreeMap::new(),
}, },
}))?; })
})?;
Ok(get_tags::v3::Response { Ok(get_tags::v3::Response {
tags: tags_event.content.tags, tags: tags_event.content.tags,

@ -1,7 +1,7 @@
use ruma::events::ToDeviceEventType; use ruma::events::ToDeviceEventType;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use crate::{Error, Result, Ruma, services}; use crate::{services, Error, Result, Ruma};
use ruma::{ use ruma::{
api::{ api::{
client::{error::ErrorKind, to_device::send_event_to_device}, client::{error::ErrorKind, to_device::send_event_to_device},
@ -54,7 +54,8 @@ pub async fn send_event_to_device_route(
} }
match target_device_id_maybe { match target_device_id_maybe {
DeviceIdOrAllDevices::DeviceId(target_device_id) => services().users.add_to_device_event( DeviceIdOrAllDevices::DeviceId(target_device_id) => {
services().users.add_to_device_event(
sender_user, sender_user,
target_user_id, target_user_id,
&target_device_id, &target_device_id,
@ -62,7 +63,8 @@ pub async fn send_event_to_device_route(
event.deserialize_as().map_err(|_| { event.deserialize_as().map_err(|_| {
Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid") Error::BadRequest(ErrorKind::InvalidParam, "Event is invalid")
})?, })?,
)?, )?
}
DeviceIdOrAllDevices::AllDevices => { DeviceIdOrAllDevices::AllDevices => {
for target_device_id in services().users.all_device_ids(target_user_id) { for target_device_id in services().users.all_device_ids(target_user_id) {
@ -82,7 +84,8 @@ pub async fn send_event_to_device_route(
} }
// Save transaction id with empty data // Save transaction id with empty data
services().transaction_ids services()
.transaction_ids
.add_txnid(sender_user, sender_device, &body.txn_id, &[])?; .add_txnid(sender_user, sender_device, &body.txn_id, &[])?;
Ok(send_event_to_device::v3::Response {}) Ok(send_event_to_device::v3::Response {})

@ -1,4 +1,4 @@
use crate::{utils, Error, Result, Ruma, services}; use crate::{services, utils, Error, Result, Ruma};
use ruma::api::client::{error::ErrorKind, typing::create_typing_event}; use ruma::api::client::{error::ErrorKind, typing::create_typing_event};
/// # `PUT /_matrix/client/r0/rooms/{roomId}/typing/{userId}` /// # `PUT /_matrix/client/r0/rooms/{roomId}/typing/{userId}`
@ -11,7 +11,11 @@ pub async fn create_typing_event_route(
let sender_user = body.sender_user.as_ref().expect("user is authenticated"); let sender_user = body.sender_user.as_ref().expect("user is authenticated");
if !services().rooms.state_cache.is_joined(sender_user, &body.room_id)? { if !services()
.rooms
.state_cache
.is_joined(sender_user, &body.room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"You are not in this room.", "You are not in this room.",
@ -25,8 +29,10 @@ pub async fn create_typing_event_route(
duration.as_millis() as u64 + utils::millis_since_unix_epoch(), duration.as_millis() as u64 + utils::millis_since_unix_epoch(),
)?; )?;
} else { } else {
services().rooms services()
.edus.typing .rooms
.edus
.typing
.typing_remove(sender_user, &body.room_id)?; .typing_remove(sender_user, &body.room_id)?;
} }

@ -1,4 +1,4 @@
use crate::{Result, Ruma, services}; use crate::{services, Result, Ruma};
use ruma::{ use ruma::{
api::client::user_directory::search_users, api::client::user_directory::search_users,
events::{ events::{
@ -48,13 +48,16 @@ pub async fn search_users_route(
return None; return None;
} }
let user_is_in_public_rooms = let user_is_in_public_rooms = services()
services().rooms .rooms
.state_cache.rooms_joined(&user_id) .state_cache
.rooms_joined(&user_id)
.filter_map(|r| r.ok()) .filter_map(|r| r.ok())
.any(|room| { .any(|room| {
services().rooms services()
.state_accessor.room_state_get(&room, &StateEventType::RoomJoinRules, "") .rooms
.state_accessor
.room_state_get(&room, &StateEventType::RoomJoinRules, "")
.map_or(false, |event| { .map_or(false, |event| {
event.map_or(false, |event| { event.map_or(false, |event| {
serde_json::from_str(event.content.get()) serde_json::from_str(event.content.get())
@ -71,7 +74,8 @@ pub async fn search_users_route(
let user_is_in_shared_rooms = services() let user_is_in_shared_rooms = services()
.rooms .rooms
.user.get_shared_rooms(vec![sender_user.clone(), user_id.clone()]) .user
.get_shared_rooms(vec![sender_user.clone(), user_id.clone()])
.ok()? .ok()?
.next() .next()
.is_some(); .is_some();

@ -1,4 +1,4 @@
use crate::{Result, Ruma, services}; use crate::{services, Result, Ruma};
use hmac::{Hmac, Mac, NewMac}; use hmac::{Hmac, Mac, NewMac};
use ruma::{api::client::voip::get_turn_server_info, SecondsSinceUnixEpoch}; use ruma::{api::client::voip::get_turn_server_info, SecondsSinceUnixEpoch};
use sha1::Sha1; use sha1::Sha1;

@ -1,4 +1,4 @@
pub mod client_server;
pub mod server_server;
pub mod appservice_server; pub mod appservice_server;
pub mod client_server;
pub mod ruma_wrapper; pub mod ruma_wrapper;
pub mod server_server;

@ -24,7 +24,7 @@ use serde::Deserialize;
use tracing::{debug, error, warn}; use tracing::{debug, error, warn};
use super::{Ruma, RumaResponse}; use super::{Ruma, RumaResponse};
use crate::{Error, Result, api::server_server, services}; use crate::{api::server_server, services, Error, Result};
#[async_trait] #[async_trait]
impl<T, B> FromRequest<B> for Ruma<T> impl<T, B> FromRequest<B> for Ruma<T>
@ -197,10 +197,10 @@ where
request_map.insert("content".to_owned(), json_body.clone()); request_map.insert("content".to_owned(), json_body.clone());
}; };
let keys_result = services().rooms.event_handler.fetch_signing_keys( let keys_result = services()
&x_matrix.origin, .rooms
vec![x_matrix.key.to_owned()], .event_handler
) .fetch_signing_keys(&x_matrix.origin, vec![x_matrix.key.to_owned()])
.await; .await;
let keys = match keys_result { let keys = match keys_result {

@ -1,6 +1,7 @@
use crate::{ use crate::{
api::client_server::{self, claim_keys_helper, get_keys_helper}, api::client_server::{self, claim_keys_helper, get_keys_helper},
utils, Error, PduEvent, Result, Ruma, services, service::pdu::{gen_event_id_canonical_json, PduBuilder}, service::pdu::{gen_event_id_canonical_json, PduBuilder},
services, utils, Error, PduEvent, Result, Ruma,
}; };
use axum::{response::IntoResponse, Json}; use axum::{response::IntoResponse, Json};
use futures_util::{stream::FuturesUnordered, StreamExt}; use futures_util::{stream::FuturesUnordered, StreamExt};
@ -138,7 +139,8 @@ where
let mut write_destination_to_cache = false; let mut write_destination_to_cache = false;
let cached_result = services().globals let cached_result = services()
.globals
.actual_destination_cache .actual_destination_cache
.read() .read()
.unwrap() .unwrap()
@ -191,7 +193,10 @@ where
.to_string() .to_string()
.into(), .into(),
); );
request_map.insert("origin".to_owned(), services().globals.server_name().as_str().into()); request_map.insert(
"origin".to_owned(),
services().globals.server_name().as_str().into(),
);
request_map.insert("destination".to_owned(), destination.as_str().into()); request_map.insert("destination".to_owned(), destination.as_str().into());
let mut request_json = let mut request_json =
@ -238,7 +243,11 @@ where
let url = reqwest_request.url().clone(); let url = reqwest_request.url().clone();
let response = services().globals.federation_client().execute(reqwest_request).await; let response = services()
.globals
.federation_client()
.execute(reqwest_request)
.await;
match response { match response {
Ok(mut response) => { Ok(mut response) => {
@ -278,7 +287,12 @@ where
if status == 200 { if status == 200 {
let response = T::IncomingResponse::try_from_http_response(http_response); let response = T::IncomingResponse::try_from_http_response(http_response);
if response.is_ok() && write_destination_to_cache { if response.is_ok() && write_destination_to_cache {
services().globals.actual_destination_cache.write().unwrap().insert( services()
.globals
.actual_destination_cache
.write()
.unwrap()
.insert(
Box::<ServerName>::from(destination), Box::<ServerName>::from(destination),
(actual_destination, host), (actual_destination, host),
); );
@ -329,9 +343,7 @@ fn add_port_to_hostname(destination_str: &str) -> FedDest {
/// Returns: actual_destination, host header /// Returns: actual_destination, host header
/// Implemented according to the specification at https://matrix.org/docs/spec/server_server/r0.1.4#resolving-server-names /// Implemented according to the specification at https://matrix.org/docs/spec/server_server/r0.1.4#resolving-server-names
/// Numbers in comments below refer to bullet points in linked section of specification /// Numbers in comments below refer to bullet points in linked section of specification
async fn find_actual_destination( async fn find_actual_destination(destination: &'_ ServerName) -> (FedDest, FedDest) {
destination: &'_ ServerName,
) -> (FedDest, FedDest) {
let destination_str = destination.as_str().to_owned(); let destination_str = destination.as_str().to_owned();
let mut hostname = destination_str.clone(); let mut hostname = destination_str.clone();
let actual_destination = match get_ip_with_port(&destination_str) { let actual_destination = match get_ip_with_port(&destination_str) {
@ -364,12 +376,18 @@ async fn find_actual_destination(
// 3.3: SRV lookup successful // 3.3: SRV lookup successful
let force_port = hostname_override.port(); let force_port = hostname_override.port();
if let Ok(override_ip) = services().globals if let Ok(override_ip) = services()
.globals
.dns_resolver() .dns_resolver()
.lookup_ip(hostname_override.hostname()) .lookup_ip(hostname_override.hostname())
.await .await
{ {
services().globals.tls_name_override.write().unwrap().insert( services()
.globals
.tls_name_override
.write()
.unwrap()
.insert(
delegated_hostname.clone(), delegated_hostname.clone(),
( (
override_ip.iter().collect(), override_ip.iter().collect(),
@ -400,14 +418,23 @@ async fn find_actual_destination(
Some(hostname_override) => { Some(hostname_override) => {
let force_port = hostname_override.port(); let force_port = hostname_override.port();
if let Ok(override_ip) = services().globals if let Ok(override_ip) = services()
.globals
.dns_resolver() .dns_resolver()
.lookup_ip(hostname_override.hostname()) .lookup_ip(hostname_override.hostname())
.await .await
{ {
services().globals.tls_name_override.write().unwrap().insert( services()
.globals
.tls_name_override
.write()
.unwrap()
.insert(
hostname.clone(), hostname.clone(),
(override_ip.iter().collect(), force_port.unwrap_or(8448)), (
override_ip.iter().collect(),
force_port.unwrap_or(8448),
),
); );
} else { } else {
warn!("Using SRV record, but could not resolve to IP"); warn!("Using SRV record, but could not resolve to IP");
@ -443,10 +470,9 @@ async fn find_actual_destination(
(actual_destination, hostname) (actual_destination, hostname)
} }
async fn query_srv_record( async fn query_srv_record(hostname: &'_ str) -> Option<FedDest> {
hostname: &'_ str, if let Ok(Some(host_port)) = services()
) -> Option<FedDest> { .globals
if let Ok(Some(host_port)) = services().globals
.dns_resolver() .dns_resolver()
.srv_lookup(format!("_matrix._tcp.{}", hostname)) .srv_lookup(format!("_matrix._tcp.{}", hostname))
.await .await
@ -465,11 +491,10 @@ async fn query_srv_record(
} }
} }
async fn request_well_known( async fn request_well_known(destination: &str) -> Option<String> {
destination: &str,
) -> Option<String> {
let body: serde_json::Value = serde_json::from_str( let body: serde_json::Value = serde_json::from_str(
&services().globals &services()
.globals
.default_client() .default_client()
.get(&format!( .get(&format!(
"https://{}/.well-known/matrix/server", "https://{}/.well-known/matrix/server",
@ -664,15 +689,22 @@ pub async fn send_transaction_message_route(
Some(id) => id, Some(id) => id,
None => { None => {
// Event is invalid // Event is invalid
resolved_map.insert(event_id, Err(Error::bad_database("Event needs a valid RoomId."))); resolved_map.insert(
event_id,
Err(Error::bad_database("Event needs a valid RoomId.")),
);
continue; continue;
} }
}; };
services().rooms.event_handler.acl_check(&sender_servername, &room_id)?; services()
.rooms
.event_handler
.acl_check(&sender_servername, &room_id)?;
let mutex = Arc::clone( let mutex = Arc::clone(
services().globals services()
.globals
.roomid_mutex_federation .roomid_mutex_federation
.write() .write()
.unwrap() .unwrap()
@ -683,7 +715,10 @@ pub async fn send_transaction_message_route(
let start_time = Instant::now(); let start_time = Instant::now();
resolved_map.insert( resolved_map.insert(
event_id.clone(), event_id.clone(),
services().rooms.event_handler.handle_incoming_pdu( services()
.rooms
.event_handler
.handle_incoming_pdu(
&sender_servername, &sender_servername,
&event_id, &event_id,
&room_id, &room_id,
@ -727,7 +762,13 @@ pub async fn send_transaction_message_route(
.event_ids .event_ids
.iter() .iter()
.filter_map(|id| { .filter_map(|id| {
services().rooms.timeline.get_pdu_count(id).ok().flatten().map(|r| (id, r)) services()
.rooms
.timeline
.get_pdu_count(id)
.ok()
.flatten()
.map(|r| (id, r))
}) })
.max_by_key(|(_, count)| *count) .max_by_key(|(_, count)| *count)
{ {
@ -744,11 +785,11 @@ pub async fn send_transaction_message_route(
content: ReceiptEventContent(receipt_content), content: ReceiptEventContent(receipt_content),
room_id: room_id.clone(), room_id: room_id.clone(),
}; };
services().rooms.edus.read_receipt.readreceipt_update( services()
&user_id, .rooms
&room_id, .edus
event, .read_receipt
)?; .readreceipt_update(&user_id, &room_id, event)?;
} else { } else {
// TODO fetch missing events // TODO fetch missing events
info!("No known event ids in read receipt: {:?}", user_updates); info!("No known event ids in read receipt: {:?}", user_updates);
@ -757,7 +798,11 @@ pub async fn send_transaction_message_route(
} }
} }
Edu::Typing(typing) => { Edu::Typing(typing) => {
if services().rooms.state_cache.is_joined(&typing.user_id, &typing.room_id)? { if services()
.rooms
.state_cache
.is_joined(&typing.user_id, &typing.room_id)?
{
if typing.typing { if typing.typing {
services().rooms.edus.typing.typing_add( services().rooms.edus.typing.typing_add(
&typing.user_id, &typing.user_id,
@ -765,16 +810,16 @@ pub async fn send_transaction_message_route(
3000 + utils::millis_since_unix_epoch(), 3000 + utils::millis_since_unix_epoch(),
)?; )?;
} else { } else {
services().rooms.edus.typing.typing_remove( services()
&typing.user_id, .rooms
&typing.room_id, .edus
)?; .typing
.typing_remove(&typing.user_id, &typing.room_id)?;
} }
} }
} }
Edu::DeviceListUpdate(DeviceListUpdateContent { user_id, .. }) => { Edu::DeviceListUpdate(DeviceListUpdateContent { user_id, .. }) => {
services().users services().users.mark_device_key_update(&user_id)?;
.mark_device_key_update(&user_id)?;
} }
Edu::DirectToDevice(DirectDeviceContent { Edu::DirectToDevice(DirectDeviceContent {
sender, sender,
@ -810,7 +855,9 @@ pub async fn send_transaction_message_route(
} }
DeviceIdOrAllDevices::AllDevices => { DeviceIdOrAllDevices::AllDevices => {
for target_device_id in services().users.all_device_ids(target_user_id) { for target_device_id in
services().users.all_device_ids(target_user_id)
{
services().users.add_to_device_event( services().users.add_to_device_event(
&sender, &sender,
target_user_id, target_user_id,
@ -830,7 +877,8 @@ pub async fn send_transaction_message_route(
} }
// Save transaction id with empty data // Save transaction id with empty data
services().transaction_ids services()
.transaction_ids
.add_txnid(&sender, None, &message_id, &[])?; .add_txnid(&sender, None, &message_id, &[])?;
} }
Edu::SigningKeyUpdate(SigningKeyUpdateContent { Edu::SigningKeyUpdate(SigningKeyUpdateContent {
@ -854,7 +902,12 @@ pub async fn send_transaction_message_route(
} }
} }
Ok(send_transaction_message::v1::Response { pdus: resolved_map.into_iter().map(|(e, r)| (e, r.map_err(|e| e.to_string()))).collect() }) Ok(send_transaction_message::v1::Response {
pdus: resolved_map
.into_iter()
.map(|(e, r)| (e, r.map_err(|e| e.to_string())))
.collect(),
})
} }
/// # `GET /_matrix/federation/v1/event/{eventId}` /// # `GET /_matrix/federation/v1/event/{eventId}`
@ -875,7 +928,8 @@ pub async fn get_event_route(
.expect("server is authenticated"); .expect("server is authenticated");
let event = services() let event = services()
.rooms.timeline .rooms
.timeline
.get_pdu_json(&body.event_id)? .get_pdu_json(&body.event_id)?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Event not found."))?; .ok_or(Error::BadRequest(ErrorKind::NotFound, "Event not found."))?;
@ -887,7 +941,11 @@ pub async fn get_event_route(
let room_id = <&RoomId>::try_from(room_id_str) let room_id = <&RoomId>::try_from(room_id_str)
.map_err(|_| Error::bad_database("Invalid room id field in event in database"))?; .map_err(|_| Error::bad_database("Invalid room id field in event in database"))?;
if !services().rooms.state_cache.server_in_room(sender_servername, room_id)? { if !services()
.rooms
.state_cache
.server_in_room(sender_servername, room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"Server is not in room", "Server is not in room",
@ -916,14 +974,21 @@ pub async fn get_missing_events_route(
.as_ref() .as_ref()
.expect("server is authenticated"); .expect("server is authenticated");
if !services().rooms.state_cache.server_in_room(sender_servername, &body.room_id)? { if !services()
.rooms
.state_cache
.server_in_room(sender_servername, &body.room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"Server is not in room", "Server is not in room",
)); ));
} }
services().rooms.event_handler.acl_check(&sender_servername, &body.room_id)?; services()
.rooms
.event_handler
.acl_check(&sender_servername, &body.room_id)?;
let mut queued_events = body.latest_events.clone(); let mut queued_events = body.latest_events.clone();
let mut events = Vec::new(); let mut events = Vec::new();
@ -988,17 +1053,25 @@ pub async fn get_event_authorization_route(
.as_ref() .as_ref()
.expect("server is authenticated"); .expect("server is authenticated");
if !services().rooms.state_cache.server_in_room(sender_servername, &body.room_id)? { if !services()
.rooms
.state_cache
.server_in_room(sender_servername, &body.room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"Server is not in room.", "Server is not in room.",
)); ));
} }
services().rooms.event_handler.acl_check(&sender_servername, &body.room_id)?; services()
.rooms
.event_handler
.acl_check(&sender_servername, &body.room_id)?;
let event = services() let event = services()
.rooms.timeline .rooms
.timeline
.get_pdu_json(&body.event_id)? .get_pdu_json(&body.event_id)?
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Event not found."))?; .ok_or(Error::BadRequest(ErrorKind::NotFound, "Event not found."))?;
@ -1010,7 +1083,11 @@ pub async fn get_event_authorization_route(
let room_id = <&RoomId>::try_from(room_id_str) let room_id = <&RoomId>::try_from(room_id_str)
.map_err(|_| Error::bad_database("Invalid room id field in event in database"))?; .map_err(|_| Error::bad_database("Invalid room id field in event in database"))?;
let auth_chain_ids = services().rooms.auth_chain.get_auth_chain(room_id, vec![Arc::from(&*body.event_id)]).await?; let auth_chain_ids = services()
.rooms
.auth_chain
.get_auth_chain(room_id, vec![Arc::from(&*body.event_id)])
.await?;
Ok(get_event_authorization::v1::Response { Ok(get_event_authorization::v1::Response {
auth_chain: auth_chain_ids auth_chain: auth_chain_ids
@ -1035,17 +1112,25 @@ pub async fn get_room_state_route(
.as_ref() .as_ref()
.expect("server is authenticated"); .expect("server is authenticated");
if !services().rooms.state_cache.server_in_room(sender_servername, &body.room_id)? { if !services()
.rooms
.state_cache
.server_in_room(sender_servername, &body.room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"Server is not in room.", "Server is not in room.",
)); ));
} }
services().rooms.event_handler.acl_check(&sender_servername, &body.room_id)?; services()
.rooms
.event_handler
.acl_check(&sender_servername, &body.room_id)?;
let shortstatehash = services() let shortstatehash = services()
.rooms.state_accessor .rooms
.state_accessor
.pdu_shortstatehash(&body.event_id)? .pdu_shortstatehash(&body.event_id)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
@ -1053,24 +1138,37 @@ pub async fn get_room_state_route(
))?; ))?;
let pdus = services() let pdus = services()
.rooms.state_accessor .rooms
.state_accessor
.state_full_ids(shortstatehash) .state_full_ids(shortstatehash)
.await? .await?
.into_iter() .into_iter()
.map(|(_, id)| { .map(|(_, id)| {
PduEvent::convert_to_outgoing_federation_event( PduEvent::convert_to_outgoing_federation_event(
services().rooms.timeline.get_pdu_json(&id).unwrap().unwrap(), services()
.rooms
.timeline
.get_pdu_json(&id)
.unwrap()
.unwrap(),
) )
}) })
.collect(); .collect();
let auth_chain_ids = let auth_chain_ids = services()
services().rooms.auth_chain.get_auth_chain(&body.room_id, vec![Arc::from(&*body.event_id)]).await?; .rooms
.auth_chain
.get_auth_chain(&body.room_id, vec![Arc::from(&*body.event_id)])
.await?;
Ok(get_room_state::v1::Response { Ok(get_room_state::v1::Response {
auth_chain: auth_chain_ids auth_chain: auth_chain_ids
.map(|id| { .map(|id| {
services().rooms.timeline.get_pdu_json(&id).map(|maybe_json| { services()
.rooms
.timeline
.get_pdu_json(&id)
.map(|maybe_json| {
PduEvent::convert_to_outgoing_federation_event(maybe_json.unwrap()) PduEvent::convert_to_outgoing_federation_event(maybe_json.unwrap())
}) })
}) })
@ -1095,17 +1193,25 @@ pub async fn get_room_state_ids_route(
.as_ref() .as_ref()
.expect("server is authenticated"); .expect("server is authenticated");
if !services().rooms.state_cache.server_in_room(sender_servername, &body.room_id)? { if !services()
.rooms
.state_cache
.server_in_room(sender_servername, &body.room_id)?
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"Server is not in room.", "Server is not in room.",
)); ));
} }
services().rooms.event_handler.acl_check(&sender_servername, &body.room_id)?; services()
.rooms
.event_handler
.acl_check(&sender_servername, &body.room_id)?;
let shortstatehash = services() let shortstatehash = services()
.rooms.state_accessor .rooms
.state_accessor
.pdu_shortstatehash(&body.event_id)? .pdu_shortstatehash(&body.event_id)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
@ -1113,15 +1219,19 @@ pub async fn get_room_state_ids_route(
))?; ))?;
let pdu_ids = services() let pdu_ids = services()
.rooms.state_accessor .rooms
.state_accessor
.state_full_ids(shortstatehash) .state_full_ids(shortstatehash)
.await? .await?
.into_iter() .into_iter()
.map(|(_, id)| (*id).to_owned()) .map(|(_, id)| (*id).to_owned())
.collect(); .collect();
let auth_chain_ids = let auth_chain_ids = services()
services().rooms.auth_chain.get_auth_chain(&body.room_id, vec![Arc::from(&*body.event_id)]).await?; .rooms
.auth_chain
.get_auth_chain(&body.room_id, vec![Arc::from(&*body.event_id)])
.await?;
Ok(get_room_state_ids::v1::Response { Ok(get_room_state_ids::v1::Response {
auth_chain_ids: auth_chain_ids.map(|id| (*id).to_owned()).collect(), auth_chain_ids: auth_chain_ids.map(|id| (*id).to_owned()).collect(),
@ -1151,10 +1261,14 @@ pub async fn create_join_event_template_route(
.as_ref() .as_ref()
.expect("server is authenticated"); .expect("server is authenticated");
services().rooms.event_handler.acl_check(&sender_servername, &body.room_id)?; services()
.rooms
.event_handler
.acl_check(&sender_servername, &body.room_id)?;
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -1164,9 +1278,11 @@ pub async fn create_join_event_template_route(
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
// TODO: Conduit does not implement restricted join rules yet, we always reject // TODO: Conduit does not implement restricted join rules yet, we always reject
let join_rules_event = let join_rules_event = services().rooms.state_accessor.room_state_get(
services().rooms.state_accessor &body.room_id,
.room_state_get(&body.room_id, &StateEventType::RoomJoinRules, "")?; &StateEventType::RoomJoinRules,
"",
)?;
let join_rules_event_content: Option<RoomJoinRulesEventContent> = join_rules_event let join_rules_event_content: Option<RoomJoinRulesEventContent> = join_rules_event
.as_ref() .as_ref()
@ -1212,13 +1328,18 @@ pub async fn create_join_event_template_route(
}) })
.expect("member event is valid value"); .expect("member event is valid value");
let (pdu, pdu_json) = services().rooms.timeline.create_hash_and_sign_event(PduBuilder { let (pdu, pdu_json) = services().rooms.timeline.create_hash_and_sign_event(
PduBuilder {
event_type: RoomEventType::RoomMember, event_type: RoomEventType::RoomMember,
content, content,
unsigned: None, unsigned: None,
state_key: Some(body.user_id.to_string()), state_key: Some(body.user_id.to_string()),
redacts: None, redacts: None,
}, &body.user_id, &body.room_id, &state_lock)?; },
&body.user_id,
&body.room_id,
&state_lock,
)?;
drop(state_lock); drop(state_lock);
@ -1244,12 +1365,17 @@ async fn create_join_event(
)); ));
} }
services().rooms.event_handler.acl_check(&sender_servername, room_id)?; services()
.rooms
.event_handler
.acl_check(&sender_servername, room_id)?;
// TODO: Conduit does not implement restricted join rules yet, we always reject // TODO: Conduit does not implement restricted join rules yet, we always reject
let join_rules_event = services() let join_rules_event = services().rooms.state_accessor.room_state_get(
.rooms.state_accessor room_id,
.room_state_get(room_id, &StateEventType::RoomJoinRules, "")?; &StateEventType::RoomJoinRules,
"",
)?;
let join_rules_event_content: Option<RoomJoinRulesEventContent> = join_rules_event let join_rules_event_content: Option<RoomJoinRulesEventContent> = join_rules_event
.as_ref() .as_ref()
@ -1275,7 +1401,8 @@ async fn create_join_event(
// We need to return the state prior to joining, let's keep a reference to that here // We need to return the state prior to joining, let's keep a reference to that here
let shortstatehash = services() let shortstatehash = services()
.rooms.state .rooms
.state
.get_room_shortstatehash(room_id)? .get_room_shortstatehash(room_id)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
@ -1307,7 +1434,8 @@ async fn create_join_event(
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Origin field is invalid."))?; .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Origin field is invalid."))?;
let mutex = Arc::clone( let mutex = Arc::clone(
services().globals services()
.globals
.roomid_mutex_federation .roomid_mutex_federation
.write() .write()
.unwrap() .unwrap()
@ -1315,7 +1443,10 @@ async fn create_join_event(
.or_default(), .or_default(),
); );
let mutex_lock = mutex.lock().await; let mutex_lock = mutex.lock().await;
let pdu_id: Vec<u8> = services().rooms.event_handler.handle_incoming_pdu(&origin, &event_id, room_id, value, true, &pub_key_map) let pdu_id: Vec<u8> = services()
.rooms
.event_handler
.handle_incoming_pdu(&origin, &event_id, room_id, value, true, &pub_key_map)
.await? .await?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::InvalidParam, ErrorKind::InvalidParam,
@ -1323,8 +1454,15 @@ async fn create_join_event(
))?; ))?;
drop(mutex_lock); drop(mutex_lock);
let state_ids = services().rooms.state_accessor.state_full_ids(shortstatehash).await?; let state_ids = services()
let auth_chain_ids = services().rooms.auth_chain.get_auth_chain( .rooms
.state_accessor
.state_full_ids(shortstatehash)
.await?;
let auth_chain_ids = services()
.rooms
.auth_chain
.get_auth_chain(
room_id, room_id,
state_ids.iter().map(|(_, id)| id.clone()).collect(), state_ids.iter().map(|(_, id)| id.clone()).collect(),
) )
@ -1399,9 +1537,16 @@ pub async fn create_invite_route(
.as_ref() .as_ref()
.expect("server is authenticated"); .expect("server is authenticated");
services().rooms.event_handler.acl_check(&sender_servername, &body.room_id)?; services()
.rooms
.event_handler
.acl_check(&sender_servername, &body.room_id)?;
if !services().globals.supported_room_versions().contains(&body.room_version) { if !services()
.globals
.supported_room_versions()
.contains(&body.room_version)
{
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::IncompatibleRoomVersion { ErrorKind::IncompatibleRoomVersion {
room_version: body.room_version.clone(), room_version: body.room_version.clone(),
@ -1549,7 +1694,8 @@ pub async fn get_room_information_route(
let room_id = services() let room_id = services()
.rooms .rooms
.alias.resolve_local_alias(&body.room_alias)? .alias
.resolve_local_alias(&body.room_alias)?
.ok_or(Error::BadRequest( .ok_or(Error::BadRequest(
ErrorKind::NotFound, ErrorKind::NotFound,
"Room alias not found.", "Room alias not found.",
@ -1576,7 +1722,9 @@ pub async fn get_profile_information_route(
let mut blurhash = None; let mut blurhash = None;
match &body.field { match &body.field {
Some(ProfileField::DisplayName) => displayname = services().users.displayname(&body.user_id)?, Some(ProfileField::DisplayName) => {
displayname = services().users.displayname(&body.user_id)?
}
Some(ProfileField::AvatarUrl) => { Some(ProfileField::AvatarUrl) => {
avatar_url = services().users.avatar_url(&body.user_id)?; avatar_url = services().users.avatar_url(&body.user_id)?;
blurhash = services().users.blurhash(&body.user_id)? blurhash = services().users.blurhash(&body.user_id)?
@ -1600,18 +1748,14 @@ pub async fn get_profile_information_route(
/// # `POST /_matrix/federation/v1/user/keys/query` /// # `POST /_matrix/federation/v1/user/keys/query`
/// ///
/// Gets devices and identity keys for the given users. /// Gets devices and identity keys for the given users.
pub async fn get_keys_route( pub async fn get_keys_route(body: Ruma<get_keys::v1::Request>) -> Result<get_keys::v1::Response> {
body: Ruma<get_keys::v1::Request>,
) -> Result<get_keys::v1::Response> {
if !services().globals.allow_federation() { if !services().globals.allow_federation() {
return Err(Error::bad_config("Federation is disabled.")); return Err(Error::bad_config("Federation is disabled."));
} }
let result = get_keys_helper( let result = get_keys_helper(None, &body.device_keys, |u| {
None, Some(u.server_name()) == body.sender_servername.as_deref()
&body.device_keys, })
|u| Some(u.server_name()) == body.sender_servername.as_deref(),
)
.await?; .await?;
Ok(get_keys::v1::Response { Ok(get_keys::v1::Response {

@ -1,4 +1,4 @@
use super::{super::Config, watchers::Watchers, KvTree, KeyValueDatabaseEngine}; use super::{super::Config, watchers::Watchers, KeyValueDatabaseEngine, KvTree};
use crate::{utils, Result}; use crate::{utils, Result};
use std::{ use std::{
future::Future, future::Future,

@ -1,9 +1,15 @@
use std::collections::HashMap; use std::collections::HashMap;
use ruma::{UserId, DeviceId, signatures::CanonicalJsonValue, api::client::{uiaa::UiaaInfo, error::ErrorKind}, events::{RoomAccountDataEventType, AnyEphemeralRoomEvent}, serde::Raw, RoomId}; use ruma::{
use serde::{Serialize, de::DeserializeOwned}; api::client::{error::ErrorKind, uiaa::UiaaInfo},
events::{AnyEphemeralRoomEvent, RoomAccountDataEventType},
use crate::{Result, database::KeyValueDatabase, service, Error, utils, services}; serde::Raw,
signatures::CanonicalJsonValue,
DeviceId, RoomId, UserId,
};
use serde::{de::DeserializeOwned, Serialize};
use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
impl service::account_data::Data for KeyValueDatabase { impl service::account_data::Data for KeyValueDatabase {
/// Places one event in the account data of the user and removes the previous entry. /// Places one event in the account data of the user and removes the previous entry.

@ -55,10 +55,13 @@ impl service::appservice::Data for KeyValueDatabase {
} }
fn iter_ids<'a>(&'a self) -> Result<Box<dyn Iterator<Item = Result<String>> + 'a>> { fn iter_ids<'a>(&'a self) -> Result<Box<dyn Iterator<Item = Result<String>> + 'a>> {
Ok(Box::new(self.id_appserviceregistrations.iter().map(|(id, _)| { Ok(Box::new(self.id_appserviceregistrations.iter().map(
utils::string_from_bytes(&id) |(id, _)| {
.map_err(|_| Error::bad_database("Invalid id bytes in id_appserviceregistrations.")) utils::string_from_bytes(&id).map_err(|_| {
}))) Error::bad_database("Invalid id bytes in id_appserviceregistrations.")
})
},
)))
} }
fn all(&self) -> Result<Vec<(String, serde_yaml::Value)>> { fn all(&self) -> Result<Vec<(String, serde_yaml::Value)>> {

@ -2,9 +2,13 @@ use std::collections::BTreeMap;
use async_trait::async_trait; use async_trait::async_trait;
use futures_util::{stream::FuturesUnordered, StreamExt}; use futures_util::{stream::FuturesUnordered, StreamExt};
use ruma::{signatures::Ed25519KeyPair, UserId, DeviceId, ServerName, api::federation::discovery::{ServerSigningKeys, VerifyKey}, ServerSigningKeyId, MilliSecondsSinceUnixEpoch}; use ruma::{
api::federation::discovery::{ServerSigningKeys, VerifyKey},
signatures::Ed25519KeyPair,
DeviceId, MilliSecondsSinceUnixEpoch, ServerName, ServerSigningKeyId, UserId,
};
use crate::{Result, service, database::KeyValueDatabase, Error, utils, services}; use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
pub const COUNTER: &[u8] = b"c"; pub const COUNTER: &[u8] = b"c";
@ -35,28 +39,24 @@ impl service::globals::Data for KeyValueDatabase {
// Return when *any* user changed his key // Return when *any* user changed his key
// TODO: only send for user they share a room with // TODO: only send for user they share a room with
futures.push( futures.push(self.todeviceid_events.watch_prefix(&userdeviceid_prefix));
self.todeviceid_events
.watch_prefix(&userdeviceid_prefix),
);
futures.push(self.userroomid_joined.watch_prefix(&userid_prefix)); futures.push(self.userroomid_joined.watch_prefix(&userid_prefix));
futures.push( futures.push(self.userroomid_invitestate.watch_prefix(&userid_prefix));
self.userroomid_invitestate
.watch_prefix(&userid_prefix),
);
futures.push(self.userroomid_leftstate.watch_prefix(&userid_prefix)); futures.push(self.userroomid_leftstate.watch_prefix(&userid_prefix));
futures.push( futures.push(
self.userroomid_notificationcount self.userroomid_notificationcount
.watch_prefix(&userid_prefix), .watch_prefix(&userid_prefix),
); );
futures.push( futures.push(self.userroomid_highlightcount.watch_prefix(&userid_prefix));
self.userroomid_highlightcount
.watch_prefix(&userid_prefix),
);
// Events for rooms we are in // Events for rooms we are in
for room_id in services().rooms.state_cache.rooms_joined(user_id).filter_map(|r| r.ok()) { for room_id in services()
.rooms
.state_cache
.rooms_joined(user_id)
.filter_map(|r| r.ok())
{
let short_roomid = services() let short_roomid = services()
.rooms .rooms
.short .short
@ -75,15 +75,9 @@ impl service::globals::Data for KeyValueDatabase {
futures.push(self.pduid_pdu.watch_prefix(&short_roomid)); futures.push(self.pduid_pdu.watch_prefix(&short_roomid));
// EDUs // EDUs
futures.push( futures.push(self.roomid_lasttypingupdate.watch_prefix(&roomid_bytes));
self.roomid_lasttypingupdate
.watch_prefix(&roomid_bytes),
);
futures.push( futures.push(self.readreceiptid_readreceipt.watch_prefix(&roomid_prefix));
self.readreceiptid_readreceipt
.watch_prefix(&roomid_prefix),
);
// Key changes // Key changes
futures.push(self.keychangeid_userid.watch_prefix(&roomid_prefix)); futures.push(self.keychangeid_userid.watch_prefix(&roomid_prefix));
@ -110,10 +104,7 @@ impl service::globals::Data for KeyValueDatabase {
futures.push(self.keychangeid_userid.watch_prefix(&userid_prefix)); futures.push(self.keychangeid_userid.watch_prefix(&userid_prefix));
// One time keys // One time keys
futures.push( futures.push(self.userid_lastonetimekeyupdate.watch_prefix(&userid_bytes));
self.userid_lastonetimekeyupdate
.watch_prefix(&userid_bytes),
);
futures.push(Box::pin(services().globals.rotate.watch())); futures.push(Box::pin(services().globals.rotate.watch()));
@ -238,10 +229,7 @@ impl service::globals::Data for KeyValueDatabase {
} }
fn bump_database_version(&self, new_version: u64) -> Result<()> { fn bump_database_version(&self, new_version: u64) -> Result<()> {
self.global self.global.insert(b"version", &new_version.to_be_bytes())?;
.insert(b"version", &new_version.to_be_bytes())?;
Ok(()) Ok(())
} }
} }

@ -1,8 +1,15 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use ruma::{UserId, serde::Raw, api::client::{backup::{BackupAlgorithm, KeyBackupData, RoomKeyBackup}, error::ErrorKind}, RoomId}; use ruma::{
api::client::{
backup::{BackupAlgorithm, KeyBackupData, RoomKeyBackup},
error::ErrorKind,
},
serde::Raw,
RoomId, UserId,
};
use crate::{Result, service, database::KeyValueDatabase, services, Error, utils}; use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
impl service::key_backups::Data for KeyValueDatabase { impl service::key_backups::Data for KeyValueDatabase {
fn create_backup( fn create_backup(
@ -118,11 +125,7 @@ impl service::key_backups::Data for KeyValueDatabase {
.transpose() .transpose()
} }
fn get_backup( fn get_backup(&self, user_id: &UserId, version: &str) -> Result<Option<Raw<BackupAlgorithm>>> {
&self,
user_id: &UserId,
version: &str,
) -> Result<Option<Raw<BackupAlgorithm>>> {
let mut key = user_id.as_bytes().to_vec(); let mut key = user_id.as_bytes().to_vec();
key.push(0xff); key.push(0xff);
key.extend_from_slice(version.as_bytes()); key.extend_from_slice(version.as_bytes());
@ -322,12 +325,7 @@ impl service::key_backups::Data for KeyValueDatabase {
Ok(()) Ok(())
} }
fn delete_room_keys( fn delete_room_keys(&self, user_id: &UserId, version: &str, room_id: &RoomId) -> Result<()> {
&self,
user_id: &UserId,
version: &str,
room_id: &RoomId,
) -> Result<()> {
let mut key = user_id.as_bytes().to_vec(); let mut key = user_id.as_bytes().to_vec();
key.push(0xff); key.push(0xff);
key.extend_from_slice(version.as_bytes()); key.extend_from_slice(version.as_bytes());

@ -1,9 +1,16 @@
use ruma::api::client::error::ErrorKind; use ruma::api::client::error::ErrorKind;
use crate::{database::KeyValueDatabase, service, Error, utils, Result}; use crate::{database::KeyValueDatabase, service, utils, Error, Result};
impl service::media::Data for KeyValueDatabase { impl service::media::Data for KeyValueDatabase {
fn create_file_metadata(&self, mxc: String, width: u32, height: u32, content_disposition: Option<&str>, content_type: Option<&str>) -> Result<Vec<u8>> { fn create_file_metadata(
&self,
mxc: String,
width: u32,
height: u32,
content_disposition: Option<&str>,
content_type: Option<&str>,
) -> Result<Vec<u8>> {
let mut key = mxc.as_bytes().to_vec(); let mut key = mxc.as_bytes().to_vec();
key.push(0xff); key.push(0xff);
key.extend_from_slice(&width.to_be_bytes()); key.extend_from_slice(&width.to_be_bytes());
@ -28,14 +35,23 @@ impl service::media::Data for KeyValueDatabase {
Ok(key) Ok(key)
} }
fn search_file_metadata(&self, mxc: String, width: u32, height: u32) -> Result<(Option<String>, Option<String>, Vec<u8>)> { fn search_file_metadata(
&self,
mxc: String,
width: u32,
height: u32,
) -> Result<(Option<String>, Option<String>, Vec<u8>)> {
let mut prefix = mxc.as_bytes().to_vec(); let mut prefix = mxc.as_bytes().to_vec();
prefix.push(0xff); prefix.push(0xff);
prefix.extend_from_slice(&0_u32.to_be_bytes()); // Width = 0 if it's not a thumbnail prefix.extend_from_slice(&0_u32.to_be_bytes()); // Width = 0 if it's not a thumbnail
prefix.extend_from_slice(&0_u32.to_be_bytes()); // Height = 0 if it's not a thumbnail prefix.extend_from_slice(&0_u32.to_be_bytes()); // Height = 0 if it's not a thumbnail
prefix.push(0xff); prefix.push(0xff);
let (key, _) = self.mediaid_file.scan_prefix(prefix).next().ok_or(Error::BadRequest(ErrorKind::NotFound, "Media not found"))?; let (key, _) = self
.mediaid_file
.scan_prefix(prefix)
.next()
.ok_or(Error::BadRequest(ErrorKind::NotFound, "Media not found"))?;
let mut parts = key.rsplit(|&b| b == 0xff); let mut parts = key.rsplit(|&b| b == 0xff);
@ -57,9 +73,7 @@ impl service::media::Data for KeyValueDatabase {
} else { } else {
Some( Some(
utils::string_from_bytes(content_disposition_bytes).map_err(|_| { utils::string_from_bytes(content_disposition_bytes).map_err(|_| {
Error::bad_database( Error::bad_database("Content Disposition in mediaid_file is invalid unicode.")
"Content Disposition in mediaid_file is invalid unicode.",
)
})?, })?,
) )
}; };

@ -1,6 +1,9 @@
use ruma::{UserId, api::client::push::{set_pusher, get_pushers}}; use ruma::{
api::client::push::{get_pushers, set_pusher},
UserId,
};
use crate::{service, database::KeyValueDatabase, Error, Result}; use crate::{database::KeyValueDatabase, service, Error, Result};
impl service::pusher::Data for KeyValueDatabase { impl service::pusher::Data for KeyValueDatabase {
fn set_pusher(&self, sender: &UserId, pusher: set_pusher::v3::Pusher) -> Result<()> { fn set_pusher(&self, sender: &UserId, pusher: set_pusher::v3::Pusher) -> Result<()> {
@ -48,10 +51,7 @@ impl service::pusher::Data for KeyValueDatabase {
.collect() .collect()
} }
fn get_pusher_senderkeys<'a>( fn get_pusher_senderkeys<'a>(&'a self, sender: &UserId) -> Box<dyn Iterator<Item = Vec<u8>>> {
&'a self,
sender: &UserId,
) -> Box<dyn Iterator<Item = Vec<u8>>> {
let mut prefix = sender.as_bytes().to_vec(); let mut prefix = sender.as_bytes().to_vec();
prefix.push(0xff); prefix.push(0xff);

@ -1,13 +1,9 @@
use ruma::{RoomId, RoomAliasId, api::client::error::ErrorKind}; use ruma::{api::client::error::ErrorKind, RoomAliasId, RoomId};
use crate::{service, database::KeyValueDatabase, utils, Error, services, Result}; use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
impl service::rooms::alias::Data for KeyValueDatabase { impl service::rooms::alias::Data for KeyValueDatabase {
fn set_alias( fn set_alias(&self, alias: &RoomAliasId, room_id: &RoomId) -> Result<()> {
&self,
alias: &RoomAliasId,
room_id: &RoomId
) -> Result<()> {
self.alias_roomid self.alias_roomid
.insert(alias.alias().as_bytes(), room_id.as_bytes())?; .insert(alias.alias().as_bytes(), room_id.as_bytes())?;
let mut aliasid = room_id.as_bytes().to_vec(); let mut aliasid = room_id.as_bytes().to_vec();
@ -17,10 +13,7 @@ impl service::rooms::alias::Data for KeyValueDatabase {
Ok(()) Ok(())
} }
fn remove_alias( fn remove_alias(&self, alias: &RoomAliasId) -> Result<()> {
&self,
alias: &RoomAliasId,
) -> Result<()> {
if let Some(room_id) = self.alias_roomid.get(alias.alias().as_bytes())? { if let Some(room_id) = self.alias_roomid.get(alias.alias().as_bytes())? {
let mut prefix = room_id.to_vec(); let mut prefix = room_id.to_vec();
prefix.push(0xff); prefix.push(0xff);
@ -38,10 +31,7 @@ impl service::rooms::alias::Data for KeyValueDatabase {
Ok(()) Ok(())
} }
fn resolve_local_alias( fn resolve_local_alias(&self, alias: &RoomAliasId) -> Result<Option<Box<RoomId>>> {
&self,
alias: &RoomAliasId
) -> Result<Option<Box<RoomId>>> {
self.alias_roomid self.alias_roomid
.get(alias.alias().as_bytes())? .get(alias.alias().as_bytes())?
.map(|bytes| { .map(|bytes| {

@ -1,6 +1,6 @@
use std::{collections::HashSet, mem::size_of, sync::Arc}; use std::{collections::HashSet, mem::size_of, sync::Arc};
use crate::{service, database::KeyValueDatabase, Result, utils}; use crate::{database::KeyValueDatabase, service, utils, Result};
impl service::rooms::auth_chain::Data for KeyValueDatabase { impl service::rooms::auth_chain::Data for KeyValueDatabase {
fn get_cached_eventid_authchain(&self, key: &[u64]) -> Result<Option<Arc<HashSet<u64>>>> { fn get_cached_eventid_authchain(&self, key: &[u64]) -> Result<Option<Arc<HashSet<u64>>>> {
@ -12,14 +12,13 @@ impl service::rooms::auth_chain::Data for KeyValueDatabase {
// We only save auth chains for single events in the db // We only save auth chains for single events in the db
if key.len() == 1 { if key.len() == 1 {
// Check DB cache // Check DB cache
let chain = self.shorteventid_authchain let chain = self
.shorteventid_authchain
.get(&key[0].to_be_bytes())? .get(&key[0].to_be_bytes())?
.map(|chain| { .map(|chain| {
chain chain
.chunks_exact(size_of::<u64>()) .chunks_exact(size_of::<u64>())
.map(|chunk| { .map(|chunk| utils::u64_from_bytes(chunk).expect("byte length is correct"))
utils::u64_from_bytes(chunk).expect("byte length is correct")
})
.collect() .collect()
}); });
@ -37,7 +36,6 @@ impl service::rooms::auth_chain::Data for KeyValueDatabase {
} }
Ok(None) Ok(None)
} }
fn cache_auth_chain(&self, key: Vec<u64>, auth_chain: Arc<HashSet<u64>>) -> Result<()> { fn cache_auth_chain(&self, key: Vec<u64>, auth_chain: Arc<HashSet<u64>>) -> Result<()> {
@ -53,7 +51,10 @@ impl service::rooms::auth_chain::Data for KeyValueDatabase {
} }
// Cache in RAM // Cache in RAM
self.auth_chain_cache.lock().unwrap().insert(key, auth_chain); self.auth_chain_cache
.lock()
.unwrap()
.insert(key, auth_chain);
Ok(()) Ok(())
} }

@ -1,6 +1,6 @@
use ruma::RoomId; use ruma::RoomId;
use crate::{service, database::KeyValueDatabase, utils, Error, Result}; use crate::{database::KeyValueDatabase, service, utils, Error, Result};
impl service::rooms::directory::Data for KeyValueDatabase { impl service::rooms::directory::Data for KeyValueDatabase {
fn set_public(&self, room_id: &RoomId) -> Result<()> { fn set_public(&self, room_id: &RoomId) -> Result<()> {

@ -1,7 +1,7 @@
mod presence; mod presence;
mod typing;
mod read_receipt; mod read_receipt;
mod typing;
use crate::{service, database::KeyValueDatabase}; use crate::{database::KeyValueDatabase, service};
impl service::rooms::edus::Data for KeyValueDatabase {} impl service::rooms::edus::Data for KeyValueDatabase {}

@ -1,8 +1,8 @@
use std::collections::HashMap; use std::collections::HashMap;
use ruma::{UserId, RoomId, events::presence::PresenceEvent, presence::PresenceState, UInt}; use ruma::{events::presence::PresenceEvent, presence::PresenceState, RoomId, UInt, UserId};
use crate::{service, database::KeyValueDatabase, utils, Error, services, Result}; use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
impl service::rooms::edus::presence::Data for KeyValueDatabase { impl service::rooms::edus::presence::Data for KeyValueDatabase {
fn update_presence( fn update_presence(

@ -1,8 +1,10 @@
use std::mem; use std::mem;
use ruma::{UserId, RoomId, events::receipt::ReceiptEvent, serde::Raw, signatures::CanonicalJsonObject}; use ruma::{
events::receipt::ReceiptEvent, serde::Raw, signatures::CanonicalJsonObject, RoomId, UserId,
};
use crate::{database::KeyValueDatabase, service, utils, Error, services, Result}; use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
impl service::rooms::edus::read_receipt::Data for KeyValueDatabase { impl service::rooms::edus::read_receipt::Data for KeyValueDatabase {
fn readreceipt_update( fn readreceipt_update(
@ -50,13 +52,15 @@ impl service::rooms::edus::read_receipt::Data for KeyValueDatabase {
&'a self, &'a self,
room_id: &RoomId, room_id: &RoomId,
since: u64, since: u64,
) -> Box<dyn Iterator< ) -> Box<
Item=Result<( dyn Iterator<
Item = Result<(
Box<UserId>, Box<UserId>,
u64, u64,
Raw<ruma::events::AnySyncEphemeralRoomEvent>, Raw<ruma::events::AnySyncEphemeralRoomEvent>,
)>, )>,
>> { >,
> {
let mut prefix = room_id.as_bytes().to_vec(); let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff); prefix.push(0xff);
let prefix2 = prefix.clone(); let prefix2 = prefix.clone();
@ -64,12 +68,14 @@ impl service::rooms::edus::read_receipt::Data for KeyValueDatabase {
let mut first_possible_edu = prefix.clone(); let mut first_possible_edu = prefix.clone();
first_possible_edu.extend_from_slice(&(since + 1).to_be_bytes()); // +1 so we don't send the event at since first_possible_edu.extend_from_slice(&(since + 1).to_be_bytes()); // +1 so we don't send the event at since
Box::new(self.readreceiptid_readreceipt Box::new(
self.readreceiptid_readreceipt
.iter_from(&first_possible_edu, false) .iter_from(&first_possible_edu, false)
.take_while(move |(k, _)| k.starts_with(&prefix2)) .take_while(move |(k, _)| k.starts_with(&prefix2))
.map(move |(k, v)| { .map(move |(k, v)| {
let count = let count = utils::u64_from_bytes(
utils::u64_from_bytes(&k[prefix.len()..prefix.len() + mem::size_of::<u64>()]) &k[prefix.len()..prefix.len() + mem::size_of::<u64>()],
)
.map_err(|_| Error::bad_database("Invalid readreceiptid count in db."))?; .map_err(|_| Error::bad_database("Invalid readreceiptid count in db."))?;
let user_id = UserId::parse( let user_id = UserId::parse(
utils::string_from_bytes(&k[prefix.len() + mem::size_of::<u64>() + 1..]) utils::string_from_bytes(&k[prefix.len() + mem::size_of::<u64>() + 1..])
@ -79,8 +85,11 @@ impl service::rooms::edus::read_receipt::Data for KeyValueDatabase {
) )
.map_err(|_| Error::bad_database("Invalid readreceiptid userid in db."))?; .map_err(|_| Error::bad_database("Invalid readreceiptid userid in db."))?;
let mut json = serde_json::from_slice::<CanonicalJsonObject>(&v).map_err(|_| { let mut json =
Error::bad_database("Read receipt in roomlatestid_roomlatest is invalid json.") serde_json::from_slice::<CanonicalJsonObject>(&v).map_err(|_| {
Error::bad_database(
"Read receipt in roomlatestid_roomlatest is invalid json.",
)
})?; })?;
json.remove("room_id"); json.remove("room_id");
@ -88,18 +97,15 @@ impl service::rooms::edus::read_receipt::Data for KeyValueDatabase {
user_id, user_id,
count, count,
Raw::from_json( Raw::from_json(
serde_json::value::to_raw_value(&json).expect("json is valid raw value"), serde_json::value::to_raw_value(&json)
.expect("json is valid raw value"),
), ),
)) ))
})) }),
)
} }
fn private_read_set( fn private_read_set(&self, room_id: &RoomId, user_id: &UserId, count: u64) -> Result<()> {
&self,
room_id: &RoomId,
user_id: &UserId,
count: u64,
) -> Result<()> {
let mut key = room_id.as_bytes().to_vec(); let mut key = room_id.as_bytes().to_vec();
key.push(0xff); key.push(0xff);
key.extend_from_slice(user_id.as_bytes()); key.extend_from_slice(user_id.as_bytes());

@ -1,16 +1,11 @@
use std::collections::HashSet; use std::collections::HashSet;
use ruma::{UserId, RoomId}; use ruma::{RoomId, UserId};
use crate::{database::KeyValueDatabase, service, utils, Error, services, Result}; use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
impl service::rooms::edus::typing::Data for KeyValueDatabase { impl service::rooms::edus::typing::Data for KeyValueDatabase {
fn typing_add( fn typing_add(&self, user_id: &UserId, room_id: &RoomId, timeout: u64) -> Result<()> {
&self,
user_id: &UserId,
room_id: &RoomId,
timeout: u64,
) -> Result<()> {
let mut prefix = room_id.as_bytes().to_vec(); let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff); prefix.push(0xff);
@ -30,11 +25,7 @@ impl service::rooms::edus::typing::Data for KeyValueDatabase {
Ok(()) Ok(())
} }
fn typing_remove( fn typing_remove(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
&self,
user_id: &UserId,
room_id: &RoomId,
) -> Result<()> {
let mut prefix = room_id.as_bytes().to_vec(); let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff); prefix.push(0xff);
@ -53,17 +44,16 @@ impl service::rooms::edus::typing::Data for KeyValueDatabase {
} }
if found_outdated { if found_outdated {
self.roomid_lasttypingupdate self.roomid_lasttypingupdate.insert(
.insert(room_id.as_bytes(), &services().globals.next_count()?.to_be_bytes())?; room_id.as_bytes(),
&services().globals.next_count()?.to_be_bytes(),
)?;
} }
Ok(()) Ok(())
} }
fn last_typing_update( fn last_typing_update(&self, room_id: &RoomId) -> Result<u64> {
&self,
room_id: &RoomId,
) -> Result<u64> {
Ok(self Ok(self
.roomid_lasttypingupdate .roomid_lasttypingupdate
.get(room_id.as_bytes())? .get(room_id.as_bytes())?
@ -76,10 +66,7 @@ impl service::rooms::edus::typing::Data for KeyValueDatabase {
.unwrap_or(0)) .unwrap_or(0))
} }
fn typings_all( fn typings_all(&self, room_id: &RoomId) -> Result<HashSet<Box<UserId>>> {
&self,
room_id: &RoomId,
) -> Result<HashSet<Box<UserId>>> {
let mut prefix = room_id.as_bytes().to_vec(); let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff); prefix.push(0xff);

@ -1,6 +1,6 @@
use ruma::{UserId, DeviceId, RoomId}; use ruma::{DeviceId, RoomId, UserId};
use crate::{service, database::KeyValueDatabase, Result}; use crate::{database::KeyValueDatabase, service, Result};
impl service::rooms::lazy_loading::Data for KeyValueDatabase { impl service::rooms::lazy_loading::Data for KeyValueDatabase {
fn lazy_load_was_sent_before( fn lazy_load_was_sent_before(

@ -1,6 +1,6 @@
use ruma::RoomId; use ruma::RoomId;
use crate::{service, database::KeyValueDatabase, Result, services}; use crate::{database::KeyValueDatabase, service, services, Result};
impl service::rooms::metadata::Data for KeyValueDatabase { impl service::rooms::metadata::Data for KeyValueDatabase {
fn exists(&self, room_id: &RoomId) -> Result<bool> { fn exists(&self, room_id: &RoomId) -> Result<bool> {

@ -1,6 +1,6 @@
use ruma::{EventId, signatures::CanonicalJsonObject}; use ruma::{signatures::CanonicalJsonObject, EventId};
use crate::{service, database::KeyValueDatabase, PduEvent, Error, Result}; use crate::{database::KeyValueDatabase, service, Error, PduEvent, Result};
impl service::rooms::outlier::Data for KeyValueDatabase { impl service::rooms::outlier::Data for KeyValueDatabase {
fn get_outlier_pdu_json(&self, event_id: &EventId) -> Result<Option<CanonicalJsonObject>> { fn get_outlier_pdu_json(&self, event_id: &EventId) -> Result<Option<CanonicalJsonObject>> {

@ -1,8 +1,8 @@
use std::sync::Arc; use std::sync::Arc;
use ruma::{RoomId, EventId}; use ruma::{EventId, RoomId};
use crate::{service, database::KeyValueDatabase, Result}; use crate::{database::KeyValueDatabase, service, Result};
impl service::rooms::pdu_metadata::Data for KeyValueDatabase { impl service::rooms::pdu_metadata::Data for KeyValueDatabase {
fn mark_as_referenced(&self, room_id: &RoomId, event_ids: &[Arc<EventId>]) -> Result<()> { fn mark_as_referenced(&self, room_id: &RoomId, event_ids: &[Arc<EventId>]) -> Result<()> {

@ -2,7 +2,7 @@ use std::mem::size_of;
use ruma::RoomId; use ruma::RoomId;
use crate::{service, database::KeyValueDatabase, utils, Result, services}; use crate::{database::KeyValueDatabase, service, services, utils, Result};
impl service::rooms::search::Data for KeyValueDatabase { impl service::rooms::search::Data for KeyValueDatabase {
fn index_pdu<'a>(&self, shortroomid: u64, pdu_id: &[u8], message_body: String) -> Result<()> { fn index_pdu<'a>(&self, shortroomid: u64, pdu_id: &[u8], message_body: String) -> Result<()> {
@ -27,7 +27,9 @@ impl service::rooms::search::Data for KeyValueDatabase {
room_id: &RoomId, room_id: &RoomId,
search_string: &str, search_string: &str,
) -> Result<Option<(Box<dyn Iterator<Item = Vec<u8>>>, Vec<String>)>> { ) -> Result<Option<(Box<dyn Iterator<Item = Vec<u8>>>, Vec<String>)>> {
let prefix = services().rooms.short let prefix = services()
.rooms
.short
.get_shortroomid(room_id)? .get_shortroomid(room_id)?
.expect("room exists") .expect("room exists")
.to_be_bytes() .to_be_bytes()

@ -1,14 +1,11 @@
use std::sync::Arc; use std::sync::Arc;
use ruma::{EventId, events::StateEventType, RoomId}; use ruma::{events::StateEventType, EventId, RoomId};
use crate::{Result, database::KeyValueDatabase, service, utils, Error, services}; use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
impl service::rooms::short::Data for KeyValueDatabase { impl service::rooms::short::Data for KeyValueDatabase {
fn get_or_create_shorteventid( fn get_or_create_shorteventid(&self, event_id: &EventId) -> Result<u64> {
&self,
event_id: &EventId,
) -> Result<u64> {
if let Some(short) = self.eventidshort_cache.lock().unwrap().get_mut(event_id) { if let Some(short) = self.eventidshort_cache.lock().unwrap().get_mut(event_id) {
return Ok(*short); return Ok(*short);
} }
@ -180,10 +177,7 @@ impl service::rooms::short::Data for KeyValueDatabase {
} }
/// Returns (shortstatehash, already_existed) /// Returns (shortstatehash, already_existed)
fn get_or_create_shortstatehash( fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> Result<(u64, bool)> {
&self,
state_hash: &[u8],
) -> Result<(u64, bool)> {
Ok(match self.statehash_shortstatehash.get(state_hash)? { Ok(match self.statehash_shortstatehash.get(state_hash)? {
Some(shortstatehash) => ( Some(shortstatehash) => (
utils::u64_from_bytes(&shortstatehash) utils::u64_from_bytes(&shortstatehash)
@ -209,10 +203,7 @@ impl service::rooms::short::Data for KeyValueDatabase {
.transpose() .transpose()
} }
fn get_or_create_shortroomid( fn get_or_create_shortroomid(&self, room_id: &RoomId) -> Result<u64> {
&self,
room_id: &RoomId,
) -> Result<u64> {
Ok(match self.roomid_shortroomid.get(room_id.as_bytes())? { Ok(match self.roomid_shortroomid.get(room_id.as_bytes())? {
Some(short) => utils::u64_from_bytes(&short) Some(short) => utils::u64_from_bytes(&short)
.map_err(|_| Error::bad_database("Invalid shortroomid in db."))?, .map_err(|_| Error::bad_database("Invalid shortroomid in db."))?,

@ -1,10 +1,10 @@
use ruma::{RoomId, EventId}; use ruma::{EventId, RoomId};
use tokio::sync::MutexGuard;
use std::sync::Arc;
use std::collections::HashSet; use std::collections::HashSet;
use std::fmt::Debug; use std::fmt::Debug;
use std::sync::Arc;
use tokio::sync::MutexGuard;
use crate::{service, database::KeyValueDatabase, utils, Error, Result}; use crate::{database::KeyValueDatabase, service, utils, Error, Result};
impl service::rooms::state::Data for KeyValueDatabase { impl service::rooms::state::Data for KeyValueDatabase {
fn get_room_shortstatehash(&self, room_id: &RoomId) -> Result<Option<u64>> { fn get_room_shortstatehash(&self, room_id: &RoomId) -> Result<Option<u64>> {
@ -17,7 +17,10 @@ impl service::rooms::state::Data for KeyValueDatabase {
}) })
} }
fn set_room_state(&self, room_id: &RoomId, new_shortstatehash: u64, fn set_room_state(
&self,
room_id: &RoomId,
new_shortstatehash: u64,
_mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room state mutex _mutex_lock: &MutexGuard<'_, ()>, // Take mutex guard to make sure users get the room state mutex
) -> Result<()> { ) -> Result<()> {
self.roomid_shortstatehash self.roomid_shortstatehash

@ -1,13 +1,18 @@
use std::{collections::{BTreeMap, HashMap}, sync::Arc}; use std::{
collections::{BTreeMap, HashMap},
sync::Arc,
};
use crate::{database::KeyValueDatabase, service, PduEvent, Error, utils, Result, services}; use crate::{database::KeyValueDatabase, service, services, utils, Error, PduEvent, Result};
use async_trait::async_trait; use async_trait::async_trait;
use ruma::{EventId, events::StateEventType, RoomId}; use ruma::{events::StateEventType, EventId, RoomId};
#[async_trait] #[async_trait]
impl service::rooms::state_accessor::Data for KeyValueDatabase { impl service::rooms::state_accessor::Data for KeyValueDatabase {
async fn state_full_ids(&self, shortstatehash: u64) -> Result<BTreeMap<u64, Arc<EventId>>> { async fn state_full_ids(&self, shortstatehash: u64) -> Result<BTreeMap<u64, Arc<EventId>>> {
let full_state = services().rooms.state_compressor let full_state = services()
.rooms
.state_compressor
.load_shortstatehash_info(shortstatehash)? .load_shortstatehash_info(shortstatehash)?
.pop() .pop()
.expect("there is always one layer") .expect("there is always one layer")
@ -15,7 +20,10 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
let mut result = BTreeMap::new(); let mut result = BTreeMap::new();
let mut i = 0; let mut i = 0;
for compressed in full_state.into_iter() { for compressed in full_state.into_iter() {
let parsed = services().rooms.state_compressor.parse_compressed_state_event(compressed)?; let parsed = services()
.rooms
.state_compressor
.parse_compressed_state_event(compressed)?;
result.insert(parsed.0, parsed.1); result.insert(parsed.0, parsed.1);
i += 1; i += 1;
@ -30,7 +38,9 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
&self, &self,
shortstatehash: u64, shortstatehash: u64,
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> { ) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
let full_state = services().rooms.state_compressor let full_state = services()
.rooms
.state_compressor
.load_shortstatehash_info(shortstatehash)? .load_shortstatehash_info(shortstatehash)?
.pop() .pop()
.expect("there is always one layer") .expect("there is always one layer")
@ -39,7 +49,10 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
let mut result = HashMap::new(); let mut result = HashMap::new();
let mut i = 0; let mut i = 0;
for compressed in full_state { for compressed in full_state {
let (_, eventid) = services().rooms.state_compressor.parse_compressed_state_event(compressed)?; let (_, eventid) = services()
.rooms
.state_compressor
.parse_compressed_state_event(compressed)?;
if let Some(pdu) = services().rooms.timeline.get_pdu(&eventid)? { if let Some(pdu) = services().rooms.timeline.get_pdu(&eventid)? {
result.insert( result.insert(
( (
@ -69,11 +82,17 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<Option<Arc<EventId>>> { ) -> Result<Option<Arc<EventId>>> {
let shortstatekey = match services().rooms.short.get_shortstatekey(event_type, state_key)? { let shortstatekey = match services()
.rooms
.short
.get_shortstatekey(event_type, state_key)?
{
Some(s) => s, Some(s) => s,
None => return Ok(None), None => return Ok(None),
}; };
let full_state = services().rooms.state_compressor let full_state = services()
.rooms
.state_compressor
.load_shortstatehash_info(shortstatehash)? .load_shortstatehash_info(shortstatehash)?
.pop() .pop()
.expect("there is always one layer") .expect("there is always one layer")
@ -82,7 +101,10 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
.into_iter() .into_iter()
.find(|bytes| bytes.starts_with(&shortstatekey.to_be_bytes())) .find(|bytes| bytes.starts_with(&shortstatekey.to_be_bytes()))
.and_then(|compressed| { .and_then(|compressed| {
services().rooms.state_compressor.parse_compressed_state_event(compressed) services()
.rooms
.state_compressor
.parse_compressed_state_event(compressed)
.ok() .ok()
.map(|(_, id)| id) .map(|(_, id)| id)
})) }))
@ -96,7 +118,9 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
state_key: &str, state_key: &str,
) -> Result<Option<Arc<PduEvent>>> { ) -> Result<Option<Arc<PduEvent>>> {
self.state_get_id(shortstatehash, event_type, state_key)? self.state_get_id(shortstatehash, event_type, state_key)?
.map_or(Ok(None), |event_id| services().rooms.timeline.get_pdu(&event_id)) .map_or(Ok(None), |event_id| {
services().rooms.timeline.get_pdu(&event_id)
})
} }
/// Returns the state hash for this pdu. /// Returns the state hash for this pdu.
@ -122,7 +146,9 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
&self, &self,
room_id: &RoomId, room_id: &RoomId,
) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> { ) -> Result<HashMap<(StateEventType, String), Arc<PduEvent>>> {
if let Some(current_shortstatehash) = services().rooms.state.get_room_shortstatehash(room_id)? { if let Some(current_shortstatehash) =
services().rooms.state.get_room_shortstatehash(room_id)?
{
self.state_full(current_shortstatehash).await self.state_full(current_shortstatehash).await
} else { } else {
Ok(HashMap::new()) Ok(HashMap::new())
@ -136,7 +162,9 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<Option<Arc<EventId>>> { ) -> Result<Option<Arc<EventId>>> {
if let Some(current_shortstatehash) = services().rooms.state.get_room_shortstatehash(room_id)? { if let Some(current_shortstatehash) =
services().rooms.state.get_room_shortstatehash(room_id)?
{
self.state_get_id(current_shortstatehash, event_type, state_key) self.state_get_id(current_shortstatehash, event_type, state_key)
} else { } else {
Ok(None) Ok(None)
@ -150,7 +178,9 @@ impl service::rooms::state_accessor::Data for KeyValueDatabase {
event_type: &StateEventType, event_type: &StateEventType,
state_key: &str, state_key: &str,
) -> Result<Option<Arc<PduEvent>>> { ) -> Result<Option<Arc<PduEvent>>> {
if let Some(current_shortstatehash) = services().rooms.state.get_room_shortstatehash(room_id)? { if let Some(current_shortstatehash) =
services().rooms.state.get_room_shortstatehash(room_id)?
{
self.state_get(current_shortstatehash, event_type, state_key) self.state_get(current_shortstatehash, event_type, state_key)
} else { } else {
Ok(None) Ok(None)

@ -1,9 +1,13 @@
use std::{collections::HashSet, sync::Arc}; use std::{collections::HashSet, sync::Arc};
use regex::Regex; use regex::Regex;
use ruma::{UserId, RoomId, events::{AnyStrippedStateEvent, AnySyncStateEvent}, serde::Raw, ServerName}; use ruma::{
events::{AnyStrippedStateEvent, AnySyncStateEvent},
serde::Raw,
RoomId, ServerName, UserId,
};
use crate::{service, database::KeyValueDatabase, services, Result, Error, utils}; use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
impl service::rooms::state_cache::Data for KeyValueDatabase { impl service::rooms::state_cache::Data for KeyValueDatabase {
fn mark_as_once_joined(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> { fn mark_as_once_joined(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
@ -32,7 +36,12 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
Ok(()) Ok(())
} }
fn mark_as_invited(&self, user_id: &UserId, room_id: &RoomId, last_state: Option<Vec<Raw<AnyStrippedStateEvent>>>) -> Result<()> { fn mark_as_invited(
&self,
user_id: &UserId,
room_id: &RoomId,
last_state: Option<Vec<Raw<AnyStrippedStateEvent>>>,
) -> Result<()> {
let mut roomuser_id = room_id.as_bytes().to_vec(); let mut roomuser_id = room_id.as_bytes().to_vec();
roomuser_id.push(0xff); roomuser_id.push(0xff);
roomuser_id.extend_from_slice(user_id.as_bytes()); roomuser_id.extend_from_slice(user_id.as_bytes());
@ -46,8 +55,10 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
&serde_json::to_vec(&last_state.unwrap_or_default()) &serde_json::to_vec(&last_state.unwrap_or_default())
.expect("state to bytes always works"), .expect("state to bytes always works"),
)?; )?;
self.roomuserid_invitecount self.roomuserid_invitecount.insert(
.insert(&roomuser_id, &services().globals.next_count()?.to_be_bytes())?; &roomuser_id,
&services().globals.next_count()?.to_be_bytes(),
)?;
self.userroomid_joined.remove(&userroom_id)?; self.userroomid_joined.remove(&userroom_id)?;
self.roomuserid_joined.remove(&roomuser_id)?; self.roomuserid_joined.remove(&roomuser_id)?;
self.userroomid_leftstate.remove(&userroom_id)?; self.userroomid_leftstate.remove(&userroom_id)?;
@ -69,8 +80,10 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
&userroom_id, &userroom_id,
&serde_json::to_vec(&Vec::<Raw<AnySyncStateEvent>>::new()).unwrap(), &serde_json::to_vec(&Vec::<Raw<AnySyncStateEvent>>::new()).unwrap(),
)?; // TODO )?; // TODO
self.roomuserid_leftcount self.roomuserid_leftcount.insert(
.insert(&roomuser_id, &services().globals.next_count()?.to_be_bytes())?; &roomuser_id,
&services().globals.next_count()?.to_be_bytes(),
)?;
self.userroomid_joined.remove(&userroom_id)?; self.userroomid_joined.remove(&userroom_id)?;
self.roomuserid_joined.remove(&roomuser_id)?; self.roomuserid_joined.remove(&roomuser_id)?;
self.userroomid_invitestate.remove(&userroom_id)?; self.userroomid_invitestate.remove(&userroom_id)?;
@ -324,7 +337,8 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
let mut prefix = room_id.as_bytes().to_vec(); let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff); prefix.push(0xff);
Box::new(self.roomuseroncejoinedids Box::new(
self.roomuseroncejoinedids
.scan_prefix(prefix) .scan_prefix(prefix)
.map(|(key, _)| { .map(|(key, _)| {
UserId::parse( UserId::parse(
@ -334,11 +348,14 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
.expect("rsplit always returns an element"), .expect("rsplit always returns an element"),
) )
.map_err(|_| { .map_err(|_| {
Error::bad_database("User ID in room_useroncejoined is invalid unicode.") Error::bad_database(
"User ID in room_useroncejoined is invalid unicode.",
)
})?, })?,
) )
.map_err(|_| Error::bad_database("User ID in room_useroncejoined is invalid.")) .map_err(|_| Error::bad_database("User ID in room_useroncejoined is invalid."))
})) }),
)
} }
/// Returns an iterator over all invited members of a room. /// Returns an iterator over all invited members of a room.
@ -350,7 +367,8 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
let mut prefix = room_id.as_bytes().to_vec(); let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff); prefix.push(0xff);
Box::new(self.roomuserid_invitecount Box::new(
self.roomuserid_invitecount
.scan_prefix(prefix) .scan_prefix(prefix)
.map(|(key, _)| { .map(|(key, _)| {
UserId::parse( UserId::parse(
@ -364,7 +382,8 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
})?, })?,
) )
.map_err(|_| Error::bad_database("User ID in roomuserid_invited is invalid.")) .map_err(|_| Error::bad_database("User ID in roomuserid_invited is invalid."))
})) }),
)
} }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
@ -403,7 +422,8 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
&'a self, &'a self,
user_id: &UserId, user_id: &UserId,
) -> Box<dyn Iterator<Item = Result<Box<RoomId>>> + 'a> { ) -> Box<dyn Iterator<Item = Result<Box<RoomId>>> + 'a> {
Box::new(self.userroomid_joined Box::new(
self.userroomid_joined
.scan_prefix(user_id.as_bytes().to_vec()) .scan_prefix(user_id.as_bytes().to_vec())
.map(|(key, _)| { .map(|(key, _)| {
RoomId::parse( RoomId::parse(
@ -417,7 +437,8 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
})?, })?,
) )
.map_err(|_| Error::bad_database("Room ID in userroomid_joined is invalid.")) .map_err(|_| Error::bad_database("Room ID in userroomid_joined is invalid."))
})) }),
)
} }
/// Returns an iterator over all rooms a user was invited to. /// Returns an iterator over all rooms a user was invited to.
@ -429,7 +450,8 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
let mut prefix = user_id.as_bytes().to_vec(); let mut prefix = user_id.as_bytes().to_vec();
prefix.push(0xff); prefix.push(0xff);
Box::new(self.userroomid_invitestate Box::new(
self.userroomid_invitestate
.scan_prefix(prefix) .scan_prefix(prefix)
.map(|(key, state)| { .map(|(key, state)| {
let room_id = RoomId::parse( let room_id = RoomId::parse(
@ -442,13 +464,17 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
Error::bad_database("Room ID in userroomid_invited is invalid unicode.") Error::bad_database("Room ID in userroomid_invited is invalid unicode.")
})?, })?,
) )
.map_err(|_| Error::bad_database("Room ID in userroomid_invited is invalid."))?; .map_err(|_| {
Error::bad_database("Room ID in userroomid_invited is invalid.")
})?;
let state = serde_json::from_slice(&state) let state = serde_json::from_slice(&state).map_err(|_| {
.map_err(|_| Error::bad_database("Invalid state in userroomid_invitestate."))?; Error::bad_database("Invalid state in userroomid_invitestate.")
})?;
Ok((room_id, state)) Ok((room_id, state))
})) }),
)
} }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
@ -502,7 +528,8 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
let mut prefix = user_id.as_bytes().to_vec(); let mut prefix = user_id.as_bytes().to_vec();
prefix.push(0xff); prefix.push(0xff);
Box::new(self.userroomid_leftstate Box::new(
self.userroomid_leftstate
.scan_prefix(prefix) .scan_prefix(prefix)
.map(|(key, state)| { .map(|(key, state)| {
let room_id = RoomId::parse( let room_id = RoomId::parse(
@ -515,13 +542,17 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
Error::bad_database("Room ID in userroomid_invited is invalid unicode.") Error::bad_database("Room ID in userroomid_invited is invalid unicode.")
})?, })?,
) )
.map_err(|_| Error::bad_database("Room ID in userroomid_invited is invalid."))?; .map_err(|_| {
Error::bad_database("Room ID in userroomid_invited is invalid.")
})?;
let state = serde_json::from_slice(&state) let state = serde_json::from_slice(&state).map_err(|_| {
.map_err(|_| Error::bad_database("Invalid state in userroomid_leftstate."))?; Error::bad_database("Invalid state in userroomid_leftstate.")
})?;
Ok((room_id, state)) Ok((room_id, state))
})) }),
)
} }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]

@ -1,6 +1,10 @@
use std::{collections::HashSet, mem::size_of}; use std::{collections::HashSet, mem::size_of};
use crate::{service::{self, rooms::state_compressor::data::StateDiff}, database::KeyValueDatabase, Error, utils, Result}; use crate::{
database::KeyValueDatabase,
service::{self, rooms::state_compressor::data::StateDiff},
utils, Error, Result,
};
impl service::rooms::state_compressor::Data for KeyValueDatabase { impl service::rooms::state_compressor::Data for KeyValueDatabase {
fn get_statediff(&self, shortstatehash: u64) -> Result<StateDiff> { fn get_statediff(&self, shortstatehash: u64) -> Result<StateDiff> {
@ -10,11 +14,7 @@ impl service::rooms::state_compressor::Data for KeyValueDatabase {
.ok_or_else(|| Error::bad_database("State hash does not exist"))?; .ok_or_else(|| Error::bad_database("State hash does not exist"))?;
let parent = let parent =
utils::u64_from_bytes(&value[0..size_of::<u64>()]).expect("bytes have right length"); utils::u64_from_bytes(&value[0..size_of::<u64>()]).expect("bytes have right length");
let parent = if parent != 0 { let parent = if parent != 0 { Some(parent) } else { None };
Some(parent)
} else {
None
};
let mut add_mode = true; let mut add_mode = true;
let mut added = HashSet::new(); let mut added = HashSet::new();
@ -35,7 +35,11 @@ impl service::rooms::state_compressor::Data for KeyValueDatabase {
i += 2 * size_of::<u64>(); i += 2 * size_of::<u64>();
} }
Ok(StateDiff { parent, added, removed }) Ok(StateDiff {
parent,
added,
removed,
})
} }
fn save_statediff(&self, shortstatehash: u64, diff: StateDiff) -> Result<()> { fn save_statediff(&self, shortstatehash: u64, diff: StateDiff) -> Result<()> {

@ -1,13 +1,17 @@
use std::{collections::hash_map, mem::size_of, sync::Arc}; use std::{collections::hash_map, mem::size_of, sync::Arc};
use ruma::{UserId, RoomId, api::client::error::ErrorKind, EventId, signatures::CanonicalJsonObject}; use ruma::{
api::client::error::ErrorKind, signatures::CanonicalJsonObject, EventId, RoomId, UserId,
};
use tracing::error; use tracing::error;
use crate::{service, database::KeyValueDatabase, utils, Error, PduEvent, Result, services}; use crate::{database::KeyValueDatabase, service, services, utils, Error, PduEvent, Result};
impl service::rooms::timeline::Data for KeyValueDatabase { impl service::rooms::timeline::Data for KeyValueDatabase {
fn first_pdu_in_room(&self, room_id: &RoomId) -> Result<Option<Arc<PduEvent>>> { fn first_pdu_in_room(&self, room_id: &RoomId) -> Result<Option<Arc<PduEvent>>> {
let prefix = services().rooms.short let prefix = services()
.rooms
.short
.get_shortroomid(room_id)? .get_shortroomid(room_id)?
.expect("room exists") .expect("room exists")
.to_be_bytes() .to_be_bytes()
@ -82,10 +86,7 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
} }
/// Returns the json of a pdu. /// Returns the json of a pdu.
fn get_non_outlier_pdu_json( fn get_non_outlier_pdu_json(&self, event_id: &EventId) -> Result<Option<CanonicalJsonObject>> {
&self,
event_id: &EventId,
) -> Result<Option<CanonicalJsonObject>> {
self.eventid_pduid self.eventid_pduid
.get(event_id.as_bytes())? .get(event_id.as_bytes())?
.map(|pduid| { .map(|pduid| {
@ -187,10 +188,17 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
.map_err(|_| Error::bad_database("PDU has invalid count bytes.")) .map_err(|_| Error::bad_database("PDU has invalid count bytes."))
} }
fn append_pdu(&self, pdu_id: &[u8], pdu: &PduEvent, json: &CanonicalJsonObject, count: u64) -> Result<()> { fn append_pdu(
&self,
pdu_id: &[u8],
pdu: &PduEvent,
json: &CanonicalJsonObject,
count: u64,
) -> Result<()> {
self.pduid_pdu.insert( self.pduid_pdu.insert(
pdu_id, pdu_id,
&serde_json::to_vec(json).expect("CanonicalJsonObject is always a valid"))?; &serde_json::to_vec(json).expect("CanonicalJsonObject is always a valid"),
)?;
self.lasttimelinecount_cache self.lasttimelinecount_cache
.lock() .lock()
@ -209,7 +217,8 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
if self.pduid_pdu.get(pdu_id)?.is_some() { if self.pduid_pdu.get(pdu_id)?.is_some() {
self.pduid_pdu.insert( self.pduid_pdu.insert(
pdu_id, pdu_id,
&serde_json::to_vec(pdu).expect("CanonicalJsonObject is always a valid"))?; &serde_json::to_vec(pdu).expect("CanonicalJsonObject is always a valid"),
)?;
Ok(()) Ok(())
} else { } else {
Err(Error::BadRequest( Err(Error::BadRequest(
@ -227,7 +236,9 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
room_id: &RoomId, room_id: &RoomId,
since: u64, since: u64,
) -> Result<Box<dyn Iterator<Item = Result<(Vec<u8>, PduEvent)>>>> { ) -> Result<Box<dyn Iterator<Item = Result<(Vec<u8>, PduEvent)>>>> {
let prefix = services().rooms.short let prefix = services()
.rooms
.short
.get_shortroomid(room_id)? .get_shortroomid(room_id)?
.expect("room exists") .expect("room exists")
.to_be_bytes() .to_be_bytes()
@ -239,8 +250,8 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
let user_id = user_id.to_owned(); let user_id = user_id.to_owned();
Ok(Box::new(self Ok(Box::new(
.pduid_pdu self.pduid_pdu
.iter_from(&first_pdu_id, false) .iter_from(&first_pdu_id, false)
.take_while(move |(k, _)| k.starts_with(&prefix)) .take_while(move |(k, _)| k.starts_with(&prefix))
.map(move |(pdu_id, v)| { .map(move |(pdu_id, v)| {
@ -250,7 +261,8 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
pdu.remove_transaction_id()?; pdu.remove_transaction_id()?;
} }
Ok((pdu_id, pdu)) Ok((pdu_id, pdu))
}))) }),
))
} }
/// Returns an iterator over all events and their tokens in a room that happened before the /// Returns an iterator over all events and their tokens in a room that happened before the
@ -262,7 +274,9 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
until: u64, until: u64,
) -> Result<Box<dyn Iterator<Item = Result<(Vec<u8>, PduEvent)>>>> { ) -> Result<Box<dyn Iterator<Item = Result<(Vec<u8>, PduEvent)>>>> {
// Create the first part of the full pdu id // Create the first part of the full pdu id
let prefix = services().rooms.short let prefix = services()
.rooms
.short
.get_shortroomid(room_id)? .get_shortroomid(room_id)?
.expect("room exists") .expect("room exists")
.to_be_bytes() .to_be_bytes()
@ -275,8 +289,8 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
let user_id = user_id.to_owned(); let user_id = user_id.to_owned();
Ok(Box::new(self Ok(Box::new(
.pduid_pdu self.pduid_pdu
.iter_from(current, true) .iter_from(current, true)
.take_while(move |(k, _)| k.starts_with(&prefix)) .take_while(move |(k, _)| k.starts_with(&prefix))
.map(move |(pdu_id, v)| { .map(move |(pdu_id, v)| {
@ -286,7 +300,8 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
pdu.remove_transaction_id()?; pdu.remove_transaction_id()?;
} }
Ok((pdu_id, pdu)) Ok((pdu_id, pdu))
}))) }),
))
} }
fn pdus_after<'a>( fn pdus_after<'a>(
@ -296,7 +311,9 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
from: u64, from: u64,
) -> Result<Box<dyn Iterator<Item = Result<(Vec<u8>, PduEvent)>>>> { ) -> Result<Box<dyn Iterator<Item = Result<(Vec<u8>, PduEvent)>>>> {
// Create the first part of the full pdu id // Create the first part of the full pdu id
let prefix = services().rooms.short let prefix = services()
.rooms
.short
.get_shortroomid(room_id)? .get_shortroomid(room_id)?
.expect("room exists") .expect("room exists")
.to_be_bytes() .to_be_bytes()
@ -309,8 +326,8 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
let user_id = user_id.to_owned(); let user_id = user_id.to_owned();
Ok(Box::new(self Ok(Box::new(
.pduid_pdu self.pduid_pdu
.iter_from(current, false) .iter_from(current, false)
.take_while(move |(k, _)| k.starts_with(&prefix)) .take_while(move |(k, _)| k.starts_with(&prefix))
.map(move |(pdu_id, v)| { .map(move |(pdu_id, v)| {
@ -320,10 +337,16 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
pdu.remove_transaction_id()?; pdu.remove_transaction_id()?;
} }
Ok((pdu_id, pdu)) Ok((pdu_id, pdu))
}))) }),
))
} }
fn increment_notification_counts(&self, room_id: &RoomId, notifies: Vec<Box<UserId>>, highlights: Vec<Box<UserId>>) -> Result<()> { fn increment_notification_counts(
&self,
room_id: &RoomId,
notifies: Vec<Box<UserId>>,
highlights: Vec<Box<UserId>>,
) -> Result<()> {
let notifies_batch = Vec::new(); let notifies_batch = Vec::new();
let highlights_batch = Vec::new(); let highlights_batch = Vec::new();
for user in notifies { for user in notifies {

@ -1,6 +1,6 @@
use ruma::{UserId, RoomId}; use ruma::{RoomId, UserId};
use crate::{service, database::KeyValueDatabase, utils, Error, Result, services}; use crate::{database::KeyValueDatabase, service, services, utils, Error, Result};
impl service::rooms::user::Data for KeyValueDatabase { impl service::rooms::user::Data for KeyValueDatabase {
fn reset_notification_counts(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> { fn reset_notification_counts(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
@ -50,7 +50,11 @@ impl service::rooms::user::Data for KeyValueDatabase {
token: u64, token: u64,
shortstatehash: u64, shortstatehash: u64,
) -> Result<()> { ) -> Result<()> {
let shortroomid = services().rooms.short.get_shortroomid(room_id)?.expect("room exists"); let shortroomid = services()
.rooms
.short
.get_shortroomid(room_id)?
.expect("room exists");
let mut key = shortroomid.to_be_bytes().to_vec(); let mut key = shortroomid.to_be_bytes().to_vec();
key.extend_from_slice(&token.to_be_bytes()); key.extend_from_slice(&token.to_be_bytes());
@ -60,7 +64,11 @@ impl service::rooms::user::Data for KeyValueDatabase {
} }
fn get_token_shortstatehash(&self, room_id: &RoomId, token: u64) -> Result<Option<u64>> { fn get_token_shortstatehash(&self, room_id: &RoomId, token: u64) -> Result<Option<u64>> {
let shortroomid = services().rooms.short.get_shortroomid(room_id)?.expect("room exists"); let shortroomid = services()
.rooms
.short
.get_shortroomid(room_id)?
.expect("room exists");
let mut key = shortroomid.to_be_bytes().to_vec(); let mut key = shortroomid.to_be_bytes().to_vec();
key.extend_from_slice(&token.to_be_bytes()); key.extend_from_slice(&token.to_be_bytes());
@ -102,13 +110,15 @@ impl service::rooms::user::Data for KeyValueDatabase {
}); });
// We use the default compare function because keys are sorted correctly (not reversed) // We use the default compare function because keys are sorted correctly (not reversed)
Ok(Box::new(Box::new(utils::common_elements(iterators, Ord::cmp) Ok(Box::new(Box::new(
utils::common_elements(iterators, Ord::cmp)
.expect("users is not empty") .expect("users is not empty")
.map(|bytes| { .map(|bytes| {
RoomId::parse(utils::string_from_bytes(&*bytes).map_err(|_| { RoomId::parse(utils::string_from_bytes(&*bytes).map_err(|_| {
Error::bad_database("Invalid RoomId bytes in userroomid_joined") Error::bad_database("Invalid RoomId bytes in userroomid_joined")
})?) })?)
.map_err(|_| Error::bad_database("Invalid RoomId in userroomid_joined.")) .map_err(|_| Error::bad_database("Invalid RoomId in userroomid_joined."))
})))) }),
)))
} }
} }

@ -1,6 +1,6 @@
use ruma::{UserId, DeviceId, TransactionId}; use ruma::{DeviceId, TransactionId, UserId};
use crate::{service, database::KeyValueDatabase, Result}; use crate::{database::KeyValueDatabase, service, Result};
impl service::transaction_ids::Data for KeyValueDatabase { impl service::transaction_ids::Data for KeyValueDatabase {
fn add_txnid( fn add_txnid(

@ -1,4 +1,8 @@
use ruma::{UserId, DeviceId, signatures::CanonicalJsonValue, api::client::{uiaa::UiaaInfo, error::ErrorKind}}; use ruma::{
api::client::{error::ErrorKind, uiaa::UiaaInfo},
signatures::CanonicalJsonValue,
DeviceId, UserId,
};
use crate::{database::KeyValueDatabase, service, Error, Result}; use crate::{database::KeyValueDatabase, service, Error, Result};

@ -1,9 +1,20 @@
use std::{mem::size_of, collections::BTreeMap}; use std::{collections::BTreeMap, mem::size_of};
use ruma::{api::client::{filter::IncomingFilterDefinition, error::ErrorKind, device::Device}, UserId, RoomAliasId, MxcUri, DeviceId, MilliSecondsSinceUnixEpoch, DeviceKeyId, encryption::{OneTimeKey, CrossSigningKey, DeviceKeys}, serde::Raw, events::{AnyToDeviceEvent, StateEventType}, DeviceKeyAlgorithm, UInt}; use ruma::{
api::client::{device::Device, error::ErrorKind, filter::IncomingFilterDefinition},
encryption::{CrossSigningKey, DeviceKeys, OneTimeKey},
events::{AnyToDeviceEvent, StateEventType},
serde::Raw,
DeviceId, DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch, MxcUri, RoomAliasId,
UInt, UserId,
};
use tracing::warn; use tracing::warn;
use crate::{service::{self, users::clean_signatures}, database::KeyValueDatabase, Error, utils, services, Result}; use crate::{
database::KeyValueDatabase,
service::{self, users::clean_signatures},
services, utils, Error, Result,
};
impl service::users::Data for KeyValueDatabase { impl service::users::Data for KeyValueDatabase {
/// Check if a user has an account on this homeserver. /// Check if a user has an account on this homeserver.
@ -274,18 +285,21 @@ impl service::users::Data for KeyValueDatabase {
let mut prefix = user_id.as_bytes().to_vec(); let mut prefix = user_id.as_bytes().to_vec();
prefix.push(0xff); prefix.push(0xff);
// All devices have metadata // All devices have metadata
Box::new(self.userdeviceid_metadata Box::new(
self.userdeviceid_metadata
.scan_prefix(prefix) .scan_prefix(prefix)
.map(|(bytes, _)| { .map(|(bytes, _)| {
Ok(utils::string_from_bytes( Ok(utils::string_from_bytes(
bytes bytes.rsplit(|&b| b == 0xff).next().ok_or_else(|| {
.rsplit(|&b| b == 0xff) Error::bad_database("UserDevice ID in db is invalid.")
.next() })?,
.ok_or_else(|| Error::bad_database("UserDevice ID in db is invalid."))?,
) )
.map_err(|_| Error::bad_database("Device ID in userdeviceid_metadata is invalid."))? .map_err(|_| {
Error::bad_database("Device ID in userdeviceid_metadata is invalid.")
})?
.into()) .into())
})) }),
)
} }
/// Replaces the access token of one device. /// Replaces the access token of one device.
@ -341,8 +355,10 @@ impl service::users::Data for KeyValueDatabase {
&serde_json::to_vec(&one_time_key_value).expect("OneTimeKey::to_vec always works"), &serde_json::to_vec(&one_time_key_value).expect("OneTimeKey::to_vec always works"),
)?; )?;
self.userid_lastonetimekeyupdate self.userid_lastonetimekeyupdate.insert(
.insert(user_id.as_bytes(), &services().globals.next_count()?.to_be_bytes())?; user_id.as_bytes(),
&services().globals.next_count()?.to_be_bytes(),
)?;
Ok(()) Ok(())
} }
@ -372,8 +388,10 @@ impl service::users::Data for KeyValueDatabase {
prefix.extend_from_slice(key_algorithm.as_ref().as_bytes()); prefix.extend_from_slice(key_algorithm.as_ref().as_bytes());
prefix.push(b':'); prefix.push(b':');
self.userid_lastonetimekeyupdate self.userid_lastonetimekeyupdate.insert(
.insert(user_id.as_bytes(), &services().globals.next_count()?.to_be_bytes())?; user_id.as_bytes(),
&services().globals.next_count()?.to_be_bytes(),
)?;
self.onetimekeyid_onetimekeys self.onetimekeyid_onetimekeys
.scan_prefix(prefix) .scan_prefix(prefix)
@ -617,7 +635,8 @@ impl service::users::Data for KeyValueDatabase {
let to = to.unwrap_or(u64::MAX); let to = to.unwrap_or(u64::MAX);
Box::new(self.keychangeid_userid Box::new(
self.keychangeid_userid
.iter_from(&start, false) .iter_from(&start, false)
.take_while(move |(k, _)| { .take_while(move |(k, _)| {
k.starts_with(&prefix) k.starts_with(&prefix)
@ -635,20 +654,28 @@ impl service::users::Data for KeyValueDatabase {
}) })
.map(|(_, bytes)| { .map(|(_, bytes)| {
UserId::parse(utils::string_from_bytes(&bytes).map_err(|_| { UserId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("User ID in devicekeychangeid_userid is invalid unicode.") Error::bad_database(
"User ID in devicekeychangeid_userid is invalid unicode.",
)
})?) })?)
.map_err(|_| Error::bad_database("User ID in devicekeychangeid_userid is invalid.")) .map_err(|_| {
})) Error::bad_database("User ID in devicekeychangeid_userid is invalid.")
})
}),
)
} }
fn mark_device_key_update( fn mark_device_key_update(&self, user_id: &UserId) -> Result<()> {
&self,
user_id: &UserId,
) -> Result<()> {
let count = services().globals.next_count()?.to_be_bytes(); let count = services().globals.next_count()?.to_be_bytes();
for room_id in services().rooms.state_cache.rooms_joined(user_id).filter_map(|r| r.ok()) { for room_id in services()
.rooms
.state_cache
.rooms_joined(user_id)
.filter_map(|r| r.ok())
{
// Don't send key updates to unencrypted rooms // Don't send key updates to unencrypted rooms
if services().rooms if services()
.rooms
.state_accessor .state_accessor
.room_state_get(&room_id, &StateEventType::RoomEncryption, "")? .room_state_get(&room_id, &StateEventType::RoomEncryption, "")?
.is_none() .is_none()
@ -883,20 +910,19 @@ impl service::users::Data for KeyValueDatabase {
let mut key = user_id.as_bytes().to_vec(); let mut key = user_id.as_bytes().to_vec();
key.push(0xff); key.push(0xff);
Box::new(self.userdeviceid_metadata Box::new(
self.userdeviceid_metadata
.scan_prefix(key) .scan_prefix(key)
.map(|(_, bytes)| { .map(|(_, bytes)| {
serde_json::from_slice::<Device>(&bytes) serde_json::from_slice::<Device>(&bytes).map_err(|_| {
.map_err(|_| Error::bad_database("Device in userdeviceid_metadata is invalid.")) Error::bad_database("Device in userdeviceid_metadata is invalid.")
})) })
}),
)
} }
/// Creates a new sync filter. Returns the filter id. /// Creates a new sync filter. Returns the filter id.
fn create_filter( fn create_filter(&self, user_id: &UserId, filter: &IncomingFilterDefinition) -> Result<String> {
&self,
user_id: &UserId,
filter: &IncomingFilterDefinition,
) -> Result<String> {
let filter_id = utils::random_string(4); let filter_id = utils::random_string(4);
let mut key = user_id.as_bytes().to_vec(); let mut key = user_id.as_bytes().to_vec();

@ -1,8 +1,16 @@
pub mod abstraction; pub mod abstraction;
pub mod key_value; pub mod key_value;
use crate::{utils, Config, Error, Result, service::{users, globals, uiaa, rooms::{self, state_compressor::CompressedStateEvent}, account_data, media, key_backups, transaction_ids, sending, appservice, pusher}, services, PduEvent, Services, SERVICES}; use crate::{
service::{
account_data, appservice, globals, key_backups, media, pusher,
rooms::{self, state_compressor::CompressedStateEvent},
sending, transaction_ids, uiaa, users,
},
services, utils, Config, Error, PduEvent, Result, Services, SERVICES,
};
use abstraction::KeyValueDatabaseEngine; use abstraction::KeyValueDatabaseEngine;
use abstraction::KvTree;
use directories::ProjectDirs; use directories::ProjectDirs;
use futures_util::{stream::FuturesUnordered, StreamExt}; use futures_util::{stream::FuturesUnordered, StreamExt};
use lru_cache::LruCache; use lru_cache::LruCache;
@ -12,7 +20,8 @@ use ruma::{
GlobalAccountDataEvent, GlobalAccountDataEventType, StateEventType, GlobalAccountDataEvent, GlobalAccountDataEventType, StateEventType,
}, },
push::Ruleset, push::Ruleset,
DeviceId, EventId, RoomId, UserId, signatures::CanonicalJsonValue, signatures::CanonicalJsonValue,
DeviceId, EventId, RoomId, UserId,
}; };
use std::{ use std::{
collections::{BTreeMap, HashMap, HashSet}, collections::{BTreeMap, HashMap, HashSet},
@ -25,7 +34,6 @@ use std::{
}; };
use tokio::sync::{mpsc, OwnedRwLockReadGuard, RwLock as TokioRwLock, Semaphore}; use tokio::sync::{mpsc, OwnedRwLockReadGuard, RwLock as TokioRwLock, Semaphore};
use tracing::{debug, error, info, warn}; use tracing::{debug, error, info, warn};
use abstraction::KvTree;
pub struct KeyValueDatabase { pub struct KeyValueDatabase {
_db: Arc<dyn KeyValueDatabaseEngine>, _db: Arc<dyn KeyValueDatabaseEngine>,
@ -399,7 +407,6 @@ impl KeyValueDatabase {
(100.0 * config.conduit_cache_capacity_modifier) as usize, (100.0 * config.conduit_cache_capacity_modifier) as usize,
)), )),
lasttimelinecount_cache: Mutex::new(HashMap::new()), lasttimelinecount_cache: Mutex::new(HashMap::new()),
}); });
let services_raw = Box::new(Services::build(Arc::clone(&db), config)?); let services_raw = Box::new(Services::build(Arc::clone(&db), config)?);
@ -407,7 +414,6 @@ impl KeyValueDatabase {
// This is the first and only time we initialize the SERVICE static // This is the first and only time we initialize the SERVICE static
*SERVICES.write().unwrap() = Some(Box::leak(services_raw)); *SERVICES.write().unwrap() = Some(Box::leak(services_raw));
// Matrix resource ownership is based on the server name; changing it // Matrix resource ownership is based on the server name; changing it
// requires recreating the database from scratch. // requires recreating the database from scratch.
if services().users.count()? > 0 { if services().users.count()? > 0 {
@ -570,7 +576,10 @@ impl KeyValueDatabase {
let states_parents = last_roomsstatehash.map_or_else( let states_parents = last_roomsstatehash.map_or_else(
|| Ok(Vec::new()), || Ok(Vec::new()),
|&last_roomsstatehash| { |&last_roomsstatehash| {
services().rooms.state_compressor.load_shortstatehash_info(dbg!(last_roomsstatehash)) services()
.rooms
.state_compressor
.load_shortstatehash_info(dbg!(last_roomsstatehash))
}, },
)?; )?;
@ -643,14 +652,15 @@ impl KeyValueDatabase {
current_state = HashSet::new(); current_state = HashSet::new();
current_sstatehash = Some(sstatehash); current_sstatehash = Some(sstatehash);
let event_id = db let event_id = db.shorteventid_eventid.get(&seventid).unwrap().unwrap();
.shorteventid_eventid
.get(&seventid)
.unwrap()
.unwrap();
let string = utils::string_from_bytes(&event_id).unwrap(); let string = utils::string_from_bytes(&event_id).unwrap();
let event_id = <&EventId>::try_from(string.as_str()).unwrap(); let event_id = <&EventId>::try_from(string.as_str()).unwrap();
let pdu = services().rooms.timeline.get_pdu(event_id).unwrap().unwrap(); let pdu = services()
.rooms
.timeline
.get_pdu(event_id)
.unwrap()
.unwrap();
if Some(&pdu.room_id) != current_room.as_ref() { if Some(&pdu.room_id) != current_room.as_ref() {
current_room = Some(pdu.room_id.clone()); current_room = Some(pdu.room_id.clone());
@ -764,8 +774,7 @@ impl KeyValueDatabase {
.peekable(); .peekable();
while iter.peek().is_some() { while iter.peek().is_some() {
db.tokenids db.tokenids.insert_batch(&mut iter.by_ref().take(1000))?;
.insert_batch(&mut iter.by_ref().take(1000))?;
println!("smaller batch done"); println!("smaller batch done");
} }
@ -803,8 +812,7 @@ impl KeyValueDatabase {
// Force E2EE device list updates so we can send them over federation // Force E2EE device list updates so we can send them over federation
for user_id in services().users.iter().filter_map(|r| r.ok()) { for user_id in services().users.iter().filter_map(|r| r.ok()) {
services().users services().users.mark_device_key_update(&user_id)?;
.mark_device_key_update(&user_id)?;
} }
services().globals.bump_database_version(10)?; services().globals.bump_database_version(10)?;
@ -825,7 +833,8 @@ impl KeyValueDatabase {
info!( info!(
"Loaded {} database with version {}", "Loaded {} database with version {}",
services().globals.config.database_backend, latest_database_version services().globals.config.database_backend,
latest_database_version
); );
} else { } else {
services() services()
@ -837,7 +846,8 @@ impl KeyValueDatabase {
warn!( warn!(
"Created new {} database with version {}", "Created new {} database with version {}",
services().globals.config.database_backend, latest_database_version services().globals.config.database_backend,
latest_database_version
); );
} }
@ -862,9 +872,7 @@ impl KeyValueDatabase {
} }
}; };
services() services().sending.start_handler(sending_receiver);
.sending
.start_handler(sending_receiver);
Self::start_cleanup_task().await; Self::start_cleanup_task().await;
@ -898,7 +906,8 @@ impl KeyValueDatabase {
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
let timer_interval = Duration::from_secs(services().globals.config.cleanup_second_interval as u64); let timer_interval =
Duration::from_secs(services().globals.config.cleanup_second_interval as u64);
tokio::spawn(async move { tokio::spawn(async move {
let mut i = interval(timer_interval); let mut i = interval(timer_interval);
@ -937,8 +946,10 @@ fn set_emergency_access() -> Result<bool> {
let conduit_user = UserId::parse_with_server_name("conduit", services().globals.server_name()) let conduit_user = UserId::parse_with_server_name("conduit", services().globals.server_name())
.expect("@conduit:server_name is a valid UserId"); .expect("@conduit:server_name is a valid UserId");
services().users services().users.set_password(
.set_password(&conduit_user, services().globals.emergency_password().as_deref())?; &conduit_user,
services().globals.emergency_password().as_deref(),
)?;
let (ruleset, res) = match services().globals.emergency_password() { let (ruleset, res) = match services().globals.emergency_password() {
Some(_) => (Ruleset::server_default(&conduit_user), Ok(true)), Some(_) => (Ruleset::server_default(&conduit_user), Ok(true)),
@ -951,7 +962,8 @@ fn set_emergency_access() -> Result<bool> {
GlobalAccountDataEventType::PushRules.to_string().into(), GlobalAccountDataEventType::PushRules.to_string().into(),
&serde_json::to_value(&GlobalAccountDataEvent { &serde_json::to_value(&GlobalAccountDataEvent {
content: PushRulesEventContent { global: ruleset }, content: PushRulesEventContent { global: ruleset },
}).expect("to json value always works"), })
.expect("to json value always works"),
)?; )?;
res res

@ -7,22 +7,27 @@
#![allow(clippy::suspicious_else_formatting)] #![allow(clippy::suspicious_else_formatting)]
#![deny(clippy::dbg_macro)] #![deny(clippy::dbg_macro)]
pub mod api;
mod config; mod config;
mod database; mod database;
mod service; mod service;
pub mod api;
mod utils; mod utils;
use std::{cell::Cell, sync::{RwLock, Arc}}; use std::{
cell::Cell,
sync::{Arc, RwLock},
};
pub use api::ruma_wrapper::{Ruma, RumaResponse};
pub use config::Config; pub use config::Config;
pub use service::{pdu::PduEvent, Services};
pub use utils::error::{Error, Result}; pub use utils::error::{Error, Result};
pub use service::{Services, pdu::PduEvent};
pub use api::ruma_wrapper::{Ruma, RumaResponse};
pub static SERVICES: RwLock<Option<&'static Services>> = RwLock::new(None); pub static SERVICES: RwLock<Option<&'static Services>> = RwLock::new(None);
pub fn services<'a>() -> &'static Services { pub fn services<'a>() -> &'static Services {
&SERVICES.read().unwrap().expect("SERVICES should be initialized when this is called") &SERVICES
.read()
.unwrap()
.expect("SERVICES should be initialized when this is called")
} }

@ -1,7 +1,11 @@
use std::collections::HashMap; use std::collections::HashMap;
use ruma::{UserId, RoomId, events::{RoomAccountDataEventType, AnyEphemeralRoomEvent}, serde::Raw};
use crate::Result; use crate::Result;
use ruma::{
events::{AnyEphemeralRoomEvent, RoomAccountDataEventType},
serde::Raw,
RoomId, UserId,
};
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
/// Places one event in the account data of the user and removes the previous entry. /// Places one event in the account data of the user and removes the previous entry.

@ -3,9 +3,7 @@ mod data;
pub use data::Data; pub use data::Data;
use ruma::{ use ruma::{
api::client::{ api::client::error::ErrorKind,
error::ErrorKind,
},
events::{AnyEphemeralRoomEvent, RoomAccountDataEventType}, events::{AnyEphemeralRoomEvent, RoomAccountDataEventType},
serde::Raw, serde::Raw,
signatures::CanonicalJsonValue, signatures::CanonicalJsonValue,

@ -28,7 +28,15 @@ use ruma::{
use serde_json::value::to_raw_value; use serde_json::value::to_raw_value;
use tokio::sync::{mpsc, MutexGuard, RwLock, RwLockReadGuard}; use tokio::sync::{mpsc, MutexGuard, RwLock, RwLockReadGuard};
use crate::{Result, services, Error, api::{server_server, client_server::{AUTO_GEN_PASSWORD_LENGTH, leave_all_rooms}}, PduEvent, utils::{HtmlEscape, self}}; use crate::{
api::{
client_server::{leave_all_rooms, AUTO_GEN_PASSWORD_LENGTH},
server_server,
},
services,
utils::{self, HtmlEscape},
Error, PduEvent, Result,
};
use super::pdu::PduBuilder; use super::pdu::PduBuilder;
@ -153,7 +161,6 @@ enum AdminCommand {
EnableRoom { room_id: Box<RoomId> }, EnableRoom { room_id: Box<RoomId> },
} }
#[derive(Debug)] #[derive(Debug)]
pub enum AdminRoomEvent { pub enum AdminRoomEvent {
ProcessMessage(String), ProcessMessage(String),
@ -166,15 +173,13 @@ pub struct Service {
} }
impl Service { impl Service {
pub fn start_handler( pub fn start_handler(&self, mut receiver: mpsc::UnboundedReceiver<AdminRoomEvent>) {
&self,
mut receiver: mpsc::UnboundedReceiver<AdminRoomEvent>,
) {
tokio::spawn(async move { tokio::spawn(async move {
// TODO: Use futures when we have long admin commands // TODO: Use futures when we have long admin commands
//let mut futures = FuturesUnordered::new(); //let mut futures = FuturesUnordered::new();
let conduit_user = UserId::parse(format!("@conduit:{}", services().globals.server_name())) let conduit_user =
UserId::parse(format!("@conduit:{}", services().globals.server_name()))
.expect("@conduit:server_name is valid"); .expect("@conduit:server_name is valid");
let conduit_room = services() let conduit_room = services()
@ -193,7 +198,8 @@ impl Service {
mutex_lock: &MutexGuard<'_, ()>| { mutex_lock: &MutexGuard<'_, ()>| {
services() services()
.rooms .rooms
.timeline.build_and_append_pdu( .timeline
.build_and_append_pdu(
PduBuilder { PduBuilder {
event_type: RoomEventType::RoomMessage, event_type: RoomEventType::RoomMessage,
content: to_raw_value(&message) content: to_raw_value(&message)
@ -316,9 +322,11 @@ impl Service {
) -> Result<RoomMessageEventContent> { ) -> Result<RoomMessageEventContent> {
let reply_message_content = match command { let reply_message_content = match command {
AdminCommand::RegisterAppservice => { AdminCommand::RegisterAppservice => {
if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```" { if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```"
{
let appservice_config = body[1..body.len() - 1].join("\n"); let appservice_config = body[1..body.len() - 1].join("\n");
let parsed_config = serde_yaml::from_str::<serde_yaml::Value>(&appservice_config); let parsed_config =
serde_yaml::from_str::<serde_yaml::Value>(&appservice_config);
match parsed_config { match parsed_config {
Ok(yaml) => match services().appservice.register_appservice(yaml) { Ok(yaml) => match services().appservice.register_appservice(yaml) {
Ok(id) => RoomMessageEventContent::text_plain(format!( Ok(id) => RoomMessageEventContent::text_plain(format!(
@ -343,7 +351,10 @@ impl Service {
} }
AdminCommand::UnregisterAppservice { AdminCommand::UnregisterAppservice {
appservice_identifier, appservice_identifier,
} => match services().appservice.unregister_appservice(&appservice_identifier) { } => match services()
.appservice
.unregister_appservice(&appservice_identifier)
{
Ok(()) => RoomMessageEventContent::text_plain("Appservice unregistered."), Ok(()) => RoomMessageEventContent::text_plain("Appservice unregistered."),
Err(e) => RoomMessageEventContent::text_plain(format!( Err(e) => RoomMessageEventContent::text_plain(format!(
"Failed to unregister appservice: {}", "Failed to unregister appservice: {}",
@ -351,7 +362,11 @@ impl Service {
)), )),
}, },
AdminCommand::ListAppservices => { AdminCommand::ListAppservices => {
if let Ok(appservices) = services().appservice.iter_ids().map(|ids| ids.collect::<Vec<_>>()) { if let Ok(appservices) = services()
.appservice
.iter_ids()
.map(|ids| ids.collect::<Vec<_>>())
{
let count = appservices.len(); let count = appservices.len();
let output = format!( let output = format!(
"Appservices ({}): {}", "Appservices ({}): {}",
@ -399,7 +414,11 @@ impl Service {
Err(e) => RoomMessageEventContent::text_plain(e.to_string()), Err(e) => RoomMessageEventContent::text_plain(e.to_string()),
}, },
AdminCommand::IncomingFederation => { AdminCommand::IncomingFederation => {
let map = services().globals.roomid_federationhandletime.read().unwrap(); let map = services()
.globals
.roomid_federationhandletime
.read()
.unwrap();
let mut msg: String = format!("Handling {} incoming pdus:\n", map.len()); let mut msg: String = format!("Handling {} incoming pdus:\n", map.len());
for (r, (e, i)) in map.iter() { for (r, (e, i)) in map.iter() {
@ -426,7 +445,10 @@ impl Service {
Error::bad_database("Invalid room id field in event in database") Error::bad_database("Invalid room id field in event in database")
})?; })?;
let start = Instant::now(); let start = Instant::now();
let count = services().rooms.auth_chain.get_auth_chain(room_id, vec![event_id]) let count = services()
.rooms
.auth_chain
.get_auth_chain(room_id, vec![event_id])
.await? .await?
.count(); .count();
let elapsed = start.elapsed(); let elapsed = start.elapsed();
@ -439,7 +461,8 @@ impl Service {
} }
} }
AdminCommand::ParsePdu => { AdminCommand::ParsePdu => {
if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```" { if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```"
{
let string = body[1..body.len() - 1].join("\n"); let string = body[1..body.len() - 1].join("\n");
match serde_json::from_str(&string) { match serde_json::from_str(&string) {
Ok(value) => { Ok(value) => {
@ -477,15 +500,18 @@ impl Service {
} }
AdminCommand::GetPdu { event_id } => { AdminCommand::GetPdu { event_id } => {
let mut outlier = false; let mut outlier = false;
let mut pdu_json = services().rooms.timeline.get_non_outlier_pdu_json(&event_id)?; let mut pdu_json = services()
.rooms
.timeline
.get_non_outlier_pdu_json(&event_id)?;
if pdu_json.is_none() { if pdu_json.is_none() {
outlier = true; outlier = true;
pdu_json = services().rooms.timeline.get_pdu_json(&event_id)?; pdu_json = services().rooms.timeline.get_pdu_json(&event_id)?;
} }
match pdu_json { match pdu_json {
Some(json) => { Some(json) => {
let json_text = let json_text = serde_json::to_string_pretty(&json)
serde_json::to_string_pretty(&json).expect("canonical json is valid json"); .expect("canonical json is valid json");
RoomMessageEventContent::text_html( RoomMessageEventContent::text_html(
format!( format!(
"{}\n```json\n{}\n```", "{}\n```json\n{}\n```",
@ -539,7 +565,10 @@ impl Service {
if !services().users.exists(&user_id)? if !services().users.exists(&user_id)?
|| services().users.is_deactivated(&user_id)? || services().users.is_deactivated(&user_id)?
|| user_id || user_id
== UserId::parse_with_server_name("conduit", services().globals.server_name()) == UserId::parse_with_server_name(
"conduit",
services().globals.server_name(),
)
.expect("conduit user exists") .expect("conduit user exists")
{ {
return Ok(RoomMessageEventContent::text_plain( return Ok(RoomMessageEventContent::text_plain(
@ -549,7 +578,10 @@ impl Service {
let new_password = utils::random_string(AUTO_GEN_PASSWORD_LENGTH); let new_password = utils::random_string(AUTO_GEN_PASSWORD_LENGTH);
match services().users.set_password(&user_id, Some(new_password.as_str())) { match services()
.users
.set_password(&user_id, Some(new_password.as_str()))
{
Ok(()) => RoomMessageEventContent::text_plain(format!( Ok(()) => RoomMessageEventContent::text_plain(format!(
"Successfully reset the password for user {}: {}", "Successfully reset the password for user {}: {}",
user_id, new_password user_id, new_password
@ -590,7 +622,8 @@ impl Service {
// Default to pretty displayname // Default to pretty displayname
let displayname = format!("{} ⚡️", user_id.localpart()); let displayname = format!("{} ⚡️", user_id.localpart());
services().users services()
.users
.set_displayname(&user_id, Some(displayname.clone()))?; .set_displayname(&user_id, Some(displayname.clone()))?;
// Initial account data // Initial account data
@ -604,7 +637,8 @@ impl Service {
content: ruma::events::push_rules::PushRulesEventContent { content: ruma::events::push_rules::PushRulesEventContent {
global: ruma::push::Ruleset::server_default(&user_id), global: ruma::push::Ruleset::server_default(&user_id),
}, },
}).expect("to json value always works"), })
.expect("to json value always works"),
)?; )?;
// we dont add a device since we're not the user, just the creator // we dont add a device since we're not the user, just the creator
@ -651,7 +685,8 @@ impl Service {
} }
} }
AdminCommand::DeactivateAll { leave_rooms, force } => { AdminCommand::DeactivateAll { leave_rooms, force } => {
if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```" { if body.len() > 2 && body[0].trim() == "```" && body.last().unwrap().trim() == "```"
{
let usernames = body.clone().drain(1..body.len() - 1).collect::<Vec<_>>(); let usernames = body.clone().drain(1..body.len() - 1).collect::<Vec<_>>();
let mut user_ids: Vec<&UserId> = Vec::new(); let mut user_ids: Vec<&UserId> = Vec::new();
@ -672,8 +707,7 @@ impl Service {
let mut admins = Vec::new(); let mut admins = Vec::new();
if !force { if !force {
user_ids.retain(|&user_id| { user_ids.retain(|&user_id| match services().users.is_admin(user_id) {
match services().users.is_admin(user_id) {
Ok(is_admin) => match is_admin { Ok(is_admin) => match is_admin {
true => { true => {
admins.push(user_id.localpart()); admins.push(user_id.localpart());
@ -682,7 +716,6 @@ impl Service {
false => true, false => true,
}, },
Err(_) => false, Err(_) => false,
}
}) })
} }
@ -783,8 +816,8 @@ impl Service {
} else { } else {
// Wrap the usage line in a code block, and add a yaml block example // Wrap the usage line in a code block, and add a yaml block example
// This makes the usage of e.g. `register-appservice` more accurate // This makes the usage of e.g. `register-appservice` more accurate
let re = let re = Regex::new("(?m)^USAGE:\n (.*?)\n\n")
Regex::new("(?m)^USAGE:\n (.*?)\n\n").expect("Regex compilation should not fail"); .expect("Regex compilation should not fail");
re.replace_all(&text, "USAGE:\n<pre>$1[nobr]\n[commandbodyblock]</pre>") re.replace_all(&text, "USAGE:\n<pre>$1[nobr]\n[commandbodyblock]</pre>")
.replace("[commandbodyblock]", &command_body) .replace("[commandbodyblock]", &command_body)
}; };
@ -808,7 +841,8 @@ impl Service {
services().rooms.short.get_or_create_shortroomid(&room_id)?; services().rooms.short.get_or_create_shortroomid(&room_id)?;
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -818,7 +852,8 @@ impl Service {
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
// Create a user for the server // Create a user for the server
let conduit_user = UserId::parse_with_server_name("conduit", services().globals.server_name()) let conduit_user =
UserId::parse_with_server_name("conduit", services().globals.server_name())
.expect("@conduit:server_name is valid"); .expect("@conduit:server_name is valid");
services().users.create(&conduit_user, None)?; services().users.create(&conduit_user, None)?;
@ -1002,7 +1037,8 @@ impl Service {
user_id: &UserId, user_id: &UserId,
displayname: String, displayname: String,
) -> Result<()> { ) -> Result<()> {
let admin_room_alias: Box<RoomAliasId> = format!("#admins:{}", services().globals.server_name()) let admin_room_alias: Box<RoomAliasId> =
format!("#admins:{}", services().globals.server_name())
.try_into() .try_into()
.expect("#admins:server_name is a valid alias name"); .expect("#admins:server_name is a valid alias name");
let room_id = services() let room_id = services()
@ -1012,7 +1048,8 @@ impl Service {
.expect("Admin room must exist"); .expect("Admin room must exist");
let mutex_state = Arc::clone( let mutex_state = Arc::clone(
services().globals services()
.globals
.roomid_mutex_state .roomid_mutex_state
.write() .write()
.unwrap() .unwrap()
@ -1022,7 +1059,8 @@ impl Service {
let state_lock = mutex_state.lock().await; let state_lock = mutex_state.lock().await;
// Use the server user to grant the new admin's power level // Use the server user to grant the new admin's power level
let conduit_user = UserId::parse_with_server_name("conduit", services().globals.server_name()) let conduit_user =
UserId::parse_with_server_name("conduit", services().globals.server_name())
.expect("@conduit:server_name is valid"); .expect("@conduit:server_name is valid");
// Invite and join the real user // Invite and join the real user

@ -1,7 +1,11 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use async_trait::async_trait; use async_trait::async_trait;
use ruma::{signatures::Ed25519KeyPair, DeviceId, UserId, ServerName, api::federation::discovery::{ServerSigningKeys, VerifyKey}, ServerSigningKeyId}; use ruma::{
api::federation::discovery::{ServerSigningKeys, VerifyKey},
signatures::Ed25519KeyPair,
DeviceId, ServerName, ServerSigningKeyId, UserId,
};
use crate::Result; use crate::Result;

@ -4,7 +4,7 @@ pub use data::Data;
use crate::api::server_server::FedDest; use crate::api::server_server::FedDest;
use crate::service::*; use crate::service::*;
use crate::{Config, utils, Error, Result}; use crate::{utils, Config, Error, Result};
use ruma::{ use ruma::{
api::{ api::{
client::sync::sync_events, client::sync::sync_events,
@ -89,12 +89,8 @@ impl Default for RotationHandler {
} }
} }
impl Service { impl Service {
pub fn load( pub fn load(db: Arc<dyn Data>, config: Config) -> Result<Self> {
db: Arc<dyn Data>,
config: Config,
) -> Result<Self> {
let keypair = db.load_keypair(); let keypair = db.load_keypair();
let keypair = match keypair { let keypair = match keypair {

@ -1,7 +1,11 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use ruma::{api::client::backup::{BackupAlgorithm, RoomKeyBackup, KeyBackupData}, serde::Raw, UserId, RoomId};
use crate::Result; use crate::Result;
use ruma::{
api::client::backup::{BackupAlgorithm, KeyBackupData, RoomKeyBackup},
serde::Raw,
RoomId, UserId,
};
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
fn create_backup( fn create_backup(
@ -21,16 +25,10 @@ pub trait Data: Send + Sync {
fn get_latest_backup_version(&self, user_id: &UserId) -> Result<Option<String>>; fn get_latest_backup_version(&self, user_id: &UserId) -> Result<Option<String>>;
fn get_latest_backup( fn get_latest_backup(&self, user_id: &UserId)
&self, -> Result<Option<(String, Raw<BackupAlgorithm>)>>;
user_id: &UserId,
) -> Result<Option<(String, Raw<BackupAlgorithm>)>>;
fn get_backup( fn get_backup(&self, user_id: &UserId, version: &str) -> Result<Option<Raw<BackupAlgorithm>>>;
&self,
user_id: &UserId,
version: &str,
) -> Result<Option<Raw<BackupAlgorithm>>>;
fn add_key( fn add_key(
&self, &self,
@ -68,12 +66,7 @@ pub trait Data: Send + Sync {
fn delete_all_keys(&self, user_id: &UserId, version: &str) -> Result<()>; fn delete_all_keys(&self, user_id: &UserId, version: &str) -> Result<()>;
fn delete_room_keys( fn delete_room_keys(&self, user_id: &UserId, version: &str, room_id: &RoomId) -> Result<()>;
&self,
user_id: &UserId,
version: &str,
room_id: &RoomId,
) -> Result<()>;
fn delete_room_key( fn delete_room_key(
&self, &self,

@ -1,7 +1,7 @@
mod data; mod data;
pub use data::Data; pub use data::Data;
use crate::{utils, Error, Result, services}; use crate::{services, utils, Error, Result};
use ruma::{ use ruma::{
api::client::{ api::client::{
backup::{BackupAlgorithm, KeyBackupData, RoomKeyBackup}, backup::{BackupAlgorithm, KeyBackupData, RoomKeyBackup},
@ -65,7 +65,8 @@ impl Service {
session_id: &str, session_id: &str,
key_data: &Raw<KeyBackupData>, key_data: &Raw<KeyBackupData>,
) -> Result<()> { ) -> Result<()> {
self.db.add_key(user_id, version, room_id, session_id, key_data) self.db
.add_key(user_id, version, room_id, session_id, key_data)
} }
pub fn count_keys(&self, user_id: &UserId, version: &str) -> Result<usize> { pub fn count_keys(&self, user_id: &UserId, version: &str) -> Result<usize> {
@ -123,6 +124,7 @@ impl Service {
room_id: &RoomId, room_id: &RoomId,
session_id: &str, session_id: &str,
) -> Result<()> { ) -> Result<()> {
self.db.delete_room_key(user_id, version, room_id, session_id) self.db
.delete_room_key(user_id, version, room_id, session_id)
} }
} }

@ -1,8 +1,20 @@
use crate::Result; use crate::Result;
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
fn create_file_metadata(&self, mxc: String, width: u32, height: u32, content_disposition: Option<&str>, content_type: Option<&str>) -> Result<Vec<u8>>; fn create_file_metadata(
&self,
mxc: String,
width: u32,
height: u32,
content_disposition: Option<&str>,
content_type: Option<&str>,
) -> Result<Vec<u8>>;
/// Returns content_disposition, content_type and the metadata key. /// Returns content_disposition, content_type and the metadata key.
fn search_file_metadata(&self, mxc: String, width: u32, height: u32) -> Result<(Option<String>, Option<String>, Vec<u8>)>; fn search_file_metadata(
&self,
mxc: String,
width: u32,
height: u32,
) -> Result<(Option<String>, Option<String>, Vec<u8>)>;
} }

@ -1,8 +1,8 @@
mod data; mod data;
pub use data::Data; pub use data::Data;
use crate::{services, utils, Error, Result};
use image::{imageops::FilterType, GenericImageView}; use image::{imageops::FilterType, GenericImageView};
use crate::{utils, Error, Result, services};
use std::{mem, sync::Arc}; use std::{mem, sync::Arc};
use tokio::{ use tokio::{
fs::File, fs::File,
@ -29,7 +29,9 @@ impl Service {
file: &[u8], file: &[u8],
) -> Result<()> { ) -> Result<()> {
// Width, Height = 0 if it's not a thumbnail // Width, Height = 0 if it's not a thumbnail
let key = self.db.create_file_metadata(mxc, 0, 0, content_disposition, content_type)?; let key = self
.db
.create_file_metadata(mxc, 0, 0, content_disposition, content_type)?;
let path = services().globals.get_media_file(&key); let path = services().globals.get_media_file(&key);
let mut f = File::create(path).await?; let mut f = File::create(path).await?;
@ -48,7 +50,9 @@ impl Service {
height: u32, height: u32,
file: &[u8], file: &[u8],
) -> Result<()> { ) -> Result<()> {
let key = self.db.create_file_metadata(mxc, width, height, content_disposition, content_type)?; let key =
self.db
.create_file_metadata(mxc, width, height, content_disposition, content_type)?;
let path = services().globals.get_media_file(&key); let path = services().globals.get_media_file(&key);
let mut f = File::create(path).await?; let mut f = File::create(path).await?;
@ -59,12 +63,13 @@ impl Service {
/// Downloads a file. /// Downloads a file.
pub async fn get(&self, mxc: String) -> Result<Option<FileMeta>> { pub async fn get(&self, mxc: String) -> Result<Option<FileMeta>> {
if let Ok((content_disposition, content_type, key)) = self.db.search_file_metadata(mxc, 0, 0) { if let Ok((content_disposition, content_type, key)) =
self.db.search_file_metadata(mxc, 0, 0)
{
let path = services().globals.get_media_file(&key); let path = services().globals.get_media_file(&key);
let mut file = Vec::new(); let mut file = Vec::new();
File::open(path).await?.read_to_end(&mut file).await?; File::open(path).await?.read_to_end(&mut file).await?;
Ok(Some(FileMeta { Ok(Some(FileMeta {
content_disposition, content_disposition,
content_type, content_type,
@ -108,7 +113,9 @@ impl Service {
.thumbnail_properties(width, height) .thumbnail_properties(width, height)
.unwrap_or((0, 0, false)); // 0, 0 because that's the original file .unwrap_or((0, 0, false)); // 0, 0 because that's the original file
if let Ok((content_disposition, content_type, key)) = self.db.search_file_metadata(mxc.clone(), width, height) { if let Ok((content_disposition, content_type, key)) =
self.db.search_file_metadata(mxc.clone(), width, height)
{
// Using saved thumbnail // Using saved thumbnail
let path = services().globals.get_media_file(&key); let path = services().globals.get_media_file(&key);
let mut file = Vec::new(); let mut file = Vec::new();
@ -119,7 +126,9 @@ impl Service {
content_type, content_type,
file: file.to_vec(), file: file.to_vec(),
})) }))
} else if let Ok((content_disposition, content_type, key)) = self.db.search_file_metadata(mxc.clone(), 0, 0) { } else if let Ok((content_disposition, content_type, key)) =
self.db.search_file_metadata(mxc.clone(), 0, 0)
{
// Generate a thumbnail // Generate a thumbnail
let path = services().globals.get_media_file(&key); let path = services().globals.get_media_file(&key);
let mut file = Vec::new(); let mut file = Vec::new();
@ -180,7 +189,13 @@ impl Service {
thumbnail.write_to(&mut thumbnail_bytes, image::ImageOutputFormat::Png)?; thumbnail.write_to(&mut thumbnail_bytes, image::ImageOutputFormat::Png)?;
// Save thumbnail in database so we don't have to generate it again next time // Save thumbnail in database so we don't have to generate it again next time
let thumbnail_key = self.db.create_file_metadata(mxc, width, height, content_disposition.as_deref(), content_type.as_deref())?; let thumbnail_key = self.db.create_file_metadata(
mxc,
width,
height,
content_disposition.as_deref(),
content_type.as_deref(),
)?;
let path = services().globals.get_media_file(&thumbnail_key); let path = services().globals.get_media_file(&thumbnail_key);
let mut f = File::create(path).await?; let mut f = File::create(path).await?;

@ -5,7 +5,7 @@ use std::{
use lru_cache::LruCache; use lru_cache::LruCache;
use crate::{Result, Config}; use crate::{Config, Result};
pub mod account_data; pub mod account_data;
pub mod admin; pub mod admin;
@ -49,7 +49,8 @@ impl Services {
+ key_backups::Data + key_backups::Data
+ media::Data, + media::Data,
>( >(
db: Arc<D>, config: Config db: Arc<D>,
config: Config,
) -> Result<Self> { ) -> Result<Self> {
Ok(Self { Ok(Self {
appservice: appservice::Service { db: db.clone() }, appservice: appservice::Service { db: db.clone() },
@ -76,30 +77,26 @@ impl Services {
state: rooms::state::Service { db: db.clone() }, state: rooms::state::Service { db: db.clone() },
state_accessor: rooms::state_accessor::Service { db: db.clone() }, state_accessor: rooms::state_accessor::Service { db: db.clone() },
state_cache: rooms::state_cache::Service { db: db.clone() }, state_cache: rooms::state_cache::Service { db: db.clone() },
state_compressor: rooms::state_compressor::Service { db: db.clone(), stateinfo_cache: Mutex::new(LruCache::new((100.0 * config.conduit_cache_capacity_modifier) as usize,)) }, state_compressor: rooms::state_compressor::Service {
timeline: rooms::timeline::Service { db: db.clone(), lasttimelinecount_cache: Mutex::new(HashMap::new()) }, db: db.clone(),
user: rooms::user::Service { db: db.clone() }, stateinfo_cache: Mutex::new(LruCache::new(
}, (100.0 * config.conduit_cache_capacity_modifier) as usize,
transaction_ids: transaction_ids::Service { )),
db: db.clone()
},
uiaa: uiaa::Service {
db: db.clone()
}, },
users: users::Service { timeline: rooms::timeline::Service {
db: db.clone() db: db.clone(),
lasttimelinecount_cache: Mutex::new(HashMap::new()),
}, },
account_data: account_data::Service { user: rooms::user::Service { db: db.clone() },
db: db.clone()
}, },
transaction_ids: transaction_ids::Service { db: db.clone() },
uiaa: uiaa::Service { db: db.clone() },
users: users::Service { db: db.clone() },
account_data: account_data::Service { db: db.clone() },
admin: admin::Service { sender: todo!() }, admin: admin::Service { sender: todo!() },
globals: globals::Service::load(db.clone(), config)?, globals: globals::Service::load(db.clone(), config)?,
key_backups: key_backups::Service { key_backups: key_backups::Service { db: db.clone() },
db: db.clone() media: media::Service { db: db.clone() },
},
media: media::Service {
db: db.clone()
},
sending: sending::Service { sending: sending::Service {
maximum_requests: todo!(), maximum_requests: todo!(),
sender: todo!(), sender: todo!(),

@ -1,4 +1,4 @@
use crate::{Error, services}; use crate::{services, Error};
use ruma::{ use ruma::{
events::{ events::{
room::member::RoomMemberEventContent, AnyEphemeralRoomEvent, AnyRoomEvent, AnyStateEvent, room::member::RoomMemberEventContent, AnyEphemeralRoomEvent, AnyRoomEvent, AnyStateEvent,

@ -1,5 +1,8 @@
use ruma::{UserId, api::client::push::{set_pusher, get_pushers}};
use crate::Result; use crate::Result;
use ruma::{
api::client::push::{get_pushers, set_pusher},
UserId,
};
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
fn set_pusher(&self, sender: &UserId, pusher: set_pusher::v3::Pusher) -> Result<()>; fn set_pusher(&self, sender: &UserId, pusher: set_pusher::v3::Pusher) -> Result<()>;
@ -8,8 +11,5 @@ pub trait Data: Send + Sync {
fn get_pushers(&self, sender: &UserId) -> Result<Vec<get_pushers::v3::Pusher>>; fn get_pushers(&self, sender: &UserId) -> Result<Vec<get_pushers::v3::Pusher>>;
fn get_pusher_senderkeys<'a>( fn get_pusher_senderkeys<'a>(&'a self, sender: &UserId) -> Box<dyn Iterator<Item = Vec<u8>>>;
&'a self,
sender: &UserId,
) -> Box<dyn Iterator<Item = Vec<u8>>>;
} }

@ -79,7 +79,11 @@ impl Service {
//*reqwest_request.timeout_mut() = Some(Duration::from_secs(5)); //*reqwest_request.timeout_mut() = Some(Duration::from_secs(5));
let url = reqwest_request.url().clone(); let url = reqwest_request.url().clone();
let response = services().globals.default_client().execute(reqwest_request).await; let response = services()
.globals
.default_client()
.execute(reqwest_request)
.await;
match response { match response {
Ok(mut response) => { Ok(mut response) => {
@ -196,7 +200,8 @@ impl Service {
let ctx = PushConditionRoomCtx { let ctx = PushConditionRoomCtx {
room_id: room_id.to_owned(), room_id: room_id.to_owned(),
member_count: 10_u32.into(), // TODO: get member count efficiently member_count: 10_u32.into(), // TODO: get member count efficiently
user_display_name: services().users user_display_name: services()
.users
.displayname(user)? .displayname(user)?
.unwrap_or_else(|| user.localpart().to_owned()), .unwrap_or_else(|| user.localpart().to_owned()),
users_power_levels: power_levels.users.clone(), users_power_levels: power_levels.users.clone(),
@ -276,8 +281,8 @@ impl Service {
let user_name = services().users.displayname(&event.sender)?; let user_name = services().users.displayname(&event.sender)?;
notifi.sender_display_name = user_name.as_deref(); notifi.sender_display_name = user_name.as_deref();
let room_name = if let Some(room_name_pdu) = let room_name = if let Some(room_name_pdu) = services()
services().rooms .rooms
.state_accessor .state_accessor
.room_state_get(&event.room_id, &StateEventType::RoomName, "")? .room_state_get(&event.room_id, &StateEventType::RoomName, "")?
{ {
@ -290,10 +295,7 @@ impl Service {
notifi.room_name = room_name.as_deref(); notifi.room_name = room_name.as_deref();
self.send_request( self.send_request(url, send_event_notification::v1::Request::new(notifi))
url,
send_event_notification::v1::Request::new(notifi),
)
.await?; .await?;
} }

@ -1,25 +1,15 @@
use ruma::{RoomId, RoomAliasId};
use crate::Result; use crate::Result;
use ruma::{RoomAliasId, RoomId};
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
/// Creates or updates the alias to the given room id. /// Creates or updates the alias to the given room id.
fn set_alias( fn set_alias(&self, alias: &RoomAliasId, room_id: &RoomId) -> Result<()>;
&self,
alias: &RoomAliasId,
room_id: &RoomId
) -> Result<()>;
/// Forgets about an alias. Returns an error if the alias did not exist. /// Forgets about an alias. Returns an error if the alias did not exist.
fn remove_alias( fn remove_alias(&self, alias: &RoomAliasId) -> Result<()>;
&self,
alias: &RoomAliasId,
) -> Result<()>;
/// Looks up the roomid for the given alias. /// Looks up the roomid for the given alias.
fn resolve_local_alias( fn resolve_local_alias(&self, alias: &RoomAliasId) -> Result<Option<Box<RoomId>>>;
&self,
alias: &RoomAliasId,
) -> Result<Option<Box<RoomId>>>;
/// Returns all local aliases that point to the given room /// Returns all local aliases that point to the given room
fn local_aliases_for_room( fn local_aliases_for_room(

@ -3,8 +3,8 @@ use std::sync::Arc;
pub use data::Data; pub use data::Data;
use ruma::{RoomAliasId, RoomId};
use crate::Result; use crate::Result;
use ruma::{RoomAliasId, RoomId};
pub struct Service { pub struct Service {
db: Arc<dyn Data>, db: Arc<dyn Data>,
@ -12,19 +12,12 @@ pub struct Service {
impl Service { impl Service {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn set_alias( pub fn set_alias(&self, alias: &RoomAliasId, room_id: &RoomId) -> Result<()> {
&self,
alias: &RoomAliasId,
room_id: &RoomId,
) -> Result<()> {
self.db.set_alias(alias, room_id) self.db.set_alias(alias, room_id)
} }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn remove_alias( pub fn remove_alias(&self, alias: &RoomAliasId) -> Result<()> {
&self,
alias: &RoomAliasId,
) -> Result<()> {
self.db.remove_alias(alias) self.db.remove_alias(alias)
} }

@ -1,7 +1,11 @@
use std::{collections::HashSet, sync::Arc};
use crate::Result; use crate::Result;
use std::{collections::HashSet, sync::Arc};
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
fn get_cached_eventid_authchain(&self, shorteventid: &[u64]) -> Result<Option<Arc<HashSet<u64>>>>; fn get_cached_eventid_authchain(
fn cache_auth_chain(&self, shorteventid: Vec<u64>, auth_chain: Arc<HashSet<u64>>) -> Result<()>; &self,
shorteventid: &[u64],
) -> Result<Option<Arc<HashSet<u64>>>>;
fn cache_auth_chain(&self, shorteventid: Vec<u64>, auth_chain: Arc<HashSet<u64>>)
-> Result<()>;
} }

@ -1,11 +1,14 @@
mod data; mod data;
use std::{sync::Arc, collections::{HashSet, BTreeSet}}; use std::{
collections::{BTreeSet, HashSet},
sync::Arc,
};
pub use data::Data; pub use data::Data;
use ruma::{RoomId, EventId, api::client::error::ErrorKind}; use ruma::{api::client::error::ErrorKind, EventId, RoomId};
use tracing::log::warn; use tracing::log::warn;
use crate::{Result, services, Error}; use crate::{services, Error, Result};
pub struct Service { pub struct Service {
db: Arc<dyn Data>, db: Arc<dyn Data>,
@ -56,7 +59,11 @@ impl Service {
} }
let chunk_key: Vec<u64> = chunk.iter().map(|(short, _)| short).copied().collect(); let chunk_key: Vec<u64> = chunk.iter().map(|(short, _)| short).copied().collect();
if let Some(cached) = services().rooms.auth_chain.get_cached_eventid_authchain(&chunk_key)? { if let Some(cached) = services()
.rooms
.auth_chain
.get_cached_eventid_authchain(&chunk_key)?
{
hits += 1; hits += 1;
full_auth_chain.extend(cached.iter().copied()); full_auth_chain.extend(cached.iter().copied());
continue; continue;
@ -68,13 +75,18 @@ impl Service {
let mut misses2 = 0; let mut misses2 = 0;
let mut i = 0; let mut i = 0;
for (sevent_id, event_id) in chunk { for (sevent_id, event_id) in chunk {
if let Some(cached) = services().rooms.auth_chain.get_cached_eventid_authchain(&[sevent_id])? { if let Some(cached) = services()
.rooms
.auth_chain
.get_cached_eventid_authchain(&[sevent_id])?
{
hits2 += 1; hits2 += 1;
chunk_cache.extend(cached.iter().copied()); chunk_cache.extend(cached.iter().copied());
} else { } else {
misses2 += 1; misses2 += 1;
let auth_chain = Arc::new(self.get_auth_chain_inner(room_id, &event_id)?); let auth_chain = Arc::new(self.get_auth_chain_inner(room_id, &event_id)?);
services().rooms services()
.rooms
.auth_chain .auth_chain
.cache_auth_chain(vec![sevent_id], Arc::clone(&auth_chain))?; .cache_auth_chain(vec![sevent_id], Arc::clone(&auth_chain))?;
println!( println!(
@ -97,8 +109,10 @@ impl Service {
misses2 misses2
); );
let chunk_cache = Arc::new(chunk_cache); let chunk_cache = Arc::new(chunk_cache);
services().rooms services()
.auth_chain.cache_auth_chain(chunk_key, Arc::clone(&chunk_cache))?; .rooms
.auth_chain
.cache_auth_chain(chunk_key, Arc::clone(&chunk_cache))?;
full_auth_chain.extend(chunk_cache.iter()); full_auth_chain.extend(chunk_cache.iter());
} }
@ -115,11 +129,7 @@ impl Service {
} }
#[tracing::instrument(skip(self, event_id))] #[tracing::instrument(skip(self, event_id))]
fn get_auth_chain_inner( fn get_auth_chain_inner(&self, room_id: &RoomId, event_id: &EventId) -> Result<HashSet<u64>> {
&self,
room_id: &RoomId,
event_id: &EventId,
) -> Result<HashSet<u64>> {
let mut todo = vec![Arc::from(event_id)]; let mut todo = vec![Arc::from(event_id)];
let mut found = HashSet::new(); let mut found = HashSet::new();
@ -131,7 +141,8 @@ impl Service {
} }
for auth_event in &pdu.auth_events { for auth_event in &pdu.auth_events {
let sauthevent = services() let sauthevent = services()
.rooms.short .rooms
.short
.get_or_create_shorteventid(auth_event)?; .get_or_create_shorteventid(auth_event)?;
if !found.contains(&sauthevent) { if !found.contains(&sauthevent) {

@ -1,5 +1,5 @@
use ruma::RoomId;
use crate::Result; use crate::Result;
use ruma::RoomId;
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
/// Adds the room to the public room directory /// Adds the room to the public room directory

@ -1,7 +1,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use ruma::{UserId, RoomId, events::presence::PresenceEvent};
use crate::Result; use crate::Result;
use ruma::{events::presence::PresenceEvent, RoomId, UserId};
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
/// Adds a presence event which will be saved until a new event replaces it. /// Adds a presence event which will be saved until a new event replaces it.

@ -2,7 +2,7 @@ mod data;
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
pub use data::Data; pub use data::Data;
use ruma::{RoomId, UserId, events::presence::PresenceEvent}; use ruma::{events::presence::PresenceEvent, RoomId, UserId};
use crate::Result; use crate::Result;

@ -1,5 +1,5 @@
use ruma::{RoomId, events::receipt::ReceiptEvent, UserId, serde::Raw};
use crate::Result; use crate::Result;
use ruma::{events::receipt::ReceiptEvent, serde::Raw, RoomId, UserId};
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
/// Replaces the previous read receipt. /// Replaces the previous read receipt.
@ -15,13 +15,15 @@ pub trait Data: Send + Sync {
&self, &self,
room_id: &RoomId, room_id: &RoomId,
since: u64, since: u64,
) -> Box<dyn Iterator< ) -> Box<
dyn Iterator<
Item = Result<( Item = Result<(
Box<UserId>, Box<UserId>,
u64, u64,
Raw<ruma::events::AnySyncEphemeralRoomEvent>, Raw<ruma::events::AnySyncEphemeralRoomEvent>,
)>, )>,
>>; >,
>;
/// Sets a private read marker at `count`. /// Sets a private read marker at `count`.
fn private_read_set(&self, room_id: &RoomId, user_id: &UserId, count: u64) -> Result<()>; fn private_read_set(&self, room_id: &RoomId, user_id: &UserId, count: u64) -> Result<()>;

@ -3,8 +3,8 @@ use std::sync::Arc;
pub use data::Data; pub use data::Data;
use ruma::{RoomId, UserId, events::receipt::ReceiptEvent, serde::Raw};
use crate::Result; use crate::Result;
use ruma::{events::receipt::ReceiptEvent, serde::Raw, RoomId, UserId};
pub struct Service { pub struct Service {
db: Arc<dyn Data>, db: Arc<dyn Data>,

@ -1,6 +1,6 @@
use std::collections::HashSet;
use crate::Result; use crate::Result;
use ruma::{UserId, RoomId}; use ruma::{RoomId, UserId};
use std::collections::HashSet;
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
/// Sets a user as typing until the timeout timestamp is reached or roomtyping_remove is /// Sets a user as typing until the timeout timestamp is reached or roomtyping_remove is

@ -2,7 +2,7 @@ mod data;
use std::sync::Arc; use std::sync::Arc;
pub use data::Data; pub use data::Data;
use ruma::{UserId, RoomId, events::SyncEphemeralRoomEvent}; use ruma::{events::SyncEphemeralRoomEvent, RoomId, UserId};
use crate::Result; use crate::Result;

@ -1,22 +1,33 @@
/// An async function that can recursively call itself. /// An async function that can recursively call itself.
type AsyncRecursiveType<'a, T> = Pin<Box<dyn Future<Output = T> + 'a + Send>>; type AsyncRecursiveType<'a, T> = Pin<Box<dyn Future<Output = T> + 'a + Send>>;
use ruma::{RoomVersionId, signatures::CanonicalJsonObject, api::federation::discovery::{get_server_keys, get_remote_server_keys}}; use ruma::{
use tokio::sync::Semaphore; api::federation::discovery::{get_remote_server_keys, get_server_keys},
signatures::CanonicalJsonObject,
RoomVersionId,
};
use std::{ use std::{
collections::{btree_map, hash_map, BTreeMap, HashMap, HashSet}, collections::{btree_map, hash_map, BTreeMap, HashMap, HashSet},
pin::Pin, pin::Pin,
sync::{Arc, RwLock, RwLockWriteGuard}, sync::{Arc, RwLock, RwLockWriteGuard},
time::{Duration, Instant, SystemTime}, time::{Duration, Instant, SystemTime},
}; };
use tokio::sync::Semaphore;
use futures_util::{Future, stream::FuturesUnordered, StreamExt}; use futures_util::{stream::FuturesUnordered, Future, StreamExt};
use ruma::{ use ruma::{
api::{ api::{
client::error::ErrorKind, client::error::ErrorKind,
federation::{event::{get_event, get_room_state_ids}, membership::create_join_event, discovery::get_remote_server_keys_batch::{v2::QueryCriteria, self}}, federation::{
discovery::get_remote_server_keys_batch::{self, v2::QueryCriteria},
event::{get_event, get_room_state_ids},
membership::create_join_event,
},
},
events::{
room::{create::RoomCreateEventContent, server_acl::RoomServerAclEventContent},
StateEventType,
}, },
events::{room::{create::RoomCreateEventContent, server_acl::RoomServerAclEventContent}, StateEventType},
int, int,
serde::Base64, serde::Base64,
signatures::CanonicalJsonValue, signatures::CanonicalJsonValue,
@ -24,9 +35,9 @@ use ruma::{
uint, EventId, MilliSecondsSinceUnixEpoch, RoomId, ServerName, ServerSigningKeyId, uint, EventId, MilliSecondsSinceUnixEpoch, RoomId, ServerName, ServerSigningKeyId,
}; };
use serde_json::value::{to_raw_value, RawValue as RawJsonValue}; use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
use tracing::{error, info, trace, warn, debug}; use tracing::{debug, error, info, trace, warn};
use crate::{service::*, services, Result, Error, PduEvent}; use crate::{service::*, services, Error, PduEvent, Result};
pub struct Service; pub struct Service;
@ -72,10 +83,7 @@ impl Service {
)); ));
} }
if services() if services().rooms.metadata.is_disabled(room_id)? {
.rooms
.metadata
.is_disabled(room_id)? {
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"Federation of this room is currently disabled on this server.", "Federation of this room is currently disabled on this server.",
@ -94,7 +102,8 @@ impl Service {
.ok_or_else(|| Error::bad_database("Failed to find create event in db."))?; .ok_or_else(|| Error::bad_database("Failed to find create event in db."))?;
let first_pdu_in_room = services() let first_pdu_in_room = services()
.rooms.timeline .rooms
.timeline
.first_pdu_in_room(room_id)? .first_pdu_in_room(room_id)?
.ok_or_else(|| Error::bad_database("Failed to find first pdu in db."))?; .ok_or_else(|| Error::bad_database("Failed to find first pdu in db."))?;
@ -113,21 +122,20 @@ impl Service {
} }
// 9. Fetch any missing prev events doing all checks listed here starting at 1. These are timeline events // 9. Fetch any missing prev events doing all checks listed here starting at 1. These are timeline events
let (sorted_prev_events, mut eventid_info) = self.fetch_unknown_prev_events( let (sorted_prev_events, mut eventid_info) = self
.fetch_unknown_prev_events(
origin, origin,
&create_event, &create_event,
room_id, room_id,
pub_key_map, pub_key_map,
incoming_pdu.prev_events.clone(), incoming_pdu.prev_events.clone(),
).await?; )
.await?;
let mut errors = 0; let mut errors = 0;
for prev_id in dbg!(sorted_prev_events) { for prev_id in dbg!(sorted_prev_events) {
// Check for disabled again because it might have changed // Check for disabled again because it might have changed
if services() if services().rooms.metadata.is_disabled(room_id)? {
.rooms
.metadata
.is_disabled(room_id)? {
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::Forbidden, ErrorKind::Forbidden,
"Federation of this room is currently disabled on this server.", "Federation of this room is currently disabled on this server.",
@ -224,7 +232,10 @@ impl Service {
.write() .write()
.unwrap() .unwrap()
.insert(room_id.to_owned(), (event_id.to_owned(), start_time)); .insert(room_id.to_owned(), (event_id.to_owned(), start_time));
let r = services().rooms.event_handler.upgrade_outlier_to_timeline_pdu( let r = services()
.rooms
.event_handler
.upgrade_outlier_to_timeline_pdu(
incoming_pdu, incoming_pdu,
val, val,
&create_event, &create_event,
@ -252,8 +263,7 @@ impl Service {
room_id: &'a RoomId, room_id: &'a RoomId,
value: BTreeMap<String, CanonicalJsonValue>, value: BTreeMap<String, CanonicalJsonValue>,
pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, Base64>>>, pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, Base64>>>,
) -> AsyncRecursiveType<'a, Result<(Arc<PduEvent>, BTreeMap<String, CanonicalJsonValue>)>> ) -> AsyncRecursiveType<'a, Result<(Arc<PduEvent>, BTreeMap<String, CanonicalJsonValue>)>> {
{
Box::pin(async move { Box::pin(async move {
// TODO: For RoomVersion6 we must check that Raw<..> is canonical do we anywhere?: https://matrix.org/docs/spec/rooms/v6#canonical-json // TODO: For RoomVersion6 we must check that Raw<..> is canonical do we anywhere?: https://matrix.org/docs/spec/rooms/v6#canonical-json
@ -282,14 +292,22 @@ impl Service {
Err(e) => { Err(e) => {
// Drop // Drop
warn!("Dropping bad event {}: {}", event_id, e); warn!("Dropping bad event {}: {}", event_id, e);
return Err(Error::BadRequest(ErrorKind::InvalidParam, "Signature verification failed")); return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Signature verification failed",
));
} }
Ok(ruma::signatures::Verified::Signatures) => { Ok(ruma::signatures::Verified::Signatures) => {
// Redact // Redact
warn!("Calculated hash does not match: {}", event_id); warn!("Calculated hash does not match: {}", event_id);
match ruma::signatures::redact(&value, room_version_id) { match ruma::signatures::redact(&value, room_version_id) {
Ok(obj) => obj, Ok(obj) => obj,
Err(_) => return Err(Error::BadRequest(ErrorKind::InvalidParam, "Redaction failed")), Err(_) => {
return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Redaction failed",
))
}
} }
} }
Ok(ruma::signatures::Verified::All) => value, Ok(ruma::signatures::Verified::All) => value,
@ -376,7 +394,8 @@ impl Service {
&incoming_pdu, &incoming_pdu,
None::<PduEvent>, // TODO: third party invite None::<PduEvent>, // TODO: third party invite
|k, s| auth_events.get(&(k.to_string().into(), s.to_owned())), |k, s| auth_events.get(&(k.to_string().into(), s.to_owned())),
).map_err(|_e| Error::BadRequest(ErrorKind::InvalidParam, "Auth check failed"))? )
.map_err(|_e| Error::BadRequest(ErrorKind::InvalidParam, "Auth check failed"))?
{ {
return Err(Error::BadRequest( return Err(Error::BadRequest(
ErrorKind::InvalidParam, ErrorKind::InvalidParam,
@ -415,9 +434,13 @@ impl Service {
if services() if services()
.rooms .rooms
.pdu_metadata.is_event_soft_failed(&incoming_pdu.event_id)? .pdu_metadata
.is_event_soft_failed(&incoming_pdu.event_id)?
{ {
return Err(Error::BadRequest(ErrorKind::InvalidParam, "Event has been soft failed")); return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Event has been soft failed",
));
} }
info!("Upgrading {} to timeline pdu", incoming_pdu.event_id); info!("Upgrading {} to timeline pdu", incoming_pdu.event_id);
@ -448,7 +471,13 @@ impl Service {
.pdu_shortstatehash(prev_event)?; .pdu_shortstatehash(prev_event)?;
let state = if let Some(shortstatehash) = prev_event_sstatehash { let state = if let Some(shortstatehash) = prev_event_sstatehash {
Some(services().rooms.state_accessor.state_full_ids(shortstatehash).await) Some(
services()
.rooms
.state_accessor
.state_full_ids(shortstatehash)
.await,
)
} else { } else {
None None
}; };
@ -466,10 +495,10 @@ impl Service {
})?; })?;
if let Some(state_key) = &prev_pdu.state_key { if let Some(state_key) = &prev_pdu.state_key {
let shortstatekey = services() let shortstatekey = services().rooms.short.get_or_create_shortstatekey(
.rooms &prev_pdu.kind.to_string().into(),
.short state_key,
.get_or_create_shortstatekey(&prev_pdu.kind.to_string().into(), state_key)?; )?;
state.insert(shortstatekey, Arc::from(prev_event)); state.insert(shortstatekey, Arc::from(prev_event));
// Now it's the state after the pdu // Now it's the state after the pdu
@ -483,15 +512,19 @@ impl Service {
let mut okay = true; let mut okay = true;
for prev_eventid in &incoming_pdu.prev_events { for prev_eventid in &incoming_pdu.prev_events {
let prev_event = if let Ok(Some(pdu)) = services().rooms.timeline.get_pdu(prev_eventid) { let prev_event =
if let Ok(Some(pdu)) = services().rooms.timeline.get_pdu(prev_eventid) {
pdu pdu
} else { } else {
okay = false; okay = false;
break; break;
}; };
let sstatehash = let sstatehash = if let Ok(Some(s)) = services()
if let Ok(Some(s)) = services().rooms.state_accessor.pdu_shortstatehash(prev_eventid) { .rooms
.state_accessor
.pdu_shortstatehash(prev_eventid)
{
s s
} else { } else {
okay = false; okay = false;
@ -513,10 +546,7 @@ impl Service {
.await?; .await?;
if let Some(state_key) = &prev_event.state_key { if let Some(state_key) = &prev_event.state_key {
let shortstatekey = services() let shortstatekey = services().rooms.short.get_or_create_shortstatekey(
.rooms
.short
.get_or_create_shortstatekey(
&prev_event.kind.to_string().into(), &prev_event.kind.to_string().into(),
state_key, state_key,
)?; )?;
@ -528,7 +558,8 @@ impl Service {
let mut starting_events = Vec::with_capacity(leaf_state.len()); let mut starting_events = Vec::with_capacity(leaf_state.len());
for (k, id) in leaf_state { for (k, id) in leaf_state {
if let Ok((ty, st_key)) = services().rooms.short.get_statekey_from_short(k) { if let Ok((ty, st_key)) = services().rooms.short.get_statekey_from_short(k)
{
// FIXME: Undo .to_string().into() when StateMap // FIXME: Undo .to_string().into() when StateMap
// is updated to use StateEventType // is updated to use StateEventType
state.insert((ty.to_string().into(), st_key), id.clone()); state.insert((ty.to_string().into(), st_key), id.clone());
@ -567,10 +598,8 @@ impl Service {
new_state new_state
.into_iter() .into_iter()
.map(|((event_type, state_key), event_id)| { .map(|((event_type, state_key), event_id)| {
let shortstatekey = services() let shortstatekey =
.rooms services().rooms.short.get_or_create_shortstatekey(
.short
.get_or_create_shortstatekey(
&event_type.to_string().into(), &event_type.to_string().into(),
&state_key, &state_key,
)?; )?;
@ -618,15 +647,14 @@ impl Service {
let mut state: BTreeMap<_, Arc<EventId>> = BTreeMap::new(); let mut state: BTreeMap<_, Arc<EventId>> = BTreeMap::new();
for (pdu, _) in state_vec { for (pdu, _) in state_vec {
let state_key = pdu let state_key = pdu.state_key.clone().ok_or_else(|| {
.state_key Error::bad_database("Found non-state pdu in state events.")
.clone() })?;
.ok_or_else(|| Error::bad_database("Found non-state pdu in state events."))?;
let shortstatekey = services() let shortstatekey = services().rooms.short.get_or_create_shortstatekey(
.rooms &pdu.kind.to_string().into(),
.short &state_key,
.get_or_create_shortstatekey(&pdu.kind.to_string().into(), &state_key)?; )?;
match state.entry(shortstatekey) { match state.entry(shortstatekey) {
btree_map::Entry::Vacant(v) => { btree_map::Entry::Vacant(v) => {
@ -648,7 +676,9 @@ impl Service {
if state.get(&create_shortstatekey).map(|id| id.as_ref()) if state.get(&create_shortstatekey).map(|id| id.as_ref())
!= Some(&create_event.event_id) != Some(&create_event.event_id)
{ {
return Err(Error::bad_database("Incoming event refers to wrong create event.")); return Err(Error::bad_database(
"Incoming event refers to wrong create event.",
));
} }
state_at_incoming_event = Some(state); state_at_incoming_event = Some(state);
@ -683,7 +713,9 @@ impl Service {
.map_err(|_e| Error::BadRequest(ErrorKind::InvalidParam, "Auth check failed."))?; .map_err(|_e| Error::BadRequest(ErrorKind::InvalidParam, "Auth check failed."))?;
if !check_result { if !check_result {
return Err(Error::bad_database("Event has failed auth check with state at the event.")); return Err(Error::bad_database(
"Event has failed auth check with state at the event.",
));
} }
info!("Auth check succeeded"); info!("Auth check succeeded");
@ -703,10 +735,7 @@ impl Service {
// Now we calculate the set of extremities this room has after the incoming event has been // Now we calculate the set of extremities this room has after the incoming event has been
// applied. We start with the previous extremities (aka leaves) // applied. We start with the previous extremities (aka leaves)
info!("Calculating extremities"); info!("Calculating extremities");
let mut extremities = services() let mut extremities = services().rooms.state.get_forward_extremities(room_id)?;
.rooms
.state
.get_forward_extremities(room_id)?;
// Remove any forward extremities that are referenced by this incoming event's prev_events // Remove any forward extremities that are referenced by this incoming event's prev_events
for prev_event in &incoming_pdu.prev_events { for prev_event in &incoming_pdu.prev_events {
@ -716,8 +745,15 @@ impl Service {
} }
// Only keep those extremities were not referenced yet // Only keep those extremities were not referenced yet
extremities extremities.retain(|id| {
.retain(|id| !matches!(services().rooms.pdu_metadata.is_event_referenced(room_id, id), Ok(true))); !matches!(
services()
.rooms
.pdu_metadata
.is_event_referenced(room_id, id),
Ok(true)
)
});
info!("Compressing state at event"); info!("Compressing state at event");
let state_ids_compressed = state_at_incoming_event let state_ids_compressed = state_at_incoming_event
@ -733,10 +769,7 @@ impl Service {
// 13. Check if the event passes auth based on the "current state" of the room, if not "soft fail" it // 13. Check if the event passes auth based on the "current state" of the room, if not "soft fail" it
info!("Starting soft fail auth check"); info!("Starting soft fail auth check");
let auth_events = services() let auth_events = services().rooms.state.get_auth_events(
.rooms
.state
.get_auth_events(
room_id, room_id,
&incoming_pdu.kind, &incoming_pdu.kind,
&incoming_pdu.sender, &incoming_pdu.sender,
@ -749,7 +782,8 @@ impl Service {
&incoming_pdu, &incoming_pdu,
None::<PduEvent>, None::<PduEvent>,
|k, s| auth_events.get(&(k.clone(), s.to_owned())), |k, s| auth_events.get(&(k.clone(), s.to_owned())),
).map_err(|_e| Error::BadRequest(ErrorKind::InvalidParam, "Auth check failed."))?; )
.map_err(|_e| Error::BadRequest(ErrorKind::InvalidParam, "Auth check failed."))?;
if soft_fail { if soft_fail {
services().rooms.timeline.append_incoming_pdu( services().rooms.timeline.append_incoming_pdu(
@ -767,7 +801,10 @@ impl Service {
.rooms .rooms
.pdu_metadata .pdu_metadata
.mark_event_soft_failed(&incoming_pdu.event_id)?; .mark_event_soft_failed(&incoming_pdu.event_id)?;
return Err(Error::BadRequest(ErrorKind::InvalidParam, "Event has been soft failed")); return Err(Error::BadRequest(
ErrorKind::InvalidParam,
"Event has been soft failed",
));
} }
if incoming_pdu.state_key.is_some() { if incoming_pdu.state_key.is_some() {
@ -789,15 +826,12 @@ impl Service {
info!("Loading extremities"); info!("Loading extremities");
for id in dbg!(&extremities) { for id in dbg!(&extremities) {
match services() match services().rooms.timeline.get_pdu(id)? {
.rooms
.timeline
.get_pdu(id)?
{
Some(leaf_pdu) => { Some(leaf_pdu) => {
extremity_sstatehashes.insert( extremity_sstatehashes.insert(
services() services()
.rooms.state_accessor .rooms
.state_accessor
.pdu_shortstatehash(&leaf_pdu.event_id)? .pdu_shortstatehash(&leaf_pdu.event_id)?
.ok_or_else(|| { .ok_or_else(|| {
error!( error!(
@ -829,10 +863,10 @@ impl Service {
// We also add state after incoming event to the fork states // We also add state after incoming event to the fork states
let mut state_after = state_at_incoming_event.clone(); let mut state_after = state_at_incoming_event.clone();
if let Some(state_key) = &incoming_pdu.state_key { if let Some(state_key) = &incoming_pdu.state_key {
let shortstatekey = services() let shortstatekey = services().rooms.short.get_or_create_shortstatekey(
.rooms &incoming_pdu.kind.to_string().into(),
.short state_key,
.get_or_create_shortstatekey(&incoming_pdu.kind.to_string().into(), state_key)?; )?;
state_after.insert(shortstatekey, Arc::from(&*incoming_pdu.event_id)); state_after.insert(shortstatekey, Arc::from(&*incoming_pdu.event_id));
} }
@ -921,10 +955,10 @@ impl Service {
state state
.into_iter() .into_iter()
.map(|((event_type, state_key), event_id)| { .map(|((event_type, state_key), event_id)| {
let shortstatekey = services() let shortstatekey = services().rooms.short.get_or_create_shortstatekey(
.rooms &event_type.to_string().into(),
.short &state_key,
.get_or_create_shortstatekey(&event_type.to_string().into(), &state_key)?; )?;
services() services()
.rooms .rooms
.state_compressor .state_compressor
@ -936,7 +970,10 @@ impl Service {
// Set the new room state to the resolved state // Set the new room state to the resolved state
if update_state { if update_state {
info!("Forcing new room state"); info!("Forcing new room state");
let sstatehash = services().rooms.state_compressor.save_state(room_id, new_room_state)?; let sstatehash = services()
.rooms
.state_compressor
.save_state(room_id, new_room_state)?;
services() services()
.rooms .rooms
.state .state
@ -951,8 +988,7 @@ impl Service {
// We use the `state_at_event` instead of `state_after` so we accurately // We use the `state_at_event` instead of `state_after` so we accurately
// represent the state for this event. // represent the state for this event.
let pdu_id = services().rooms.timeline let pdu_id = services().rooms.timeline.append_incoming_pdu(
.append_incoming_pdu(
&incoming_pdu, &incoming_pdu,
val, val,
extremities.iter().map(|e| (**e).to_owned()).collect(), extremities.iter().map(|e| (**e).to_owned()).collect(),
@ -1141,8 +1177,10 @@ impl Service {
room_id: &RoomId, room_id: &RoomId,
pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, Base64>>>, pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, Base64>>>,
initial_set: Vec<Arc<EventId>>, initial_set: Vec<Arc<EventId>>,
) -> Result<(Vec<Arc<EventId>>, HashMap<Arc<EventId>, ) -> Result<(
(Arc<PduEvent>, BTreeMap<String, CanonicalJsonValue>)>)> { Vec<Arc<EventId>>,
HashMap<Arc<EventId>, (Arc<PduEvent>, BTreeMap<String, CanonicalJsonValue>)>,
)> {
let mut graph: HashMap<Arc<EventId>, _> = HashMap::new(); let mut graph: HashMap<Arc<EventId>, _> = HashMap::new();
let mut eventid_info = HashMap::new(); let mut eventid_info = HashMap::new();
let mut todo_outlier_stack: Vec<Arc<EventId>> = initial_set; let mut todo_outlier_stack: Vec<Arc<EventId>> = initial_set;
@ -1223,7 +1261,8 @@ impl Service {
.map_or_else(|| uint!(0), |info| info.0.origin_server_ts), .map_or_else(|| uint!(0), |info| info.0.origin_server_ts),
), ),
)) ))
}).map_err(|_| Error::bad_database("Error sorting prev events"))?; })
.map_err(|_| Error::bad_database("Error sorting prev events"))?;
Ok((sorted, eventid_info)) Ok((sorted, eventid_info))
} }
@ -1253,9 +1292,12 @@ impl Service {
let signature_ids = signature_object.keys().cloned().collect::<Vec<_>>(); let signature_ids = signature_object.keys().cloned().collect::<Vec<_>>();
let fetch_res = self.fetch_signing_keys( let fetch_res = self
.fetch_signing_keys(
signature_server.as_str().try_into().map_err(|_| { signature_server.as_str().try_into().map_err(|_| {
Error::BadServerResponse("Invalid servername in signatures of server response pdu.") Error::BadServerResponse(
"Invalid servername in signatures of server response pdu.",
)
})?, })?,
signature_ids, signature_ids,
) )
@ -1336,8 +1378,9 @@ impl Service {
let signature_ids = signature_object.keys().cloned().collect::<Vec<_>>(); let signature_ids = signature_object.keys().cloned().collect::<Vec<_>>();
let contains_all_ids = let contains_all_ids = |keys: &BTreeMap<String, Base64>| {
|keys: &BTreeMap<String, Base64>| signature_ids.iter().all(|id| keys.contains_key(id)); signature_ids.iter().all(|id| keys.contains_key(id))
};
let origin = <&ServerName>::try_from(signature_server.as_str()).map_err(|_| { let origin = <&ServerName>::try_from(signature_server.as_str()).map_err(|_| {
Error::BadServerResponse("Invalid servername in signatures of server response pdu.") Error::BadServerResponse("Invalid servername in signatures of server response pdu.")
@ -1373,8 +1416,10 @@ impl Service {
room_version: &RoomVersionId, room_version: &RoomVersionId,
pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, Base64>>>, pub_key_map: &RwLock<BTreeMap<String, BTreeMap<String, Base64>>>,
) -> Result<()> { ) -> Result<()> {
let mut servers: BTreeMap<Box<ServerName>, BTreeMap<Box<ServerSigningKeyId>, QueryCriteria>> = let mut servers: BTreeMap<
BTreeMap::new(); Box<ServerName>,
BTreeMap<Box<ServerSigningKeyId>, QueryCriteria>,
> = BTreeMap::new();
{ {
let mut pkm = pub_key_map let mut pkm = pub_key_map
@ -1440,11 +1485,9 @@ impl Service {
.into_iter() .into_iter()
.map(|(server, _)| async move { .map(|(server, _)| async move {
( (
services().sending services()
.send_federation_request( .sending
&server, .send_federation_request(&server, get_server_keys::v2::Request::new())
get_server_keys::v2::Request::new(),
)
.await, .await,
server, server,
) )
@ -1472,10 +1515,11 @@ impl Service {
/// Returns Ok if the acl allows the server /// Returns Ok if the acl allows the server
pub fn acl_check(&self, server_name: &ServerName, room_id: &RoomId) -> Result<()> { pub fn acl_check(&self, server_name: &ServerName, room_id: &RoomId) -> Result<()> {
let acl_event = match services() let acl_event = match services().rooms.state_accessor.room_state_get(
.rooms.state_accessor room_id,
.room_state_get(room_id, &StateEventType::RoomServerAcl, "")? &StateEventType::RoomServerAcl,
{ "",
)? {
Some(acl) => acl, Some(acl) => acl,
None => return Ok(()), None => return Ok(()),
}; };
@ -1587,7 +1631,9 @@ impl Service {
.ok() .ok()
.and_then(|resp| resp.server_key.deserialize().ok()) .and_then(|resp| resp.server_key.deserialize().ok())
{ {
services().globals.add_signing_key(origin, server_key.clone())?; services()
.globals
.add_signing_key(origin, server_key.clone())?;
result.extend( result.extend(
server_key server_key

@ -1,5 +1,5 @@
use ruma::{RoomId, DeviceId, UserId};
use crate::Result; use crate::Result;
use ruma::{DeviceId, RoomId, UserId};
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
fn lazy_load_was_sent_before( fn lazy_load_was_sent_before(
@ -15,7 +15,7 @@ pub trait Data: Send + Sync {
user_id: &UserId, user_id: &UserId,
device_id: &DeviceId, device_id: &DeviceId,
room_id: &RoomId, room_id: &RoomId,
confirmed_user_ids: &mut dyn Iterator<Item=&UserId>, confirmed_user_ids: &mut dyn Iterator<Item = &UserId>,
) -> Result<()>; ) -> Result<()>;
fn lazy_load_reset( fn lazy_load_reset(

@ -1,15 +1,19 @@
mod data; mod data;
use std::{collections::{HashSet, HashMap}, sync::{Mutex, Arc}}; use std::{
collections::{HashMap, HashSet},
sync::{Arc, Mutex},
};
pub use data::Data; pub use data::Data;
use ruma::{DeviceId, UserId, RoomId}; use ruma::{DeviceId, RoomId, UserId};
use crate::Result; use crate::Result;
pub struct Service { pub struct Service {
db: Arc<dyn Data>, db: Arc<dyn Data>,
lazy_load_waiting: Mutex<HashMap<(Box<UserId>, Box<DeviceId>, Box<RoomId>, u64), HashSet<Box<UserId>>>>, lazy_load_waiting:
Mutex<HashMap<(Box<UserId>, Box<DeviceId>, Box<RoomId>, u64), HashSet<Box<UserId>>>>,
} }
impl Service { impl Service {
@ -21,7 +25,8 @@ impl Service {
room_id: &RoomId, room_id: &RoomId,
ll_user: &UserId, ll_user: &UserId,
) -> Result<bool> { ) -> Result<bool> {
self.db.lazy_load_was_sent_before(user_id, device_id, room_id, ll_user) self.db
.lazy_load_was_sent_before(user_id, device_id, room_id, ll_user)
} }
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
@ -58,7 +63,12 @@ impl Service {
room_id.to_owned(), room_id.to_owned(),
since, since,
)) { )) {
self.db.lazy_load_confirm_delivery(user_id, device_id, room_id, &mut user_ids.iter().map(|&u| &*u))?; self.db.lazy_load_confirm_delivery(
user_id,
device_id,
room_id,
&mut user_ids.iter().map(|&u| &*u),
)?;
} else { } else {
// Ignore // Ignore
} }

@ -1,5 +1,5 @@
use ruma::RoomId;
use crate::Result; use crate::Result;
use ruma::RoomId;
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
fn exists(&self, room_id: &RoomId) -> Result<bool>; fn exists(&self, room_id: &RoomId) -> Result<bool>;

@ -16,7 +16,25 @@ pub mod state_compressor;
pub mod timeline; pub mod timeline;
pub mod user; pub mod user;
pub trait Data: alias::Data + auth_chain::Data + directory::Data + edus::Data + lazy_loading::Data + metadata::Data + outlier::Data + pdu_metadata::Data + search::Data + short::Data + state::Data + state_accessor::Data + state_cache::Data + state_compressor::Data + timeline::Data + user::Data {} pub trait Data:
alias::Data
+ auth_chain::Data
+ directory::Data
+ edus::Data
+ lazy_loading::Data
+ metadata::Data
+ outlier::Data
+ pdu_metadata::Data
+ search::Data
+ short::Data
+ state::Data
+ state_accessor::Data
+ state_cache::Data
+ state_compressor::Data
+ timeline::Data
+ user::Data
{
}
pub struct Service { pub struct Service {
pub alias: alias::Service, pub alias: alias::Service,

@ -2,9 +2,9 @@ mod data;
use std::sync::Arc; use std::sync::Arc;
pub use data::Data; pub use data::Data;
use ruma::{EventId, signatures::CanonicalJsonObject}; use ruma::{signatures::CanonicalJsonObject, EventId};
use crate::{Result, PduEvent}; use crate::{PduEvent, Result};
pub struct Service { pub struct Service {
db: Arc<dyn Data>, db: Arc<dyn Data>,

@ -1,7 +1,7 @@
use std::sync::Arc; use std::sync::Arc;
use ruma::{EventId, RoomId};
use crate::Result; use crate::Result;
use ruma::{EventId, RoomId};
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
fn mark_as_referenced(&self, room_id: &RoomId, event_ids: &[Arc<EventId>]) -> Result<()>; fn mark_as_referenced(&self, room_id: &RoomId, event_ids: &[Arc<EventId>]) -> Result<()>;

@ -2,7 +2,7 @@ mod data;
use std::sync::Arc; use std::sync::Arc;
pub use data::Data; pub use data::Data;
use ruma::{RoomId, EventId}; use ruma::{EventId, RoomId};
use crate::Result; use crate::Result;

@ -1,5 +1,5 @@
use ruma::RoomId;
use crate::Result; use crate::Result;
use ruma::RoomId;
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
fn index_pdu<'a>(&self, shortroomid: u64, pdu_id: &[u8], message_body: String) -> Result<()>; fn index_pdu<'a>(&self, shortroomid: u64, pdu_id: &[u8], message_body: String) -> Result<()>;

@ -12,7 +12,12 @@ pub struct Service {
impl Service { impl Service {
#[tracing::instrument(skip(self))] #[tracing::instrument(skip(self))]
pub fn index_pdu<'a>(&self, shortroomid: u64, pdu_id: &[u8], message_body: String) -> Result<()> { pub fn index_pdu<'a>(
&self,
shortroomid: u64,
pdu_id: &[u8],
message_body: String,
) -> Result<()> {
self.db.index_pdu(shortroomid, pdu_id, message_body) self.db.index_pdu(shortroomid, pdu_id, message_body)
} }

@ -1,13 +1,10 @@
use std::sync::Arc; use std::sync::Arc;
use ruma::{EventId, events::StateEventType, RoomId};
use crate::Result; use crate::Result;
use ruma::{events::StateEventType, EventId, RoomId};
pub trait Data: Send + Sync { pub trait Data: Send + Sync {
fn get_or_create_shorteventid( fn get_or_create_shorteventid(&self, event_id: &EventId) -> Result<u64>;
&self,
event_id: &EventId,
) -> Result<u64>;
fn get_shortstatekey( fn get_shortstatekey(
&self, &self,
@ -26,15 +23,9 @@ pub trait Data: Send + Sync {
fn get_statekey_from_short(&self, shortstatekey: u64) -> Result<(StateEventType, String)>; fn get_statekey_from_short(&self, shortstatekey: u64) -> Result<(StateEventType, String)>;
/// Returns (shortstatehash, already_existed) /// Returns (shortstatehash, already_existed)
fn get_or_create_shortstatehash( fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> Result<(u64, bool)>;
&self,
state_hash: &[u8],
) -> Result<(u64, bool)>;
fn get_shortroomid(&self, room_id: &RoomId) -> Result<Option<u64>>; fn get_shortroomid(&self, room_id: &RoomId) -> Result<Option<u64>>;
fn get_or_create_shortroomid( fn get_or_create_shortroomid(&self, room_id: &RoomId) -> Result<u64>;
&self,
room_id: &RoomId,
) -> Result<u64>;
} }

@ -2,19 +2,16 @@ mod data;
use std::sync::Arc; use std::sync::Arc;
pub use data::Data; pub use data::Data;
use ruma::{EventId, events::StateEventType, RoomId}; use ruma::{events::StateEventType, EventId, RoomId};
use crate::{Result, Error, utils, services}; use crate::{services, utils, Error, Result};
pub struct Service { pub struct Service {
db: Arc<dyn Data>, db: Arc<dyn Data>,
} }
impl Service { impl Service {
pub fn get_or_create_shorteventid( pub fn get_or_create_shorteventid(&self, event_id: &EventId) -> Result<u64> {
&self,
event_id: &EventId,
) -> Result<u64> {
self.db.get_or_create_shorteventid(event_id) self.db.get_or_create_shorteventid(event_id)
} }
@ -43,10 +40,7 @@ impl Service {
} }
/// Returns (shortstatehash, already_existed) /// Returns (shortstatehash, already_existed)
pub fn get_or_create_shortstatehash( pub fn get_or_create_shortstatehash(&self, state_hash: &[u8]) -> Result<(u64, bool)> {
&self,
state_hash: &[u8],
) -> Result<(u64, bool)> {
self.db.get_or_create_shortstatehash(state_hash) self.db.get_or_create_shortstatehash(state_hash)
} }
@ -54,10 +48,7 @@ impl Service {
self.db.get_shortroomid(room_id) self.db.get_shortroomid(room_id)
} }
pub fn get_or_create_shortroomid( pub fn get_or_create_shortroomid(&self, room_id: &RoomId) -> Result<u64> {
&self,
room_id: &RoomId,
) -> Result<u64> {
self.db.get_or_create_shortroomid(room_id) self.db.get_or_create_shortroomid(room_id)
} }
} }

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save