Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(mail-ingest): an empty IMAP folder is not asked for a uid range

Maurice011cc9f11f46
diff --git a/server/src/nest/mail-ingest/mail-ingest-imap.client.ts b/server/src/nest/mail-ingest/mail-ingest-imap.client.ts
index 475b8d78a..e64add5b4 100644
--- a/server/src/nest/mail-ingest/mail-ingest-imap.client.ts
+++ b/server/src/nest/mail-ingest/mail-ingest-imap.client.ts
@@ -69,14 +69,19 @@ async function vettedAddress(host: string): Promise<string> {
}
class ImapFlowSession implements ImapSession {
+ /** How many mails the open folder holds; an empty one is not asked for a range, which some servers refuse. */
+ private exists = 0;
+
constructor(private readonly client: ImapFlow) {}
async open(mailbox: string): Promise<{ uidValidity: number }> {
const box = await this.client.mailboxOpen(mailbox);
+ this.exists = box.exists;
return { uidValidity: Number(box.uidValidity) };
}
async listAfter(lastUid: number): Promise<ImapMailRef[]> {
+ if (this.exists === 0) return [];
const found: ImapMailRef[] = [];
// `n:*` always names the newest mail, even below n, so the range alone does not filter.
for await (const message of this.client.fetch(`${lastUid + 1}:*`, { uid: true, size: true }, { uid: true })) {
diff --git a/server/tests/unit/nest/mail-ingest/mail-ingest-imap.client.test.ts b/server/tests/unit/nest/mail-ingest/mail-ingest-imap.client.test.ts
index c525b1d35..473bf5984 100644
--- a/server/tests/unit/nest/mail-ingest/mail-ingest-imap.client.test.ts
+++ b/server/tests/unit/nest/mail-ingest/mail-ingest-imap.client.test.ts
@@ -13,7 +13,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
const { checkSsrf, flow, ImapFlow } = vi.hoisted(() => {
const flow = {
connect: vi.fn(async () => undefined),
- mailboxOpen: vi.fn(async () => ({ uidValidity: 77n })),
+ mailboxOpen: vi.fn(async () => ({ uidValidity: 77n, exists: 3 })),
fetch: vi.fn(),
fetchOne: vi.fn(),
mailboxCreate: vi.fn(async () => ({ path: 'TREK.processed', created: false })),
@@ -101,6 +101,14 @@ describe('MailIngestImapClient', () => {
expect(flow.messageMove).toHaveBeenCalledTimes(1);
});
+ it('does not ask an empty folder for a range', async () => {
+ flow.mailboxOpen.mockResolvedValueOnce({ uidValidity: 5n, exists: 0 });
+ const session = await new MailIngestImapClient().connect(CONFIG);
+ await session.open('INBOX');
+ expect(await session.listAfter(0)).toEqual([]);
+ expect(flow.fetch).not.toHaveBeenCalled();
+ });
+
it('logs out, and closes a connection that broke on its own', async () => {
const session = await new MailIngestImapClient().connect(CONFIG);
await session.close();