From a6bb9bbe68dd023e2299b37eba13ad0df256a671 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 31 Aug 2021 21:20:03 +0200 Subject: [PATCH] Fix a bunch of clippy lints --- src/client_server/directory.rs | 3 +- src/client_server/profile.rs | 26 +++---- src/client_server/sync.rs | 2 +- src/database.rs | 6 +- src/database/abstraction/sqlite.rs | 4 +- src/database/account_data.rs | 2 +- src/database/rooms.rs | 110 +++++++++++++---------------- src/server_server.rs | 9 +-- 8 files changed, 77 insertions(+), 85 deletions(-) diff --git a/src/client_server/directory.rs b/src/client_server/directory.rs index 589aacd..e913da0 100644 --- a/src/client_server/directory.rs +++ b/src/client_server/directory.rs @@ -233,8 +233,7 @@ pub async fn get_public_rooms_filtered_helper( .map_err(|_| { Error::bad_database("Invalid room name event in database.") })? - .name - .map(|n| n.to_owned().into())) + .name) })?, num_joined_members: db .rooms diff --git a/src/client_server/profile.rs b/src/client_server/profile.rs index de1baba..1ffb81c 100644 --- a/src/client_server/profile.rs +++ b/src/client_server/profile.rs @@ -32,10 +32,9 @@ pub async fn set_displayname_route( .set_displayname(&sender_user, body.displayname.clone())?; // Send a new membership event and presence update into all joined rooms - let all_rooms_joined = db.rooms.rooms_joined(&sender_user).collect::>(); - - for (pdu_builder, room_id) in all_rooms_joined - .into_iter() + let all_rooms_joined: Vec<_> = db + .rooms + .rooms_joined(&sender_user) .filter_map(|r| r.ok()) .map(|room_id| { Ok::<_, Error>(( @@ -53,7 +52,7 @@ pub async fn set_displayname_route( .ok_or_else(|| { Error::bad_database( "Tried to send displayname update for user not in the \ - room.", + room.", ) })? .content @@ -72,7 +71,9 @@ pub async fn set_displayname_route( )) }) .filter_map(|r| r.ok()) - { + .collect(); + + for (pdu_builder, room_id) in all_rooms_joined { let mutex_state = Arc::clone( db.globals .roomid_mutex_state @@ -166,10 +167,9 @@ pub async fn set_avatar_url_route( db.users.set_blurhash(&sender_user, body.blurhash.clone())?; // Send a new membership event and presence update into all joined rooms - let all_joined_rooms = db.rooms.rooms_joined(&sender_user).collect::>(); - - for (pdu_builder, room_id) in all_joined_rooms - .into_iter() + let all_joined_rooms: Vec<_> = db + .rooms + .rooms_joined(&sender_user) .filter_map(|r| r.ok()) .map(|room_id| { Ok::<_, Error>(( @@ -187,7 +187,7 @@ pub async fn set_avatar_url_route( .ok_or_else(|| { Error::bad_database( "Tried to send displayname update for user not in the \ - room.", + room.", ) })? .content @@ -206,7 +206,9 @@ pub async fn set_avatar_url_route( )) }) .filter_map(|r| r.ok()) - { + .collect(); + + for (pdu_builder, room_id) in all_joined_rooms { let mutex_state = Arc::clone( db.globals .roomid_mutex_state diff --git a/src/client_server/sync.rs b/src/client_server/sync.rs index f7f2454..d3470d9 100644 --- a/src/client_server/sync.rs +++ b/src/client_server/sync.rs @@ -335,7 +335,7 @@ async fn sync_helper( true, state_events, ) - } else if timeline_pdus.len() == 0 && since_shortstatehash == Some(current_shortstatehash) { + } else if timeline_pdus.is_empty() && since_shortstatehash == Some(current_shortstatehash) { // No state changes (Vec::new(), None, None, false, Vec::new()) } else { diff --git a/src/database.rs b/src/database.rs index 79571f6..7abddbb 100644 --- a/src/database.rs +++ b/src/database.rs @@ -644,7 +644,7 @@ impl Database { if db.globals.database_version()? < 9 { // Update tokenids db layout - let batch = db + let mut iter = db .rooms .tokenids .iter() @@ -672,9 +672,7 @@ impl Database { println!("new {:?}", new_key); Some((new_key, Vec::new())) }) - .collect::>(); - - let mut iter = batch.into_iter().peekable(); + .peekable(); while iter.peek().is_some() { db.rooms diff --git a/src/database/abstraction/sqlite.rs b/src/database/abstraction/sqlite.rs index 99deeba..06e371e 100644 --- a/src/database/abstraction/sqlite.rs +++ b/src/database/abstraction/sqlite.rs @@ -65,12 +65,12 @@ impl Engine { self.writer.lock() } - fn read_lock<'a>(&'a self) -> &'a Connection { + fn read_lock(&self) -> &Connection { self.read_conn_tls .get_or(|| Self::prepare_conn(&self.path, self.cache_size_per_thread).unwrap()) } - fn read_lock_iterator<'a>(&'a self) -> &'a Connection { + fn read_lock_iterator(&self) -> &Connection { self.read_iterator_conn_tls .get_or(|| Self::prepare_conn(&self.path, self.cache_size_per_thread).unwrap()) } diff --git a/src/database/account_data.rs b/src/database/account_data.rs index e1d4c62..1a8ad76 100644 --- a/src/database/account_data.rs +++ b/src/database/account_data.rs @@ -40,7 +40,7 @@ impl AccountData { roomuserdataid.push(0xff); roomuserdataid.extend_from_slice(&event_type.as_bytes()); - let mut key = prefix.clone(); + let mut key = prefix; key.extend_from_slice(event_type.as_bytes()); let json = serde_json::to_value(data).expect("all types here can be serialized"); // TODO: maybe add error handling diff --git a/src/database/rooms.rs b/src/database/rooms.rs index 59ed950..4d47694 100644 --- a/src/database/rooms.rs +++ b/src/database/rooms.rs @@ -385,7 +385,7 @@ impl Rooms { self.save_state_from_diff( new_shortstatehash, statediffnew.clone(), - statediffremoved.clone(), + statediffremoved, 2, // every state change is 2 event changes on average states_parents, )?; @@ -497,8 +497,7 @@ impl Rooms { Ok(response) } else { - let mut response = Vec::new(); - response.push((shortstatehash, added.clone(), added, removed)); + let response = vec![(shortstatehash, added.clone(), added, removed)]; self.stateinfo_cache .lock() .unwrap() @@ -609,7 +608,7 @@ impl Rooms { return Ok(()); } - if parent_states.len() == 0 { + if parent_states.is_empty() { // There is no parent layer, create a new state let mut value = 0_u64.to_be_bytes().to_vec(); // 0 means no parent for new in &statediffnew { @@ -689,7 +688,7 @@ impl Rooms { state_hash: &StateHashId, globals: &super::globals::Globals, ) -> Result<(u64, bool)> { - Ok(match self.statehash_shortstatehash.get(&state_hash)? { + Ok(match self.statehash_shortstatehash.get(state_hash)? { Some(shortstatehash) => ( utils::u64_from_bytes(&shortstatehash) .map_err(|_| Error::bad_database("Invalid shortstatehash in db."))?, @@ -698,7 +697,7 @@ impl Rooms { None => { let shortstatehash = globals.next_count()?; self.statehash_shortstatehash - .insert(&state_hash, &shortstatehash.to_be_bytes())?; + .insert(state_hash, &shortstatehash.to_be_bytes())?; (shortstatehash, false) } }) @@ -1768,8 +1767,8 @@ impl Rooms { }; self.save_state_from_diff( shortstatehash, - statediffnew.clone(), - statediffremoved.clone(), + statediffnew, + statediffremoved, 1_000_000, // high number because no state will be based on this one states_parents, )?; @@ -1914,15 +1913,14 @@ impl Rooms { let mut key = shortroomid.to_be_bytes().to_vec(); key.extend_from_slice(&token.to_be_bytes()); - Ok(self - .roomsynctoken_shortstatehash + self.roomsynctoken_shortstatehash .get(&key)? .map(|bytes| { utils::u64_from_bytes(&bytes).map_err(|_| { Error::bad_database("Invalid shortstatehash in roomsynctoken_shortstatehash") }) }) - .transpose()?) + .transpose() } /// Creates a new persisted data unit and adds it to a room. @@ -2475,16 +2473,15 @@ impl Rooms { self.roomuserid_leftcount.remove(&roomuser_id)?; } member::MembershipState::Leave | member::MembershipState::Ban => { - if update_joined_count { - if self + if update_joined_count + && self .room_members(room_id) .chain(self.room_members_invited(room_id)) .filter_map(|r| r.ok()) .all(|u| u.server_name() != user_id.server_name()) - { - self.roomserverids.remove(&roomserver_id)?; - self.serverroomids.remove(&serverroom_id)?; - } + { + self.roomserverids.remove(&roomserver_id)?; + self.serverroomids.remove(&serverroom_id)?; } self.userroomid_leftstate.insert( &userroom_id, @@ -2621,45 +2618,43 @@ impl Rooms { if let Some(b) = maybe { Ok(b) + } else if let Some(namespaces) = appservice.1.get("namespaces") { + let users = namespaces + .get("users") + .and_then(|users| users.as_sequence()) + .map_or_else(Vec::new, |users| { + users + .iter() + .filter_map(|users| Regex::new(users.get("regex")?.as_str()?).ok()) + .collect::>() + }); + + let bridge_user_id = appservice + .1 + .get("sender_localpart") + .and_then(|string| string.as_str()) + .and_then(|string| { + UserId::parse_with_server_name(string, db.globals.server_name()).ok() + }); + + let in_room = bridge_user_id + .map_or(false, |id| self.is_joined(&id, room_id).unwrap_or(false)) + || self.room_members(&room_id).any(|userid| { + userid.map_or(false, |userid| { + users.iter().any(|r| r.is_match(userid.as_str())) + }) + }); + + self.appservice_in_room_cache + .write() + .unwrap() + .entry(room_id.clone()) + .or_default() + .insert(appservice.0.clone(), in_room); + + Ok(in_room) } else { - if let Some(namespaces) = appservice.1.get("namespaces") { - let users = namespaces - .get("users") - .and_then(|users| users.as_sequence()) - .map_or_else(Vec::new, |users| { - users - .iter() - .filter_map(|users| Regex::new(users.get("regex")?.as_str()?).ok()) - .collect::>() - }); - - let bridge_user_id = appservice - .1 - .get("sender_localpart") - .and_then(|string| string.as_str()) - .and_then(|string| { - UserId::parse_with_server_name(string, db.globals.server_name()).ok() - }); - - let in_room = bridge_user_id - .map_or(false, |id| self.is_joined(&id, room_id).unwrap_or(false)) - || self.room_members(&room_id).any(|userid| { - userid.map_or(false, |userid| { - users.iter().any(|r| r.is_match(userid.as_str())) - }) - }); - - self.appservice_in_room_cache - .write() - .unwrap() - .entry(room_id.clone()) - .or_default() - .insert(appservice.0.clone(), in_room); - - Ok(in_room) - } else { - Ok(false) - } + Ok(false) } } @@ -3452,10 +3447,7 @@ impl Rooms { } // Cache in RAM - self.auth_chain_cache - .lock() - .unwrap() - .insert(key.clone(), chain); + self.auth_chain_cache.lock().unwrap().insert(key, chain); Ok(()) } diff --git a/src/server_server.rs b/src/server_server.rs index b965fcf..24e8e4b 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -147,7 +147,7 @@ where let result = find_actual_destination(globals, &destination).await; - (result.0, result.1.clone().into_uri_string()) + (result.0, result.1.into_uri_string()) }; let actual_destination_str = actual_destination.clone().into_https_string(); @@ -1529,7 +1529,7 @@ async fn upgrade_outlier_to_timeline_pdu( None }; - if !state_res::event_auth::auth_check( + let check_result = state_res::event_auth::auth_check( &room_version, &incoming_pdu, previous_create.clone(), @@ -1543,8 +1543,9 @@ async fn upgrade_outlier_to_timeline_pdu( .and_then(|event_id| db.rooms.get_pdu(&event_id).ok().flatten()) }, ) - .map_err(|_e| "Auth check failed.".to_owned())? - { + .map_err(|_e| "Auth check failed.".to_owned())?; + + if !check_result { return Err("Event has failed auth check with state at the event.".into()); } debug!("Auth check succeeded.");