@@ -210,7 +210,8 @@ impl App {
210210 pub async fn sync ( & mut self , opts : SyncOptions ) -> Result < SyncResult > {
211211 let mut messages_stored: u64 = 0 ;
212212 let mut chats_stored: u64 = 0 ;
213- let mut per_chat: Vec < ChatSyncSummary > = Vec :: new ( ) ;
213+ let mut per_chat_map: std:: collections:: HashMap < i64 , ChatSyncSummary > =
214+ std:: collections:: HashMap :: new ( ) ;
214215
215216 // Build ignore set for fast lookup.
216217 let ignore_set: HashSet < i64 > = opts. ignore_chat_ids . iter ( ) . copied ( ) . collect ( ) ;
@@ -462,7 +463,7 @@ impl App {
462463 // Track per-chat summary if messages were synced
463464 if count > 0 {
464465 // Build topic summaries for forums
465- let topics = if is_forum && !topic_counts. is_empty ( ) {
466+ let new_topics : Vec < TopicSyncSummary > = if is_forum && !topic_counts. is_empty ( ) {
466467 let mut topic_summaries = Vec :: new ( ) ;
467468 for ( tid, msg_count) in & topic_counts {
468469 let topic_name = self
@@ -479,19 +480,35 @@ impl App {
479480 messages_synced : * msg_count,
480481 } ) ;
481482 }
482- // Sort by message count descending
483- topic_summaries. sort_by ( |a, b| b. messages_synced . cmp ( & a. messages_synced ) ) ;
484483 topic_summaries
485484 } else {
486485 Vec :: new ( )
487486 } ;
488487
489- per_chat. push ( ChatSyncSummary {
490- chat_id : id,
491- chat_name : name. clone ( ) ,
492- messages_synced : count as u64 ,
493- topics,
494- } ) ;
488+ // Aggregate into per_chat_map
489+ per_chat_map
490+ . entry ( id)
491+ . and_modify ( |existing| {
492+ existing. messages_synced += count as u64 ;
493+ // Merge topics by topic_id
494+ for new_topic in & new_topics {
495+ if let Some ( existing_topic) = existing
496+ . topics
497+ . iter_mut ( )
498+ . find ( |t| t. topic_id == new_topic. topic_id )
499+ {
500+ existing_topic. messages_synced += new_topic. messages_synced ;
501+ } else {
502+ existing. topics . push ( new_topic. clone ( ) ) ;
503+ }
504+ }
505+ } )
506+ . or_insert ( ChatSyncSummary {
507+ chat_id : id,
508+ chat_name : name. clone ( ) ,
509+ messages_synced : count as u64 ,
510+ topics : new_topics,
511+ } ) ;
495512 }
496513 }
497514
@@ -667,7 +684,7 @@ impl App {
667684 // Track per-chat summary if messages were synced
668685 if count > 0 {
669686 // Build topic summaries for forums
670- let topics = if is_forum && !topic_counts. is_empty ( ) {
687+ let new_topics : Vec < TopicSyncSummary > = if is_forum && !topic_counts. is_empty ( ) {
671688 let mut topic_summaries = Vec :: new ( ) ;
672689 for ( tid, msg_count) in & topic_counts {
673690 let topic_name = self
@@ -684,19 +701,35 @@ impl App {
684701 messages_synced : * msg_count,
685702 } ) ;
686703 }
687- // Sort by message count descending
688- topic_summaries. sort_by ( |a, b| b. messages_synced . cmp ( & a. messages_synced ) ) ;
689704 topic_summaries
690705 } else {
691706 Vec :: new ( )
692707 } ;
693708
694- per_chat. push ( ChatSyncSummary {
695- chat_id : id,
696- chat_name : name. clone ( ) ,
697- messages_synced : count as u64 ,
698- topics,
699- } ) ;
709+ // Aggregate into per_chat_map
710+ per_chat_map
711+ . entry ( id)
712+ . and_modify ( |existing| {
713+ existing. messages_synced += count as u64 ;
714+ // Merge topics by topic_id
715+ for new_topic in & new_topics {
716+ if let Some ( existing_topic) = existing
717+ . topics
718+ . iter_mut ( )
719+ . find ( |t| t. topic_id == new_topic. topic_id )
720+ {
721+ existing_topic. messages_synced += new_topic. messages_synced ;
722+ } else {
723+ existing. topics . push ( new_topic. clone ( ) ) ;
724+ }
725+ }
726+ } )
727+ . or_insert ( ChatSyncSummary {
728+ chat_id : id,
729+ chat_name : name. clone ( ) ,
730+ messages_synced : count as u64 ,
731+ topics : new_topics,
732+ } ) ;
700733 }
701734 }
702735
@@ -709,6 +742,17 @@ impl App {
709742 chats_stored, messages_stored
710743 ) ;
711744
745+ // Convert HashMap to Vec and sort topics by message count descending
746+ let per_chat: Vec < ChatSyncSummary > = per_chat_map
747+ . into_values ( )
748+ . map ( |mut summary| {
749+ summary
750+ . topics
751+ . sort_by ( |a, b| b. messages_synced . cmp ( & a. messages_synced ) ) ;
752+ summary
753+ } )
754+ . collect ( ) ;
755+
712756 Ok ( SyncResult {
713757 messages_stored,
714758 chats_stored,
0 commit comments