11use std:: {
2- collections:: HashMap ,
2+ collections:: { HashMap , HashSet } ,
33 fs,
44 path:: { Path , PathBuf } ,
55} ;
@@ -23,6 +23,8 @@ pub struct Name {
2323 pub full : String ,
2424 /// Combined handle details from iMessage's database
2525 pub details : String ,
26+ /// Set of original handle IDs that map to this name
27+ pub handle_ids : HashSet < i32 > ,
2628}
2729
2830impl Name {
@@ -50,6 +52,7 @@ impl Name {
5052 last : last. unwrap_or_default ( ) ,
5153 full,
5254 details : String :: new ( ) ,
55+ handle_ids : HashSet :: new ( ) ,
5356 } )
5457 }
5558
@@ -82,6 +85,7 @@ impl Name {
8285 last : String :: new ( ) ,
8386 full : String :: new ( ) ,
8487 details : details. into ( ) ,
88+ handle_ids : HashSet :: new ( ) ,
8589 }
8690 }
8791}
@@ -95,6 +99,7 @@ impl Name {
9599 last : String :: new ( ) ,
96100 full : String :: new ( ) ,
97101 details : name. to_string ( ) ,
102+ handle_ids : HashSet :: new ( ) ,
98103 }
99104 }
100105}
@@ -244,18 +249,28 @@ impl ContactsIndex {
244249 participants : & HashMap < i32 , String > ,
245250 deduped_handles : & HashMap < i32 , i32 > ,
246251 ) -> HashMap < i32 , Name > {
247- let mut result = HashMap :: new ( ) ;
248-
249- for ( handle_id, details) in participants {
250- if let Some ( deduped_id) = deduped_handles. get ( handle_id) {
251- let mut name = self
252- . lookup ( details)
253- . unwrap_or_else ( || Name :: from_details ( details. clone ( ) ) ) ;
254-
255- // Update details field for the resolved Name
256- name. details = details. clone ( ) ;
257- result. insert ( * deduped_id, name) ;
258- }
252+ let mut result: HashMap < i32 , Name > = HashMap :: new ( ) ;
253+
254+ for ( & handle_id, details) in participants {
255+ let Some ( & deduped_id) = deduped_handles. get ( & handle_id) else {
256+ continue ;
257+ } ;
258+
259+ result
260+ . entry ( deduped_id)
261+ . and_modify ( |name| {
262+ name. handle_ids . insert ( handle_id) ;
263+ } )
264+ . or_insert_with ( || {
265+ let mut name = self
266+ . lookup ( details)
267+ . unwrap_or_else ( || Name :: from_details ( details. clone ( ) ) ) ;
268+
269+ // Keep the original details string for display/fallback
270+ name. details = details. clone ( ) ;
271+ name. handle_ids . insert ( handle_id) ;
272+ name
273+ } ) ;
259274 }
260275
261276 result
0 commit comments