Avoid possible panic in Room::most_active_project

Participants' locations might momentarily reference projects that have already been unshared.
This commit is contained in:
Max Brunsfeld 2023-10-09 15:04:01 -07:00
parent bdcbf9b92e
commit 1d29709c32

View file

@ -600,27 +600,30 @@ impl Room {
/// Returns the most 'active' projects, defined as most people in the project /// Returns the most 'active' projects, defined as most people in the project
pub fn most_active_project(&self) -> Option<(u64, u64)> { pub fn most_active_project(&self) -> Option<(u64, u64)> {
let mut projects = HashMap::default(); let mut project_hosts_and_guest_counts = HashMap::<u64, (Option<u64>, u32)>::default();
let mut hosts = HashMap::default();
for participant in self.remote_participants.values() { for participant in self.remote_participants.values() {
match participant.location { match participant.location {
ParticipantLocation::SharedProject { project_id } => { ParticipantLocation::SharedProject { project_id } => {
*projects.entry(project_id).or_insert(0) += 1; project_hosts_and_guest_counts
.entry(project_id)
.or_default()
.1 += 1;
} }
ParticipantLocation::External | ParticipantLocation::UnsharedProject => {} ParticipantLocation::External | ParticipantLocation::UnsharedProject => {}
} }
for project in &participant.projects { for project in &participant.projects {
*projects.entry(project.id).or_insert(0) += 1; project_hosts_and_guest_counts
hosts.insert(project.id, participant.user.id); .entry(project.id)
.or_default()
.0 = Some(participant.user.id);
} }
} }
let mut pairs: Vec<(u64, usize)> = projects.into_iter().collect(); project_hosts_and_guest_counts
pairs.sort_by_key(|(_, count)| *count as i32); .into_iter()
.filter_map(|(id, (host, guest_count))| Some((id, host?, guest_count)))
pairs .max_by_key(|(_, _, guest_count)| *guest_count)
.first() .map(|(id, host, _)| (id, host))
.map(|(project_id, _)| (*project_id, hosts[&project_id]))
} }
async fn handle_room_updated( async fn handle_room_updated(