mirror of
https://github.com/bulwarkmail/webmail.git
synced 2026-11-03 00:13:09 +00:00
A message that failed for a passing reason (a dropped connection, a server error, an expired token) used to count as done, so continuing the import skipped it for good, and a slow upload followed by a cancel imported the messages after it twice. - The checkpoint keeps every message that is done, also past a gap, and the next run of the same import tries only the rest again. - Ten failures in a row, a full mailbox (overQuota) or a refusal (forbidden) stop the import with a message instead of going through the whole file. - An upload without an answer pauses the import instead of failing the message: 30 seconds, then until the upload window ends. - Email/import, the export's Blob/get and folder listing pass the client's own 429 pause by, and a 429 on them doesn't pause the rest of the app. - Signing out stops the job of that account only, writes no checkpoint and imports nothing more afterwards; another account's sign-out leaves the job alone. - One tab imports a file at a time (Web Locks), and Import waits until the dialog knows whether an earlier run can be continued. The import dialog tests now wait for the Import button to be enabled before clicking it, since it stays off during that lookup.
69 lines
2.4 KiB
TypeScript
69 lines
2.4 KiB
TypeScript
import { runImport, type ImportFile } from "@/lib/mail-import/engine";
|
|
import type { IJMAPClient } from "@/lib/jmap/client-interface";
|
|
import { useAuthStore } from "@/stores/auth-store";
|
|
import { useEmailStore } from "@/stores/email-store";
|
|
import { useMailTransferStore } from "@/stores/mail-transfer-store";
|
|
|
|
export interface StartImportInput {
|
|
files: ImportFile[];
|
|
/** JMAP account of the target. */
|
|
accountId: string;
|
|
/** null = the top level (with keepFolders). */
|
|
targetMailboxId: string | null;
|
|
keepFolders: boolean;
|
|
resume: boolean;
|
|
}
|
|
|
|
/** Runs an import as the dialog's job and refreshes the mail lists when it ends. */
|
|
export function startImport(input: StartImportInput): void {
|
|
const auth = useAuthStore.getState();
|
|
if (!auth.client) return;
|
|
const client: IJMAPClient = auth.client;
|
|
const store = useMailTransferStore.getState();
|
|
const controller = new AbortController();
|
|
const scope = auth.activeAccountId ?? "default";
|
|
let ended: () => void = () => {};
|
|
const finished = new Promise<void>((resolve) => { ended = resolve; });
|
|
store.startJob({
|
|
kind: "import",
|
|
controller,
|
|
scope,
|
|
finished,
|
|
progress: { phase: "running", imported: 0, failed: 0, skipped: {}, failures: [], fraction: 0, foldersCreated: 0 },
|
|
});
|
|
const hasPst = input.files.some((f) => f.format === "pst");
|
|
void (async () => {
|
|
try {
|
|
await run();
|
|
} finally {
|
|
ended();
|
|
}
|
|
})();
|
|
async function run() {
|
|
// The PST reader (and its worker) load only for an Outlook file.
|
|
const openPst = hasPst ? (await import("@/lib/mail-import/pst-source")).openPstSource : undefined;
|
|
const result = await runImport({
|
|
client,
|
|
accountId: input.accountId,
|
|
targetMailboxId: input.targetMailboxId,
|
|
keepFolders: input.keepFolders,
|
|
files: input.files,
|
|
scope,
|
|
resume: input.resume,
|
|
signal: controller.signal,
|
|
onProgress: (progress) => useMailTransferStore.getState().updateProgress(progress),
|
|
openPst,
|
|
});
|
|
useMailTransferStore.getState().updateProgress(result);
|
|
// Show what arrived, if the same account is still the one on screen.
|
|
if (useAuthStore.getState().client !== client || result.imported === 0) return;
|
|
const email = useEmailStore.getState();
|
|
try {
|
|
await email.fetchMailboxes(client);
|
|
await email.fetchEmails(client, email.selectedMailbox || undefined, { background: true });
|
|
} catch {
|
|
/* the next refresh shows them */
|
|
}
|
|
}
|
|
}
|