Remove unnecessary clones

master
Amanda Graven 2021-01-11 14:12:11 +01:00 committed by Damir Jelić
parent 077c20ed74
commit 9efece4f7a
1 changed files with 46 additions and 54 deletions

View File

@ -221,65 +221,57 @@ 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>
async fn calculate_name(&self) -> String { async fn calculate_name(&self) -> String {
let name; let summary = {
let alias;
{
let inner = self.inner.read().unwrap(); let inner = self.inner.read().unwrap();
name = inner.base_info.name.clone();
alias = inner.base_info.canonical_alias.clone();
}
if let Some(name) = name { if let Some(name) = &inner.base_info.name {
let name = name.trim(); let name = name.trim();
name.to_string() return name.to_string();
} else if let Some(alias) = alias { } else if let Some(alias) = &inner.base_info.canonical_alias {
let alias = alias.alias().trim(); let alias = alias.alias().trim();
alias.to_string() return alias.to_string();
}
inner.summary.clone()
};
// TODO what should we do here? We have correct counts only if lazy
// loading is used.
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_user_id = |u: &str| u == self.own_user_id().as_str();
let members: Vec<RoomMember> = if summary.heroes.is_empty() {
self.active_members()
.await
.filter(|m| future::ready(!is_own_member(m)))
.take(5)
.collect()
.await
} else { } else {
// TODO what should we do here? We have correct counts only if lazy stream::iter(summary.heroes.iter())
// loading is used. .filter(|u| future::ready(!is_own_user_id(u)))
let summary = { self.inner.read().unwrap().summary.clone() }; .filter_map(|u| async move {
let user_id = UserId::try_from(u.as_str()).ok()?;
self.get_member(&user_id).await
})
.collect()
.await
};
let joined = summary.joined_member_count; info!(
let invited = summary.invited_member_count; "Calculating name for {}, own user {} hero count {} heroes {:#?}",
let heroes_count = summary.heroes.len() as u64; self.room_id(),
self.own_user_id,
heroes_count,
summary.heroes
);
let is_own_member = |m: &RoomMember| m.user_id() == &*self.own_user_id; let inner = self.inner.read().unwrap();
let is_own_user_id = |u: &str| u == self.own_user_id().as_str(); inner
.base_info
let members: Vec<RoomMember> = if summary.heroes.is_empty() { .calculate_room_name(joined, invited, members)
self.active_members()
.await
.filter(|m| future::ready(!is_own_member(m)))
.take(5)
.collect()
.await
} else {
stream::iter(summary.heroes.iter())
.filter(|u| future::ready(!is_own_user_id(u)))
.filter_map(|u| async move {
let user_id = UserId::try_from(u.as_str()).ok()?;
self.get_member(&user_id).await
})
.collect()
.await
};
info!(
"Calculating name for {}, own user {} hero count {} heroes {:#?}",
self.room_id(),
self.own_user_id,
heroes_count,
summary.heroes
);
self.inner
.read()
.unwrap()
.base_info
.calculate_room_name(joined, invited, members)
}
} }
pub(crate) fn clone_info(&self) -> RoomInfo { pub(crate) fn clone_info(&self) -> RoomInfo {