| @@ -2,7 +2,7 @@ |
| 2 | //! |
2 | //! |
| 3 | //! The types defined here get used throughout iamb. |
3 | //! The types defined here get used throughout iamb. |
| 4 | use std::borrow::Cow; |
4 | use std::borrow::Cow; |
| 5 | use std::collections::hash_map::IntoIter; |
5 | use std::collections::hash_map::{Entry, IntoIter}; |
| 6 | use std::collections::{BTreeSet, HashMap, HashSet}; |
6 | use std::collections::{BTreeSet, HashMap, HashSet}; |
| 7 | use std::convert::TryFrom; |
7 | use std::convert::TryFrom; |
| 8 | use std::fmt::{self, Display}; |
8 | use std::fmt::{self, Display}; |
| @@ -878,6 +878,91 @@ impl UnreadInfo { |
| 878 | } |
878 | } |
| 879 | } |
879 | } |
| 880 | |
880 | |
| |
881 | /// Track the display names for users and render any needed disambiguation for |
| |
882 | /// those with overlapping names. |
| |
883 | #[derive(Default)] |
| |
884 | pub struct DisplayNameStore { |
| |
885 | by_ids: HashMap<OwnedUserId, String>, |
| |
886 | by_names: HashMap<String, HashSet<OwnedUserId>>, |
| |
887 | } |
| |
888 | |
| |
889 | impl DisplayNameStore { |
| |
890 | /// Update the `HashSet` associated with a given displayname. |
| |
891 | /// |
| |
892 | /// Note that this *could* be done more elegantly using the Entry API, but |
| |
893 | /// is intentionally written in a way to avoid cloning conflicting display |
| |
894 | /// names. |
| |
895 | fn set_by_name(&mut self, user_id: OwnedUserId, name: &str) { |
| |
896 | if let Some(existing) = self.by_names.get_mut(name) { |
| |
897 | existing.insert(user_id); |
| |
898 | } else { |
| |
899 | self.by_names.insert(name.to_owned(), HashSet::from([user_id])); |
| |
900 | } |
| |
901 | } |
| |
902 | |
| |
903 | /// Track a new user ID to displayname mapping, or unset any existing ones. |
| |
904 | pub fn set(&mut self, user_id: OwnedUserId, name: Option<String>) { |
| |
905 | if let Some(name) = name.as_deref() { |
| |
906 | self.set_by_name(user_id.clone(), name); |
| |
907 | } |
| |
908 | |
| |
909 | let previous = match (self.by_ids.entry(user_id), name) { |
| |
910 | // Nothing to do! |
| |
911 | (Entry::Vacant(_), None) => None, |
| |
912 | |
| |
913 | // Setting initial display name for user: |
| |
914 | (Entry::Vacant(v), Some(name)) => { |
| |
915 | v.insert(name); |
| |
916 | None |
| |
917 | }, |
| |
918 | |
| |
919 | // Unsetting display name: |
| |
920 | (Entry::Occupied(o), None) => Some(o.remove_entry()), |
| |
921 | |
| |
922 | // Replacing existing name: |
| |
923 | (Entry::Occupied(mut o), Some(name)) => { |
| |
924 | if o.get() == &name { |
| |
925 | None |
| |
926 | } else { |
| |
927 | Some((o.key().clone(), o.insert(name))) |
| |
928 | } |
| |
929 | }, |
| |
930 | }; |
| |
931 | |
| |
932 | let Some((user_id, previous)) = previous else { |
| |
933 | return; |
| |
934 | }; |
| |
935 | |
| |
936 | let Some(users) = self.by_names.get_mut(&previous) else { |
| |
937 | return; |
| |
938 | }; |
| |
939 | |
| |
940 | users.remove(&user_id); |
| |
941 | |
| |
942 | if users.is_empty() { |
| |
943 | self.by_names.remove(&previous); |
| |
944 | } |
| |
945 | } |
| |
946 | |
| |
947 | pub fn get<'a>(&'a self, user_id: &UserId) -> Option<Cow<'a, str>> { |
| |
948 | let displayname = self.by_ids.get(user_id)?; |
| |
949 | let users = self.by_names.get(displayname)?; |
| |
950 | |
| |
951 | if !users.contains(user_id) { |
| |
952 | // Internal consistency error? Assume no display name: |
| |
953 | return None; |
| |
954 | } |
| |
955 | |
| |
956 | if users.len() == 1 { |
| |
957 | // Unambiguous! |
| |
958 | return Some(Cow::Borrowed(displayname.as_str())); |
| |
959 | } |
| |
960 | |
| |
961 | // Ambiguous username, so include unique user ID: |
| |
962 | Some(Cow::Owned(format!("{displayname} ({user_id})"))) |
| |
963 | } |
| |
964 | } |
| |
965 | |
| 881 | /// Information about room's the user's joined. |
966 | /// Information about room's the user's joined. |
| 882 | pub struct RoomInfo { |
967 | pub struct RoomInfo { |
| 883 | /// The display name for this room. |
968 | /// The display name for this room. |
| @@ -919,7 +1004,7 @@ pub struct RoomInfo { |
| 919 | pub users_typing: Option<(Instant, Vec<OwnedUserId>)>, |
1004 | pub users_typing: Option<(Instant, Vec<OwnedUserId>)>, |
| 920 | |
1005 | |
| 921 | /// The display names for users in this room. |
1006 | /// The display names for users in this room. |
| 922 | pub display_names: HashMap<OwnedUserId, String>, |
1007 | pub display_names: DisplayNameStore, |
| 923 | |
1008 | |
| 924 | /// The last time the room was rendered, used to detect if it is currently open. |
1009 | /// The last time the room was rendered, used to detect if it is currently open. |
| 925 | pub draw_last: Option<Instant>, |
1010 | pub draw_last: Option<Instant>, |
| @@ -2409,4 +2494,60 @@ pub mod tests { |
| 2409 | let res = complete_cmdbar(&text, &mut cursor, &store); |
2494 | let res = complete_cmdbar(&text, &mut cursor, &store); |
| 2410 | assert_eq!(res, users); |
2495 | assert_eq!(res, users); |
| 2411 | } |
2496 | } |
| |
2497 | |
| |
2498 | #[test] |
| |
2499 | fn test_ambiguous_displaynames() { |
| |
2500 | let mut store = DisplayNameStore::default(); |
| |
2501 | |
| |
2502 | store.set(TEST_USER1.clone(), Some("John".into())); |
| |
2503 | store.set(TEST_USER2.clone(), Some("John".into())); |
| |
2504 | store.set(TEST_USER3.clone(), Some("Jane".into())); |
| |
2505 | store.set(TEST_USER4.clone(), Some("Alice".into())); |
| |
2506 | store.set(TEST_USER5.clone(), Some("Bob".into())); |
| |
2507 | |
| |
2508 | // TEST_USER1 and TEST_USER2 are both ambiguous, while the other are unambiguous: |
| |
2509 | assert_eq!(store.get(&TEST_USER1).unwrap().as_ref(), "John (@user1:example.com)"); |
| |
2510 | assert_eq!(store.get(&TEST_USER2).unwrap().as_ref(), "John (@user2:example.com)"); |
| |
2511 | assert_eq!(store.get(&TEST_USER3).unwrap().as_ref(), "Jane"); |
| |
2512 | assert_eq!(store.get(&TEST_USER4).unwrap().as_ref(), "Alice"); |
| |
2513 | assert_eq!(store.get(&TEST_USER5).unwrap().as_ref(), "Bob"); |
| |
2514 | |
| |
2515 | // TEST_USER1 becomes unambiguous when TEST_USER2 changes: |
| |
2516 | store.set(TEST_USER2.clone(), Some("Eve".into())); |
| |
2517 | assert_eq!(store.get(&TEST_USER1).unwrap().as_ref(), "John"); |
| |
2518 | assert_eq!(store.get(&TEST_USER2).unwrap().as_ref(), "Eve"); |
| |
2519 | assert_eq!(store.get(&TEST_USER3).unwrap().as_ref(), "Jane"); |
| |
2520 | assert_eq!(store.get(&TEST_USER4).unwrap().as_ref(), "Alice"); |
| |
2521 | assert_eq!(store.get(&TEST_USER5).unwrap().as_ref(), "Bob"); |
| |
2522 | |
| |
2523 | // TEST_USER5 becomes ambiguous when TEST_USER2 once again changes their name to match: |
| |
2524 | store.set(TEST_USER2.clone(), Some("Bob".into())); |
| |
2525 | assert_eq!(store.get(&TEST_USER1).unwrap().as_ref(), "John"); |
| |
2526 | assert_eq!(store.get(&TEST_USER2).unwrap().as_ref(), "Bob (@user2:example.com)"); |
| |
2527 | assert_eq!(store.get(&TEST_USER3).unwrap().as_ref(), "Jane"); |
| |
2528 | assert_eq!(store.get(&TEST_USER4).unwrap().as_ref(), "Alice"); |
| |
2529 | assert_eq!(store.get(&TEST_USER5).unwrap().as_ref(), "Bob (@user5:example.com)"); |
| |
2530 | |
| |
2531 | // Now "Everyone is John": |
| |
2532 | store.set(TEST_USER2.clone(), Some("John".into())); |
| |
2533 | store.set(TEST_USER3.clone(), Some("John".into())); |
| |
2534 | store.set(TEST_USER4.clone(), Some("John".into())); |
| |
2535 | store.set(TEST_USER5.clone(), Some("John".into())); |
| |
2536 | assert_eq!(store.get(&TEST_USER1).unwrap().as_ref(), "John (@user1:example.com)"); |
| |
2537 | assert_eq!(store.get(&TEST_USER2).unwrap().as_ref(), "John (@user2:example.com)"); |
| |
2538 | assert_eq!(store.get(&TEST_USER3).unwrap().as_ref(), "John (@user3:example.com)"); |
| |
2539 | assert_eq!(store.get(&TEST_USER4).unwrap().as_ref(), "John (@user4:example.com)"); |
| |
2540 | assert_eq!(store.get(&TEST_USER5).unwrap().as_ref(), "John (@user5:example.com)"); |
| |
2541 | |
| |
2542 | // 2-5 unset their displayname: |
| |
2543 | store.set(TEST_USER2.clone(), None); |
| |
2544 | store.set(TEST_USER3.clone(), None); |
| |
2545 | store.set(TEST_USER4.clone(), None); |
| |
2546 | store.set(TEST_USER5.clone(), None); |
| |
2547 | assert_eq!(store.get(&TEST_USER1).unwrap().as_ref(), "John"); |
| |
2548 | assert_eq!(store.get(&TEST_USER2), None); |
| |
2549 | assert_eq!(store.get(&TEST_USER3), None); |
| |
2550 | assert_eq!(store.get(&TEST_USER4), None); |
| |
2551 | assert_eq!(store.get(&TEST_USER5), None); |
| |
2552 | } |
| 2412 | } |
2553 | } |