From 3124bae377cd8b5971508e5c7dfa442b35ce51a8 Mon Sep 17 00:00:00 2001 From: usmannasir Date: Thu, 5 Mar 2026 05:56:13 +0500 Subject: [PATCH] Sort messages by date using IMAP SORT extension instead of UID order --- webmail/services/imap_client.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/webmail/services/imap_client.py b/webmail/services/imap_client.py index 57976909a..ff81fe996 100644 --- a/webmail/services/imap_client.py +++ b/webmail/services/imap_client.py @@ -149,13 +149,28 @@ class IMAPClient: def list_messages(self, folder='INBOX', page=1, per_page=25, sort='date_desc'): self._select(folder) - status, data = self.conn.uid('search', None, 'ALL') - if status != 'OK': - return {'messages': [], 'total': 0, 'page': page, 'pages': 0} - uids = data[0].split() if data[0] else [] - if sort == 'date_desc': - uids = list(reversed(uids)) + # Try IMAP SORT for proper date ordering (Dovecot supports this) + uids = [] + try: + if sort == 'date_desc': + status, data = self.conn.uid('sort', '(REVERSE DATE)', 'UTF-8', 'ALL') + else: + status, data = self.conn.uid('sort', '(DATE)', 'UTF-8', 'ALL') + if status == 'OK' and data[0]: + uids = data[0].split() + except Exception: + pass + + # Fallback to search + reverse UIDs if SORT not supported + if not uids: + status, data = self.conn.uid('search', None, 'ALL') + if status != 'OK': + return {'messages': [], 'total': 0, 'page': page, 'pages': 0} + uids = data[0].split() if data[0] else [] + if sort == 'date_desc': + uids = list(reversed(uids)) + total = len(uids) pages = max(1, (total + per_page - 1) // per_page) page = max(1, min(page, pages))