fix: some compile time errors

Only 174 errors left!
This commit is contained in:
Timo Kösters 2022-09-06 23:15:09 +02:00 committed by Nyaaori
parent 82e7f57b38
commit 057f8364cc
No known key found for this signature in database
GPG key ID: E7819C3ED4D1F82E
118 changed files with 2139 additions and 2433 deletions

View file

@ -1,6 +1,8 @@
use crate::{database::KeyValueDatabase, service, utils, Error};
impl service::appservice::Data for KeyValueDatabase {
/// Registers an appservice and returns the ID to the caller
pub fn register_appservice(&self, yaml: serde_yaml::Value) -> Result<String> {
fn register_appservice(&self, yaml: serde_yaml::Value) -> Result<String> {
// TODO: Rumaify
let id = yaml.get("id").unwrap().as_str().unwrap();
self.id_appserviceregistrations.insert(

View file

@ -1,13 +1,13 @@
mod account_data;
mod admin;
//mod account_data;
//mod admin;
mod appservice;
mod globals;
mod key_backups;
mod media;
mod pdu;
//mod globals;
//mod key_backups;
//mod media;
//mod pdu;
mod pusher;
mod rooms;
mod sending;
//mod sending;
mod transaction_ids;
mod uiaa;
mod users;

View file

@ -1,3 +1,7 @@
use ruma::{UserId, api::client::push::{set_pusher, get_pushers}};
use crate::{service, database::KeyValueDatabase, Error};
impl service::pusher::Data for KeyValueDatabase {
fn set_pusher(&self, sender: &UserId, pusher: set_pusher::v3::Pusher) -> Result<()> {
let mut key = sender.as_bytes().to_vec();

View file

@ -1,4 +1,8 @@
impl service::room::alias::Data for KeyValueDatabase {
use ruma::{RoomId, RoomAliasId, api::client::error::ErrorKind};
use crate::{service, database::KeyValueDatabase, utils, Error, services};
impl service::rooms::alias::Data for KeyValueDatabase {
fn set_alias(
&self,
alias: &RoomAliasId,
@ -8,7 +12,7 @@ impl service::room::alias::Data for KeyValueDatabase {
.insert(alias.alias().as_bytes(), room_id.as_bytes())?;
let mut aliasid = room_id.as_bytes().to_vec();
aliasid.push(0xff);
aliasid.extend_from_slice(&globals.next_count()?.to_be_bytes());
aliasid.extend_from_slice(&services().globals.next_count()?.to_be_bytes());
self.aliasid_alias.insert(&aliasid, &*alias.as_bytes())?;
Ok(())
}

View file

@ -1,10 +1,14 @@
impl service::room::directory::Data for KeyValueDatabase {
use ruma::RoomId;
use crate::{service, database::KeyValueDatabase, utils, Error};
impl service::rooms::directory::Data for KeyValueDatabase {
fn set_public(&self, room_id: &RoomId) -> Result<()> {
self.publicroomids.insert(room_id.as_bytes(), &[])?;
self.publicroomids.insert(room_id.as_bytes(), &[])
}
fn set_not_public(&self, room_id: &RoomId) -> Result<()> {
self.publicroomids.remove(room_id.as_bytes())?;
self.publicroomids.remove(room_id.as_bytes())
}
fn is_public_room(&self, room_id: &RoomId) -> Result<bool> {

View file

@ -0,0 +1,3 @@
mod presence;
mod typing;
mod read_receipt;

View file

@ -1,4 +1,10 @@
impl service::room::edus::presence::Data for KeyValueDatabase {
use std::collections::HashMap;
use ruma::{UserId, RoomId, events::presence::PresenceEvent, presence::PresenceState, UInt};
use crate::{service, database::KeyValueDatabase, utils, Error, services};
impl service::rooms::edus::presence::Data for KeyValueDatabase {
fn update_presence(
&self,
user_id: &UserId,
@ -7,7 +13,7 @@ impl service::room::edus::presence::Data for KeyValueDatabase {
) -> Result<()> {
// TODO: Remove old entry? Or maybe just wipe completely from time to time?
let count = globals.next_count()?.to_be_bytes();
let count = services().globals.next_count()?.to_be_bytes();
let mut presence_id = room_id.as_bytes().to_vec();
presence_id.push(0xff);
@ -101,6 +107,7 @@ impl service::room::edus::presence::Data for KeyValueDatabase {
Ok(hashmap)
}
/*
fn presence_maintain(&self, db: Arc<TokioRwLock<Database>>) {
// TODO @M0dEx: move this to a timed tasks module
tokio::spawn(async move {
@ -117,6 +124,7 @@ impl service::room::edus::presence::Data for KeyValueDatabase {
}
});
}
*/
}
fn parse_presence_event(bytes: &[u8]) -> Result<PresenceEvent> {

View file

@ -1,4 +1,10 @@
impl service::room::edus::read_receipt::Data for KeyValueDatabase {
use std::mem;
use ruma::{UserId, RoomId, events::receipt::ReceiptEvent, serde::Raw, signatures::CanonicalJsonObject};
use crate::{database::KeyValueDatabase, service, utils, Error, services};
impl service::rooms::edus::read_receipt::Data for KeyValueDatabase {
fn readreceipt_update(
&self,
user_id: &UserId,
@ -28,7 +34,7 @@ impl service::room::edus::read_receipt::Data for KeyValueDatabase {
}
let mut room_latest_id = prefix;
room_latest_id.extend_from_slice(&globals.next_count()?.to_be_bytes());
room_latest_id.extend_from_slice(&services().globals.next_count()?.to_be_bytes());
room_latest_id.push(0xff);
room_latest_id.extend_from_slice(user_id.as_bytes());
@ -40,7 +46,7 @@ impl service::room::edus::read_receipt::Data for KeyValueDatabase {
Ok(())
}
pub fn readreceipts_since<'a>(
fn readreceipts_since<'a>(
&'a self,
room_id: &RoomId,
since: u64,
@ -102,7 +108,7 @@ impl service::room::edus::read_receipt::Data for KeyValueDatabase {
.insert(&key, &count.to_be_bytes())?;
self.roomuserid_lastprivatereadupdate
.insert(&key, &globals.next_count()?.to_be_bytes())?;
.insert(&key, &services().globals.next_count()?.to_be_bytes())
}
fn private_read_get(&self, room_id: &RoomId, user_id: &UserId) -> Result<Option<u64>> {

View file

@ -1,15 +1,20 @@
impl service::room::edus::typing::Data for KeyValueDatabase {
use std::collections::HashSet;
use ruma::{UserId, RoomId};
use crate::{database::KeyValueDatabase, service, utils, Error, services};
impl service::rooms::edus::typing::Data for KeyValueDatabase {
fn typing_add(
&self,
user_id: &UserId,
room_id: &RoomId,
timeout: u64,
globals: &super::super::globals::Globals,
) -> Result<()> {
let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff);
let count = globals.next_count()?.to_be_bytes();
let count = services().globals.next_count()?.to_be_bytes();
let mut room_typing_id = prefix;
room_typing_id.extend_from_slice(&timeout.to_be_bytes());
@ -49,7 +54,7 @@ impl service::room::edus::typing::Data for KeyValueDatabase {
if found_outdated {
self.roomid_lasttypingupdate
.insert(room_id.as_bytes(), &globals.next_count()?.to_be_bytes())?;
.insert(room_id.as_bytes(), &services().globals.next_count()?.to_be_bytes())?;
}
Ok(())

View file

@ -1,4 +1,8 @@
impl service::room::lazy_load::Data for KeyValueDatabase {
use ruma::{UserId, DeviceId, RoomId};
use crate::{service, database::KeyValueDatabase};
impl service::rooms::lazy_loading::Data for KeyValueDatabase {
fn lazy_load_was_sent_before(
&self,
user_id: &UserId,

View file

@ -1,4 +1,8 @@
impl service::room::metadata::Data for KeyValueDatabase {
use ruma::RoomId;
use crate::{service, database::KeyValueDatabase};
impl service::rooms::metadata::Data for KeyValueDatabase {
fn exists(&self, room_id: &RoomId) -> Result<bool> {
let prefix = match self.get_shortroomid(room_id)? {
Some(b) => b.to_be_bytes().to_vec(),

View file

@ -1,14 +1,13 @@
mod state;
mod alias;
mod directory;
mod edus;
mod event_handler;
mod lazy_loading;
//mod event_handler;
mod lazy_load;
mod metadata;
mod outlier;
mod pdu_metadata;
mod search;
mod short;
//mod short;
mod state;
mod state_accessor;
mod state_cache;

View file

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

View file

@ -1,4 +1,10 @@
impl service::room::pdu_metadata::Data for KeyValueDatabase {
use std::sync::Arc;
use ruma::{RoomId, EventId};
use crate::{service, database::KeyValueDatabase};
impl service::rooms::pdu_metadata::Data for KeyValueDatabase {
fn mark_as_referenced(&self, room_id: &RoomId, event_ids: &[Arc<EventId>]) -> Result<()> {
for prev in event_ids {
let mut key = room_id.as_bytes().to_vec();

View file

@ -1,7 +1,12 @@
impl service::room::search::Data for KeyValueDatabase {
use std::mem::size_of;
use ruma::RoomId;
use crate::{service, database::KeyValueDatabase, utils};
impl service::rooms::search::Data for KeyValueDatabase {
fn index_pdu<'a>(&self, room_id: &RoomId, pdu_id: u64, message_body: String) -> Result<()> {
let mut batch = body
let mut batch = message_body
.split_terminator(|c: char| !c.is_alphanumeric())
.filter(|s| !s.is_empty())
.filter(|word| word.len() <= 50)
@ -14,7 +19,7 @@ impl service::room::search::Data for KeyValueDatabase {
(key, Vec::new())
});
self.tokenids.insert_batch(&mut batch)?;
self.tokenids.insert_batch(&mut batch)
}
fn search_pdus<'a>(
@ -64,3 +69,4 @@ impl service::room::search::Data for KeyValueDatabase {
)
}))
}
}

View file

@ -1,4 +1,11 @@
impl service::room::state::Data for KeyValueDatabase {
use ruma::{RoomId, EventId};
use std::sync::Arc;
use std::{sync::MutexGuard, collections::HashSet};
use std::fmt::Debug;
use crate::{service, database::KeyValueDatabase, utils, Error};
impl service::rooms::state::Data for KeyValueDatabase {
fn get_room_shortstatehash(&self, room_id: &RoomId) -> Result<Option<u64>> {
self.roomid_shortstatehash
.get(room_id.as_bytes())?
@ -9,21 +16,21 @@ impl service::room::state::Data for KeyValueDatabase {
})
}
fn set_room_state(&self, room_id: &RoomId, new_shortstatehash: u64
_mutex_lock: &MutexGuard<'_, StateLock>, // Take mutex guard to make sure users get the room state mutex
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
) -> Result<()> {
self.roomid_shortstatehash
.insert(room_id.as_bytes(), &new_shortstatehash.to_be_bytes())?;
Ok(())
}
fn set_event_state(&self) -> Result<()> {
db.shorteventid_shortstatehash
fn set_event_state(&self, shorteventid: Vec<u8>, shortstatehash: Vec<u8>) -> Result<()> {
self.shorteventid_shortstatehash
.insert(&shorteventid.to_be_bytes(), &shortstatehash.to_be_bytes())?;
Ok(())
}
fn get_pdu_leaves(&self, room_id: &RoomId) -> Result<HashSet<Arc<EventId>>> {
fn get_forward_extremities(&self, room_id: &RoomId) -> Result<HashSet<Arc<EventId>>> {
let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff);
@ -38,11 +45,11 @@ impl service::room::state::Data for KeyValueDatabase {
.collect()
}
fn set_forward_extremities(
fn set_forward_extremities<'a>(
&self,
room_id: &RoomId,
event_ids: impl IntoIterator<Item = &'a EventId> + Debug,
_mutex_lock: &MutexGuard<'_, StateLock>, // 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<()> {
let mut prefix = room_id.as_bytes().to_vec();
prefix.push(0xff);

View file

@ -1,4 +1,11 @@
impl service::room::state_accessor::Data for KeyValueDatabase {
use std::{collections::{BTreeMap, HashMap}, sync::Arc};
use crate::{database::KeyValueDatabase, service, PduEvent, Error, utils};
use async_trait::async_trait;
use ruma::{EventId, events::StateEventType, RoomId};
#[async_trait]
impl service::rooms::state_accessor::Data for KeyValueDatabase {
async fn state_full_ids(&self, shortstatehash: u64) -> Result<BTreeMap<u64, Arc<EventId>>> {
let full_state = self
.load_shortstatehash_info(shortstatehash)?
@ -149,3 +156,4 @@ impl service::room::state_accessor::Data for KeyValueDatabase {
Ok(None)
}
}
}

View file

@ -1,8 +1,12 @@
impl service::room::state_cache::Data for KeyValueDatabase {
fn mark_as_once_joined(user_id: &UserId, room_id: &RoomId) -> Result<()> {
use ruma::{UserId, RoomId};
use crate::{service, database::KeyValueDatabase};
impl service::rooms::state_cache::Data for KeyValueDatabase {
fn mark_as_once_joined(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
let mut userroom_id = user_id.as_bytes().to_vec();
userroom_id.push(0xff);
userroom_id.extend_from_slice(room_id.as_bytes());
self.roomuseroncejoinedids.insert(&userroom_id, &[])?;
self.roomuseroncejoinedids.insert(&userroom_id, &[])
}
}

View file

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

View file

@ -1,4 +1,11 @@
impl service::room::timeline::Data for KeyValueDatabase {
use std::{collections::hash_map, mem::size_of, sync::Arc};
use ruma::{UserId, RoomId, api::client::error::ErrorKind, EventId, signatures::CanonicalJsonObject};
use tracing::error;
use crate::{service, database::KeyValueDatabase, utils, Error, PduEvent};
impl service::rooms::timeline::Data for KeyValueDatabase {
fn last_timeline_count(&self, sender_user: &UserId, room_id: &RoomId) -> Result<u64> {
match self
.lasttimelinecount_cache
@ -37,7 +44,7 @@ impl service::room::timeline::Data for KeyValueDatabase {
}
/// Returns the json of a pdu.
pub fn get_pdu_json(&self, event_id: &EventId) -> Result<Option<CanonicalJsonObject>> {
fn get_pdu_json(&self, event_id: &EventId) -> Result<Option<CanonicalJsonObject>> {
self.eventid_pduid
.get(event_id.as_bytes())?
.map_or_else(
@ -55,7 +62,7 @@ impl service::room::timeline::Data for KeyValueDatabase {
}
/// Returns the json of a pdu.
pub fn get_non_outlier_pdu_json(
fn get_non_outlier_pdu_json(
&self,
event_id: &EventId,
) -> Result<Option<CanonicalJsonObject>> {
@ -74,14 +81,14 @@ impl service::room::timeline::Data for KeyValueDatabase {
}
/// Returns the pdu's id.
pub fn get_pdu_id(&self, event_id: &EventId) -> Result<Option<Vec<u8>>> {
fn get_pdu_id(&self, event_id: &EventId) -> Result<Option<Vec<u8>>> {
self.eventid_pduid.get(event_id.as_bytes())
}
/// Returns the pdu.
///
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
pub fn get_non_outlier_pdu(&self, event_id: &EventId) -> Result<Option<PduEvent>> {
fn get_non_outlier_pdu(&self, event_id: &EventId) -> Result<Option<PduEvent>> {
self.eventid_pduid
.get(event_id.as_bytes())?
.map(|pduid| {
@ -99,7 +106,7 @@ impl service::room::timeline::Data for KeyValueDatabase {
/// Returns the pdu.
///
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
pub fn get_pdu(&self, event_id: &EventId) -> Result<Option<Arc<PduEvent>>> {
fn get_pdu(&self, event_id: &EventId) -> Result<Option<Arc<PduEvent>>> {
if let Some(p) = self.pdu_cache.lock().unwrap().get_mut(event_id) {
return Ok(Some(Arc::clone(p)));
}
@ -135,7 +142,7 @@ impl service::room::timeline::Data for KeyValueDatabase {
/// Returns the pdu.
///
/// This does __NOT__ check the outliers `Tree`.
pub fn get_pdu_from_id(&self, pdu_id: &[u8]) -> Result<Option<PduEvent>> {
fn get_pdu_from_id(&self, pdu_id: &[u8]) -> Result<Option<PduEvent>> {
self.pduid_pdu.get(pdu_id)?.map_or(Ok(None), |pdu| {
Ok(Some(
serde_json::from_slice(&pdu)
@ -145,7 +152,7 @@ impl service::room::timeline::Data for KeyValueDatabase {
}
/// Returns the pdu as a `BTreeMap<String, CanonicalJsonValue>`.
pub fn get_pdu_json_from_id(&self, pdu_id: &[u8]) -> Result<Option<CanonicalJsonObject>> {
fn get_pdu_json_from_id(&self, pdu_id: &[u8]) -> Result<Option<CanonicalJsonObject>> {
self.pduid_pdu.get(pdu_id)?.map_or(Ok(None), |pdu| {
Ok(Some(
serde_json::from_slice(&pdu)
@ -155,7 +162,7 @@ impl service::room::timeline::Data for KeyValueDatabase {
}
/// Returns the `count` of this pdu's id.
pub fn pdu_count(&self, pdu_id: &[u8]) -> Result<u64> {
fn pdu_count(&self, pdu_id: &[u8]) -> Result<u64> {
utils::u64_from_bytes(&pdu_id[pdu_id.len() - size_of::<u64>()..])
.map_err(|_| Error::bad_database("PDU has invalid count bytes."))
}
@ -178,7 +185,7 @@ impl service::room::timeline::Data for KeyValueDatabase {
/// Returns an iterator over all events in a room that happened after the event with id `since`
/// in chronological order.
pub fn pdus_since<'a>(
fn pdus_since<'a>(
&'a self,
user_id: &UserId,
room_id: &RoomId,
@ -212,7 +219,7 @@ impl service::room::timeline::Data for KeyValueDatabase {
/// Returns an iterator over all events and their tokens in a room that happened before the
/// event with id `until` in reverse-chronological order.
pub fn pdus_until<'a>(
fn pdus_until<'a>(
&'a self,
user_id: &UserId,
room_id: &RoomId,
@ -246,7 +253,7 @@ impl service::room::timeline::Data for KeyValueDatabase {
}))
}
pub fn pdus_after<'a>(
fn pdus_after<'a>(
&'a self,
user_id: &UserId,
room_id: &RoomId,

View file

@ -1,4 +1,8 @@
impl service::room::user::Data for KeyValueDatabase {
use ruma::{UserId, RoomId};
use crate::{service, database::KeyValueDatabase, utils, Error};
impl service::rooms::user::Data for KeyValueDatabase {
fn reset_notification_counts(&self, user_id: &UserId, room_id: &RoomId) -> Result<()> {
let mut userroom_id = user_id.as_bytes().to_vec();
userroom_id.push(0xff);

View file

@ -1,5 +1,9 @@
impl service::pusher::Data for KeyValueDatabase {
pub fn add_txnid(
use ruma::{UserId, DeviceId, TransactionId};
use crate::{service, database::KeyValueDatabase};
impl service::transaction_ids::Data for KeyValueDatabase {
fn add_txnid(
&self,
user_id: &UserId,
device_id: Option<&DeviceId>,
@ -17,7 +21,7 @@ impl service::pusher::Data for KeyValueDatabase {
Ok(())
}
pub fn existing_txnid(
fn existing_txnid(
&self,
user_id: &UserId,
device_id: Option<&DeviceId>,

View file

@ -1,3 +1,9 @@
use std::io::ErrorKind;
use ruma::{UserId, DeviceId, signatures::CanonicalJsonValue, api::client::uiaa::UiaaInfo};
use crate::{database::KeyValueDatabase, service, Error};
impl service::uiaa::Data for KeyValueDatabase {
fn set_uiaa_request(
&self,

View file

@ -1,11 +1,18 @@
use std::{mem::size_of, collections::BTreeMap};
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 tracing::warn;
use crate::{service::{self, users::clean_signatures}, database::KeyValueDatabase, Error, utils, services};
impl service::users::Data for KeyValueDatabase {
/// Check if a user has an account on this homeserver.
pub fn exists(&self, user_id: &UserId) -> Result<bool> {
fn exists(&self, user_id: &UserId) -> Result<bool> {
Ok(self.userid_password.get(user_id.as_bytes())?.is_some())
}
/// Check if account is deactivated
pub fn is_deactivated(&self, user_id: &UserId) -> Result<bool> {
fn is_deactivated(&self, user_id: &UserId) -> Result<bool> {
Ok(self
.userid_password
.get(user_id.as_bytes())?
@ -16,33 +23,13 @@ impl service::users::Data for KeyValueDatabase {
.is_empty())
}
/// Check if a user is an admin
pub fn is_admin(
&self,
user_id: &UserId,
rooms: &super::rooms::Rooms,
globals: &super::globals::Globals,
) -> Result<bool> {
let admin_room_alias_id = RoomAliasId::parse(format!("#admins:{}", globals.server_name()))
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Invalid alias."))?;
let admin_room_id = rooms.id_from_alias(&admin_room_alias_id)?.unwrap();
rooms.is_joined(user_id, &admin_room_id)
}
/// Create a new user account on this homeserver.
pub fn create(&self, user_id: &UserId, password: Option<&str>) -> Result<()> {
self.set_password(user_id, password)?;
Ok(())
}
/// Returns the number of users registered on this server.
pub fn count(&self) -> Result<usize> {
fn count(&self) -> Result<usize> {
Ok(self.userid_password.iter().count())
}
/// Find out which user an access token belongs to.
pub fn find_from_token(&self, token: &str) -> Result<Option<(Box<UserId>, String)>> {
fn find_from_token(&self, token: &str) -> Result<Option<(Box<UserId>, String)>> {
self.token_userdeviceid
.get(token.as_bytes())?
.map_or(Ok(None), |bytes| {
@ -69,7 +56,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Returns an iterator over all users on this homeserver.
pub fn iter(&self) -> impl Iterator<Item = Result<Box<UserId>>> + '_ {
fn iter(&self) -> impl Iterator<Item = Result<Box<UserId>>> + '_ {
self.userid_password.iter().map(|(bytes, _)| {
UserId::parse(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("User ID in userid_password is invalid unicode.")
@ -81,7 +68,7 @@ impl service::users::Data for KeyValueDatabase {
/// 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 fn list_local_users(&self) -> Result<Vec<String>> {
fn list_local_users(&self) -> Result<Vec<String>> {
let users: Vec<String> = self
.userid_password
.iter()
@ -113,7 +100,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Returns the password hash for the given user.
pub fn password_hash(&self, user_id: &UserId) -> Result<Option<String>> {
fn password_hash(&self, user_id: &UserId) -> Result<Option<String>> {
self.userid_password
.get(user_id.as_bytes())?
.map_or(Ok(None), |bytes| {
@ -124,7 +111,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Hash and set the user's password to the Argon2 hash
pub fn set_password(&self, user_id: &UserId, password: Option<&str>) -> Result<()> {
fn set_password(&self, user_id: &UserId, password: Option<&str>) -> Result<()> {
if let Some(password) = password {
if let Ok(hash) = utils::calculate_hash(password) {
self.userid_password
@ -143,7 +130,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Returns the displayname of a user on this homeserver.
pub fn displayname(&self, user_id: &UserId) -> Result<Option<String>> {
fn displayname(&self, user_id: &UserId) -> Result<Option<String>> {
self.userid_displayname
.get(user_id.as_bytes())?
.map_or(Ok(None), |bytes| {
@ -154,7 +141,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Sets a new displayname or removes it if displayname is None. You still need to nofify all rooms of this change.
pub fn set_displayname(&self, user_id: &UserId, displayname: Option<String>) -> Result<()> {
fn set_displayname(&self, user_id: &UserId, displayname: Option<String>) -> Result<()> {
if let Some(displayname) = displayname {
self.userid_displayname
.insert(user_id.as_bytes(), displayname.as_bytes())?;
@ -166,7 +153,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Get the avatar_url of a user.
pub fn avatar_url(&self, user_id: &UserId) -> Result<Option<Box<MxcUri>>> {
fn avatar_url(&self, user_id: &UserId) -> Result<Option<Box<MxcUri>>> {
self.userid_avatarurl
.get(user_id.as_bytes())?
.map(|bytes| {
@ -179,7 +166,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Sets a new avatar_url or removes it if avatar_url is None.
pub fn set_avatar_url(&self, user_id: &UserId, avatar_url: Option<Box<MxcUri>>) -> Result<()> {
fn set_avatar_url(&self, user_id: &UserId, avatar_url: Option<Box<MxcUri>>) -> Result<()> {
if let Some(avatar_url) = avatar_url {
self.userid_avatarurl
.insert(user_id.as_bytes(), avatar_url.to_string().as_bytes())?;
@ -191,7 +178,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Get the blurhash of a user.
pub fn blurhash(&self, user_id: &UserId) -> Result<Option<String>> {
fn blurhash(&self, user_id: &UserId) -> Result<Option<String>> {
self.userid_blurhash
.get(user_id.as_bytes())?
.map(|bytes| {
@ -204,7 +191,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Sets a new avatar_url or removes it if avatar_url is None.
pub fn set_blurhash(&self, user_id: &UserId, blurhash: Option<String>) -> Result<()> {
fn set_blurhash(&self, user_id: &UserId, blurhash: Option<String>) -> Result<()> {
if let Some(blurhash) = blurhash {
self.userid_blurhash
.insert(user_id.as_bytes(), blurhash.as_bytes())?;
@ -216,7 +203,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Adds a new device to a user.
pub fn create_device(
fn create_device(
&self,
user_id: &UserId,
device_id: &DeviceId,
@ -250,7 +237,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Removes a device from a user.
pub fn remove_device(&self, user_id: &UserId, device_id: &DeviceId) -> Result<()> {
fn remove_device(&self, user_id: &UserId, device_id: &DeviceId) -> Result<()> {
let mut userdeviceid = user_id.as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
@ -280,7 +267,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Returns an iterator over all device ids of this user.
pub fn all_device_ids<'a>(
fn all_device_ids<'a>(
&'a self,
user_id: &UserId,
) -> impl Iterator<Item = Result<Box<DeviceId>>> + 'a {
@ -302,7 +289,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Replaces the access token of one device.
pub fn set_token(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result<()> {
fn set_token(&self, user_id: &UserId, device_id: &DeviceId, token: &str) -> Result<()> {
let mut userdeviceid = user_id.as_bytes().to_vec();
userdeviceid.push(0xff);
userdeviceid.extend_from_slice(device_id.as_bytes());
@ -325,13 +312,12 @@ impl service::users::Data for KeyValueDatabase {
Ok(())
}
pub fn add_one_time_key(
fn add_one_time_key(
&self,
user_id: &UserId,
device_id: &DeviceId,
one_time_key_key: &DeviceKeyId,
one_time_key_value: &Raw<OneTimeKey>,
globals: &super::globals::Globals,
) -> Result<()> {
let mut key = user_id.as_bytes().to_vec();
key.push(0xff);
@ -356,12 +342,12 @@ impl service::users::Data for KeyValueDatabase {
)?;
self.userid_lastonetimekeyupdate
.insert(user_id.as_bytes(), &globals.next_count()?.to_be_bytes())?;
.insert(user_id.as_bytes(), &services().globals.next_count()?.to_be_bytes())?;
Ok(())
}
pub fn last_one_time_keys_update(&self, user_id: &UserId) -> Result<u64> {
fn last_one_time_keys_update(&self, user_id: &UserId) -> Result<u64> {
self.userid_lastonetimekeyupdate
.get(user_id.as_bytes())?
.map(|bytes| {
@ -372,12 +358,11 @@ impl service::users::Data for KeyValueDatabase {
.unwrap_or(Ok(0))
}
pub fn take_one_time_key(
fn take_one_time_key(
&self,
user_id: &UserId,
device_id: &DeviceId,
key_algorithm: &DeviceKeyAlgorithm,
globals: &super::globals::Globals,
) -> Result<Option<(Box<DeviceKeyId>, Raw<OneTimeKey>)>> {
let mut prefix = user_id.as_bytes().to_vec();
prefix.push(0xff);
@ -388,7 +373,7 @@ impl service::users::Data for KeyValueDatabase {
prefix.push(b':');
self.userid_lastonetimekeyupdate
.insert(user_id.as_bytes(), &globals.next_count()?.to_be_bytes())?;
.insert(user_id.as_bytes(), &services().globals.next_count()?.to_be_bytes())?;
self.onetimekeyid_onetimekeys
.scan_prefix(prefix)
@ -411,7 +396,7 @@ impl service::users::Data for KeyValueDatabase {
.transpose()
}
pub fn count_one_time_keys(
fn count_one_time_keys(
&self,
user_id: &UserId,
device_id: &DeviceId,
@ -443,13 +428,11 @@ impl service::users::Data for KeyValueDatabase {
Ok(counts)
}
pub fn add_device_keys(
fn add_device_keys(
&self,
user_id: &UserId,
device_id: &DeviceId,
device_keys: &Raw<DeviceKeys>,
rooms: &super::rooms::Rooms,
globals: &super::globals::Globals,
) -> Result<()> {
let mut userdeviceid = user_id.as_bytes().to_vec();
userdeviceid.push(0xff);
@ -460,19 +443,17 @@ impl service::users::Data for KeyValueDatabase {
&serde_json::to_vec(&device_keys).expect("DeviceKeys::to_vec always works"),
)?;
self.mark_device_key_update(user_id, rooms, globals)?;
self.mark_device_key_update(user_id)?;
Ok(())
}
pub fn add_cross_signing_keys(
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>>,
rooms: &super::rooms::Rooms,
globals: &super::globals::Globals,
) -> Result<()> {
// TODO: Check signatures
@ -575,19 +556,17 @@ impl service::users::Data for KeyValueDatabase {
.insert(user_id.as_bytes(), &user_signing_key_key)?;
}
self.mark_device_key_update(user_id, rooms, globals)?;
self.mark_device_key_update(user_id)?;
Ok(())
}
pub fn sign_key(
fn sign_key(
&self,
target_id: &UserId,
key_id: &str,
signature: (String, String),
sender_id: &UserId,
rooms: &super::rooms::Rooms,
globals: &super::globals::Globals,
) -> Result<()> {
let mut key = target_id.as_bytes().to_vec();
key.push(0xff);
@ -619,12 +598,12 @@ impl service::users::Data for KeyValueDatabase {
)?;
// TODO: Should we notify about this change?
self.mark_device_key_update(target_id, rooms, globals)?;
self.mark_device_key_update(target_id)?;
Ok(())
}
pub fn keys_changed<'a>(
fn keys_changed<'a>(
&'a self,
user_or_room_id: &str,
from: u64,
@ -662,16 +641,14 @@ impl service::users::Data for KeyValueDatabase {
})
}
pub fn mark_device_key_update(
fn mark_device_key_update(
&self,
user_id: &UserId,
rooms: &super::rooms::Rooms,
globals: &super::globals::Globals,
) -> Result<()> {
let count = globals.next_count()?.to_be_bytes();
for room_id in rooms.rooms_joined(user_id).filter_map(|r| r.ok()) {
let count = services().globals.next_count()?.to_be_bytes();
for room_id in services().rooms.rooms_joined(user_id).filter_map(|r| r.ok()) {
// Don't send key updates to unencrypted rooms
if rooms
if services().rooms
.room_state_get(&room_id, &StateEventType::RoomEncryption, "")?
.is_none()
{
@ -693,7 +670,7 @@ impl service::users::Data for KeyValueDatabase {
Ok(())
}
pub fn get_device_keys(
fn get_device_keys(
&self,
user_id: &UserId,
device_id: &DeviceId,
@ -709,7 +686,7 @@ impl service::users::Data for KeyValueDatabase {
})
}
pub fn get_master_key<F: Fn(&UserId) -> bool>(
fn get_master_key<F: Fn(&UserId) -> bool>(
&self,
user_id: &UserId,
allowed_signatures: F,
@ -730,7 +707,7 @@ impl service::users::Data for KeyValueDatabase {
})
}
pub fn get_self_signing_key<F: Fn(&UserId) -> bool>(
fn get_self_signing_key<F: Fn(&UserId) -> bool>(
&self,
user_id: &UserId,
allowed_signatures: F,
@ -751,7 +728,7 @@ impl service::users::Data for KeyValueDatabase {
})
}
pub fn get_user_signing_key(&self, user_id: &UserId) -> Result<Option<Raw<CrossSigningKey>>> {
fn get_user_signing_key(&self, user_id: &UserId) -> Result<Option<Raw<CrossSigningKey>>> {
self.userid_usersigningkeyid
.get(user_id.as_bytes())?
.map_or(Ok(None), |key| {
@ -763,20 +740,19 @@ impl service::users::Data for KeyValueDatabase {
})
}
pub fn add_to_device_event(
fn add_to_device_event(
&self,
sender: &UserId,
target_user_id: &UserId,
target_device_id: &DeviceId,
event_type: &str,
content: serde_json::Value,
globals: &super::globals::Globals,
) -> Result<()> {
let mut key = target_user_id.as_bytes().to_vec();
key.push(0xff);
key.extend_from_slice(target_device_id.as_bytes());
key.push(0xff);
key.extend_from_slice(&globals.next_count()?.to_be_bytes());
key.extend_from_slice(&services().globals.next_count()?.to_be_bytes());
let mut json = serde_json::Map::new();
json.insert("type".to_owned(), event_type.to_owned().into());
@ -790,7 +766,7 @@ impl service::users::Data for KeyValueDatabase {
Ok(())
}
pub fn get_to_device_events(
fn get_to_device_events(
&self,
user_id: &UserId,
device_id: &DeviceId,
@ -812,7 +788,7 @@ impl service::users::Data for KeyValueDatabase {
Ok(events)
}
pub fn remove_to_device_events(
fn remove_to_device_events(
&self,
user_id: &UserId,
device_id: &DeviceId,
@ -833,7 +809,7 @@ impl service::users::Data for KeyValueDatabase {
.map(|(key, _)| {
Ok::<_, Error>((
key.clone(),
utils::u64_from_bytes(&key[key.len() - mem::size_of::<u64>()..key.len()])
utils::u64_from_bytes(&key[key.len() - size_of::<u64>()..key.len()])
.map_err(|_| Error::bad_database("ToDeviceId has invalid count bytes."))?,
))
})
@ -846,7 +822,7 @@ impl service::users::Data for KeyValueDatabase {
Ok(())
}
pub fn update_device_metadata(
fn update_device_metadata(
&self,
user_id: &UserId,
device_id: &DeviceId,
@ -871,7 +847,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Get device metadata.
pub fn get_device_metadata(
fn get_device_metadata(
&self,
user_id: &UserId,
device_id: &DeviceId,
@ -889,7 +865,7 @@ impl service::users::Data for KeyValueDatabase {
})
}
pub fn get_devicelist_version(&self, user_id: &UserId) -> Result<Option<u64>> {
fn get_devicelist_version(&self, user_id: &UserId) -> Result<Option<u64>> {
self.userid_devicelistversion
.get(user_id.as_bytes())?
.map_or(Ok(None), |bytes| {
@ -899,7 +875,7 @@ impl service::users::Data for KeyValueDatabase {
})
}
pub fn all_devices_metadata<'a>(
fn all_devices_metadata<'a>(
&'a self,
user_id: &UserId,
) -> impl Iterator<Item = Result<Device>> + 'a {
@ -915,7 +891,7 @@ impl service::users::Data for KeyValueDatabase {
}
/// Creates a new sync filter. Returns the filter id.
pub fn create_filter(
fn create_filter(
&self,
user_id: &UserId,
filter: &IncomingFilterDefinition,
@ -934,7 +910,7 @@ impl service::users::Data for KeyValueDatabase {
Ok(filter_id)
}
pub fn get_filter(
fn get_filter(
&self,
user_id: &UserId,
filter_id: &str,