fix(mail-ingest): create IMAP archive folders only when needed

This commit is contained in:
Luffy
2026-12-03 16:06:04 +08:00
committed by Maurice
parent 543af0cbcb
commit 7713e37a8d
4 changed files with 99 additions and 6 deletions
@@ -24,6 +24,8 @@ export interface ImapSession {
listAfter(lastUid: number): Promise<ImapMailRef[]>;
/** One mail's raw bytes; null for a uid that is gone. */
source(uid: number): Promise<Buffer | null>;
/** The folder's server path, or null if the folder does not exist. */
findFolder(parts: readonly string[]): Promise<string | null>;
/** Makes the folder (made of these parts, in the server's own delimiter) unless it exists; answers its path. */
ensureFolder(parts: readonly string[]): Promise<string>;
move(uids: readonly number[], folder: string): Promise<void>;
@@ -69,6 +71,8 @@ async function vettedAddress(host: string): Promise<string> {
return verdict.resolvedIp;
}
class ImapFolderError extends Error {}
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;
@@ -99,9 +103,35 @@ class ImapFlowSession implements ImapSession {
return message && Buffer.isBuffer(message.source) ? message.source : null;
}
async findFolder(parts: readonly string[]): Promise<string | null> {
try {
const status = await this.client.status([...parts], { uidValidity: true });
if (status) return status.path;
} catch (err) {
// ImapFlow uses LIST to confirm that a mailbox refused by STATUS is missing.
if (err instanceof Error && 'code' in err && err.code === 'NotFound') return null;
throw new ImapFolderError(`Could not check IMAP folder ${parts.join('/')}. ${describeImapFailure(err)}`, {
cause: err,
});
}
throw new ImapFolderError(`The IMAP server did not return the status of folder ${parts.join('/')}`);
}
async ensureFolder(parts: readonly string[]): Promise<string> {
const created = await this.client.mailboxCreate([...parts]);
return created.path;
const existing = await this.findFolder(parts);
if (existing !== null) return existing;
try {
const created = await this.client.mailboxCreate([...parts]);
return created.path;
} catch (err) {
// The folder may have been created between the status check and CREATE.
const appeared = await this.findFolder(parts).catch(() => null);
if (appeared !== null) return appeared;
throw new ImapFolderError(`Could not create IMAP folder ${parts.join('/')}. ${describeImapFailure(err)}`, {
cause: err,
});
}
}
async move(uids: readonly number[], folder: string): Promise<void> {
@@ -128,7 +158,7 @@ function dateOf(value: Date | string | undefined): Date | null {
/** What an admin reads when the box cannot be read: the reason, never the password. */
export function describeImapFailure(err: unknown): string {
if (err instanceof SsrfBlockedError) return err.message;
if (err instanceof SsrfBlockedError || err instanceof ImapFolderError) return err.message;
const failure = (err ?? {}) as {
authenticationFailed?: boolean;
code?: string;
@@ -128,7 +128,9 @@ export class MailIngestImapJob implements OnApplicationBootstrap {
* it waited {@link DEFER_DAYS} already, and is then given up the same way.
*/
private async retryDeferred(session: ImapSession, now: Date): Promise<number> {
await session.open(await session.ensureFolder(DEFERRED_FOLDER));
const folder = await session.findFolder(DEFERRED_FOLDER);
if (folder === null) return 0;
await session.open(folder);
const due = (await session.listAfter(0)).slice(0, DEFERRED_PER_RUN);
if (due.length === 0) return 0;
const outcomes = await this.takeAll(session, due);