diff --git a/src/client_server/directory.rs b/src/client_server/directory.rs index 5c93e22..0065e51 100644 --- a/src/client_server/directory.rs +++ b/src/client_server/directory.rs @@ -251,8 +251,7 @@ pub(crate) 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 e2a2d6c..5a8c7d2 100644 --- a/src/client_server/profile.rs +++ b/src/client_server/profile.rs @@ -37,10 +37,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>(( @@ -58,7 +57,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 @@ -77,7 +76,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 @@ -181,10 +182,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>(( @@ -202,7 +202,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 @@ -221,7 +221,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 6612e2f..2d5ad27 100644 --- a/src/client_server/sync.rs +++ b/src/client_server/sync.rs @@ -356,7 +356,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 e9dd661..6ea0abd 100644 --- a/src/database.rs +++ b/src/database.rs @@ -655,7 +655,7 @@ impl Database { if db.globals.database_version()? < 9 { // Update tokenids db layout - let batch = db + let mut iter = db .rooms .tokenids .iter() @@ -683,9 +683,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 4b47454..4245483 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) } }) @@ -1784,8 +1783,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, )?; @@ -1930,15 +1929,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. @@ -2491,16 +2489,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, @@ -2637,45 +2634,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) } } @@ -3477,10 +3472,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 e2b1344..b81610e 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(); @@ -1562,7 +1562,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(), @@ -1576,8 +1576,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.");