From 59c8652ce8b175789a54b5ac3f59afd607d3b57a Mon Sep 17 00:00:00 2001 From: Austin Ray Date: Fri, 21 May 2021 19:18:11 -0400 Subject: [PATCH] base: fix empty room name calculation Step 3.iii of the naming algorithm[0] covers name calculation for empty rooms; however, it isn't an else condition for steps 3.i and 3.ii. It's a final conditional in the algorithm before returning the name. The current implementation treats it as an else condition. To fix this, use step 3.i and 3.ii to calculate a room name then check if the user is alone. If alone, return "Empty room (was...)" containing any former member names. If alone and there aren't any former members, return "Empty room" Fixes #133 [0] https://matrix.org/docs/spec/client_server/latest#calculating-the-display-name-for-a-room --- matrix_sdk_base/src/rooms/mod.rs | 33 ++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/matrix_sdk_base/src/rooms/mod.rs b/matrix_sdk_base/src/rooms/mod.rs index 244955b7..6dc9ba77 100644 --- a/matrix_sdk_base/src/rooms/mod.rs +++ b/matrix_sdk_base/src/rooms/mod.rs @@ -160,7 +160,7 @@ fn calculate_room_name( let invited_joined = invited_member_count + joined_member_count; let invited_joined_minus_one = invited_joined.saturating_sub(1); - if heroes_count >= invited_joined_minus_one { + let names = if heroes_count >= invited_joined_minus_one { let mut names = heroes; // stabilize ordering names.sort_unstable(); @@ -173,7 +173,18 @@ fn calculate_room_name( // the `else`? format!("{}, and {} others", names.join(", "), (invited_joined - heroes_count)) } else { - "Empty room".to_string() + "".to_string() + }; + + // User is alone. + if invited_joined <= 1 { + if names.is_empty() { + "Empty room".to_string() + } else { + format!("Empty room (was {})", names) + } + } else { + names } } @@ -194,5 +205,23 @@ mod tests { actual = calculate_room_name(5, 0, vec!["a", "b", "c"]); assert_eq!("a, b, c, and 2 others", actual); + + actual = calculate_room_name(0, 0, vec![]); + assert_eq!("Empty room", actual); + + actual = calculate_room_name(1, 0, vec![]); + assert_eq!("Empty room", actual); + + actual = calculate_room_name(0, 1, vec![]); + assert_eq!("Empty room", actual); + + actual = calculate_room_name(1, 0, vec!["a"]); + assert_eq!("Empty room (was a)", actual); + + actual = calculate_room_name(1, 0, vec!["a", "b"]); + assert_eq!("Empty room (was a, b)", actual); + + actual = calculate_room_name(1, 0, vec!["a", "b", "c"]); + assert_eq!("Empty room (was a, b, c)", actual); } }