Email for professionals and teams
feat(import): run email imports as background jobs with progress
Hieu Nguyen (Jack)93baed44b096
diff --git a/CLAUDE.md b/CLAUDE.mdindex e9de67b..472827a 100644--- a/CLAUDE.md+++ b/CLAUDE.md@@ -135,6 +135,10 @@ The composer's bell (`FollowUpMenu`) sends `followUpDays`; `deliverEmail` arms aOptional tables are detected with `isMissingTableError` (`src/lib/database-errors.ts`): drizzle rethrows driver errors as "Failed query" with the SQLite message on `cause`, so matching `String(error)` misses it.+### Imports are background jobs++`/api/import/messages` (uploads) and `/api/import/imap` only validate and create a row in `import_jobs`, then answer 202 with the job; nothing is imported inside the request. `processImportJob` (`src/lib/import/jobs.ts`) handles one chunk per `import.run` message on `OUTBOUND_QUEUE` (like webhook retries, so no extra binding), records counters and the resume `cursor` on the row, and enqueues the next chunk itself. Uploaded files are split and staged in the bucket as `import-jobs/<id>/<n>.bin` batches (`staging.ts`), deleted as consumed. IMAP jobs read the folder list from `spec`, but the password never touches the database: it rides along in each queue message, so a lost message fails the job instead of resuming. Cancelling flips the status; every chunk's final write is conditional on the job still being active. `sweepImportJobs` (cron / Node scheduler) fails jobs idle for 30 minutes and prunes finished records after 7 days. The import page polls `GET /api/import/jobs`; `DELETE /api/import/jobs/[id]` cancels an active job or dismisses a finished one. `import_jobs` is in `INTERNAL_TABLES`, so backups skip it.+### Search is an FTS5 index kept by triggers`messages_fts` (migration 0030) is an external-content FTS5 table over `messages`; three triggers in the same migration keep it in sync on insert, update and delete, so no application code touches the index. `buildSearchConditions` in `src/lib/search/conditions.ts` turns the Gmail-style grammar (`src/lib/search/query-utils.ts`) into a `rowid IN (SELECT rowid FROM messages_fts WHERE messages_fts MATCH ?)` predicate plus plain column filters. Two consequences: the bootstrap SQL in `src/lib/setup/migration.ts` is split with `splitSqlStatements`, which keeps trigger bodies whole; and the backup coverage check skips `messages_fts%`, since the shadow tables are derived and repopulate on restore. `wrangler d1 export` does not work on databases with virtual tables; the app's own JSON backup is unaffected.diff --git a/drizzle/migrations/0077_add_import_jobs.sql b/drizzle/migrations/0077_add_import_jobs.sqlnew file mode 100644index 0000000..69ceb61--- /dev/null+++ b/drizzle/migrations/0077_add_import_jobs.sql@@ -0,0 +1,26 @@+CREATE TABLE `import_jobs` (+ `id` text PRIMARY KEY NOT NULL,+ `user_id` text NOT NULL,+ `mailbox_id` text NOT NULL,+ `kind` text NOT NULL,+ `label` text NOT NULL,+ `status` text DEFAULT 'queued' NOT NULL,+ `total` integer DEFAULT 0 NOT NULL,+ `processed` integer DEFAULT 0 NOT NULL,+ `imported` integer DEFAULT 0 NOT NULL,+ `skipped` integer DEFAULT 0 NOT NULL,+ `spec` text NOT NULL,+ `cursor` text DEFAULT '{}' NOT NULL,+ `errors` text DEFAULT '[]' NOT NULL,+ `error_count` integer DEFAULT 0 NOT NULL,+ `error` text,+ `created_at` integer NOT NULL,+ `started_at` integer,+ `updated_at` integer NOT NULL,+ `finished_at` integer,+ FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade,+ FOREIGN KEY (`mailbox_id`) REFERENCES `mailboxes`(`id`) ON UPDATE no action ON DELETE cascade+);+--> statement-breakpoint+CREATE INDEX `import_jobs_user_idx` ON `import_jobs` (`user_id`,`created_at`);--> statement-breakpoint+CREATE INDEX `import_jobs_status_idx` ON `import_jobs` (`status`,`updated_at`);diff --git a/drizzle/migrations/meta/_journal.json b/drizzle/migrations/meta/_journal.jsonindex eac0164..4cecdf3 100644--- a/drizzle/migrations/meta/_journal.json+++ b/drizzle/migrations/meta/_journal.json@@ -477,6 +477,13 @@"when": 1792851600000,"tag": "0076_add_navigation_cover_index","breakpoints": true+ },+ {+ "idx": 69,+ "version": "6",+ "when": 1792855200000,+ "tag": "0077_add_import_jobs",+ "breakpoints": true}]}diff --git a/env.d.ts b/env.d.tsindex 5c0ea66..9ab2dc9 100644--- a/env.d.ts+++ b/env.d.ts@@ -8,10 +8,11 @@ interface CloudflareEnv {AI_MODEL?: string;AI_BASE_URL?: string;AI_API_KEY?: string;- // The outbound queue also carries webhook retries so that scheduled redelivery needs no extra binding.+ // The outbound queue also carries webhook retries and import job chunks so that scheduled redelivery needs no extra binding.OUTBOUND_QUEUE: Queue<| import("./src/lib/email/send").OutboundQueueMessage| import("./src/lib/email/webhooks").WebhookRetryMessage+ | import("./src/lib/import/jobs-types").ImportJobMessage>;ASSETS: Fetcher;IMAGES: ImagesBinding;diff --git a/public/locales/ar.json b/public/locales/ar.jsonindex 7714b5c..ca6d78e 100644--- a/public/locales/ar.json+++ b/public/locales/ar.json@@ -2068,6 +2068,19 @@"importPage.useTls": "استخدام TLS","importPage.imapNote": "يستورد IMAP أقسام المصدر المحددة تلقائيًا. تُكتشف المجلدات من الحساب المصدر وتُستورد إلى مجلدات Mailflare الجديدة أو الموجودة المطابقة.","importPage.importSources": "استيراد المصادر المحددة",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "ملفات Takeout","importPage.takeoutHint": "اختر ملفات .zip من Google Takeout أو ملف .mbox الموجود داخلها. تتم قراءتها في متصفحك، ولا يُرفع سوى الرسائل.",diff --git a/public/locales/bn.json b/public/locales/bn.jsonindex 6e3c1c9..55dc900 100644--- a/public/locales/bn.json+++ b/public/locales/bn.json@@ -2006,6 +2006,19 @@"importPage.useTls": "TLS ব্যবহার করুন","importPage.imapNote": "IMAP নির্বাচিত উৎস বিভাগগুলো স্বয়ংক্রিয়ভাবে ইমপোর্ট করে। উৎস অ্যাকাউন্ট থেকে ফোল্ডার খুঁজে বের করা হয় এবং মিলে যাওয়া নতুন বা বিদ্যমান Mailflare ফোল্ডারে ইমপোর্ট করা হয়।","importPage.importSources": "নির্বাচিত উৎস ইমপোর্ট করুন",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout ফাইল","importPage.takeoutHint": "Google Takeout-এর .zip ফাইল বা তার ভেতরের .mbox বেছে নিন। এগুলো আপনার ব্রাউজারে পড়া হয়; শুধু বার্তাগুলো আপলোড হয়।",diff --git a/public/locales/de.json b/public/locales/de.jsonindex b50d6b0..a4ef76e 100644--- a/public/locales/de.json+++ b/public/locales/de.json@@ -2008,6 +2008,19 @@"importPage.useTls": "TLS verwenden","importPage.imapNote": "IMAP importiert die ausgewählten Quellbereiche automatisch. Ordner werden im Quellkonto erkannt und in passende neue oder vorhandene Mailflare-Ordner importiert.","importPage.importSources": "Ausgewählte Quellen importieren",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout-Dateien","importPage.takeoutHint": "Wählen Sie die .zip-Dateien aus Google Takeout oder die .mbox darin. Sie werden im Browser gelesen, hochgeladen werden nur die Nachrichten.",diff --git a/public/locales/es.json b/public/locales/es.jsonindex 018c5f8..5cb79c2 100644--- a/public/locales/es.json+++ b/public/locales/es.json@@ -2023,6 +2023,19 @@"importPage.useTls": "Usar TLS","importPage.imapNote": "IMAP importa automáticamente las secciones de origen seleccionadas. Las carpetas se descubren en la cuenta de origen y se importan a carpetas de Mailflare nuevas o existentes que coincidan.","importPage.importSources": "Importar los orígenes seleccionados",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Archivos de Takeout","importPage.takeoutHint": "Elige los archivos .zip de Google Takeout o el .mbox que contienen. Se leen en tu navegador; solo se suben los mensajes.",diff --git a/public/locales/fa.json b/public/locales/fa.jsonindex 7039b7e..1afbcac 100644--- a/public/locales/fa.json+++ b/public/locales/fa.json@@ -2008,6 +2008,19 @@"importPage.useTls": "استفاده از TLS","importPage.imapNote": "IMAP بخشهای منبع انتخابشده را بهطور خودکار درونریزی میکند. پوشهها از حساب منبع کشف میشوند و در پوشههای جدید یا موجود مطابق Mailflare درونریزی میشوند.","importPage.importSources": "درونریزی منابع انتخابشده",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "فایلهای Takeout","importPage.takeoutHint": "فایلهای .zip از Google Takeout یا فایل .mbox داخل آنها را انتخاب کنید. آنها در مرورگر شما خوانده میشوند و فقط پیامها بارگذاری میشوند.",diff --git a/public/locales/fr.json b/public/locales/fr.jsonindex 02b794e..aed5c18 100644--- a/public/locales/fr.json+++ b/public/locales/fr.json@@ -2023,6 +2023,19 @@"importPage.useTls": "Utiliser TLS","importPage.imapNote": "IMAP importe automatiquement les sections source sélectionnées. Les dossiers sont découverts sur le compte source et importés dans des dossiers Mailflare correspondants, nouveaux ou existants.","importPage.importSources": "Importer les sources sélectionnées",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Fichiers Takeout","importPage.takeoutHint": "Choisissez les fichiers .zip de Google Takeout, ou le .mbox qu'ils contiennent. Ils sont lus dans votre navigateur ; seuls les messages sont envoyés.",diff --git a/public/locales/gu.json b/public/locales/gu.jsonindex 3508b0f..ef6fdba 100644--- a/public/locales/gu.json+++ b/public/locales/gu.json@@ -2008,6 +2008,19 @@"importPage.useTls": "TLS વાપરો","importPage.imapNote": "IMAP પસંદ કરેલા સ્રોત વિભાગો આપમેળે આયાત કરે છે. સ્રોત એકાઉન્ટમાંથી ફોલ્ડર શોધાય છે અને મેળ ખાતા નવા કે હાલના Mailflare ફોલ્ડરમાં આયાત થાય છે.","importPage.importSources": "પસંદ કરેલા સ્રોત આયાત કરો",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout ફાઇલો","importPage.takeoutHint": "Google Takeoutની .zip ફાઇલો અથવા તેની અંદરની .mbox પસંદ કરો. તે તમારા બ્રાઉઝરમાં વંચાય છે; ફક્ત સંદેશાઓ અપલોડ થાય છે.",diff --git a/public/locales/ha.json b/public/locales/ha.jsonindex 12bd898..3db90f1 100644--- a/public/locales/ha.json+++ b/public/locales/ha.json@@ -2008,6 +2008,19 @@"importPage.useTls": "Yi amfani da TLS","importPage.imapNote": "IMAP yana shigo da sassan tushen da aka zaɓa kai-tsaye. Ana gano manyan fayiloli daga asusun tushe a shigo da su zuwa manyan fayilolin Mailflare sababbi ko da ke akwai masu dacewa.","importPage.importSources": "Shigo da tushen da aka zaɓa",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Fayilolin Takeout","importPage.takeoutHint": "Zaɓi fayilolin .zip daga Google Takeout, ko fayil ɗin .mbox da ke cikinsu. Ana karanta su a cikin burauzarka; saƙonni kaɗai ake lodawa.",diff --git a/public/locales/hi.json b/public/locales/hi.jsonindex ea78553..2b172af 100644--- a/public/locales/hi.json+++ b/public/locales/hi.json@@ -2008,6 +2008,19 @@"importPage.useTls": "TLS का उपयोग करें","importPage.imapNote": "IMAP चुने गए स्रोत अनुभाग अपने-आप इंपोर्ट करता है। फ़ोल्डर स्रोत खाते से खोजे जाते हैं और मेल खाते नए या मौजूदा Mailflare फ़ोल्डर में इंपोर्ट किए जाते हैं।","importPage.importSources": "चुने हुए स्रोत इंपोर्ट करें",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout फ़ाइलें","importPage.takeoutHint": "Google Takeout की .zip फ़ाइलें या उनके अंदर की .mbox चुनें। ये आपके ब्राउज़र में पढ़ी जाती हैं; केवल संदेश अपलोड होते हैं।",diff --git a/public/locales/id.json b/public/locales/id.jsonindex 23a82af..fb2bb84 100644--- a/public/locales/id.json+++ b/public/locales/id.json@@ -1993,6 +1993,19 @@"importPage.useTls": "Gunakan TLS","importPage.imapNote": "IMAP mengimpor bagian sumber yang dipilih secara otomatis. Folder ditemukan dari akun sumber dan diimpor ke folder Mailflare baru atau yang sudah ada yang sesuai.","importPage.importSources": "Impor sumber yang dipilih",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "File Takeout","importPage.takeoutHint": "Pilih file .zip dari Google Takeout, atau file .mbox di dalamnya. File dibaca di browser Anda; hanya pesan yang diunggah.",diff --git a/public/locales/it.json b/public/locales/it.jsonindex a5256c7..1dfbcb1 100644--- a/public/locales/it.json+++ b/public/locales/it.json@@ -2023,6 +2023,19 @@"importPage.useTls": "Usa TLS","importPage.imapNote": "L'importazione IMAP importa automaticamente le sezioni di origine selezionate. Le cartelle vengono rilevate dall'account di origine e importate in cartelle Mailflare nuove o esistenti corrispondenti.","importPage.importSources": "Importa le origini selezionate",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "File di Takeout","importPage.takeoutHint": "Scegli i file .zip di Google Takeout o il file .mbox al loro interno. Vengono letti nel browser; vengono caricati solo i messaggi.",diff --git a/public/locales/ja.json b/public/locales/ja.jsonindex a1a427d..dce1760 100644--- a/public/locales/ja.json+++ b/public/locales/ja.json@@ -1993,6 +1993,19 @@"importPage.useTls": "TLS を使用","importPage.imapNote": "IMAP では、選択したソースのセクションが自動的にインポートされます。フォルダはソースのアカウントから検出され、対応する新規または既存の Mailflare フォルダにインポートされます。","importPage.importSources": "選択したソースをインポート",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout ファイル","importPage.takeoutHint": "Google Takeout の .zip ファイル、またはその中の .mbox を選択してください。ファイルはブラウザで読み込まれ、アップロードされるのはメッセージだけです。",diff --git a/public/locales/kn.json b/public/locales/kn.jsonindex 83ce690..72e9d57 100644--- a/public/locales/kn.json+++ b/public/locales/kn.json@@ -2008,6 +2008,19 @@"importPage.useTls": "TLS ಬಳಸಿ","importPage.imapNote": "IMAP ಆಯ್ಕೆ ಮಾಡಿದ ಮೂಲ ವಿಭಾಗಗಳನ್ನು ಸ್ವಯಂಚಾಲಿತವಾಗಿ ಆಮದು ಮಾಡುತ್ತದೆ. ಫೋಲ್ಡರ್ಗಳನ್ನು ಮೂಲ ಖಾತೆಯಿಂದ ಪತ್ತೆಹಚ್ಚಿ ಹೊಂದುವ ಹೊಸ ಅಥವಾ ಅಸ್ತಿತ್ವದಲ್ಲಿರುವ Mailflare ಫೋಲ್ಡರ್ಗಳಿಗೆ ಆಮದು ಮಾಡಲಾಗುತ್ತದೆ.","importPage.importSources": "ಆಯ್ಕೆ ಮಾಡಿದ ಮೂಲಗಳನ್ನು ಆಮದು ಮಾಡಿ",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout ಫೈಲ್ಗಳು","importPage.takeoutHint": "Google Takeout ನ .zip ಫೈಲ್ಗಳನ್ನು ಅಥವಾ ಅವುಗಳೊಳಗಿನ .mbox ಅನ್ನು ಆಯ್ಕೆಮಾಡಿ. ಅವುಗಳನ್ನು ನಿಮ್ಮ ಬ್ರೌಸರ್ನಲ್ಲಿ ಓದಲಾಗುತ್ತದೆ; ಸಂದೇಶಗಳನ್ನು ಮಾತ್ರ ಅಪ್ಲೋಡ್ ಮಾಡಲಾಗುತ್ತದೆ.",diff --git a/public/locales/ko.json b/public/locales/ko.jsonindex 28f4d51..dc221f3 100644--- a/public/locales/ko.json+++ b/public/locales/ko.json@@ -2008,6 +2008,19 @@"importPage.useTls": "TLS 사용","importPage.imapNote": "IMAP은 선택한 소스 섹션을 자동으로 가져옵니다. 폴더는 소스 계정에서 검색되어 일치하는 새 Mailflare 폴더 또는 기존 폴더로 가져옵니다.","importPage.importSources": "선택한 소스 가져오기",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout 파일","importPage.takeoutHint": "Google Takeout의 .zip 파일 또는 그 안의 .mbox를 선택하세요. 파일은 브라우저에서 읽히며 메시지만 업로드됩니다.",diff --git a/public/locales/ml.json b/public/locales/ml.jsonindex b201d1e..c804b33 100644--- a/public/locales/ml.json+++ b/public/locales/ml.json@@ -2008,6 +2008,19 @@"importPage.useTls": "TLS ഉപയോഗിക്കുക","importPage.imapNote": "IMAP തിരഞ്ഞെടുത്ത ഉറവിട വിഭാഗങ്ങൾ സ്വയമേവ ഇറക്കുമതി ചെയ്യുന്നു. ഫോൾഡറുകൾ ഉറവിട അക്കൗണ്ടിൽ നിന്ന് കണ്ടെത്തി യോജിച്ച പുതിയതോ നിലവിലുള്ളതോ ആയ Mailflare ഫോൾഡറുകളിലേക്ക് ഇറക്കുമതി ചെയ്യുന്നു.","importPage.importSources": "തിരഞ്ഞെടുത്ത ഉറവിടങ്ങൾ ഇറക്കുമതി ചെയ്യുക",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout ഫയലുകൾ","importPage.takeoutHint": "Google Takeout-ലെ .zip ഫയലുകളോ അവയ്ക്കുള്ളിലെ .mbox-ഓ തിരഞ്ഞെടുക്കുക. അവ നിങ്ങളുടെ ബ്രൗസറിൽ വായിക്കുന്നു; സന്ദേശങ്ങൾ മാത്രം അപ്ലോഡ് ചെയ്യുന്നു.",diff --git a/public/locales/mr.json b/public/locales/mr.jsonindex 8dac0a3..6c33754 100644--- a/public/locales/mr.json+++ b/public/locales/mr.json@@ -2006,6 +2006,19 @@"importPage.useTls": "TLS वापरा","importPage.imapNote": "IMAP निवडलेले स्रोत विभाग आपोआप आयात करते. स्रोत खात्यातून फोल्डर शोधले जातात आणि जुळणाऱ्या नवीन किंवा विद्यमान Mailflare फोल्डरमध्ये आयात केले जातात.","importPage.importSources": "निवडलेले स्रोत आयात करा",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout फाइल्स","importPage.takeoutHint": "Google Takeout च्या .zip फाइल्स किंवा त्यातील .mbox निवडा. त्या तुमच्या ब्राउझरमध्ये वाचल्या जातात; फक्त संदेश अपलोड होतात.",diff --git a/public/locales/ms.json b/public/locales/ms.jsonindex e2cee8b..3bbe87d 100644--- a/public/locales/ms.json+++ b/public/locales/ms.json@@ -2008,6 +2008,19 @@"importPage.useTls": "Gunakan TLS","importPage.imapNote": "IMAP mengimport bahagian sumber yang dipilih secara automatik. Folder ditemui daripada akaun sumber dan diimport ke dalam folder Mailflare baharu atau sedia ada yang sepadan.","importPage.importSources": "Import sumber yang dipilih",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Fail Takeout","importPage.takeoutHint": "Pilih fail .zip daripada Google Takeout, atau fail .mbox di dalamnya. Fail dibaca dalam pelayar anda; hanya mesej yang dimuat naik.",diff --git a/public/locales/nl.json b/public/locales/nl.jsonindex 81afbed..a44d9cc 100644--- a/public/locales/nl.json+++ b/public/locales/nl.json@@ -2008,6 +2008,19 @@"importPage.useTls": "TLS gebruiken","importPage.imapNote": "IMAP importeert geselecteerde bronsecties automatisch. Mappen worden ontdekt in het bronaccount en geïmporteerd in overeenkomende nieuwe of bestaande Mailflare-mappen.","importPage.importSources": "Geselecteerde bronnen importeren",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout-bestanden","importPage.takeoutHint": "Kies de .zip-bestanden van Google Takeout, of het .mbox-bestand daarin. Ze worden in je browser gelezen; alleen de berichten worden geüpload.",diff --git a/public/locales/pa.json b/public/locales/pa.jsonindex 3b36578..372d235 100644--- a/public/locales/pa.json+++ b/public/locales/pa.json@@ -2008,6 +2008,19 @@"importPage.useTls": "TLS ਵਰਤੋ","importPage.imapNote": "IMAP ਚੁਣੇ ਸਰੋਤ ਭਾਗਾਂ ਨੂੰ ਆਪਣੇ-ਆਪ ਆਯਾਤ ਕਰਦਾ ਹੈ। ਫੋਲਡਰ ਸਰੋਤ ਖਾਤੇ ਤੋਂ ਖੋਜੇ ਜਾਂਦੇ ਹਨ ਅਤੇ ਮੇਲ ਖਾਂਦੇ ਨਵੇਂ ਜਾਂ ਮੌਜੂਦਾ Mailflare ਫੋਲਡਰਾਂ ਵਿੱਚ ਆਯਾਤ ਕੀਤੇ ਜਾਂਦੇ ਹਨ।","importPage.importSources": "ਚੁਣੇ ਸਰੋਤ ਆਯਾਤ ਕਰੋ",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout ਫ਼ਾਈਲਾਂ","importPage.takeoutHint": "Google Takeout ਦੀਆਂ .zip ਫ਼ਾਈਲਾਂ ਜਾਂ ਉਹਨਾਂ ਅੰਦਰਲੀ .mbox ਚੁਣੋ। ਇਹ ਤੁਹਾਡੇ ਬ੍ਰਾਊਜ਼ਰ ਵਿੱਚ ਪੜ੍ਹੀਆਂ ਜਾਂਦੀਆਂ ਹਨ; ਸਿਰਫ਼ ਸੁਨੇਹੇ ਅੱਪਲੋਡ ਹੁੰਦੇ ਹਨ।",diff --git a/public/locales/pl.json b/public/locales/pl.jsonindex 01aebfa..1edc47c 100644--- a/public/locales/pl.json+++ b/public/locales/pl.json@@ -2038,6 +2038,19 @@"importPage.useTls": "Użyj TLS","importPage.imapNote": "IMAP automatycznie importuje wybrane sekcje źródłowe. Foldery są wykrywane na koncie źródłowym i importowane do pasujących nowych lub istniejących folderów Mailflare.","importPage.importSources": "Importuj wybrane źródła",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Pliki Takeout","importPage.takeoutHint": "Wybierz pliki .zip z Google Takeout lub plik .mbox z ich wnętrza. Są odczytywane w przeglądarce; przesyłane są tylko wiadomości.",diff --git a/public/locales/pt-BR.json b/public/locales/pt-BR.jsonindex fe88cf5..4236d4f 100644--- a/public/locales/pt-BR.json+++ b/public/locales/pt-BR.json@@ -2023,6 +2023,19 @@"importPage.useTls": "Usar TLS","importPage.imapNote": "O IMAP importa automaticamente as seções de origem selecionadas. As pastas são descobertas na conta de origem e importadas para pastas novas ou existentes do Mailflare.","importPage.importSources": "Importar as origens selecionadas",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Arquivos do Takeout","importPage.takeoutHint": "Escolha os arquivos .zip do Google Takeout ou o .mbox dentro deles. Eles são lidos no seu navegador; apenas as mensagens são enviadas.",diff --git a/public/locales/pt-PT.json b/public/locales/pt-PT.jsonindex 050879d..d247e4f 100644--- a/public/locales/pt-PT.json+++ b/public/locales/pt-PT.json@@ -2023,6 +2023,19 @@"importPage.useTls": "Usar TLS","importPage.imapNote": "O IMAP importa automaticamente as seções de origem selecionadas. As pastas são descobertas na conta de origem e importadas para pastas novas ou existentes do Mailflare.","importPage.importSources": "Importar as origens selecionadas",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Ficheiros do Takeout","importPage.takeoutHint": "Escolha os ficheiros .zip do Google Takeout ou o .mbox dentro deles. São lidos no seu navegador; apenas as mensagens são carregadas.",diff --git a/public/locales/ru.json b/public/locales/ru.jsonindex 5514d1c..12fa4fc 100644--- a/public/locales/ru.json+++ b/public/locales/ru.json@@ -2038,6 +2038,19 @@"importPage.useTls": "Использовать TLS","importPage.imapNote": "IMAP автоматически импортирует выбранные разделы-источники. Папки обнаруживаются в исходном аккаунте и импортируются в соответствующие новые или существующие папки Mailflare.","importPage.importSources": "Импортировать выбранные источники",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Файлы Takeout","importPage.takeoutHint": "Выберите .zip-файлы из Google Takeout или .mbox внутри них. Они читаются в браузере; загружаются только письма.",diff --git a/public/locales/si.json b/public/locales/si.jsonindex fc3c1b9..2041345 100644--- a/public/locales/si.json+++ b/public/locales/si.json@@ -2008,6 +2008,19 @@"importPage.useTls": "TLS භාවිත කරන්න","importPage.imapNote": "IMAP මගින් තෝරාගත් මූලාශ්ර කොටස් ස්වයංක්රීයව ආයාත කෙරේ. ෆෝල්ඩර මූලාශ්ර ගිණුමෙන් සොයාගෙන, ගැළපෙන නව හෝ පවතින Mailflare ෆෝල්ඩර වෙත ආයාත කෙරේ.","importPage.importSources": "තෝරාගත් මූලාශ්ර ආයාත කරන්න",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout ගොනු","importPage.takeoutHint": "Google Takeout හි .zip ගොනු හෝ ඒවා තුළ ඇති .mbox තෝරන්න. ඒවා ඔබගේ බ්රවුසරයේ කියවනු ලැබේ; පණිවිඩ පමණක් උඩුගත කෙරේ.",diff --git a/public/locales/sw.json b/public/locales/sw.jsonindex d4bf7b2..da3dc6f 100644--- a/public/locales/sw.json+++ b/public/locales/sw.json@@ -2008,6 +2008,19 @@"importPage.useTls": "Tumia TLS","importPage.imapNote": "IMAP huleta sehemu za chanzo zilizochaguliwa kiotomatiki. Folda hugunduliwa kutoka akaunti ya chanzo na kuletwa kwenye folda mpya au zilizopo zinazolingana za Mailflare.","importPage.importSources": "Leta vyanzo vilivyochaguliwa",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Faili za Takeout","importPage.takeoutHint": "Chagua faili za .zip kutoka Google Takeout, au faili ya .mbox iliyo ndani yake. Zinasomwa kwenye kivinjari chako; ujumbe pekee ndio hupakiwa.",diff --git a/public/locales/ta.json b/public/locales/ta.jsonindex e95bc62..13193ac 100644--- a/public/locales/ta.json+++ b/public/locales/ta.json@@ -2008,6 +2008,19 @@"importPage.useTls": "TLS ஐப் பயன்படுத்து","importPage.imapNote": "IMAP தேர்ந்தெடுத்த மூலப் பிரிவுகளைத் தானாக இறக்குமதி செய்கிறது. கோப்புறைகள் மூலக் கணக்கிலிருந்து கண்டறியப்பட்டு, பொருந்தும் புதிய அல்லது இருக்கும் Mailflare கோப்புறைகளில் இறக்குமதி செய்யப்படுகின்றன.","importPage.importSources": "தேர்ந்தெடுத்த மூலங்களை இறக்குமதி செய்",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout கோப்புகள்","importPage.takeoutHint": "Google Takeout இன் .zip கோப்புகளை அல்லது அவற்றுக்குள் உள்ள .mbox ஐத் தேர்ந்தெடுக்கவும். அவை உங்கள் உலாவியில் படிக்கப்படும்; செய்திகள் மட்டுமே பதிவேற்றப்படும்.",diff --git a/public/locales/te.json b/public/locales/te.jsonindex 9cb54ab..fbeebd5 100644--- a/public/locales/te.json+++ b/public/locales/te.json@@ -2006,6 +2006,19 @@"importPage.useTls": "TLS ఉపయోగించండి","importPage.imapNote": "IMAP ఎంచుకున్న మూలం విభాగాలను స్వయంచాలకంగా దిగుమతి చేస్తుంది. ఫోల్డర్లు మూలం ఖాతా నుండి కనుగొనబడి, సరిపోలే కొత్త లేదా ఉన్న Mailflare ఫోల్డర్లలోకి దిగుమతి చేయబడతాయి.","importPage.importSources": "ఎంచుకున్న మూలాలను దిగుమతి చేయండి",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout ఫైల్లు","importPage.takeoutHint": "Google Takeout యొక్క .zip ఫైల్లను లేదా వాటిలోని .mbox ను ఎంచుకోండి. అవి మీ బ్రౌజర్లో చదవబడతాయి; సందేశాలు మాత్రమే అప్లోడ్ అవుతాయి.",diff --git a/public/locales/th.json b/public/locales/th.jsonindex c58708f..f5fb17a 100644--- a/public/locales/th.json+++ b/public/locales/th.json@@ -2008,6 +2008,19 @@"importPage.useTls": "ใช้ TLS","importPage.imapNote": "IMAP นำเข้าส่วนต้นทางที่เลือกโดยอัตโนมัติ โฟลเดอร์จะถูกค้นพบจากบัญชีต้นทางและนำเข้าไปยังโฟลเดอร์ Mailflare ใหม่หรือที่มีอยู่ที่ตรงกัน","importPage.importSources": "นำเข้าต้นทางที่เลือก",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "ไฟล์ Takeout","importPage.takeoutHint": "เลือกไฟล์ .zip จาก Google Takeout หรือไฟล์ .mbox ข้างใน ไฟล์จะถูกอ่านในเบราว์เซอร์ และอัปโหลดเฉพาะข้อความเท่านั้น",diff --git a/public/locales/tr.json b/public/locales/tr.jsonindex 231852d..a9fcedc 100644--- a/public/locales/tr.json+++ b/public/locales/tr.json@@ -1957,6 +1957,19 @@"importPage.useTls": "TLS kullan","importPage.imapNote": "IMAP, seçili kaynak bölümlerini otomatik olarak içe aktarır. Klasörler kaynak hesaptan keşfedilir ve eşleşen yeni veya mevcut Mailflare klasörlerine aktarılır.","importPage.importSources": "Seçili kaynakları içe aktar",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout dosyaları","importPage.takeoutHint": "Google Takeout'tan gelen .zip dosyalarını veya içindeki .mbox dosyasını seçin. Dosyalar tarayıcınızda okunur; yalnızca iletiler yüklenir.",diff --git a/public/locales/uk.json b/public/locales/uk.jsonindex 3d26a20..cb3e867 100644--- a/public/locales/uk.json+++ b/public/locales/uk.json@@ -2038,6 +2038,19 @@"importPage.useTls": "Використовувати TLS","importPage.imapNote": "IMAP автоматично імпортує вибрані розділи джерела. Папки виявляються з облікового запису джерела та імпортуються до відповідних нових або наявних папок Mailflare.","importPage.importSources": "Імпортувати вибрані джерела",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Файли Takeout","importPage.takeoutHint": "Виберіть .zip-файли з Google Takeout або .mbox усередині них. Вони читаються в браузері; завантажуються лише листи.",diff --git a/public/locales/ur.json b/public/locales/ur.jsonindex 112e1e8..5f4731e 100644--- a/public/locales/ur.json+++ b/public/locales/ur.json@@ -2006,6 +2006,19 @@"importPage.useTls": "TLS استعمال کریں","importPage.imapNote": "IMAP منتخب ماخذ حصوں کو خودکار طور پر درآمد کرتا ہے۔ فولڈرز ماخذ اکاؤنٹ سے تلاش کیے جاتے ہیں اور مماثل نئے یا موجودہ Mailflare فولڈرز میں درآمد کیے جاتے ہیں۔","importPage.importSources": "منتخب ماخذ درآمد کریں",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout فائلیں","importPage.takeoutHint": "Google Takeout کی .zip فائلیں یا ان کے اندر موجود .mbox منتخب کریں۔ یہ آپ کے براؤزر میں پڑھی جاتی ہیں؛ صرف پیغامات اپ لوڈ ہوتے ہیں۔",diff --git a/public/locales/vi.json b/public/locales/vi.jsonindex c325664..1e85c5f 100644--- a/public/locales/vi.json+++ b/public/locales/vi.json@@ -1993,6 +1993,19 @@"importPage.useTls": "Dùng TLS","importPage.imapNote": "IMAP tự động nhập các mục nguồn đã chọn. Các thư mục được khám phá từ tài khoản nguồn và nhập vào các thư mục Mailflare mới hoặc có sẵn tương ứng.","importPage.importSources": "Nhập các nguồn đã chọn",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Tệp Takeout","importPage.takeoutHint": "Chọn các tệp .zip từ Google Takeout hoặc tệp .mbox bên trong. Tệp được đọc trong trình duyệt của bạn; chỉ thư được tải lên.",diff --git a/public/locales/zh-CN.json b/public/locales/zh-CN.jsonindex 0967ee0..4db3e72 100644--- a/public/locales/zh-CN.json+++ b/public/locales/zh-CN.json@@ -1993,6 +1993,19 @@"importPage.useTls": "使用 TLS","importPage.imapNote": "IMAP 会自动导入所选的来源分区。系统会从来源账户中发现文件夹,并导入到对应的新建或现有 Mailflare 文件夹中。","importPage.importSources": "导入所选来源",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout 文件","importPage.takeoutHint": "选择 Google Takeout 导出的 .zip 文件或其中的 .mbox 文件。文件在浏览器中读取,只上传邮件。",diff --git a/server/index.ts b/server/index.tsindex 45ed774..00cb0e8 100644--- a/server/index.ts+++ b/server/index.ts@@ -9,7 +9,8 @@ import { processInboundMessage } from "@/lib/email/inbound";import { processAgentDraftJob } from "@/lib/agent/jobs/utils";import { processOutboundQueue, type OutboundQueueMessage } from "@/lib/email/send";import { processWebhookRetry, type WebhookRetryMessage } from "@/lib/email/webhooks";-import { isInboundQueueMessage, isWebhookRetryMessage } from "../worker-utils";+import { processImportJob } from "@/lib/import/jobs";+import { isImportJobMessage, isInboundQueueMessage, isWebhookRetryMessage } from "../worker-utils";import { createNodeRuntime } from "./runtime/env";import { applyMigrations } from "./runtime/migrate";import { startScheduler } from "./runtime/scheduler";@@ -36,6 +37,7 @@ async function main() {});runtime.outboundQueue.setConsumer(async (body) => {if (isWebhookRetryMessage(body)) await processWebhookRetry(env, body as WebhookRetryMessage);+ else if (isImportJobMessage(body)) await processImportJob(env, body);else await processOutboundQueue(env, body as OutboundQueueMessage);});runtime.agentQueue.setConsumer(async (body) => {diff --git a/server/runtime/scheduler.ts b/server/runtime/scheduler.tsindex 30d1774..4b02fa0 100644--- a/server/runtime/scheduler.ts+++ b/server/runtime/scheduler.ts@@ -5,6 +5,7 @@ import { runDriveTrashRetention } from "@/lib/drive/retention";import { runAccountRetention } from "@/lib/accounts/deletion";import { runJmapChangeRetention } from "@/lib/jmap/changes";import { runFollowUpReminders } from "@/lib/follow-ups/service";+import { sweepImportJobs } from "@/lib/import/jobs";/** Fire the daily 02:00 UTC backup and the recurring maintenance jobs, matching the cron triggers in wrangler.jsonc. */export function startScheduler(env: CloudflareEnv) {@@ -12,6 +13,7 @@ export function startScheduler(env: CloudflareEnv) {const timer = setInterval(() => {runAgentMaintenance(env).catch((error) => console.error("Agent maintenance failed", error));runTrashRetention(env).catch((error) => console.error("Trash retention failed", error));+ sweepImportJobs(env).catch((error) => console.error("Import job sweep failed", error));const now = new Date();runFollowUpReminders(env, now).catch((error) => console.error("Follow-up reminders failed", error));const day = now.toISOString().slice(0, 10);diff --git a/src/app/(settings)/settings/import/page.tsx b/src/app/(settings)/settings/import/page.tsxindex c97f593..bf9d3ee 100644--- a/src/app/(settings)/settings/import/page.tsx+++ b/src/app/(settings)/settings/import/page.tsx@@ -18,11 +18,11 @@ import {import { Input } from "@/components/ui/input";import { Label } from "@/components/ui/label";import { Select } from "@/components/ui/select";+import { ImportJobs } from "@/components/settings/import-jobs";import { importMessageFiles } from "@/components/settings/import-messages-utils";import { TakeoutImport } from "@/components/settings/takeout-import";import type {ImapFormState,- ImportResult,ImportSourceItem,ImportSourceSection,ImportTab,@@ -32,13 +32,12 @@ import {ensureImportDestination,fetchImapFolders,filterCustomImapFolders,- formatImportResult,getFileImportSource,getFolderImportSource,getSelectedImportSources,- importFromImap,importSourceOptions,resolveImapSourceFolder,+ startImapImport,} from "./utils";const initialImapForm: ImapFormState = {@@ -74,12 +73,13 @@ export default function SettingsImportPage() {useState<ImportSourceSection[]>(defaultSections);const [sourceDropdownOpen, setSourceDropdownOpen] = useState(false);const [files, setFiles] = useState<File[]>([]);- const [fileResult, setFileResult] = useState<ImportResult | null>(null);+ const [fileStarted, setFileStarted] = useState(false);const [fileError, setFileError] = useState<string | null>(null);const [fileLoading, setFileLoading] = useState(false);const [fileProgress, setFileProgress] = useState<ImportProgress | null>(null);const [imapForm, setImapForm] = useState<ImapFormState>(initialImapForm);- const [imapResult, setImapResult] = useState<ImportResult | null>(null);+ const [imapStarted, setImapStarted] = useState(false);+ const [jobsRefresh, setJobsRefresh] = useState(0);const [imapError, setImapError] = useState<string | null>(null);const [imapLoading, setImapLoading] = useState(false);const [imapProgress, setImapProgress] = useState<ImportProgress | null>(null);@@ -112,11 +112,11 @@ export default function SettingsImportPage() {setFileLoading(true);setFileError(null);- setFileResult(null);+ setFileStarted(false);setFileProgress({ completed: 0, total: 100, label: t("importPage.preparing") });try {const destination = await getDestination(fileImportSource);- const result = await importMessageFiles(+ await importMessageFiles(selectedMailbox.id,files,destination,@@ -124,17 +124,18 @@ export default function SettingsImportPage() {setFileProgress({completed: percentage,total: 100,- label: percentage < 70 ? t("importPage.uploading") : t("importPage.importingMessages"),+ label: t("importPage.uploading"),}),);- setFileResult(result);- window.dispatchEvent(new Event("mailflare:messages-changed"));+ setFileStarted(true);+ setJobsRefresh((count) => count + 1);} catch (error) {setFileError(error instanceof Error ? error.message : t("importPage.fileFailed"),);} finally {setFileLoading(false);+ setFileProgress(null);}}@@ -143,14 +144,13 @@ export default function SettingsImportPage() {if (!selectedMailbox?.id || selectedSources.length === 0) return;setImapLoading(true);setImapError(null);- setImapResult(null);+ setImapStarted(false);setImapProgress({completed: 0,total: 1,label: t("importPage.discovering"),});try {- const total: ImportResult = { imported: 0, skipped: 0, errors: [] };const discoveredFolders = await fetchImapFolders(imapForm);const expandedSources: ImportSourceItem[] = [];for (const source of selectedSources) {@@ -165,61 +165,24 @@ export default function SettingsImportPage() {}}- for (const [index, source] of expandedSources.entries()) {- setImapProgress({- completed: index,- total: expandedSources.length,- label: t("importPage.importingSource", { source: sourceLabel(source) }),- });- const destination = await getDestination(source);- const folder = resolveImapSourceFolder(source, discoveredFolders);- let offset = 0;- let processed = 0;- // Newest messages first, one batch per request. Without "import all"- // only the newest batch is imported.- while (true) {- const result = await importFromImap(- selectedMailbox.id,- { ...imapForm, folder },- destination,- offset,- );- total.imported = (total.imported ?? 0) + (result.imported ?? 0);- total.skipped = (total.skipped ?? 0) + (result.skipped ?? 0);- total.errors = [...(total.errors ?? []), ...(result.errors ?? [])];- processed +=- (result.imported ?? 0) +- (result.skipped ?? 0) +- (result.errors?.length ?? 0);- if (- !imapForm.importAll ||- result.nextOffset === null ||- result.nextOffset === undefined ||- result.nextOffset <= offset- )- break;- offset = result.nextOffset;- setImapProgress({- completed: index,- total: expandedSources.length,- label: t("importPage.importingSourceProgress", { source: sourceLabel(source), processed, total: result.total ?? "?" }),- });- }- setImapProgress({- completed: index + 1,- total: expandedSources.length,- label: t("importPage.importedSource", { source: sourceLabel(source) }),+ const sources: { folder: string; destination: string }[] = [];+ for (const source of expandedSources) {+ sources.push({+ folder: resolveImapSourceFolder(source, discoveredFolders),+ destination: await getDestination(source),});}- setImapResult(total);+ await startImapImport(selectedMailbox.id, imapForm, sources);+ setImapStarted(true);+ setJobsRefresh((count) => count + 1);setImapForm((current) => ({ ...current, password: "" }));- window.dispatchEvent(new Event("mailflare:messages-changed"));} catch (error) {setImapError(error instanceof Error ? error.message : t("importPage.imapFailed"),);} finally {setImapLoading(false);+ setImapProgress(null);}}@@ -355,9 +318,9 @@ export default function SettingsImportPage() {</div></div>)}- {fileResult && (+ {fileStarted && (<p className="rounded-lg border border-green-100 bg-green-50 px-4 py-3 text-sm text-green-700">- {formatImportResult(fileResult, t)}+ {t("importJobs.started")}</p>)}{fileError && (@@ -511,9 +474,9 @@ export default function SettingsImportPage() {</div></div>)}- {imapResult && (+ {imapStarted && (<p className="rounded-lg border border-green-100 bg-green-50 px-4 py-3 text-sm text-green-700">- {formatImportResult(imapResult, t)}+ {t("importJobs.started")}</p>)}{imapError && (@@ -529,6 +492,7 @@ export default function SettingsImportPage() {</CardContent></div></section>+ <ImportJobs refreshKey={jobsRefresh} /></div>);}diff --git a/src/app/(settings)/settings/import/types.d.ts b/src/app/(settings)/settings/import/types.d.tsindex 25f651c..6fee959 100644--- a/src/app/(settings)/settings/import/types.d.ts+++ b/src/app/(settings)/settings/import/types.d.ts@@ -1,12 +1,3 @@-export type ImportResult = {- imported?: number;- skipped?: number;- errors?: string[];- error?: string;- total?: number;- nextOffset?: number | null;-};-export type ImapFormState = {host: string;port: string;diff --git a/src/app/(settings)/settings/import/utils.ts b/src/app/(settings)/settings/import/utils.tsindex 2c0c071..aa32f69 100644--- a/src/app/(settings)/settings/import/utils.ts+++ b/src/app/(settings)/settings/import/utils.ts@@ -1,10 +1,9 @@-import { defaultTranslator, type Translator } from "@/lib/i18n/utils";+import type { ImportJobSummary } from "@/lib/import/jobs-types";import { authFetch } from "@/lib/auth/client";import { getMailboxFolders, invalidateFolderCache } from "@/lib/folders/client";import type {ImapFormState,ImportFolderSummary,- ImportResult,ImportSourceItem,ImportSourceOption,ImportSourceSection,@@ -95,34 +94,44 @@ async function fetchMailboxFolders(mailboxId: string): Promise<ImportFolderSummareturn data.folders ?? [];}-export async function importFromImap(+export async function startImapImport(mailboxId: string,form: ImapFormState,- destination: string,- offset = 0,-): Promise<ImportResult> {+ sources: { folder: string; destination: string }[],+): Promise<ImportJobSummary> {const response = await authFetch("/api/import/imap", {method: "POST",headers: { "Content-Type": "application/json" },body: JSON.stringify({mailboxId,- destination,+ sources,host: form.host,port: Number(form.port),secure: form.secure,username: form.username,password: form.password,- folder: form.folder,limit: Number(form.limit),- offset,+ importAll: form.importAll,}),});- const data = await readImportJson<ImportResult>(response);- if (!response.ok) throw new Error(data.error ?? "IMAP import failed");- return data;+ const data = await readImportJson<{ job?: ImportJobSummary; error?: string }>(response);+ if (!response.ok || !data.job) throw new Error(data.error ?? "IMAP import failed");+ return data.job;}-async function readImportJson<T extends { error?: string }>(response: Response): Promise<T> {+export async function fetchImportJobs(): Promise<ImportJobSummary[]> {+ const response = await authFetch("/api/import/jobs");+ if (!response.ok) throw new Error("Unable to load imports");+ return (await readImportJson<{ jobs?: ImportJobSummary[] }>(response)).jobs ?? [];+}++/** Cancels a running import, or removes a finished one from the list. */+export async function removeImportJob(id: string): Promise<void> {+ const response = await authFetch(`/api/import/jobs/${encodeURIComponent(id)}`, { method: "DELETE" });+ if (!response.ok) throw new Error("Unable to update the import");+}++async function readImportJson<T extends object>(response: Response): Promise<T> {const text = await response.text();try {return JSON.parse(text) as T;@@ -149,11 +158,6 @@ export async function fetchImapFolders(form: ImapFormState): Promise<string[]> {return data.folders ?? [];}-export function formatImportResult(result: ImportResult | null, t: Translator = defaultTranslator): string {- if (!result) return "";- return t("import.summary", { imported: result.imported ?? 0, skipped: result.skipped ?? 0 });-}-function findFolderMatch(folders: string[], aliases: string[]): string | null {const normalizedAliases = aliases.map(normalizeFolderName);return folders.find((folder) => normalizedAliases.includes(normalizeFolderName(folder))) ?? null;diff --git a/src/app/api/import/imap/route.ts b/src/app/api/import/imap/route.tsindex 369539a..ef89955 100644--- a/src/app/api/import/imap/route.ts+++ b/src/app/api/import/imap/route.ts@@ -1,14 +1,13 @@import { NextResponse } from "next/server";-import { and, eq } from "drizzle-orm";+import { and, eq, inArray } from "drizzle-orm";import { getDb } from "@/db";import { folders } from "@/db/schema";import { requireUser } from "@/lib/auth/cookies";import { getEnv } from "@/lib/cloudflare";import { RequestBodyTooLargeError } from "@/lib/http/errors";import { readJsonBody } from "@/lib/http/request";-import { getImportMessageUserId } from "@/lib/import/destination";-import { fetchImapMessages } from "@/lib/import/imap";-import { importMessagesToMailbox } from "@/lib/import/service";+import { assertSafeImapHost } from "@/lib/import/imap-utils";+import { createImapImportJob } from "@/lib/import/jobs";import { getMailboxAccessLevel } from "@/lib/mailboxes/access";import type { ImapImportRequest } from "./types";import { parseImapImportRequest } from "./utils";@@ -18,42 +17,37 @@ export async function POST(request: Request) {const user = await requireUser(env, request);let input: ReturnType<typeof parseImapImportRequest>;try {- const body = await readJsonBody<ImapImportRequest>(request, 16 * 1024);+ const body = await readJsonBody<ImapImportRequest>(request, 64 * 1024);input = parseImapImportRequest(body);+ assertSafeImapHost(input.spec.host);} catch (error) {const status = error instanceof RequestBodyTooLargeError ? 413 : 400;return NextResponse.json({ error: error instanceof Error ? error.message : "Invalid IMAP import request" }, { status });}- const access = await getMailboxAccessLevel(getDb(env), user, input.mailboxId);+ const db = getDb(env);+ const access = await getMailboxAccessLevel(db, user, input.mailboxId);if (!access?.canManage) {return NextResponse.json({ error: "Mailbox not found" }, { status: 404 });}- if (input.destination.type === "folder") {- const db = getDb(env);- const [folder] = await db+ const folderIds = Array.from(+ new Set(input.destinations.flatMap((destination) => (destination.type === "folder" ? [destination.folderId] : []))),+ );+ if (folderIds.length > 0) {+ const found = await db.select({ id: folders.id }).from(folders)- .where(and(eq(folders.id, input.destination.folderId), eq(folders.mailboxId, access.mailbox.id)))- .limit(1);- if (!folder) {+ .where(and(inArray(folders.id, folderIds), eq(folders.mailboxId, access.mailbox.id)));+ if (found.length !== folderIds.length) {return NextResponse.json({ error: "Folder not found" }, { status: 404 });}}- try {- const { messages: rawMessages, total, nextOffset } = await fetchImapMessages(input);- const result = await importMessagesToMailbox(env, {- userId: getImportMessageUserId(input.destination, user.id, access.mailbox.userId),- mailboxId: access.mailbox.id,- destination: input.destination,- messages: rawMessages,- });- return NextResponse.json({ ...result, total, nextOffset });- } catch (error) {- return NextResponse.json(- { error: error instanceof Error ? error.message : "IMAP import failed" },- { status: 502 },- );- }+ const job = await createImapImportJob(env, {+ userId: user.id,+ mailboxId: access.mailbox.id,+ password: input.password,+ spec: input.spec,+ });+ return NextResponse.json({ job }, { status: 202 });}diff --git a/src/app/api/import/imap/types.d.ts b/src/app/api/import/imap/types.d.tsindex 46893c9..7853b3a 100644--- a/src/app/api/import/imap/types.d.ts+++ b/src/app/api/import/imap/types.d.ts@@ -5,8 +5,9 @@ export type ImapImportRequest = {secure?: boolean;username?: string;password?: string;- folder?: string;+ /** Messages per batch, or the cap per folder when importAll is off. */limit?: number;- offset?: number;- destination?: string;+ importAll?: boolean;+ /** Folders to read, each with the Mailflare destination it lands in. */+ sources?: { folder?: string; destination?: string }[];};diff --git a/src/app/api/import/imap/utils.ts b/src/app/api/import/imap/utils.tsindex 947c6fb..100da7f 100644--- a/src/app/api/import/imap/utils.ts+++ b/src/app/api/import/imap/utils.ts@@ -1,36 +1,39 @@-import type { ImapImportInput } from "@/lib/import/imap-types";import { parseImportDestination } from "@/lib/import/destination";import type { ImportDestination } from "@/lib/import/destination-types";+import type { ImapImportSpec } from "@/lib/import/jobs-types";import type { ImapImportRequest } from "./types";-export function parseImapImportRequest(input: ImapImportRequest): ImapImportInput & {+const MAX_IMAP_SOURCES = 200;++export function parseImapImportRequest(input: ImapImportRequest): {mailboxId: string;- destination: ImportDestination;+ password: string;+ spec: ImapImportSpec;+ destinations: ImportDestination[];} {const host = input.host?.trim() ?? "";const port = Number(input.port ?? 993);const username = input.username?.trim() ?? "";const password = input.password ?? "";- const folder = input.folder?.trim() || "INBOX";const limit = Math.min(Math.max(Number(input.limit ?? 100), 1), 100);- const offset = Math.max(Math.floor(Number(input.offset ?? 0)) || 0, 0);- const destination = parseImportDestination(input.destination);+ const sources = (input.sources ?? []).slice(0, MAX_IMAP_SOURCES).map((source) => ({+ folder: source.folder?.trim() || "INBOX",+ destination: source.destination?.trim() || "system:inbox",+ }));if (!input.mailboxId) throw new Error("Mailbox is required");if (!host) throw new Error("IMAP host is required");if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error("IMAP port is invalid");if (!username || !password) throw new Error("IMAP username and password are required");+ if (sources.length === 0) throw new Error("Select at least one folder to import");++ // Validates every destination up front; the job parses them again per chunk.+ const destinations = sources.map((source) => parseImportDestination(source.destination));return {mailboxId: input.mailboxId,- host,- port,- secure: input.secure ?? port === 993,- username,password,- folder,- limit,- offset,- destination,+ destinations,+ spec: { host, port, secure: input.secure ?? port === 993, username, limit, importAll: input.importAll ?? false, sources },};}diff --git a/src/app/api/import/jobs/[id]/route.ts b/src/app/api/import/jobs/[id]/route.tsnew file mode 100644index 0000000..c07feea--- /dev/null+++ b/src/app/api/import/jobs/[id]/route.ts@@ -0,0 +1,18 @@+import { NextResponse } from "next/server";+import { requireSessionUser } from "@/lib/api/auth";+import { getEnv } from "@/lib/cloudflare";+import { cancelOrDismissImportJob } from "@/lib/import/jobs";++type Params = { params: Promise<{ id: string }> };++/** Cancels a running import, or removes the record of a finished one. */+export async function DELETE(request: Request, { params }: Params) {+ const env = getEnv();+ const { user, error } = await requireSessionUser(env, request);+ if (error) return error;+ const { id } = await params;+ if (!(await cancelOrDismissImportJob(env, user.id, id))) {+ return NextResponse.json({ error: "Import not found" }, { status: 404 });+ }+ return NextResponse.json({ ok: true });+}diff --git a/src/app/api/import/jobs/route.ts b/src/app/api/import/jobs/route.tsnew file mode 100644index 0000000..b5dda74--- /dev/null+++ b/src/app/api/import/jobs/route.ts@@ -0,0 +1,11 @@+import { NextResponse } from "next/server";+import { requireSessionUser } from "@/lib/api/auth";+import { getEnv } from "@/lib/cloudflare";+import { listImportJobs } from "@/lib/import/jobs";++export async function GET(request: Request) {+ const env = getEnv();+ const { user, error } = await requireSessionUser(env, request);+ if (error) return error;+ return NextResponse.json({ jobs: await listImportJobs(env, user.id) });+}diff --git a/src/app/api/import/messages/route.ts b/src/app/api/import/messages/route.tsindex 72cc6a8..1720844 100644--- a/src/app/api/import/messages/route.ts+++ b/src/app/api/import/messages/route.ts@@ -6,6 +6,7 @@ import { requireUser } from "@/lib/auth/cookies";import { getEnv } from "@/lib/cloudflare";import { RequestBodyTooLargeError } from "@/lib/http/errors";import { getImportMessageUserId } from "@/lib/import/destination";+import { createFileImportJob } from "@/lib/import/jobs";import { importMessagesToMailbox } from "@/lib/import/service";import { getMailboxAccessLevel } from "@/lib/mailboxes/access";import { parseImportForm } from "./utils";@@ -41,11 +42,27 @@ export async function POST(request: Request) {}}- const result = await importMessagesToMailbox(env, {- userId: getImportMessageUserId(input.destination, user.id, access.mailbox.userId),+ if (!input.background) {+ const result = await importMessagesToMailbox(env, {+ userId: getImportMessageUserId(input.destination, user.id, access.mailbox.userId),+ mailboxId: access.mailbox.id,+ destination: input.destination,+ messages: input.messages,+ });+ return NextResponse.json(result);+ }++ const job = await createFileImportJob(env, {+ userId: user.id,mailboxId: access.mailbox.id,- destination: input.destination,+ label: summarizeFilenames(input.filenames),+ destination: input.destinationValue,messages: input.messages,});- return NextResponse.json(result);+ return NextResponse.json({ job }, { status: 202 });+}++function summarizeFilenames(names: string[]): string {+ const label = names.slice(0, 2).join(", ");+ return (names.length > 2 ? `${label} +${names.length - 2}` : label).slice(0, 120) || "Uploaded files";}diff --git a/src/app/api/import/messages/utils.ts b/src/app/api/import/messages/utils.tsindex 69e6f11..8e2ce6f 100644--- a/src/app/api/import/messages/utils.ts+++ b/src/app/api/import/messages/utils.ts@@ -1,11 +1,13 @@import { readFormDataBody } from "@/lib/http/request";-import { parseImportDestination } from "@/lib/import/destination";+import { DEFAULT_IMPORT_DESTINATION, parseImportDestination } from "@/lib/import/destination";import type { ImportDestination } from "@/lib/import/destination-types";import { splitMboxMessages } from "@/lib/import/mbox";import type { ImportMessageInput } from "@/lib/import/types";const MAX_IMPORT_REQUEST_SIZE = 25 * 1024 * 1024;-const MAX_IMPORT_MESSAGES = 100;+/** Without `background`, the request imports inline (the Takeout uploader), so it stays small. */+const MAX_INLINE_IMPORT_MESSAGES = 100;+const MAX_BACKGROUND_IMPORT_MESSAGES = 5000;function isMboxFile(filename: string, type: string): boolean {const name = filename.toLowerCase();@@ -44,12 +46,19 @@ function getMessageFlags(flags: ImportFileFlags, filename: string): Pick<ImportMexport async function parseImportForm(request: Request): Promise<{mailboxId: string;destination: ImportDestination;+ /** The destination as submitted, which the job stores and parses again. */+ destinationValue: string;+ filenames: string[];+ /** Queue the import as a job instead of importing before responding. */+ background: boolean;messages: ImportMessageInput[];}> {const form = await readFormDataBody(request, MAX_IMPORT_REQUEST_SIZE);const mailboxId = String(form.get("mailboxId") ?? "");const destination = parseImportDestination(form.get("destination"));const flags = parseImportFlags(form.get("flags"));+ const destinationValue = String(form.get("destination") ?? "") || DEFAULT_IMPORT_DESTINATION;+ const background = form.get("background") === "1";const files = form.getAll("files").filter((value): value is File => value instanceof File && value.size > 0);const messages: ImportMessageInput[] = [];@@ -68,5 +77,12 @@ export async function parseImportForm(request: Request): Promise<{}}- return { mailboxId, destination, messages: messages.slice(0, MAX_IMPORT_MESSAGES) };+ return {+ mailboxId,+ destination,+ destinationValue,+ filenames: files.map((file) => file.name),+ background,+ messages: messages.slice(0, background ? MAX_BACKGROUND_IMPORT_MESSAGES : MAX_INLINE_IMPORT_MESSAGES),+ };}diff --git a/src/components/settings/import-jobs-utils.ts b/src/components/settings/import-jobs-utils.tsnew file mode 100644index 0000000..28fc032--- /dev/null+++ b/src/components/settings/import-jobs-utils.ts@@ -0,0 +1,14 @@+import type { ImportJobSummary } from "@/lib/import/jobs-types";++export const IMPORT_JOBS_POLL_MS = 2000;++export function isImportJobActive(job: Pick<ImportJobSummary, "status">): boolean {+ return job.status === "queued" || job.status === "running";+}++/** Whole percent, or null while the job does not know its size yet. */+export function getImportJobPercent(job: Pick<ImportJobSummary, "status" | "total" | "processed">): number | null {+ if (job.status === "completed") return 100;+ if (job.total <= 0) return null;+ return Math.min(100, Math.floor((job.processed / job.total) * 100));+}diff --git a/src/components/settings/import-jobs.tsx b/src/components/settings/import-jobs.tsxnew file mode 100644index 0000000..5c9d04e--- /dev/null+++ b/src/components/settings/import-jobs.tsx@@ -0,0 +1,140 @@+"use client";++import { useCallback, useEffect, useRef, useState } from "react";+import { useLanguage } from "@/components/language-provider";+import { Button } from "@/components/ui/button";+import type { ImportJobSummary } from "@/lib/import/jobs-types";+import { fetchImportJobs, removeImportJob } from "@/app/(settings)/settings/import/utils";+import { getImportJobPercent, IMPORT_JOBS_POLL_MS, isImportJobActive } from "./import-jobs-utils";++const STATUS_STYLE: Record<ImportJobSummary["status"], string> = {+ queued: "bg-neutral-100 text-neutral-600",+ running: "bg-blue-50 text-blue-700",+ completed: "bg-green-50 text-green-700",+ failed: "bg-red-50 text-red-700",+ cancelled: "bg-amber-50 text-amber-700",+};++/** Imports run on the server; this lists them and follows the ones still working. `refreshKey` forces a reload. */+export function ImportJobs({ refreshKey }: { refreshKey: number }) {+ const { t } = useLanguage();+ const [jobs, setJobs] = useState<ImportJobSummary[]>([]);+ const [loaded, setLoaded] = useState(false);+ const previous = useRef<Map<string, ImportJobSummary["status"]>>(new Map());++ const load = useCallback(async () => {+ try {+ const next = await fetchImportJobs();+ const finishedNow = next.some((job) => {+ const before = previous.current.get(job.id);+ return !!before && (before === "queued" || before === "running") && !isImportJobActive(job);+ });+ previous.current = new Map(next.map((job) => [job.id, job.status]));+ setJobs(next);+ if (finishedNow) window.dispatchEvent(new Event("mailflare:messages-changed"));+ } catch {+ // Keep the last list; the next poll retries.+ } finally {+ setLoaded(true);+ }+ }, []);++ const hasActive = jobs.some(isImportJobActive);++ useEffect(() => {+ void load();+ }, [load, refreshKey]);++ useEffect(() => {+ if (!hasActive) return;+ const timer = setInterval(() => void load(), IMPORT_JOBS_POLL_MS);+ return () => clearInterval(timer);+ }, [hasActive, load]);++ async function onRemove(job: ImportJobSummary) {+ try {+ await removeImportJob(job.id);+ } finally {+ await load();+ }+ }++ if (loaded && jobs.length === 0) return null;++ return (+ <section className="space-y-4" aria-live="polite">+ <div>+ <h2 className="text-xl font-semibold text-neutral-900">{t("importJobs.title")}</h2>+ <p className="mt-1 text-sm text-neutral-500">{t("importJobs.description")}</p>+ </div>+ <ul className="space-y-3">+ {jobs.map((job) => {+ const percent = getImportJobPercent(job);+ const active = isImportJobActive(job);+ return (+ <li key={job.id} className="space-y-3 rounded-2xl bg-white p-4">+ <div className="flex items-start justify-between gap-3">+ <div className="min-w-0">+ <p className="truncate text-sm font-medium text-neutral-900">{job.label}</p>+ <p className="text-xs text-neutral-500">+ {job.kind === "imap" ? t("importPage.imap") : t("importPage.backupFile")} ·{" "}+ {new Date(job.createdAt).toLocaleString()}+ </p>+ </div>+ <div className="flex shrink-0 items-center gap-2">+ <span className={`rounded-full px-2 py-0.5 text-xs font-medium ${STATUS_STYLE[job.status]}`}>+ {t(`importJobs.status.${job.status}`)}+ </span>+ <Button type="button" variant="outline" size="sm" onClick={() => void onRemove(job)}>+ {active ? t("importJobs.cancel") : t("importJobs.dismiss")}+ </Button>+ </div>+ </div>+ {active && (+ <div className="space-y-1 text-xs text-neutral-500">+ <div className="flex justify-between gap-3">+ <span className="truncate">+ {job.currentSource+ ? t("importPage.importingSource", { source: job.currentSource })+ : job.status === "queued"+ ? t("importJobs.waiting")+ : t("importPage.importingMessages")}+ </span>+ <span>{percent === null ? "…" : `${percent}%`}</span>+ </div>+ <div className="h-1.5 overflow-hidden rounded-full bg-neutral-100">+ <div+ className={`h-full bg-blue-600 transition-[width] ${percent === null ? "w-1/3 animate-pulse" : ""}`}+ style={percent === null ? undefined : { width: `${percent}%` }}+ />+ </div>+ </div>+ )}+ <p className="text-xs text-neutral-500">+ {t("importJobs.counts", {+ processed: job.processed,+ total: job.total,+ imported: job.imported,+ skipped: job.skipped,+ })}+ </p>+ {job.error && (+ <p className="rounded-lg border border-red-100 bg-red-50 px-3 py-2 text-xs text-red-700">{job.error}</p>+ )}+ {job.errorCount > 0 && (+ <details className="text-xs text-neutral-600">+ <summary className="cursor-pointer">{t("importJobs.errors", { count: job.errorCount })}</summary>+ <ul className="mt-2 list-disc space-y-1 pl-5">+ {job.errors.slice(0, 5).map((item) => (+ <li key={item}>{item}</li>+ ))}+ </ul>+ </details>+ )}+ </li>+ );+ })}+ </ul>+ </section>+ );+}diff --git a/src/components/settings/import-messages-types.d.ts b/src/components/settings/import-messages-types.d.tsindex 42f3ea3..f1a0e54 100644--- a/src/components/settings/import-messages-types.d.ts+++ b/src/components/settings/import-messages-types.d.ts@@ -1,7 +1,7 @@+import type { ImportJobSummary } from "@/lib/import/jobs-types";+export type ImportMessagesResult = {- imported?: number;- skipped?: number;- errors?: string[];+ job?: ImportJobSummary;error?: string;};diff --git a/src/components/settings/import-messages-utils.ts b/src/components/settings/import-messages-utils.tsindex 5d96450..7275193 100644--- a/src/components/settings/import-messages-utils.ts+++ b/src/components/settings/import-messages-utils.ts@@ -1,4 +1,3 @@-import { defaultTranslator, type Translator } from "@/lib/i18n/utils";import { getAuthHeaders } from "@/lib/auth/client";import type { ImportMessagesResult, ImportProgressHandler } from "./import-messages-types";@@ -11,6 +10,7 @@ export async function importMessageFiles(const form = new FormData();form.set("mailboxId", mailboxId);form.set("destination", destination);+ form.set("background", "1");for (const file of files) {form.append("files", file);}@@ -20,7 +20,7 @@ export async function importMessageFiles(request.open("POST", "/api/import/messages");getAuthHeaders().forEach((value, key) => request.setRequestHeader(key, value));request.upload.onprogress = (event) => {- if (event.lengthComputable) onProgress?.(Math.round((event.loaded / event.total) * 70));+ if (event.lengthComputable) onProgress?.(Math.round((event.loaded / event.total) * 100));};request.onerror = () => reject(new Error("Import upload failed"));request.onload = () => {@@ -29,14 +29,8 @@ export async function importMessageFiles(reject(new Error(data.error ?? "Import failed"));return;}- onProgress?.(100);resolve(data);};request.send(form);});}--export function getImportSummary(result: ImportMessagesResult | null, t: Translator = defaultTranslator): string {- if (!result) return "";- return t("import.summary", { imported: result.imported ?? 0, skipped: result.skipped ?? 0 });-}diff --git a/src/components/settings/import-messages.tsx b/src/components/settings/import-messages.tsxindex abf2a08..ddb488f 100644--- a/src/components/settings/import-messages.tsx+++ b/src/components/settings/import-messages.tsx@@ -10,7 +10,7 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/comimport { Label } from "@/components/ui/label";import { Input } from "@/components/ui/input";import type { ImportMessagesProps, ImportMessagesResult } from "./import-messages-types";-import { getImportSummary, importMessageFiles } from "./import-messages-utils";+import { importMessageFiles } from "./import-messages-utils";export function ImportMessages({ destination, sourceLabel }: ImportMessagesProps) {const { t } = useLanguage();@@ -30,7 +30,6 @@ export function ImportMessages({ destination, sourceLabel }: ImportMessagesPropstry {const nextResult = await importMessageFiles(selectedMailbox.id, files, destination);setResult(nextResult);- window.dispatchEvent(new Event("mailflare:messages-changed"));} catch (err) {setError(err instanceof Error ? err.message : t("import.failed"));} finally {@@ -70,17 +69,10 @@ export function ImportMessages({ destination, sourceLabel }: ImportMessagesProps{loading ? t("import.importing") : t("import.submit")}</Button>- {result && (- <div className="rounded-lg border border-green-100 bg-green-50 px-4 py-3 text-sm text-green-700">- <p className="font-medium">{getImportSummary(result, t)}</p>- {(result.errors ?? []).length > 0 && (- <ul className="mt-2 list-disc space-y-1 pl-5 text-xs">- {(result.errors ?? []).slice(0, 5).map((item) => (- <li key={item}>{item}</li>- ))}- </ul>- )}- </div>+ {result?.job && (+ <p className="rounded-lg border border-green-100 bg-green-50 px-4 py-3 text-sm text-green-700">+ {t("importJobs.started")}+ </p>)}{error && (diff --git a/src/db/schema/index.ts b/src/db/schema/index.tsindex 3438087..ebf9069 100644--- a/src/db/schema/index.ts+++ b/src/db/schema/index.ts@@ -1088,6 +1088,31 @@ export const driveShares = sqliteTable("drive_shares", {index("drive_shares_user_idx").on(t.userId),]);+export const importJobs = sqliteTable("import_jobs", {+ id: text("id").primaryKey(),+ userId: text("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),+ mailboxId: text("mailbox_id").notNull().references(() => mailboxes.id, { onDelete: "cascade" }),+ kind: text("kind", { enum: ["file", "imap"] }).notNull(),+ label: text("label").notNull(),+ status: text("status", { enum: ["queued", "running", "completed", "failed", "cancelled"] }).notNull().default("queued"),+ // Messages known to be part of the job; for IMAP it grows as each source folder is counted.+ total: integer("total").notNull().default(0),+ processed: integer("processed").notNull().default(0),+ imported: integer("imported").notNull().default(0),+ skipped: integer("skipped").notNull().default(0),+ // What to import (JSON, never credentials) and where the next queue message resumes (JSON).+ spec: text("spec").notNull(),+ cursor: text("cursor").notNull().default("{}"),+ // Up to a handful of per-message error strings (JSON array); errorCount is the true total.+ errors: text("errors").notNull().default("[]"),+ errorCount: integer("error_count").notNull().default(0),+ error: text("error"),+ createdAt: integer("created_at", { mode: "timestamp" }).notNull().$defaultFn(() => new Date()),+ startedAt: integer("started_at", { mode: "timestamp" }),+ updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().$defaultFn(() => new Date()),+ finishedAt: integer("finished_at", { mode: "timestamp" }),+}, (t) => [index("import_jobs_user_idx").on(t.userId, t.createdAt), index("import_jobs_status_idx").on(t.status, t.updatedAt)]);+export const schema = {users,domains,@@ -1129,4 +1154,5 @@ export const schema = {agentDraftMetadata,agentSendApprovals,mcpKeyMailboxes,+ importJobs,};diff --git a/src/lib/backups/export.ts b/src/lib/backups/export.tsindex 15dd04b..37b4e1c 100644--- a/src/lib/backups/export.ts+++ b/src/lib/backups/export.ts@@ -41,6 +41,8 @@ export const INTERNAL_TABLES = [// Web-push subscriptions are saved again by each client whenever it resolves// its push state, and they reference sessions whose rows a restore replaces."push_subscriptions",+ // Import jobs are transient progress records tied to staged objects that a backup does not carry.+ "import_jobs",];/**diff --git a/src/lib/i18n/en.json b/src/lib/i18n/en.jsonindex bb02e26..f885c64 100644--- a/src/lib/i18n/en.json+++ b/src/lib/i18n/en.json@@ -2014,6 +2014,19 @@"importPage.useTls": "Use TLS","importPage.imapNote": "IMAP imports selected source sections automatically. Folders are discovered from the source account and imported into matching new or existing Mailflare folders.","importPage.importSources": "Import selected sources",+ "importJobs.title": "Imports",+ "importJobs.description": "Imports keep running on the server, so you can leave this page.",+ "importJobs.started": "Import started. Follow its progress under Imports.",+ "importJobs.waiting": "Waiting to start",+ "importJobs.cancel": "Cancel",+ "importJobs.dismiss": "Dismiss",+ "importJobs.status.queued": "Queued",+ "importJobs.status.running": "Running",+ "importJobs.status.completed": "Completed",+ "importJobs.status.failed": "Failed",+ "importJobs.status.cancelled": "Cancelled",+ "importJobs.counts": "{processed} of {total} processed · {imported} imported · {skipped} skipped",+ "importJobs.errors": "Problems with {count} messages","importPage.takeout": "Google Takeout (.zip)","importPage.takeoutFiles": "Takeout files","importPage.takeoutHint": "Choose the .zip files from Google Takeout, or the .mbox inside them. They are read in your browser; only the messages are uploaded.",diff --git a/src/lib/import/jobs-types.d.ts b/src/lib/import/jobs-types.d.tsnew file mode 100644index 0000000..1ab22f2--- /dev/null+++ b/src/lib/import/jobs-types.d.ts@@ -0,0 +1,61 @@+export type ImportJobKind = "file" | "imap";++export type ImportJobStatus = "queued" | "running" | "completed" | "failed" | "cancelled";++/** What the browser sees; timestamps are ISO strings. */+export type ImportJobSummary = {+ id: string;+ mailboxId: string;+ kind: ImportJobKind;+ label: string;+ status: ImportJobStatus;+ /** Messages known so far; an IMAP job learns each folder's size as it reaches it. */+ total: number;+ processed: number;+ imported: number;+ skipped: number;+ /** The first few per-message errors. */+ errors: string[];+ errorCount: number;+ error: string | null;+ /** The IMAP folder being read right now. */+ currentSource: string | null;+ createdAt: string;+ startedAt: string | null;+ finishedAt: string | null;+};++export type FileImportSpec = {+ destination: string;+ /** Staged batch objects, see staging.ts. */+ batches: number;+};++export type FileImportCursor = { batch: number };++export type ImapImportSource = { folder: string; destination: string };++/** The IMAP password is deliberately not part of the spec: it only travels in queue messages. */+export type ImapImportSpec = {+ host: string;+ port: number;+ secure: boolean;+ username: string;+ limit: number;+ importAll: boolean;+ sources: ImapImportSource[];+};++export type ImapImportCursor = {+ source: number;+ /** Newest messages of the current source already handled. */+ done: number;+ /** Whether the current source's size was added to the job total. */+ counted: boolean;+};++export type ImportJobMessage = {+ kind: "import.run";+ jobId: string;+ imapPassword?: string;+};diff --git a/src/lib/import/jobs.ts b/src/lib/import/jobs.tsnew file mode 100644index 0000000..69e9575--- /dev/null+++ b/src/lib/import/jobs.ts@@ -0,0 +1,332 @@+import { and, desc, eq, inArray, lt } from "drizzle-orm";+import { getDb } from "@/db";+import { importJobs, mailboxes } from "@/db/schema";+import { newId } from "@/lib/ids";+import { getImportMessageUserId, parseImportDestination } from "./destination";+import { fetchImapMessages } from "./imap";+import { deleteStagedImport, importBatchKey, stageImportMessages, unpackImportMessages } from "./staging";+import { importMessagesToMailbox } from "./service";+import type {+ FileImportCursor,+ FileImportSpec,+ ImapImportCursor,+ ImapImportSource,+ ImapImportSpec,+ ImportJobMessage,+ ImportJobSummary,+} from "./jobs-types";+import type { ImportMailboxResult, ImportMessageInput } from "./types";++const ACTIVE = ["queued", "running"] as const;+const MAX_STORED_ERRORS = 20;+/** IMAP messages fetched per queue message; the connection is reopened for each. */+const IMAP_CHUNK_SIZE = 50;+const STALE_AFTER_MS = 30 * 60 * 1000;+const KEEP_FINISHED_MS = 7 * 24 * 60 * 60 * 1000;++type JobRow = typeof importJobs.$inferSelect;++type Chunk = {+ result: ImportMailboxResult;+ cursor: FileImportCursor | ImapImportCursor;+ totalDelta: number;+ done: boolean;+ /** Staged object that is safe to delete once the chunk is recorded. */+ consumedKey?: string;+};++function parseJson<T>(value: string, fallback: T): T {+ try {+ return JSON.parse(value) as T;+ } catch {+ return fallback;+ }+}++async function enqueue(env: CloudflareEnv, message: ImportJobMessage): Promise<void> {+ await env.OUTBOUND_QUEUE.send(message);+}++export function toImportJobSummary(row: JobRow): ImportJobSummary {+ let currentSource: string | null = null;+ if (row.kind === "imap" && row.status === "running") {+ const spec = parseJson<ImapImportSpec | null>(row.spec, null);+ const cursor = parseJson<ImapImportCursor>(row.cursor, { source: 0, done: 0, counted: false });+ currentSource = spec?.sources[cursor.source]?.folder ?? null;+ }+ return {+ id: row.id,+ mailboxId: row.mailboxId,+ kind: row.kind,+ label: row.label,+ status: row.status,+ total: row.total,+ processed: row.processed,+ imported: row.imported,+ skipped: row.skipped,+ errors: parseJson<string[]>(row.errors, []),+ errorCount: row.errorCount,+ error: row.error,+ currentSource,+ createdAt: row.createdAt.toISOString(),+ startedAt: row.startedAt?.toISOString() ?? null,+ finishedAt: row.finishedAt?.toISOString() ?? null,+ };+}++export async function listImportJobs(env: CloudflareEnv, userId: string, limit = 20): Promise<ImportJobSummary[]> {+ const rows = await getDb(env)+ .select()+ .from(importJobs)+ .where(eq(importJobs.userId, userId))+ .orderBy(desc(importJobs.createdAt))+ .limit(limit);+ return rows.map(toImportJobSummary);+}++export async function createFileImportJob(+ env: CloudflareEnv,+ input: { userId: string; mailboxId: string; label: string; destination: string; messages: ImportMessageInput[] },+): Promise<ImportJobSummary> {+ const db = getDb(env);+ const id = newId("imp");+ const batches = await stageImportMessages(env, id, input.messages);+ const spec: FileImportSpec = { destination: input.destination, batches };+ const cursor: FileImportCursor = { batch: 0 };+ try {+ const [row] = await db+ .insert(importJobs)+ .values({+ id,+ userId: input.userId,+ mailboxId: input.mailboxId,+ kind: "file",+ label: input.label,+ total: input.messages.length,+ spec: JSON.stringify(spec),+ cursor: JSON.stringify(cursor),+ })+ .returning();+ await enqueue(env, { kind: "import.run", jobId: id });+ return toImportJobSummary(row);+ } catch (error) {+ await deleteStagedImport(env, id, batches);+ await db.delete(importJobs).where(eq(importJobs.id, id));+ throw error;+ }+}++export async function createImapImportJob(+ env: CloudflareEnv,+ input: {+ userId: string;+ mailboxId: string;+ password: string;+ spec: ImapImportSpec;+ },+): Promise<ImportJobSummary> {+ const db = getDb(env);+ const id = newId("imp");+ const cursor: ImapImportCursor = { source: 0, done: 0, counted: false };+ try {+ const [row] = await db+ .insert(importJobs)+ .values({+ id,+ userId: input.userId,+ mailboxId: input.mailboxId,+ kind: "imap",+ label: `${input.spec.username} @ ${input.spec.host}`,+ spec: JSON.stringify(input.spec),+ cursor: JSON.stringify(cursor),+ })+ .returning();+ await enqueue(env, { kind: "import.run", jobId: id, imapPassword: input.password });+ return toImportJobSummary(row);+ } catch (error) {+ await db.delete(importJobs).where(eq(importJobs.id, id));+ throw error;+ }+}++async function cleanupJob(env: CloudflareEnv, row: JobRow): Promise<void> {+ if (row.kind !== "file") return;+ const spec = parseJson<FileImportSpec>(row.spec, { destination: "", batches: 0 });+ await deleteStagedImport(env, row.id, spec.batches);+}++/** Cancels a running job, or removes the record of a finished one. Returns false when it is not the user's. */+export async function cancelOrDismissImportJob(env: CloudflareEnv, userId: string, jobId: string): Promise<boolean> {+ const db = getDb(env);+ const [row] = await db+ .select()+ .from(importJobs)+ .where(and(eq(importJobs.id, jobId), eq(importJobs.userId, userId)))+ .limit(1);+ if (!row) return false;+ if (row.status === "queued" || row.status === "running") {+ const now = new Date();+ await db+ .update(importJobs)+ .set({ status: "cancelled", finishedAt: now, updatedAt: now })+ .where(and(eq(importJobs.id, jobId), inArray(importJobs.status, [...ACTIVE])));+ await cleanupJob(env, row);+ } else {+ await db.delete(importJobs).where(eq(importJobs.id, jobId));+ }+ return true;+}++/** Runs one chunk of a job and queues the next. Never throws for import failures; they land on the job. */+export async function processImportJob(env: CloudflareEnv, message: ImportJobMessage): Promise<void> {+ const db = getDb(env);+ const [job] = await db.select().from(importJobs).where(eq(importJobs.id, message.jobId)).limit(1);+ if (!job || (job.status !== "queued" && job.status !== "running")) return;++ if (job.status === "queued") {+ await db+ .update(importJobs)+ .set({ status: "running", startedAt: new Date(), updatedAt: new Date() })+ .where(and(eq(importJobs.id, job.id), eq(importJobs.status, "queued")));+ }++ let chunk: Chunk;+ try {+ chunk = job.kind === "file" ? await runFileChunk(env, job) : await runImapChunk(env, job, message.imapPassword);+ } catch (error) {+ const now = new Date();+ await db+ .update(importJobs)+ .set({+ status: "failed",+ error: error instanceof Error ? error.message : "Import failed",+ finishedAt: now,+ updatedAt: now,+ })+ .where(and(eq(importJobs.id, job.id), inArray(importJobs.status, [...ACTIVE])));+ await cleanupJob(env, job);+ return;+ }++ const now = new Date();+ const errors = [...parseJson<string[]>(job.errors, []), ...chunk.result.errors].slice(0, MAX_STORED_ERRORS);+ const recorded = await db+ .update(importJobs)+ .set({+ total: job.total + chunk.totalDelta,+ processed: job.processed + chunk.result.imported + chunk.result.skipped,+ imported: job.imported + chunk.result.imported,+ skipped: job.skipped + chunk.result.skipped,+ errors: JSON.stringify(errors),+ errorCount: job.errorCount + chunk.result.errors.length,+ cursor: JSON.stringify(chunk.cursor),+ updatedAt: now,+ ...(chunk.done ? { status: "completed" as const, finishedAt: now } : {}),+ })+ .where(and(eq(importJobs.id, job.id), inArray(importJobs.status, [...ACTIVE])))+ .returning({ id: importJobs.id });++ // Cancelled while the chunk ran: the cancel already cleaned up.+ if (!recorded.length) return;+ if (chunk.consumedKey) await env.BUCKET.delete(chunk.consumedKey).catch(() => undefined);+ if (!chunk.done) await enqueue(env, message);+}++async function getImportUserId(env: CloudflareEnv, job: JobRow, destinationValue: string): Promise<string> {+ const [mailbox] = await getDb(env)+ .select({ userId: mailboxes.userId })+ .from(mailboxes)+ .where(eq(mailboxes.id, job.mailboxId))+ .limit(1);+ if (!mailbox) throw new Error("Mailbox no longer exists");+ return getImportMessageUserId(parseImportDestination(destinationValue), job.userId, mailbox.userId);+}++async function runFileChunk(env: CloudflareEnv, job: JobRow): Promise<Chunk> {+ const spec = parseJson<FileImportSpec>(job.spec, { destination: "", batches: 0 });+ const cursor = parseJson<FileImportCursor>(job.cursor, { batch: 0 });+ const key = importBatchKey(job.id, cursor.batch);+ const object = await env.BUCKET.get(key);+ if (!object) throw new Error("Uploaded files are no longer available; upload them again");+ const messages = unpackImportMessages(new Uint8Array(await object.arrayBuffer()));+ const result = await importMessagesToMailbox(env, {+ userId: await getImportUserId(env, job, spec.destination),+ mailboxId: job.mailboxId,+ destination: parseImportDestination(spec.destination),+ messages,+ });+ const next = cursor.batch + 1;+ return { result, cursor: { batch: next }, totalDelta: 0, done: next >= spec.batches, consumedKey: key };+}++async function runImapChunk(env: CloudflareEnv, job: JobRow, password: string | undefined): Promise<Chunk> {+ if (!password) throw new Error("IMAP credentials were lost; start the import again");+ const spec = parseJson<ImapImportSpec | null>(job.spec, null);+ if (!spec) throw new Error("Import settings are unreadable");+ const cursor = parseJson<ImapImportCursor>(job.cursor, { source: 0, done: 0, counted: false });+ const source: ImapImportSource | undefined = spec.sources[cursor.source];+ const empty: ImportMailboxResult = { imported: 0, skipped: 0, errors: [] };+ if (!source) return { result: empty, cursor, totalDelta: 0, done: true };++ const batchSize = Math.min(IMAP_CHUNK_SIZE, spec.importAll ? spec.limit : spec.limit - cursor.done);+ const fetched =+ batchSize > 0+ ? await fetchImapMessages({+ host: spec.host,+ port: spec.port,+ secure: spec.secure,+ username: spec.username,+ password,+ folder: source.folder,+ limit: batchSize,+ offset: cursor.done,+ })+ : { messages: [], total: 0, nextOffset: null };++ const result = fetched.messages.length+ ? await importMessagesToMailbox(env, {+ userId: await getImportUserId(env, job, source.destination),+ mailboxId: job.mailboxId,+ destination: parseImportDestination(source.destination),+ messages: fetched.messages,+ })+ : empty;++ const done = cursor.done + fetched.messages.length;+ const totalDelta = cursor.counted ? 0 : spec.importAll ? fetched.total : Math.min(spec.limit, fetched.total);+ const sourceFinished = fetched.nextOffset === null || fetched.messages.length === 0 || (!spec.importAll && done >= spec.limit);+ if (!sourceFinished) {+ return { result, cursor: { source: cursor.source, done, counted: true }, totalDelta, done: false };+ }+ const nextSource = cursor.source + 1;+ return {+ result,+ cursor: { source: nextSource, done: 0, counted: false },+ totalDelta,+ done: nextSource >= spec.sources.length,+ };+}++/** Fails jobs whose queue messages were lost, and drops old finished records. Safe to run on every cron tick. */+export async function sweepImportJobs(env: CloudflareEnv, now = new Date()): Promise<void> {+ const db = getDb(env);+ const stale = await db+ .select()+ .from(importJobs)+ .where(and(inArray(importJobs.status, [...ACTIVE]), lt(importJobs.updatedAt, new Date(now.getTime() - STALE_AFTER_MS))));+ for (const row of stale) {+ await db+ .update(importJobs)+ .set({ status: "failed", error: "Import was interrupted", finishedAt: now, updatedAt: now })+ .where(and(eq(importJobs.id, row.id), inArray(importJobs.status, [...ACTIVE])));+ await cleanupJob(env, row);+ }+ await db+ .delete(importJobs)+ .where(+ and(+ inArray(importJobs.status, ["completed", "failed", "cancelled"]),+ lt(importJobs.finishedAt, new Date(now.getTime() - KEEP_FINISHED_MS)),+ ),+ );+}diff --git a/src/lib/import/staging.ts b/src/lib/import/staging.tsnew file mode 100644index 0000000..2e49f6b--- /dev/null+++ b/src/lib/import/staging.ts@@ -0,0 +1,74 @@+import type { ImportMessageInput } from "./types";++/** Messages per staged object, and per queue message that consumes one. */+export const IMPORT_BATCH_SIZE = 25;++export function importBatchKey(jobId: string, batch: number): string {+ return `import-jobs/${jobId}/${batch}.bin`;+}++/**+ * A staged batch is `[u32 header length][JSON header][raw message bytes...]`, so a+ * whole upload costs one bucket write per batch instead of one per message.+ */+export function packImportMessages(items: ImportMessageInput[]): Uint8Array {+ const header = new TextEncoder().encode(+ JSON.stringify(+ items.map((item) => ({ filename: item.filename, size: item.raw.byteLength, read: item.read, starred: item.starred })),+ ),+ );+ const bodySize = items.reduce((sum, item) => sum + item.raw.byteLength, 0);+ const out = new Uint8Array(4 + header.byteLength + bodySize);+ new DataView(out.buffer).setUint32(0, header.byteLength);+ out.set(header, 4);+ let offset = 4 + header.byteLength;+ for (const item of items) {+ out.set(new Uint8Array(item.raw), offset);+ offset += item.raw.byteLength;+ }+ return out;+}++export function unpackImportMessages(bytes: Uint8Array): ImportMessageInput[] {+ const headerLength = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(0);+ const header = JSON.parse(new TextDecoder().decode(bytes.subarray(4, 4 + headerLength))) as {+ filename: string;+ size: number;+ read?: boolean;+ starred?: boolean;+ }[];+ let offset = 4 + headerLength;+ return header.map(({ filename, size, read, starred }) => {+ const raw = bytes.slice(offset, offset + size).buffer as ArrayBuffer;+ offset += size;+ return { filename, raw, ...(read === undefined ? {} : { read }), ...(starred === undefined ? {} : { starred }) };+ });+}++/** Stages messages in batches and returns how many objects were written. */+export async function stageImportMessages(+ env: CloudflareEnv,+ jobId: string,+ messages: ImportMessageInput[],+): Promise<number> {+ let batches = 0;+ try {+ for (let start = 0; start < messages.length; start += IMPORT_BATCH_SIZE) {+ await env.BUCKET.put(+ importBatchKey(jobId, batches),+ packImportMessages(messages.slice(start, start + IMPORT_BATCH_SIZE)),+ );+ batches += 1;+ }+ } catch (error) {+ await deleteStagedImport(env, jobId, batches);+ throw error;+ }+ return batches;+}++export async function deleteStagedImport(env: CloudflareEnv, jobId: string, batches: number): Promise<void> {+ for (let batch = 0; batch < batches; batch += 1) {+ await env.BUCKET.delete(importBatchKey(jobId, batch)).catch(() => undefined);+ }+}diff --git a/tests/import-staging.test.mjs b/tests/import-staging.test.mjsnew file mode 100644index 0000000..cbd3281--- /dev/null+++ b/tests/import-staging.test.mjs@@ -0,0 +1,96 @@+import assert from "node:assert/strict";+import { mkdtempSync, rmSync } from "node:fs";+import { tmpdir } from "node:os";+import { dirname, join } from "node:path";+import test, { after } from "node:test";+import { fileURLToPath, pathToFileURL } from "node:url";++import { build } from "esbuild";++const root = join(dirname(fileURLToPath(import.meta.url)), "..");+const outDir = mkdtempSync(join(tmpdir(), "mailflare-import-staging-test-"));+after(() => rmSync(outDir, { recursive: true, force: true }));++await build({+ stdin: {+ contents: `export * from "./src/lib/import/staging.ts"; export * from "./src/components/settings/import-jobs-utils.ts";`,+ resolveDir: root,+ sourcefile: "import-staging-test-entry.js",+ },+ outfile: join(outDir, "entry.mjs"),+ bundle: true,+ platform: "node",+ format: "esm",+ target: "node22",+ tsconfig: join(root, "tsconfig.json"),+ logLevel: "silent",+});++const { packImportMessages, unpackImportMessages, stageImportMessages, deleteStagedImport, importBatchKey, IMPORT_BATCH_SIZE, getImportJobPercent, isImportJobActive } =+ await import(pathToFileURL(join(outDir, "entry.mjs")).href);++const encode = (text) => new TextEncoder().encode(text).buffer;+const decode = (buffer) => new TextDecoder().decode(buffer);++test("a packed batch unpacks to the same filenames and bytes", () => {+ const items = [+ { filename: "a.eml", raw: encode("Subject: one\r\n\r\nbody") },+ { filename: "inbox.mbox#2", raw: encode("") },+ { filename: "ü.eml", raw: encode("héllo") },+ ];+ const unpacked = unpackImportMessages(packImportMessages(items));+ assert.deepEqual(unpacked.map((item) => item.filename), items.map((item) => item.filename));+ assert.deepEqual(unpacked.map((item) => decode(item.raw)), ["Subject: one\r\n\r\nbody", "", "héllo"]);+});++test("unpacking works on a view into a larger buffer", () => {+ const packed = packImportMessages([{ filename: "x.eml", raw: encode("data") }]);+ const padded = new Uint8Array(packed.byteLength + 10);+ padded.set(packed, 7);+ const [item] = unpackImportMessages(padded.subarray(7, 7 + packed.byteLength));+ assert.equal(decode(item.raw), "data");+});++test("staging writes one object per batch and cleanup removes them", async () => {+ const objects = new Map();+ const env = {+ BUCKET: {+ put: async (key, value) => void objects.set(key, value),+ delete: async (key) => void objects.delete(key),+ },+ };+ const messages = Array.from({ length: IMPORT_BATCH_SIZE * 2 + 1 }, (_, index) => ({ filename: `${index}.eml`, raw: encode(`m${index}`) }));+ const batches = await stageImportMessages(env, "imp_1", messages);+ assert.equal(batches, 3);+ assert.deepEqual([...objects.keys()], [0, 1, 2].map((n) => importBatchKey("imp_1", n)));+ assert.equal(unpackImportMessages(objects.get(importBatchKey("imp_1", 2))).length, 1);+ await deleteStagedImport(env, "imp_1", batches);+ assert.equal(objects.size, 0);+});++test("a failed write removes the batches already staged", async () => {+ const objects = new Map();+ let writes = 0;+ const env = {+ BUCKET: {+ put: async (key, value) => {+ writes += 1;+ if (writes === 2) throw new Error("bucket down");+ objects.set(key, value);+ },+ delete: async (key) => void objects.delete(key),+ },+ };+ const messages = Array.from({ length: IMPORT_BATCH_SIZE + 1 }, (_, index) => ({ filename: `${index}.eml`, raw: encode("x") }));+ await assert.rejects(stageImportMessages(env, "imp_2", messages), /bucket down/);+ assert.equal(objects.size, 0);+});++test("progress is unknown until the job has a size, and capped at 100", () => {+ assert.equal(getImportJobPercent({ status: "running", total: 0, processed: 0 }), null);+ assert.equal(getImportJobPercent({ status: "running", total: 4, processed: 1 }), 25);+ assert.equal(getImportJobPercent({ status: "running", total: 4, processed: 9 }), 100);+ assert.equal(getImportJobPercent({ status: "completed", total: 0, processed: 0 }), 100);+ assert.equal(isImportJobActive({ status: "queued" }), true);+ assert.equal(isImportJobActive({ status: "failed" }), false);+});diff --git a/worker-utils.ts b/worker-utils.tsindex b598c94..fac9216 100644--- a/worker-utils.ts+++ b/worker-utils.ts@@ -1,5 +1,6 @@import type { InboundQueueMessage } from "./src/lib/email/inbound";import type { WebhookRetryMessage } from "./src/lib/email/webhooks";+import type { ImportJobMessage } from "./src/lib/import/jobs-types";export function isInboundQueueMessage(payload: unknown): payload is InboundQueueMessage {return (@@ -19,3 +20,12 @@ export function isWebhookRetryMessage(payload: unknown): payload is WebhookRetrytypeof (payload as { deliveryId?: unknown }).deliveryId === "string");}++export function isImportJobMessage(payload: unknown): payload is ImportJobMessage {+ return (+ typeof payload === "object" &&+ payload !== null &&+ (payload as { kind?: unknown }).kind === "import.run" &&+ typeof (payload as { jobId?: unknown }).jobId === "string"+ );+}diff --git a/worker.ts b/worker.tsindex 1cc596b..d70571d 100644--- a/worker.ts+++ b/worker.ts@@ -6,7 +6,8 @@ import {type InboundQueueMessage,} from "./src/lib/email/inbound";import { processOutboundQueue, type OutboundQueueMessage } from "./src/lib/email/send";-import { isInboundQueueMessage, isWebhookRetryMessage } from "./worker-utils";+import { isImportJobMessage, isInboundQueueMessage, isWebhookRetryMessage } from "./worker-utils";+import { processImportJob, sweepImportJobs } from "./src/lib/import/jobs";import { processWebhookRetry, type WebhookRetryMessage } from "./src/lib/email/webhooks";import { resolveIncomingMail, forwardMessage } from "./src/lib/email/incoming";import { getUserFromSession } from "./src/lib/auth/session";@@ -112,6 +113,8 @@ export default {await processAgentDraftJob(env, (msg.body as { jobId: string }).jobId);} else if (isWebhookRetryMessage(msg.body)) {await processWebhookRetry(env, msg.body as WebhookRetryMessage);+ } else if (isImportJobMessage(msg.body)) {+ await processImportJob(env, msg.body);} else if (typeof msg.body === "object" && msg.body !== null && (msg.body as { kind?: unknown }).kind === "email.scheduled") {await processOutboundQueue(env, msg.body as OutboundQueueMessage);} else {@@ -136,6 +139,7 @@ export default {ctx.waitUntil(runAgentMaintenance(env));ctx.waitUntil(runTrashRetention(env, new Date(controller.scheduledTime)));ctx.waitUntil(runFollowUpReminders(env, new Date(controller.scheduledTime)));+ ctx.waitUntil(sweepImportJobs(env, new Date(controller.scheduledTime)));if (controller.cron === "0 2 * * *") ctx.waitUntil(runAccountRetention(env, new Date(controller.scheduledTime)));// Drive trash is emptied once a day, with the 02:00 UTC cron; the 5-minute cron is for queue-like upkeep.if (controller.cron === "0 2 * * *") ctx.waitUntil(runDriveTrashRetention(env, new Date(controller.scheduledTime)));