From 5e42bef8adf7796eb55758e768a4904597ace268 Mon Sep 17 00:00:00 2001 From: Wayne <5291640+ringoinca@users.noreply.github.com> Date: Tue, 22 Jul 2025 02:15:41 +0300 Subject: [PATCH] IMAP syncing fix --- .../services/ingestion-connectors/ImapConnector.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/services/ingestion-connectors/ImapConnector.ts b/packages/backend/src/services/ingestion-connectors/ImapConnector.ts index 05cc52f..62265f5 100644 --- a/packages/backend/src/services/ingestion-connectors/ImapConnector.ts +++ b/packages/backend/src/services/ingestion-connectors/ImapConnector.ts @@ -34,11 +34,20 @@ export class ImapConnector implements IEmailConnector { public async *fetchEmails(userEmail: string, syncState?: SyncState | null): AsyncGenerator { await this.client.connect(); try { - await this.client.mailboxOpen('INBOX'); + const mailbox = await this.client.mailboxOpen('INBOX'); const lastUid = syncState?.imap?.maxUid; this.newMaxUid = lastUid || 0; + // Determine the highest UID in the mailbox currently. + // This ensures that even if no new emails are fetched, the sync state is updated to the latest UID. + if (mailbox.exists > 0) { + const highestUidInMailbox = mailbox.uidNext - 1; + if (highestUidInMailbox > this.newMaxUid) { + this.newMaxUid = highestUidInMailbox; + } + } + // If lastUid exists, fetch all emails with a UID greater than it. // Otherwise, fetch all emails. const searchCriteria = lastUid ? { uid: `${lastUid + 1}:*` } : { all: true };