From c1953efa6bd3b8e354bc0bcff372072694f5b043 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20K=C3=B6sters?= Date: Wed, 14 Apr 2021 10:43:31 +0200 Subject: [PATCH] chore: code cleanup --- src/client_server/membership.rs | 7 +--- src/database/pusher.rs | 2 +- src/database/rooms.rs | 71 ++++++++++++++++++--------------- src/server_server.rs | 33 +++++++-------- src/utils.rs | 2 +- 5 files changed, 59 insertions(+), 56 deletions(-) diff --git a/src/client_server/membership.rs b/src/client_server/membership.rs index dad43289..0da0747f 100644 --- a/src/client_server/membership.rs +++ b/src/client_server/membership.rs @@ -568,9 +568,7 @@ async fn join_room_by_id_helper( serde_json::from_value::( pdu.content .get("membership") - .ok_or_else(|| { - Error::BadServerResponse("Invalid member event content") - })? + .ok_or(Error::BadServerResponse("Invalid member event content"))? .clone(), ) .map_err(|_| { @@ -578,8 +576,7 @@ async fn join_room_by_id_helper( })?, &pdu.sender, Some(invite_state), - &db.account_data, - &db.globals, + db, )?; } state.insert((pdu.kind.clone(), state_key.clone()), pdu.event_id.clone()); diff --git a/src/database/pusher.rs b/src/database/pusher.rs index be305763..c2043863 100644 --- a/src/database/pusher.rs +++ b/src/database/pusher.rs @@ -222,7 +222,7 @@ pub fn get_actions<'a>( user_display_name: db .users .displayname(&user)? - .unwrap_or(user.localpart().to_owned()), + .unwrap_or_else(|| user.localpart().to_owned()), users_power_levels: power_levels.users, default_power_level: power_levels.users_default, notification_power_levels: power_levels.notifications, diff --git a/src/database/rooms.rs b/src/database/rooms.rs index caf7a095..50533609 100644 --- a/src/database/rooms.rs +++ b/src/database/rooms.rs @@ -706,14 +706,14 @@ impl Rooms { userroom_id.extend_from_slice(pdu.room_id.as_bytes()); if notify { - &self + self .userroomid_notificationcount .update_and_fetch(&userroom_id, utils::increment)? .expect("utils::increment will always put in a value"); } if highlight { - &self + self .userroomid_highlightcount .update_and_fetch(&userroom_id, utils::increment)? .expect("utils::increment will always put in a value"); @@ -743,12 +743,10 @@ impl Rooms { let membership = serde_json::from_value::( pdu.content .get("membership") - .ok_or_else(|| { - Error::BadRequest( - ErrorKind::InvalidParam, - "Invalid member event content", - ) - })? + .ok_or(Error::BadRequest( + ErrorKind::InvalidParam, + "Invalid member event content", + ))? .clone(), ) .map_err(|_| { @@ -807,8 +805,7 @@ impl Rooms { membership, &pdu.sender, invite_state, - &db.account_data, - &db.globals, + db, )?; } } @@ -1205,7 +1202,7 @@ impl Rooms { .iter() .filter_map(|event_id| Some(self.get_pdu(event_id).ok()??.depth)) .max() - .unwrap_or(uint!(0)) + .unwrap_or_else(|| uint!(0)) + uint!(1); let mut unsigned = unsigned.unwrap_or_default(); @@ -1542,8 +1539,7 @@ impl Rooms { membership: member::MembershipState, sender: &UserId, last_state: Option>>, - account_data: &super::account_data::AccountData, - globals: &super::globals::Globals, + db: &Database, ) -> Result<()> { let mut roomserver_id = room_id.as_bytes().to_vec(); roomserver_id.push(0xff); @@ -1603,23 +1599,32 @@ impl Rooms { // .ok(); // Copy old tags to new room - if let Some(tag_event) = account_data.get::( - Some(&predecessor.room_id), - user_id, - EventType::Tag, - )? { - account_data - .update(Some(room_id), user_id, EventType::Tag, &tag_event, globals) + if let Some(tag_event) = + db.account_data.get::( + Some(&predecessor.room_id), + user_id, + EventType::Tag, + )? + { + db.account_data + .update( + Some(room_id), + user_id, + EventType::Tag, + &tag_event, + &db.globals, + ) .ok(); }; // Copy direct chat flag - if let Some(mut direct_event) = account_data - .get::( - None, - user_id, - EventType::Direct, - )? { + if let Some(mut direct_event) = + db.account_data.get::( + None, + user_id, + EventType::Direct, + )? + { let mut room_ids_updated = false; for room_ids in direct_event.content.0.values_mut() { @@ -1630,12 +1635,12 @@ impl Rooms { } if room_ids_updated { - account_data.update( + db.account_data.update( None, user_id, EventType::Direct, &direct_event, - globals, + &db.globals, )?; } }; @@ -1652,7 +1657,8 @@ impl Rooms { } member::MembershipState::Invite => { // We want to know if the sender is ignored by the receiver - let is_ignored = account_data + let is_ignored = db + .account_data .get::( None, // Ignored users are in global account data &user_id, // Receiver @@ -1673,7 +1679,7 @@ impl Rooms { .expect("state to bytes always works"), )?; self.roomuserid_invitecount - .insert(&roomuser_id, &globals.next_count()?.to_be_bytes())?; + .insert(&roomuser_id, &db.globals.next_count()?.to_be_bytes())?; self.userroomid_joined.remove(&userroom_id)?; self.roomuserid_joined.remove(&roomuser_id)?; self.userroomid_leftstate.remove(&userroom_id)?; @@ -1693,7 +1699,7 @@ impl Rooms { serde_json::to_vec(&Vec::>::new()).unwrap(), )?; // TODO self.roomuserid_leftcount - .insert(&roomuser_id, &globals.next_count()?.to_be_bytes())?; + .insert(&roomuser_id, &db.globals.next_count()?.to_be_bytes())?; self.userroomid_joined.remove(&userroom_id)?; self.roomuserid_joined.remove(&roomuser_id)?; self.userroomid_invitestate.remove(&userroom_id)?; @@ -1729,8 +1735,7 @@ impl Rooms { MembershipState::Leave, user_id, last_state, - &db.account_data, - &db.globals, + db, )?; } else { let mut event = serde_json::from_value::>( diff --git a/src/server_server.rs b/src/server_server.rs index 538540a3..5b49472a 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -1413,15 +1413,16 @@ pub fn get_missing_events_route<'a>( let mut i = 0; while i < queued_events.len() && events.len() < u64::from(body.limit) as usize { if let Some(pdu) = db.rooms.get_pdu_json(&queued_events[i])? { - if body.earliest_events.contains( - &serde_json::from_value( + let event_id = + serde_json::from_value( serde_json::to_value(pdu.get("event_id").cloned().ok_or_else(|| { Error::bad_database("Event in db has no event_id field.") })?) .expect("canonical json is valid json value"), ) - .map_err(|_| Error::bad_database("Invalid event_id field in pdu in db."))?, - ) { + .map_err(|_| Error::bad_database("Invalid event_id field in pdu in db."))?; + + if body.earliest_events.contains(&event_id) { i += 1; continue; } @@ -1541,9 +1542,10 @@ pub async fn create_invite_route<'a>( serde_json::to_value( signed_event .get("sender") - .ok_or_else(|| { - Error::BadRequest(ErrorKind::InvalidParam, "Event had no sender field.") - })? + .ok_or(Error::BadRequest( + ErrorKind::InvalidParam, + "Event had no sender field.", + ))? .clone(), ) .expect("CanonicalJsonValue to serde_json::Value always works"), @@ -1553,9 +1555,10 @@ pub async fn create_invite_route<'a>( serde_json::to_value( signed_event .get("state_key") - .ok_or_else(|| { - Error::BadRequest(ErrorKind::InvalidParam, "Event had no state_key field.") - })? + .ok_or(Error::BadRequest( + ErrorKind::InvalidParam, + "Event had no state_key field.", + ))? .clone(), ) .expect("CanonicalJsonValue to serde_json::Value always works"), @@ -1586,8 +1589,7 @@ pub async fn create_invite_route<'a>( MembershipState::Invite, &sender, Some(invite_state), - &db.account_data, - &db.globals, + &db, )?; } @@ -1638,10 +1640,9 @@ pub async fn fetch_required_signing_keys( ) -> Result<()> { // We go through all the signatures we see on the value and fetch the corresponding signing // keys - for (signature_server, signature) in match event - .get("signatures") - .ok_or_else(|| Error::BadServerResponse("No signatures in server response pdu."))? - { + for (signature_server, signature) in match event.get("signatures").ok_or( + Error::BadServerResponse("No signatures in server response pdu."), + )? { CanonicalJsonValue::Object(map) => map, _ => { return Err(Error::BadServerResponse( diff --git a/src/utils.rs b/src/utils.rs index 45d9de8d..106baffd 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -69,7 +69,7 @@ pub fn calculate_hash(password: &str) -> Result { argon2::hash_encoded(password.as_bytes(), salt.as_bytes(), &hashing_config) } -pub fn common_elements<'a>( +pub fn common_elements( mut iterators: impl Iterator>>, check_order: impl Fn(&[u8], &[u8]) -> Ordering, ) -> Option>> {