From e4977d1d2a200b31dc5d8f54ecc631b98cb5fd31 Mon Sep 17 00:00:00 2001 From: Denis Kasak Date: Tue, 9 Jun 2020 23:02:01 +0200 Subject: [PATCH] Refactor member_display_name. Make it more readable, add comments. --- matrix_sdk_base/src/models/room.rs | 40 +++++++++++++++++------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/matrix_sdk_base/src/models/room.rs b/matrix_sdk_base/src/models/room.rs index 35a359fd..5cf61820 100644 --- a/matrix_sdk_base/src/models/room.rs +++ b/matrix_sdk_base/src/models/room.rs @@ -304,29 +304,35 @@ impl 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 /// 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. 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) - .map(|s| s.as_str().into()) - .unwrap_or_else(|| { - self.members - .get(id) - .map(|member| { - member - .display_name - .as_ref() - .map(|s| s.to_string()) - .unwrap_or_else(|| format!("{}", member.user_id)) - .into() - }) - .unwrap_or(id.as_ref().into()) - }) + .map(|s| s.as_str().into()); + + if let Some(name) = disambiguated_name { + // The display name of the member is non-unique so we return a disambiguated version. + name + } else if let Some(member) = self.members.get(id) { + // The display name of the member is unique so we can return it directly if it is set. + // If not, we return his MXID. + member + .display_name + .as_ref() + .map(|s| s.to_string()) + .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 {