Don't hold lock during await in name calculation

master
Amanda Graven 2021-01-11 13:15:47 +01:00 committed by Damir Jelić
parent 643526987f
commit 6c4888a123
1 changed files with 9 additions and 7 deletions

View File

@ -220,7 +220,6 @@ impl Room {
/// ///
/// [spec]: /// [spec]:
/// <https://matrix.org/docs/spec/client_server/latest#calculating-the-display-name-for-a-room> /// <https://matrix.org/docs/spec/client_server/latest#calculating-the-display-name-for-a-room>
#[allow(clippy::await_holding_lock)]
async fn calculate_name(&self) -> String { async fn calculate_name(&self) -> String {
let inner = self.inner.read().unwrap(); let inner = self.inner.read().unwrap();
@ -233,14 +232,16 @@ impl Room {
} else { } else {
// TODO what should we do here? We have correct counts only if lazy // TODO what should we do here? We have correct counts only if lazy
// loading is used. // loading is used.
let joined = inner.summary.joined_member_count; let summary = inner.summary.clone();
let invited = inner.summary.invited_member_count; drop(inner);
let heroes_count = inner.summary.heroes.len() as u64; let joined = summary.joined_member_count;
let invited = summary.invited_member_count;
let heroes_count = summary.heroes.len() as u64;
let is_own_member = |m: &RoomMember| m.user_id() == &*self.own_user_id; let is_own_member = |m: &RoomMember| m.user_id() == &*self.own_user_id;
let is_own_user_id = |u: &str| u == self.own_user_id().as_str(); let is_own_user_id = |u: &str| u == self.own_user_id().as_str();
let members: Vec<RoomMember> = if inner.summary.heroes.is_empty() { let members: Vec<RoomMember> = if summary.heroes.is_empty() {
self.active_members() self.active_members()
.await .await
.filter(|m| future::ready(!is_own_member(m))) .filter(|m| future::ready(!is_own_member(m)))
@ -248,7 +249,7 @@ impl Room {
.collect() .collect()
.await .await
} else { } else {
stream::iter(inner.summary.heroes.iter()) stream::iter(summary.heroes.iter())
.filter(|u| future::ready(!is_own_user_id(u))) .filter(|u| future::ready(!is_own_user_id(u)))
.filter_map(|u| async move { .filter_map(|u| async move {
let user_id = UserId::try_from(u.as_str()).ok()?; let user_id = UserId::try_from(u.as_str()).ok()?;
@ -263,9 +264,10 @@ impl Room {
self.room_id(), self.room_id(),
self.own_user_id, self.own_user_id,
heroes_count, heroes_count,
inner.summary.heroes summary.heroes
); );
let inner = self.inner.read().unwrap();
inner inner
.base_info .base_info
.calculate_room_name(joined, invited, members) .calculate_room_name(joined, invited, members)