Commit 50424f440b
Unregistered key
Layout: unified · split
src/base.rs +65 −35
| @@ -977,12 +977,24 @@ impl UnreadInfo { | ||
| 977 | 977 | } |
| 978 | 978 | } |
| 979 | 979 | |
| 980 | /// The [`OwnedUserId`]s for users with the given displayname in [`DisplayNameStore`]. | |
| 981 | #[derive(Default)] | |
| 982 | struct DisplayNameUsers { | |
| 983 | /// Joined or invited users. Count towards username disambiguation. | |
| 984 | active: HashSet<OwnedUserId>, | |
| 985 | ||
| 986 | /// Left and knocking users. Are always disambiguated. | |
| 987 | inactive: HashSet<OwnedUserId>, | |
| 988 | } | |
| 989 | ||
| 980 | 990 | /// Track the display names for users and render any needed disambiguation for |
| 981 | 991 | /// those with overlapping names. |
| 982 | 992 | #[derive(Default)] |
| 983 | 993 | pub struct DisplayNameStore { |
| 984 | by_ids: HashMap<OwnedUserId, String>, | |
| 985 | by_names: HashMap<String, HashSet<OwnedUserId>>, | |
| 994 | /// The boolean is the same `is_active` field as the argument to [`Self::set`]. | |
| 995 | by_ids: HashMap<OwnedUserId, (String, bool)>, | |
| 996 | ||
| 997 | by_names: HashMap<String, DisplayNameUsers>, | |
| 986 | 998 | } |
| 987 | 999 | |
| 988 | 1000 | impl DisplayNameStore { |
| @@ -991,18 +1003,29 @@ impl DisplayNameStore { | ||
| 991 | 1003 | /// Note that this *could* be done more elegantly using the Entry API, but |
| 992 | 1004 | /// is intentionally written in a way to avoid cloning conflicting display |
| 993 | 1005 | /// names. |
| 994 | fn set_by_name(&mut self, user_id: OwnedUserId, name: &str) { | |
| 1006 | fn set_by_name(&mut self, user_id: OwnedUserId, name: &str, is_active: bool) { | |
| 995 | 1007 | if let Some(existing) = self.by_names.get_mut(name) { |
| 996 | existing.insert(user_id); | |
| 1008 | if is_active { | |
| 1009 | existing.active.insert(user_id); | |
| 1010 | } else { | |
| 1011 | existing.inactive.insert(user_id); | |
| 1012 | } | |
| 997 | 1013 | } else { |
| 998 | self.by_names.insert(name.to_owned(), HashSet::from([user_id])); | |
| 1014 | let mut value = DisplayNameUsers::default(); | |
| 1015 | if is_active { | |
| 1016 | value.active.insert(user_id); | |
| 1017 | } else { | |
| 1018 | value.inactive.insert(user_id); | |
| 1019 | } | |
| 1020 | self.by_names.insert(name.to_owned(), value); | |
| 999 | 1021 | } |
| 1000 | 1022 | } |
| 1001 | 1023 | |
| 1002 | /// Track a new user ID to displayname mapping, or unset any existing ones. | |
| 1003 | pub fn set(&mut self, user_id: OwnedUserId, name: Option<String>) { | |
| 1024 | /// Track a new user ID to displayname mapping, or unset any existing ones. `is_active` tracks, | |
| 1025 | /// whether the user is an active member (invited or joined) or not. | |
| 1026 | pub fn set(&mut self, user_id: OwnedUserId, name: Option<String>, is_active: bool) { | |
| 1004 | 1027 | if let Some(name) = name.as_deref() { |
| 1005 | self.set_by_name(user_id.clone(), name); | |
| 1028 | self.set_by_name(user_id.clone(), name, is_active); | |
| 1006 | 1029 | } |
| 1007 | 1030 | |
| 1008 | 1031 | let previous = match (self.by_ids.entry(user_id), name) { |
| @@ -1011,7 +1034,7 @@ impl DisplayNameStore { | ||
| 1011 | 1034 | |
| 1012 | 1035 | // Setting initial display name for user: |
| 1013 | 1036 | (Entry::Vacant(v), Some(name)) => { |
| 1014 | v.insert(name); | |
| 1037 | v.insert((name, is_active)); | |
| 1015 | 1038 | None |
| 1016 | 1039 | }, |
| 1017 | 1040 | |
| @@ -1020,10 +1043,11 @@ impl DisplayNameStore { | ||
| 1020 | 1043 | |
| 1021 | 1044 | // Replacing existing name: |
| 1022 | 1045 | (Entry::Occupied(mut o), Some(name)) => { |
| 1023 | if o.get() == &name { | |
| 1046 | let key = (name, is_active); | |
| 1047 | if o.get() == &key { | |
| 1024 | 1048 | None |
| 1025 | 1049 | } else { |
| 1026 | Some((o.key().clone(), o.insert(name))) | |
| 1050 | Some((o.key().clone(), o.insert(key))) | |
| 1027 | 1051 | } |
| 1028 | 1052 | }, |
| 1029 | 1053 | }; |
| @@ -1032,27 +1056,32 @@ impl DisplayNameStore { | ||
| 1032 | 1056 | return; |
| 1033 | 1057 | }; |
| 1034 | 1058 | |
| 1035 | let Some(users) = self.by_names.get_mut(&previous) else { | |
| 1059 | let Some(users) = self.by_names.get_mut(&previous.0) else { | |
| 1036 | 1060 | return; |
| 1037 | 1061 | }; |
| 1038 | 1062 | |
| 1039 | users.remove(&user_id); | |
| 1063 | if previous.1 { | |
| 1064 | users.active.remove(&user_id); | |
| 1065 | } else { | |
| 1066 | users.inactive.remove(&user_id); | |
| 1067 | } | |
| 1040 | 1068 | |
| 1041 | if users.is_empty() { | |
| 1042 | self.by_names.remove(&previous); | |
| 1069 | if users.active.is_empty() && users.inactive.is_empty() { | |
| 1070 | self.by_names.remove(&previous.0); | |
| 1043 | 1071 | } |
| 1044 | 1072 | } |
| 1045 | 1073 | |
| 1046 | 1074 | pub fn get<'a>(&'a self, user_id: &UserId) -> Option<Cow<'a, str>> { |
| 1047 | let displayname = self.by_ids.get(user_id)?; | |
| 1075 | let (displayname, is_active) = self.by_ids.get(user_id)?; | |
| 1048 | 1076 | let users = self.by_names.get(displayname)?; |
| 1049 | 1077 | |
| 1050 | if !users.contains(user_id) { | |
| 1078 | if !users.active.contains(user_id) && !users.inactive.contains(user_id) { | |
| 1051 | 1079 | // Internal consistency error? Assume no display name: |
| 1052 | 1080 | return None; |
| 1053 | 1081 | } |
| 1054 | 1082 | |
| 1055 | if users.len() == 1 { | |
| 1083 | // Inactive members are always assumed to be ambiguous. | |
| 1084 | if *is_active && users.active.len() == 1 { | |
| 1056 | 1085 | // Unambiguous! |
| 1057 | 1086 | return Some(Cow::Borrowed(displayname.as_str())); |
| 1058 | 1087 | } |
| @@ -2414,11 +2443,11 @@ pub mod tests { | ||
| 2414 | 2443 | fn test_ambiguous_displaynames() { |
| 2415 | 2444 | let mut store = DisplayNameStore::default(); |
| 2416 | 2445 | |
| 2417 | store.set(TEST_USER1.clone(), Some("John".into())); | |
| 2418 | store.set(TEST_USER2.clone(), Some("John".into())); | |
| 2419 | store.set(TEST_USER3.clone(), Some("Jane".into())); | |
| 2420 | store.set(TEST_USER4.clone(), Some("Alice".into())); | |
| 2421 | store.set(TEST_USER5.clone(), Some("Bob".into())); | |
| 2446 | store.set(TEST_USER1.clone(), Some("John".into()), true); | |
| 2447 | store.set(TEST_USER2.clone(), Some("John".into()), true); | |
| 2448 | store.set(TEST_USER3.clone(), Some("Jane".into()), true); | |
| 2449 | store.set(TEST_USER4.clone(), Some("Alice".into()), true); | |
| 2450 | store.set(TEST_USER5.clone(), Some("Bob".into()), true); | |
| 2422 | 2451 | |
| 2423 | 2452 | // TEST_USER1 and TEST_USER2 are both ambiguous, while the other are unambiguous: |
| 2424 | 2453 | assert_eq!(store.get(&TEST_USER1).unwrap().as_ref(), "John (@user1:example.com)"); |
| @@ -2428,7 +2457,7 @@ pub mod tests { | ||
| 2428 | 2457 | assert_eq!(store.get(&TEST_USER5).unwrap().as_ref(), "Bob"); |
| 2429 | 2458 | |
| 2430 | 2459 | // TEST_USER1 becomes unambiguous when TEST_USER2 changes: |
| 2431 | store.set(TEST_USER2.clone(), Some("Eve".into())); | |
| 2460 | store.set(TEST_USER2.clone(), Some("Eve".into()), true); | |
| 2432 | 2461 | assert_eq!(store.get(&TEST_USER1).unwrap().as_ref(), "John"); |
| 2433 | 2462 | assert_eq!(store.get(&TEST_USER2).unwrap().as_ref(), "Eve"); |
| 2434 | 2463 | assert_eq!(store.get(&TEST_USER3).unwrap().as_ref(), "Jane"); |
| @@ -2436,7 +2465,7 @@ pub mod tests { | ||
| 2436 | 2465 | assert_eq!(store.get(&TEST_USER5).unwrap().as_ref(), "Bob"); |
| 2437 | 2466 | |
| 2438 | 2467 | // TEST_USER5 becomes ambiguous when TEST_USER2 once again changes their name to match: |
| 2439 | store.set(TEST_USER2.clone(), Some("Bob".into())); | |
| 2468 | store.set(TEST_USER2.clone(), Some("Bob".into()), true); | |
| 2440 | 2469 | assert_eq!(store.get(&TEST_USER1).unwrap().as_ref(), "John"); |
| 2441 | 2470 | assert_eq!(store.get(&TEST_USER2).unwrap().as_ref(), "Bob (@user2:example.com)"); |
| 2442 | 2471 | assert_eq!(store.get(&TEST_USER3).unwrap().as_ref(), "Jane"); |
| @@ -2444,25 +2473,26 @@ pub mod tests { | ||
| 2444 | 2473 | assert_eq!(store.get(&TEST_USER5).unwrap().as_ref(), "Bob (@user5:example.com)"); |
| 2445 | 2474 | |
| 2446 | 2475 | // Now "Everyone is John": |
| 2447 | store.set(TEST_USER2.clone(), Some("John".into())); | |
| 2448 | store.set(TEST_USER3.clone(), Some("John".into())); | |
| 2449 | store.set(TEST_USER4.clone(), Some("John".into())); | |
| 2450 | store.set(TEST_USER5.clone(), Some("John".into())); | |
| 2476 | store.set(TEST_USER2.clone(), Some("John".into()), true); | |
| 2477 | store.set(TEST_USER3.clone(), Some("John".into()), true); | |
| 2478 | store.set(TEST_USER4.clone(), Some("John".into()), true); | |
| 2479 | store.set(TEST_USER5.clone(), Some("John".into()), true); | |
| 2451 | 2480 | assert_eq!(store.get(&TEST_USER1).unwrap().as_ref(), "John (@user1:example.com)"); |
| 2452 | 2481 | assert_eq!(store.get(&TEST_USER2).unwrap().as_ref(), "John (@user2:example.com)"); |
| 2453 | 2482 | assert_eq!(store.get(&TEST_USER3).unwrap().as_ref(), "John (@user3:example.com)"); |
| 2454 | 2483 | assert_eq!(store.get(&TEST_USER4).unwrap().as_ref(), "John (@user4:example.com)"); |
| 2455 | 2484 | assert_eq!(store.get(&TEST_USER5).unwrap().as_ref(), "John (@user5:example.com)"); |
| 2456 | 2485 | |
| 2457 | // 2-5 unset their displayname: | |
| 2458 | store.set(TEST_USER2.clone(), None); | |
| 2459 | store.set(TEST_USER3.clone(), None); | |
| 2460 | store.set(TEST_USER4.clone(), None); | |
| 2461 | store.set(TEST_USER5.clone(), None); | |
| 2486 | // 2-4 unset their displayname: | |
| 2487 | store.set(TEST_USER2.clone(), None, true); | |
| 2488 | store.set(TEST_USER3.clone(), None, true); | |
| 2489 | store.set(TEST_USER4.clone(), None, true); | |
| 2490 | // and 5 leaves | |
| 2491 | store.set(TEST_USER5.clone(), Some("John".into()), false); | |
| 2462 | 2492 | assert_eq!(store.get(&TEST_USER1).unwrap().as_ref(), "John"); |
| 2463 | 2493 | assert_eq!(store.get(&TEST_USER2), None); |
| 2464 | 2494 | assert_eq!(store.get(&TEST_USER3), None); |
| 2465 | 2495 | assert_eq!(store.get(&TEST_USER4), None); |
| 2466 | assert_eq!(store.get(&TEST_USER5), None); | |
| 2496 | assert_eq!(store.get(&TEST_USER5).unwrap().as_ref(), "John (@user5:example.com)"); | |
| 2467 | 2497 | } |
| 2468 | 2498 | } |
src/worker.rs +10 −3
| @@ -40,7 +40,7 @@ use matrix_sdk::ruma::events::presence::PresenceEvent; | ||
| 40 | 40 | use matrix_sdk::ruma::events::reaction::ReactionEventContent; |
| 41 | 41 | use matrix_sdk::ruma::events::receipt::{ReceiptEventContent, ReceiptType}; |
| 42 | 42 | use matrix_sdk::ruma::events::room::encryption::RoomEncryptionEventContent; |
| 43 | use matrix_sdk::ruma::events::room::member::OriginalSyncRoomMemberEvent; | |
| 43 | use matrix_sdk::ruma::events::room::member::{MembershipState, OriginalSyncRoomMemberEvent}; | |
| 44 | 44 | use matrix_sdk::ruma::events::room::name::RoomNameEventContent; |
| 45 | 45 | use matrix_sdk::ruma::events::room::redaction::OriginalSyncRoomRedactionEvent; |
| 46 | 46 | use matrix_sdk::ruma::events::sticker::StickerEventContent; |
| @@ -360,6 +360,10 @@ async fn members_load(client: &Client, room_id: &RoomId) -> IambResult<Vec<RoomM | ||
| 360 | 360 | } |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | fn member_active(state: &MembershipState) -> bool { | |
| 364 | matches!(state, MembershipState::Invite | MembershipState::Join) | |
| 365 | } | |
| 366 | ||
| 363 | 367 | fn members_insert( |
| 364 | 368 | room_id: OwnedRoomId, |
| 365 | 369 | res: IambResult<Vec<RoomMember>>, |
| @@ -372,7 +376,9 @@ fn members_insert( | ||
| 372 | 376 | for member in members { |
| 373 | 377 | let user_id = member.user_id().to_owned(); |
| 374 | 378 | let name = member.display_name().map(|s| s.to_owned()); |
| 375 | info.display_names.set(user_id, name); | |
| 379 | let is_active = member_active(member.membership()); | |
| 380 | ||
| 381 | info.display_names.set(user_id, name, is_active); | |
| 376 | 382 | } |
| 377 | 383 | } |
| 378 | 384 | // else ??? |
| @@ -1248,7 +1254,8 @@ impl ClientWorker { | ||
| 1248 | 1254 | |
| 1249 | 1255 | let mut locked = store.lock().await; |
| 1250 | 1256 | let info = locked.application.get_room_info(room_id.to_owned()); |
| 1251 | info.display_names.set(user_id, ev.content.displayname); | |
| 1257 | let is_active = member_active(&ev.content.membership); | |
| 1258 | info.display_names.set(user_id, ev.content.displayname, is_active); | |
| 1252 | 1259 | } |
| 1253 | 1260 | }, |
| 1254 | 1261 | ); |