fix: sort email batches by date instead of UID (#231)

arclayto created

UID ordering is not reliable for all IMAP servers (e.g. Proton Bridge),
causing emails to display oldest-first. Sort by Date instead, consistent
with the Date-based sorting already used elsewhere in the codebase.

Fixes #230

Change summary

fetcher/fetcher.go | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)

Detailed changes

fetcher/fetcher.go 🔗

@@ -307,14 +307,11 @@ func FetchMailboxEmails(account *config.Account, mailbox string, limit, offset u
 			})
 		}
 
-		// Sort batch Newest -> Oldest (since IMAP usually returns Oldest->Newest or arbitrary)
-		// Assuming seqset order or standard behavior, we want to ensure we append Newest emails first
-		// so that the final list is correct.
-		// Actually, let's just sort the batch by UID desc (Newest first)
-		// Simple bubble sort for small batch
+		// Sort batch by Date descending (newest first)
+		// UID ordering is not reliable for all IMAP servers (e.g. Proton Bridge)
 		for i := 0; i < len(batchEmails); i++ {
 			for j := i + 1; j < len(batchEmails); j++ {
-				if batchEmails[j].UID > batchEmails[i].UID {
+				if batchEmails[j].Date.After(batchEmails[i].Date) {
 					batchEmails[i], batchEmails[j] = batchEmails[j], batchEmails[i]
 				}
 			}