Hot-Reloading Refactor

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-05-09 15:59:08 -07:00 committed by June 🍓🦴
parent ae1a4fd283
commit 6c1434c165
212 changed files with 5679 additions and 4206 deletions

View file

@ -5,7 +5,7 @@ use std::{
sync::{Arc, Mutex},
};
pub(crate) use data::Data;
pub use data::Data;
use ruma::{
api::client::{
device::Device,
@ -25,7 +25,7 @@ use ruma::{
use crate::{services, Error, Result};
pub(crate) struct SlidingSyncCache {
pub struct SlidingSyncCache {
lists: BTreeMap<String, SyncRequestList>,
subscriptions: BTreeMap<OwnedRoomId, sync_events::v4::RoomSubscription>,
known_rooms: BTreeMap<String, BTreeMap<OwnedRoomId, u64>>, // For every room, the roomsince number
@ -34,25 +34,23 @@ pub(crate) struct SlidingSyncCache {
type DbConnections = Mutex<BTreeMap<(OwnedUserId, OwnedDeviceId, String), Arc<Mutex<SlidingSyncCache>>>>;
pub(crate) struct Service {
pub(crate) db: &'static dyn Data,
pub(crate) connections: DbConnections,
pub struct Service {
pub db: Arc<dyn Data>,
pub connections: DbConnections,
}
impl Service {
/// Check if a user has an account on this homeserver.
pub(crate) fn exists(&self, user_id: &UserId) -> Result<bool> { self.db.exists(user_id) }
pub fn exists(&self, user_id: &UserId) -> Result<bool> { self.db.exists(user_id) }
pub(crate) fn forget_sync_request_connection(
&self, user_id: OwnedUserId, device_id: OwnedDeviceId, conn_id: String,
) {
pub fn forget_sync_request_connection(&self, user_id: OwnedUserId, device_id: OwnedDeviceId, conn_id: String) {
self.connections
.lock()
.unwrap()
.remove(&(user_id, device_id, conn_id));
}
pub(crate) fn update_sync_request_with_cache(
pub fn update_sync_request_with_cache(
&self, user_id: OwnedUserId, device_id: OwnedDeviceId, request: &mut sync_events::v4::Request,
) -> BTreeMap<String, BTreeMap<OwnedRoomId, u64>> {
let Some(conn_id) = request.conn_id.clone() else {
@ -172,7 +170,7 @@ impl Service {
cached.known_rooms.clone()
}
pub(crate) fn update_sync_subscriptions(
pub fn update_sync_subscriptions(
&self, user_id: OwnedUserId, device_id: OwnedDeviceId, conn_id: String,
subscriptions: BTreeMap<OwnedRoomId, sync_events::v4::RoomSubscription>,
) {
@ -195,7 +193,7 @@ impl Service {
cached.subscriptions = subscriptions;
}
pub(crate) fn update_sync_known_rooms(
pub fn update_sync_known_rooms(
&self, user_id: OwnedUserId, device_id: OwnedDeviceId, conn_id: String, list_id: String,
new_cached_rooms: BTreeSet<OwnedRoomId>, globalsince: u64,
) {
@ -232,10 +230,10 @@ impl Service {
}
/// Check if account is deactivated
pub(crate) fn is_deactivated(&self, user_id: &UserId) -> Result<bool> { self.db.is_deactivated(user_id) }
pub fn is_deactivated(&self, user_id: &UserId) -> Result<bool> { self.db.is_deactivated(user_id) }
/// Check if a user is an admin
pub(crate) fn is_admin(&self, user_id: &UserId) -> Result<bool> {
pub fn is_admin(&self, user_id: &UserId) -> Result<bool> {
let admin_room_alias_id = RoomAliasId::parse(format!("#admins:{}", services().globals.server_name()))
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?;
let admin_room_id = services()
@ -251,63 +249,63 @@ impl Service {
}
/// Create a new user account on this homeserver.
pub(crate) fn create(&self, user_id: &UserId, password: Option<&str>) -> Result<()> {
pub fn create(&self, user_id: &UserId, password: Option<&str>) -> Result<()> {
self.db.set_password(user_id, password)?;
Ok(())
}
/// Returns the number of users registered on this server.
pub(crate) fn count(&self) -> Result<usize> { self.db.count() }
pub fn count(&self) -> Result<usize> { self.db.count() }
/// Find out which user an access token belongs to.
pub(crate) fn find_from_token(&self, token: &str) -> Result<Option<(OwnedUserId, String)>> {
pub fn find_from_token(&self, token: &str) -> Result<Option<(OwnedUserId, String)>> {
self.db.find_from_token(token)
}
/// Returns an iterator over all users on this homeserver.
pub(crate) fn iter(&self) -> impl Iterator<Item = Result<OwnedUserId>> + '_ { self.db.iter() }
pub fn iter(&self) -> impl Iterator<Item = Result<OwnedUserId>> + '_ { self.db.iter() }
/// Returns a list of local users as list of usernames.
///
/// A user account is considered `local` if the length of it's password is
/// greater then zero.
pub(crate) fn list_local_users(&self) -> Result<Vec<String>> { self.db.list_local_users() }
pub fn list_local_users(&self) -> Result<Vec<String>> { self.db.list_local_users() }
/// Returns the password hash for the given user.
pub(crate) fn password_hash(&self, user_id: &UserId) -> Result<Option<String>> { self.db.password_hash(user_id) }
pub fn password_hash(&self, user_id: &UserId) -> Result<Option<String>> { self.db.password_hash(user_id) }
/// Hash and set the user's password to the Argon2 hash
pub(crate) fn set_password(&self, user_id: &UserId, password: Option<&str>) -> Result<()> {
pub fn set_password(&self, user_id: &UserId, password: Option<&str>) -> Result<()> {
self.db.set_password(user_id, password)
}
/// Returns the displayname of a user on this homeserver.
pub(crate) fn displayname(&self, user_id: &UserId) -> Result<Option<String>> { self.db.displayname(user_id) }
pub fn displayname(&self, user_id: &UserId) -> Result<Option<String>> { self.db.displayname(user_id) }
/// Sets a new displayname or removes it if displayname is None. You still
/// need to nofify all rooms of this change.
pub(crate) async fn set_displayname(&self, user_id: &UserId, displayname: Option<String>) -> Result<()> {
pub async fn set_displayname(&self, user_id: &UserId, displayname: Option<String>) -> Result<()> {
self.db.set_displayname(user_id, displayname)
}
/// Get the avatar_url of a user.
pub(crate) fn avatar_url(&self, user_id: &UserId) -> Result<Option<OwnedMxcUri>> { self.db.avatar_url(user_id) }
pub fn avatar_url(&self, user_id: &UserId) -> Result<Option<OwnedMxcUri>> { self.db.avatar_url(user_id) }
/// Sets a new avatar_url or removes it if avatar_url is None.
pub(crate) async fn set_avatar_url(&self, user_id: &UserId, avatar_url: Option<OwnedMxcUri>) -> Result<()> {
pub async fn set_avatar_url(&self, user_id: &UserId, avatar_url: Option<OwnedMxcUri>) -> Result<()> {
self.db.set_avatar_url(user_id, avatar_url)
}
/// Get the blurhash of a user.
pub(crate) fn blurhash(&self, user_id: &UserId) -> Result<Option<String>> { self.db.blurhash(user_id) }
pub fn blurhash(&self, user_id: &UserId) -> Result<Option<String>> { self.db.blurhash(user_id) }
/// Sets a new avatar_url or removes it if avatar_url is None.
pub(crate) async fn set_blurhash(&self, user_id: &UserId, blurhash: Option<String>) -> Result<()> {
pub async fn set_blurhash(&self, user_id: &UserId, blurhash: Option<String>) -> Result<()> {
self.db.set_blurhash(user_id, blurhash)
}
/// Adds a new device to a user.
pub(crate) fn create_device(
pub fn create_device(
&self, user_id: &UserId, device_id: &DeviceId, token: &str, initial_device_display_name: Option<String>,
) -> Result<()> {
self.db
@ -315,21 +313,21 @@ impl Service {
}
/// Removes a device from a user.
pub(crate) fn remove_device(&self, user_id: &UserId, device_id: &DeviceId) -> Result<()> {
pub fn remove_device(&self, user_id: &UserId, device_id: &DeviceId) -> Result<()> {
self.db.remove_device(user_id, device_id)
}
/// Returns an iterator over all device ids of this user.
pub(crate) fn all_device_ids<'a>(&'a self, user_id: &UserId) -> impl Iterator<Item = Result<OwnedDeviceId>> + 'a {
pub fn all_device_ids<'a>(&'a self, user_id: &UserId) -> impl Iterator<Item = Result<OwnedDeviceId>> + 'a {
self.db.all_device_ids(user_id)
}
/// Replaces the access token of one device.
pub(crate) fn set_token(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result<()> {
pub fn set_token(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result<()> {
self.db.set_token(user_id, device_id, token)
}
pub(crate) fn add_one_time_key(
pub fn add_one_time_key(
&self, user_id: &UserId, device_id: &DeviceId, one_time_key_key: &DeviceKeyId,
one_time_key_value: &Raw<OneTimeKey>,
) -> Result<()> {
@ -339,29 +337,27 @@ impl Service {
// TODO: use this ?
#[allow(dead_code)]
pub(crate) fn last_one_time_keys_update(&self, user_id: &UserId) -> Result<u64> {
pub fn last_one_time_keys_update(&self, user_id: &UserId) -> Result<u64> {
self.db.last_one_time_keys_update(user_id)
}
pub(crate) fn take_one_time_key(
pub fn take_one_time_key(
&self, user_id: &UserId, device_id: &DeviceId, key_algorithm: &DeviceKeyAlgorithm,
) -> Result<Option<(OwnedDeviceKeyId, Raw<OneTimeKey>)>> {
self.db.take_one_time_key(user_id, device_id, key_algorithm)
}
pub(crate) fn count_one_time_keys(
pub fn count_one_time_keys(
&self, user_id: &UserId, device_id: &DeviceId,
) -> Result<BTreeMap<DeviceKeyAlgorithm, UInt>> {
self.db.count_one_time_keys(user_id, device_id)
}
pub(crate) fn add_device_keys(
&self, user_id: &UserId, device_id: &DeviceId, device_keys: &Raw<DeviceKeys>,
) -> Result<()> {
pub fn add_device_keys(&self, user_id: &UserId, device_id: &DeviceId, device_keys: &Raw<DeviceKeys>) -> Result<()> {
self.db.add_device_keys(user_id, device_id, device_keys)
}
pub(crate) fn add_cross_signing_keys(
pub fn add_cross_signing_keys(
&self, user_id: &UserId, master_key: &Raw<CrossSigningKey>, self_signing_key: &Option<Raw<CrossSigningKey>>,
user_signing_key: &Option<Raw<CrossSigningKey>>, notify: bool,
) -> Result<()> {
@ -369,58 +365,56 @@ impl Service {
.add_cross_signing_keys(user_id, master_key, self_signing_key, user_signing_key, notify)
}
pub(crate) fn sign_key(
pub fn sign_key(
&self, target_id: &UserId, key_id: &str, signature: (String, String), sender_id: &UserId,
) -> Result<()> {
self.db.sign_key(target_id, key_id, signature, sender_id)
}
pub(crate) fn keys_changed<'a>(
pub fn keys_changed<'a>(
&'a self, user_or_room_id: &str, from: u64, to: Option<u64>,
) -> impl Iterator<Item = Result<OwnedUserId>> + 'a {
self.db.keys_changed(user_or_room_id, from, to)
}
pub(crate) fn mark_device_key_update(&self, user_id: &UserId) -> Result<()> {
self.db.mark_device_key_update(user_id)
}
pub fn mark_device_key_update(&self, user_id: &UserId) -> Result<()> { self.db.mark_device_key_update(user_id) }
pub(crate) fn get_device_keys(&self, user_id: &UserId, device_id: &DeviceId) -> Result<Option<Raw<DeviceKeys>>> {
pub fn get_device_keys(&self, user_id: &UserId, device_id: &DeviceId) -> Result<Option<Raw<DeviceKeys>>> {
self.db.get_device_keys(user_id, device_id)
}
pub(crate) fn parse_master_key(
pub fn parse_master_key(
&self, user_id: &UserId, master_key: &Raw<CrossSigningKey>,
) -> Result<(Vec<u8>, CrossSigningKey)> {
self.db.parse_master_key(user_id, master_key)
}
pub(crate) fn get_key(
pub fn get_key(
&self, key: &[u8], sender_user: Option<&UserId>, user_id: &UserId, allowed_signatures: &dyn Fn(&UserId) -> bool,
) -> Result<Option<Raw<CrossSigningKey>>> {
self.db
.get_key(key, sender_user, user_id, allowed_signatures)
}
pub(crate) fn get_master_key(
pub fn get_master_key(
&self, sender_user: Option<&UserId>, user_id: &UserId, allowed_signatures: &dyn Fn(&UserId) -> bool,
) -> Result<Option<Raw<CrossSigningKey>>> {
self.db
.get_master_key(sender_user, user_id, allowed_signatures)
}
pub(crate) fn get_self_signing_key(
pub fn get_self_signing_key(
&self, sender_user: Option<&UserId>, user_id: &UserId, allowed_signatures: &dyn Fn(&UserId) -> bool,
) -> Result<Option<Raw<CrossSigningKey>>> {
self.db
.get_self_signing_key(sender_user, user_id, allowed_signatures)
}
pub(crate) fn get_user_signing_key(&self, user_id: &UserId) -> Result<Option<Raw<CrossSigningKey>>> {
pub fn get_user_signing_key(&self, user_id: &UserId) -> Result<Option<Raw<CrossSigningKey>>> {
self.db.get_user_signing_key(user_id)
}
pub(crate) fn add_to_device_event(
pub fn add_to_device_event(
&self, sender: &UserId, target_user_id: &UserId, target_device_id: &DeviceId, event_type: &str,
content: serde_json::Value,
) -> Result<()> {
@ -428,35 +422,33 @@ impl Service {
.add_to_device_event(sender, target_user_id, target_device_id, event_type, content)
}
pub(crate) fn get_to_device_events(
&self, user_id: &UserId, device_id: &DeviceId,
) -> Result<Vec<Raw<AnyToDeviceEvent>>> {
pub fn get_to_device_events(&self, user_id: &UserId, device_id: &DeviceId) -> Result<Vec<Raw<AnyToDeviceEvent>>> {
self.db.get_to_device_events(user_id, device_id)
}
pub(crate) fn remove_to_device_events(&self, user_id: &UserId, device_id: &DeviceId, until: u64) -> Result<()> {
pub fn remove_to_device_events(&self, user_id: &UserId, device_id: &DeviceId, until: u64) -> Result<()> {
self.db.remove_to_device_events(user_id, device_id, until)
}
pub(crate) fn update_device_metadata(&self, user_id: &UserId, device_id: &DeviceId, device: &Device) -> Result<()> {
pub fn update_device_metadata(&self, user_id: &UserId, device_id: &DeviceId, device: &Device) -> Result<()> {
self.db.update_device_metadata(user_id, device_id, device)
}
/// Get device metadata.
pub(crate) fn get_device_metadata(&self, user_id: &UserId, device_id: &DeviceId) -> Result<Option<Device>> {
pub fn get_device_metadata(&self, user_id: &UserId, device_id: &DeviceId) -> Result<Option<Device>> {
self.db.get_device_metadata(user_id, device_id)
}
pub(crate) fn get_devicelist_version(&self, user_id: &UserId) -> Result<Option<u64>> {
pub fn get_devicelist_version(&self, user_id: &UserId) -> Result<Option<u64>> {
self.db.get_devicelist_version(user_id)
}
pub(crate) fn all_devices_metadata<'a>(&'a self, user_id: &UserId) -> impl Iterator<Item = Result<Device>> + 'a {
pub fn all_devices_metadata<'a>(&'a self, user_id: &UserId) -> impl Iterator<Item = Result<Device>> + 'a {
self.db.all_devices_metadata(user_id)
}
/// Deactivate account
pub(crate) fn deactivate_account(&self, user_id: &UserId) -> Result<()> {
pub fn deactivate_account(&self, user_id: &UserId) -> Result<()> {
// Remove all associated devices
for device_id in self.all_device_ids(user_id) {
self.remove_device(user_id, &device_id?)?;
@ -473,17 +465,17 @@ impl Service {
}
/// Creates a new sync filter. Returns the filter id.
pub(crate) fn create_filter(&self, user_id: &UserId, filter: &FilterDefinition) -> Result<String> {
pub fn create_filter(&self, user_id: &UserId, filter: &FilterDefinition) -> Result<String> {
self.db.create_filter(user_id, filter)
}
pub(crate) fn get_filter(&self, user_id: &UserId, filter_id: &str) -> Result<Option<FilterDefinition>> {
pub fn get_filter(&self, user_id: &UserId, filter_id: &str) -> Result<Option<FilterDefinition>> {
self.db.get_filter(user_id, filter_id)
}
}
/// Ensure that a user only sees signatures from themselves and the target user
pub(crate) fn clean_signatures<F: Fn(&UserId) -> bool>(
pub fn clean_signatures<F: Fn(&UserId) -> bool>(
cross_signing_key: &mut serde_json::Value, sender_user: Option<&UserId>, user_id: &UserId, allowed_signatures: F,
) -> Result<(), Error> {
if let Some(signatures) = cross_signing_key