@@ -555,10 +555,38 @@ impl Imap {
555
555
}
556
556
session. new_mail = false ;
557
557
558
+ let mut read_cnt = 0 ;
559
+ loop {
560
+ let ( n, fetch_more) = self
561
+ . fetch_new_msg_batch ( context, session, folder, folder_meaning)
562
+ . await ?;
563
+ read_cnt += n;
564
+ if !fetch_more {
565
+ return Ok ( read_cnt > 0 ) ;
566
+ }
567
+ }
568
+ }
569
+
570
+ /// Returns number of messages processed and whether the function should be called again.
571
+ async fn fetch_new_msg_batch (
572
+ & mut self ,
573
+ context : & Context ,
574
+ session : & mut Session ,
575
+ folder : & str ,
576
+ folder_meaning : FolderMeaning ,
577
+ ) -> Result < ( usize , bool ) > {
558
578
let uid_validity = get_uidvalidity ( context, folder) . await ?;
559
579
let old_uid_next = get_uid_next ( context, folder) . await ?;
580
+ info ! (
581
+ context,
582
+ "fetch_new_messages({folder}): UIDVALIDITY={uid_validity}, UIDNEXT={old_uid_next}."
583
+ ) ;
560
584
561
- let msgs = session. prefetch ( old_uid_next) . await . context ( "prefetch" ) ?;
585
+ let uids_to_prefetch = 500 ;
586
+ let msgs = session
587
+ . prefetch ( old_uid_next, uids_to_prefetch)
588
+ . await
589
+ . context ( "prefetch" ) ?;
562
590
let read_cnt = msgs. len ( ) ;
563
591
564
592
let download_limit = context. download_limit ( ) . await ?;
@@ -718,7 +746,8 @@ impl Imap {
718
746
largest_uid_fetched
719
747
} ;
720
748
721
- let actually_download_messages_future = async move {
749
+ let actually_download_messages_future = async {
750
+ let sender = sender;
722
751
let mut uids_fetch_in_batch = Vec :: with_capacity ( max ( uids_fetch. len ( ) , 1 ) ) ;
723
752
let mut fetch_partially = false ;
724
753
uids_fetch. push ( ( 0 , !uids_fetch. last ( ) . unwrap_or ( & ( 0 , false ) ) . 1 ) ) ;
@@ -753,14 +782,17 @@ impl Imap {
753
782
// if the message has arrived after selecting mailbox
754
783
// and determining its UIDNEXT and before prefetch.
755
784
let mut new_uid_next = largest_uid_fetched + 1 ;
756
- if fetch_res. is_ok ( ) {
785
+ let fetch_more = fetch_res. is_ok ( ) && {
786
+ let prefetch_uid_next = old_uid_next + uids_to_prefetch;
757
787
// If we have successfully fetched all messages we planned during prefetch,
758
788
// then we have covered at least the range between old UIDNEXT
759
789
// and UIDNEXT of the mailbox at the time of selecting it.
760
- new_uid_next = max ( new_uid_next, mailbox_uid_next) ;
790
+ new_uid_next = max ( new_uid_next, min ( prefetch_uid_next , mailbox_uid_next) ) ;
761
791
762
792
new_uid_next = max ( new_uid_next, largest_uid_skipped. unwrap_or ( 0 ) + 1 ) ;
763
- }
793
+
794
+ prefetch_uid_next < mailbox_uid_next
795
+ } ;
764
796
if new_uid_next > old_uid_next {
765
797
set_uid_next ( context, folder, new_uid_next) . await ?;
766
798
}
@@ -777,7 +809,7 @@ impl Imap {
777
809
// establish a new session if this one is broken.
778
810
fetch_res?;
779
811
780
- Ok ( read_cnt > 0 )
812
+ Ok ( ( read_cnt, fetch_more ) )
781
813
}
782
814
783
815
/// Read the recipients from old emails sent by the user and add them as contacts.
0 commit comments