Refactor member_display_name.

Make it more readable, add comments.
master
Denis Kasak 2020-06-09 23:02:01 +02:00
parent ac069152b9
commit e4977d1d2a
1 changed files with 23 additions and 17 deletions

View File

@ -304,29 +304,35 @@ impl Room {
/// Get the disambiguated display name for a member of this room. /// Get the disambiguated display name for a member of this room.
/// ///
/// If a user has no display name set, returns the MXID as a fallback. /// If a member has no display name set, returns the MXID as a fallback. Additionally, we
/// return the MXID even if there is no such member in the room.
/// ///
/// When displaying a room member's display name, clients *must* use this method to obtain the /// When displaying a room member's display name, clients *must* use this method to obtain the
/// name instead of displaying the `RoomMember::display_name` directly. This is because /// name instead of displaying the `RoomMember::display_name` directly. This is because
/// multiple users can share the same display name in which case the display name has to be /// multiple members can share the same display name in which case the display name has to be
/// disambiguated. /// disambiguated.
pub fn member_display_name<'a>(&'a self, id: &'a UserId) -> Cow<'a, str> { pub fn member_display_name<'a>(&'a self, id: &'a UserId) -> Cow<'a, str> {
self.disambiguated_display_names let disambiguated_name = self
.disambiguated_display_names
.get(id) .get(id)
.map(|s| s.as_str().into()) .map(|s| s.as_str().into());
.unwrap_or_else(|| {
self.members if let Some(name) = disambiguated_name {
.get(id) // The display name of the member is non-unique so we return a disambiguated version.
.map(|member| { name
member } else if let Some(member) = self.members.get(id) {
.display_name // The display name of the member is unique so we can return it directly if it is set.
.as_ref() // If not, we return his MXID.
.map(|s| s.to_string()) member
.unwrap_or_else(|| format!("{}", member.user_id)) .display_name
.into() .as_ref()
}) .map(|s| s.to_string())
.unwrap_or(id.as_ref().into()) .unwrap_or_else(|| format!("{}", member.user_id))
}) .into()
} else {
// There is no member with the requested MXID in the room. We still return the MXID.
id.as_ref().into()
}
} }
fn add_member(&mut self, event: &MemberEvent) -> bool { fn add_member(&mut self, event: &MemberEvent) -> bool {