diff --git a/src/client_server/backup.rs b/src/client_server/backup.rs index f33d0de..12f3bfd 100644 --- a/src/client_server/backup.rs +++ b/src/client_server/backup.rs @@ -267,12 +267,10 @@ pub async fn get_backup_key_session_route( let key_data = db .key_backups .get_session(&sender_user, &body.version, &body.room_id, &body.session_id)? - .ok_or_else(|| { - Error::BadRequest( - ErrorKind::NotFound, - "Backup key not found for this user's session.", - ) - })?; + .ok_or(Error::BadRequest( + ErrorKind::NotFound, + "Backup key not found for this user's session.", + ))?; Ok(get_backup_key_session::Response { key_data }.into()) } diff --git a/src/client_server/config.rs b/src/client_server/config.rs index 6abcba2..68cd2e0 100644 --- a/src/client_server/config.rs +++ b/src/client_server/config.rs @@ -3,7 +3,10 @@ use crate::{ConduitResult, Database, Error, Ruma}; use ruma::{ api::client::{ error::ErrorKind, - r0::config::{get_room_account_data, get_global_account_data, set_room_account_data, set_global_account_data}, + r0::config::{ + get_global_account_data, get_room_account_data, set_global_account_data, + set_room_account_data, + }, }, events::{custom::CustomEventContent, BasicEvent}, serde::Raw, @@ -45,7 +48,10 @@ pub async fn set_global_account_data_route( #[cfg_attr( feature = "conduit_bin", - put("/_matrix/client/r0/user/<_>/rooms/<_>/account_data/<_>", data = "") + put( + "/_matrix/client/r0/user/<_>/rooms/<_>/account_data/<_>", + data = "" + ) )] #[tracing::instrument(skip(db, body))] pub async fn set_room_account_data_route( @@ -97,7 +103,10 @@ pub async fn get_global_account_data_route( #[cfg_attr( feature = "conduit_bin", - get("/_matrix/client/r0/user/<_>/rooms/<_>/account_data/<_>", data = "") + get( + "/_matrix/client/r0/user/<_>/rooms/<_>/account_data/<_>", + data = "" + ) )] #[tracing::instrument(skip(db, body))] pub async fn get_room_account_data_route( @@ -108,7 +117,11 @@ pub async fn get_room_account_data_route( let data = db .account_data - .get::>(Some(&body.room_id), sender_user, body.event_type.clone().into())? + .get::>( + Some(&body.room_id), + sender_user, + body.event_type.clone().into(), + )? .ok_or(Error::BadRequest(ErrorKind::NotFound, "Data not found."))?; db.flush().await?; diff --git a/src/client_server/sync.rs b/src/client_server/sync.rs index da2ddf4..d38699c 100644 --- a/src/client_server/sync.rs +++ b/src/client_server/sync.rs @@ -136,9 +136,7 @@ pub async fn sync_events_route( .map(|since_shortstatehash| { Ok::<_, Error>( since_shortstatehash - .map(|since_shortstatehash| { - db.rooms.state_full(&room_id, since_shortstatehash) - }) + .map(|since_shortstatehash| db.rooms.state_full(since_shortstatehash)) .transpose()?, ) }) @@ -512,12 +510,7 @@ pub async fn sync_events_route( }) .and_then(|shortstatehash| { db.rooms - .state_get( - &room_id, - shortstatehash, - &EventType::RoomMember, - sender_user.as_str(), - ) + .state_get(shortstatehash, &EventType::RoomMember, sender_user.as_str()) .ok()? .ok_or_else(|| Error::bad_database("State hash in db doesn't have a state.")) .ok() diff --git a/src/client_server/thirdparty.rs b/src/client_server/thirdparty.rs index fe5b784..5d3c540 100644 --- a/src/client_server/thirdparty.rs +++ b/src/client_server/thirdparty.rs @@ -1,7 +1,6 @@ use crate::ConduitResult; use ruma::api::client::r0::thirdparty::get_protocols; -use log::warn; #[cfg(feature = "conduit_bin")] use rocket::get; use std::collections::BTreeMap; diff --git a/src/database/appservice.rs b/src/database/appservice.rs index 764291d..222eb18 100644 --- a/src/database/appservice.rs +++ b/src/database/appservice.rs @@ -55,9 +55,7 @@ impl Appservice { }) } - pub fn iter_all<'a>( - &'a self, - ) -> impl Iterator> + 'a { + pub fn iter_all(&self) -> impl Iterator> + '_ { self.iter_ids().filter_map(|id| id.ok()).map(move |id| { Ok(( id.clone(), diff --git a/src/database/media.rs b/src/database/media.rs index f958dc8..37fcb74 100644 --- a/src/database/media.rs +++ b/src/database/media.rs @@ -262,7 +262,7 @@ impl Media { } }; - image.thumbnail_exact(dbg!(exact_width), dbg!(exact_height)) + image.thumbnail_exact(exact_width, exact_height) }; let mut thumbnail_bytes = Vec::new(); diff --git a/src/database/rooms.rs b/src/database/rooms.rs index 91f468f..175d4ac 100644 --- a/src/database/rooms.rs +++ b/src/database/rooms.rs @@ -108,7 +108,6 @@ impl Rooms { pub fn state_full( &self, - room_id: &RoomId, shortstatehash: u64, ) -> Result> { Ok(self @@ -151,7 +150,6 @@ impl Rooms { #[tracing::instrument(skip(self))] pub fn state_get( &self, - room_id: &RoomId, shortstatehash: u64, event_type: &EventType, state_key: &str, @@ -257,11 +255,11 @@ impl Rooms { /// Generate a new StateHash. /// /// A unique hash made from hashing all PDU ids of the state joined with 0xff. - fn calculate_hash(&self, bytes_list: &[&[u8]]) -> Result { + fn calculate_hash(&self, bytes_list: &[&[u8]]) -> StateHashId { // We only hash the pdu's event ids, not the whole pdu let bytes = bytes_list.join(&0xff); let hash = digest::digest(&digest::SHA256, &bytes); - Ok(hash.as_ref().into()) + hash.as_ref().into() } /// Checks if a room exists. @@ -291,7 +289,7 @@ impl Rooms { .values() .map(|event_id| event_id.as_bytes()) .collect::>(), - )?; + ); let shortstatehash = match self.statehash_shortstatehash.get(&state_hash)? { Some(shortstatehash) => { @@ -353,7 +351,7 @@ impl Rooms { room_id: &RoomId, ) -> Result> { if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? { - self.state_full(&room_id, current_shortstatehash) + self.state_full(current_shortstatehash) } else { Ok(BTreeMap::new()) } @@ -368,7 +366,7 @@ impl Rooms { state_key: &str, ) -> Result> { if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? { - self.state_get(&room_id, current_shortstatehash, event_type, state_key) + self.state_get(current_shortstatehash, event_type, state_key) } else { Ok(None) } @@ -582,7 +580,7 @@ impl Rooms { { if let Some(shortstatehash) = self.pdu_shortstatehash(&pdu.event_id).unwrap() { if let Some(prev_state) = self - .state_get(&pdu.room_id, shortstatehash, &pdu.kind, &state_key) + .state_get(shortstatehash, &pdu.kind, &state_key) .unwrap() { unsigned.insert( @@ -849,7 +847,7 @@ impl Rooms { .values() .map(|event_id| &**event_id) .collect::>(), - )?; + ); let shortstatehash = match self.statehash_shortstatehash.get(&new_state_hash)? { Some(shortstatehash) => { diff --git a/src/database/sending.rs b/src/database/sending.rs index a9204c5..1cc2f91 100644 --- a/src/database/sending.rs +++ b/src/database/sending.rs @@ -1,6 +1,6 @@ use std::{ collections::HashMap, - convert::{TryFrom, TryInto}, + convert::TryFrom, fmt::Debug, sync::Arc, time::{Duration, Instant, SystemTime}, @@ -10,11 +10,11 @@ use crate::{ appservice_server, database::pusher, server_server, utils, Database, Error, PduEvent, Result, }; use federation::transactions::send_transaction_message; -use log::{error, info, warn}; +use log::{info, warn}; use ring::digest; use rocket::futures::stream::{FuturesUnordered, StreamExt}; use ruma::{ - api::{appservice, client::r0::push::Pusher, federation, OutgoingRequest}, + api::{appservice, federation, OutgoingRequest}, events::{push_rules, EventType}, uint, ServerName, UInt, UserId, }; @@ -264,7 +264,7 @@ impl Sending { futures.push( Self::handle_event( outgoing_kind, - vec![pdu_id.into()], + vec![pdu_id], &db, ) ); @@ -395,18 +395,19 @@ impl Sending { continue; } - let userid = UserId::try_from(utils::string_from_bytes(user).map_err(|e| { - ( - OutgoingKind::Push(user.clone(), pushkey.clone()), - Error::bad_database("Invalid push user string in db."), - ) - })?) - .map_err(|e| { - ( - OutgoingKind::Push(user.clone(), pushkey.clone()), - Error::bad_database("Invalid push user id in db."), - ) - })?; + let userid = + UserId::try_from(utils::string_from_bytes(user).map_err(|_| { + ( + OutgoingKind::Push(user.clone(), pushkey.clone()), + Error::bad_database("Invalid push user string in db."), + ) + })?) + .map_err(|_| { + ( + OutgoingKind::Push(user.clone(), pushkey.clone()), + Error::bad_database("Invalid push user id in db."), + ) + })?; let mut senderkey = user.clone(); senderkey.push(0xff); diff --git a/src/server_server.rs b/src/server_server.rs index 4912878..8babc89 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -21,10 +21,9 @@ use ruma::{ }, directory::{IncomingFilter, IncomingRoomNetwork}, events::EventType, - identifiers::{KeyId, KeyName}, serde::to_canonical_value, signatures::{CanonicalJsonObject, CanonicalJsonValue, PublicKeyMap}, - EventId, RoomId, RoomVersionId, ServerName, ServerSigningKeyId, SigningKeyAlgorithm, UserId, + EventId, RoomId, RoomVersionId, ServerName, ServerSigningKeyId, UserId, }; use state_res::{Event, EventMap, StateMap}; use std::{ @@ -788,23 +787,17 @@ pub async fn send_transaction_message_route<'a>( // This will create the state after any state snapshot it builds // So current_state will have the incoming event inserted to it - let mut fork_states = match build_forward_extremity_snapshots( - &db, - pdu.clone(), - server_name, - current_state, - &extremities, - &pub_key_map, - &mut auth_cache, - ) - .await - { - Ok(states) => states, - Err(_) => { - resolved_map.insert(event_id, Err("Failed to gather forward extremities".into())); - continue; - } - }; + let mut fork_states = + match build_forward_extremity_snapshots(&db, pdu.clone(), current_state, &extremities) + .await + { + Ok(states) => states, + Err(_) => { + resolved_map + .insert(event_id, Err("Failed to gather forward extremities".into())); + continue; + } + }; // Make this the state after. let mut state_after = state_at_event.clone(); @@ -1320,11 +1313,8 @@ pub(crate) async fn calculate_forward_extremities( pub(crate) async fn build_forward_extremity_snapshots( db: &Database, pdu: Arc, - origin: &ServerName, mut current_state: StateMap>, current_leaves: &[EventId], - pub_key_map: &PublicKeyMap, - auth_cache: &mut EventMap>, ) -> Result>>> { let current_shortstatehash = db.rooms.current_shortstatehash(pdu.room_id())?; @@ -1354,7 +1344,7 @@ pub(crate) async fn build_forward_extremity_snapshots( let mut state_before = db .rooms - .state_full(pdu.room_id(), pdu_shortstatehash)? + .state_full(pdu_shortstatehash)? .into_iter() .map(|(k, v)| ((k.0, Some(k.1)), Arc::new(v))) .collect::>(); @@ -1396,9 +1386,9 @@ pub(crate) fn update_resolved_state( new_state.insert( ( ev_type, - state_k.ok_or_else(|| { - Error::Conflict("update_resolved_state: State contained non state event") - })?, + state_k.ok_or(Error::Conflict( + "update_resolved_state: State contained non state event", + ))?, ), pdu.event_id.clone(), ); @@ -1426,9 +1416,9 @@ pub(crate) fn append_incoming_pdu( new_state.insert( ( ev_type.clone(), - state_k.clone().ok_or_else(|| { - Error::Conflict("append_incoming_pdu: State contained non state event") - })?, + state_k.clone().ok_or(Error::Conflict( + "append_incoming_pdu: State contained non state event", + ))?, ), state_pdu.event_id.clone(), ); @@ -1600,26 +1590,22 @@ pub fn get_room_state_ids_route<'a>( let mut todo = BTreeSet::new(); todo.insert(body.event_id.clone()); - loop { - if let Some(event_id) = todo.iter().next().cloned() { - if let Some(pdu) = db.rooms.get_pdu(&event_id)? { - todo.extend( - pdu.auth_events - .clone() - .into_iter() - .collect::>() - .difference(&auth_chain_ids) - .cloned(), - ); - auth_chain_ids.extend(pdu.auth_events.into_iter()); - } else { - warn!("Could not find pdu mentioned in auth events."); - } - - todo.remove(&event_id); + while let Some(event_id) = todo.iter().next().cloned() { + if let Some(pdu) = db.rooms.get_pdu(&event_id)? { + todo.extend( + pdu.auth_events + .clone() + .into_iter() + .collect::>() + .difference(&auth_chain_ids) + .cloned(), + ); + auth_chain_ids.extend(pdu.auth_events.into_iter()); } else { - break; + warn!("Could not find pdu mentioned in auth events."); } + + todo.remove(&event_id); } Ok(get_room_state_ids::v1::Response {