Commit 4bcc6f6467

4bcc6f6467123b2def4efb035aeea87a55458b29

parent: edc2b8d353

Unsigned

Valentin Brandl <vbrandl@riseup.net> · 2020-05-12 13:34 UTC
committer: <dkasak@termina.org.uk>

Handle duplicate display names in room

Layout: unified · split

src/room_buffer.rs +26 −2
@@ -188,14 +188,38 @@ impl RoomBuffer {
188 /// Calculate the display name for a room member from its UserId. If no member with that ID is in 188 /// Calculate the display name for a room member from its UserId. If no member with that ID is in
189 /// the room, the string representation of the ID will be returned. 189 /// the room, the string representation of the ID will be returned.
190 fn calculate_user_name(&self, user_id: &UserId) -> String { 190 fn calculate_user_name(&self, user_id: &UserId) -> String {
191 self.inner 191 let name = self
192 .inner
192 .room 193 .room
193 .borrow() 194 .borrow()
194 .members 195 .members
195 .get(user_id) 196 .get(user_id)
196 // TODO: get rid of clone? 197 // TODO: get rid of clone?
197 .and_then(|member| member.display_name.clone()) 198 .and_then(|member| member.display_name.clone())
198 .unwrap_or_else(|| format!("{}", user_id)) 199 .unwrap_or_else(|| format!("{}", user_id));
200
201 // count members with the same display name
202 let count = self
203 .inner
204 .room
205 .borrow()
206 .members
207 .values()
208 .filter(|member| {
209 member
210 .display_name
211 .as_ref()
212 .map(|n| n == &name)
213 .unwrap_or(false)
214 })
215 .count();
216
217 if count > 1 {
218 // more than one member with the same display name -> append the ID
219 format!("{} ({})", name, user_id)
220 } else {
221 name
222 }
199 } 223 }
200 224
201 fn render_event(&self, event: &impl RenderableEvent) -> String { 225 fn render_event(&self, event: &impl RenderableEvent) -> String {