mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 03:26:07 +00:00
fix(mail-ingest): an empty IMAP folder is not asked for a uid range
Some servers refuse a FETCH range on a folder with no mails, which would turn every quiet run into a failure on the status card.
This commit is contained in:
@@ -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 })) {
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user