@remit/mailbox-service 0.0.112 → 0.0.114
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/account-credentials.d.ts +83 -0
- package/dist/account-credentials.js +104 -0
- package/dist/attribute-mapper.d.ts +36 -0
- package/dist/attribute-mapper.js +119 -0
- package/dist/body-parse.d.ts +28 -0
- package/{src/body-parse.ts → dist/body-parse.js} +18 -26
- package/dist/body-part-mapper.d.ts +92 -0
- package/dist/body-part-mapper.js +301 -0
- package/dist/body-sync-queue.d.ts +59 -0
- package/dist/body-sync-queue.js +56 -0
- package/dist/body-sync-terminal.d.ts +58 -0
- package/dist/body-sync-terminal.js +95 -0
- package/dist/body-sync.d.ts +351 -0
- package/dist/body-sync.js +1204 -0
- package/dist/calendar-parts.d.ts +23 -0
- package/dist/calendar-parts.js +18 -0
- package/dist/classification-backfill.d.ts +91 -0
- package/dist/classification-backfill.js +180 -0
- package/dist/classify-message.d.ts +31 -0
- package/dist/classify-message.js +89 -0
- package/dist/connection-factory.d.ts +54 -0
- package/dist/connection-factory.js +54 -0
- package/dist/connection-test.d.ts +41 -0
- package/dist/connection-test.js +152 -0
- package/dist/external-move.d.ts +94 -0
- package/dist/external-move.js +173 -0
- package/dist/filter-operations.d.ts +42 -0
- package/dist/filter-operations.js +211 -0
- package/dist/filters/filter-config.d.ts +14 -0
- package/dist/filters/filter-config.js +10 -0
- package/{src/filters/list-id.ts → dist/filters/list-id.d.ts} +2 -16
- package/dist/filters/list-id.js +26 -0
- package/dist/filters/match.d.ts +69 -0
- package/dist/filters/match.js +165 -0
- package/dist/filters/pipeline.d.ts +64 -0
- package/dist/filters/pipeline.js +100 -0
- package/{src/flag-push-terminal.ts → dist/flag-push-terminal.d.ts} +16 -72
- package/dist/flag-push-terminal.js +63 -0
- package/dist/flag-push.d.ts +69 -0
- package/dist/flag-push.js +111 -0
- package/dist/flag-queue.d.ts +175 -0
- package/dist/flag-queue.js +291 -0
- package/dist/folder-operations.d.ts +39 -0
- package/dist/folder-operations.js +112 -0
- package/dist/folder-placement.d.ts +13 -0
- package/dist/folder-placement.js +23 -0
- package/dist/heuristics/authenticationResults.d.ts +10 -0
- package/dist/heuristics/authenticationResults.js +187 -0
- package/dist/heuristics/classifyByHeaders.d.ts +82 -0
- package/dist/heuristics/classifyByHeaders.js +365 -0
- package/dist/heuristics/classifyPlacement.d.ts +67 -0
- package/dist/heuristics/classifyPlacement.js +153 -0
- package/dist/heuristics/machineSenders.d.ts +21 -0
- package/{src/heuristics/machineSenders.ts → dist/heuristics/machineSenders.js} +34 -40
- package/dist/heuristics/senderMismatch.d.ts +81 -0
- package/dist/heuristics/senderMismatch.js +192 -0
- package/dist/heuristics/socialDomains.d.ts +14 -0
- package/{src/heuristics/socialDomains.ts → dist/heuristics/socialDomains.js} +9 -9
- package/dist/heuristics/transactionalDomains.d.ts +18 -0
- package/{src/heuristics/transactionalDomains.ts → dist/heuristics/transactionalDomains.js} +8 -8
- package/dist/imapflow-connection.d.ts +377 -0
- package/dist/imapflow-connection.js +1147 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.js +65 -0
- package/dist/list-id-backfill.d.ts +83 -0
- package/dist/list-id-backfill.js +132 -0
- package/dist/mailbox-cursor-rebuild.d.ts +74 -0
- package/dist/mailbox-cursor-rebuild.js +71 -0
- package/{src/mailbox-cursor.ts → dist/mailbox-cursor.d.ts} +20 -98
- package/dist/mailbox-cursor.js +102 -0
- package/dist/mailbox-intent.d.ts +41 -0
- package/{src/mailbox-intent.ts → dist/mailbox-intent.js} +38 -64
- package/dist/mailbox-management.d.ts +240 -0
- package/dist/mailbox-management.js +576 -0
- package/dist/mailbox-presence.d.ts +52 -0
- package/{src/mailbox-presence.ts → dist/mailbox-presence.js} +19 -30
- package/dist/mailbox-queue.d.ts +129 -0
- package/dist/mailbox-queue.js +216 -0
- package/dist/mailbox-sync.d.ts +93 -0
- package/dist/mailbox-sync.js +490 -0
- package/dist/mailbox-upstream.d.ts +38 -0
- package/{src/mailbox-upstream.ts → dist/mailbox-upstream.js} +37 -47
- package/dist/message-category.d.ts +51 -0
- package/{src/message-category.ts → dist/message-category.js} +28 -71
- package/dist/message-move.d.ts +285 -0
- package/dist/message-move.js +858 -0
- package/dist/message-parser.d.ts +14 -0
- package/dist/message-parser.js +17 -0
- package/{src/message-presence.ts → dist/message-presence.d.ts} +1 -13
- package/dist/message-presence.js +29 -0
- package/dist/message-read.d.ts +51 -0
- package/dist/message-read.js +31 -0
- package/dist/message-sync.d.ts +345 -0
- package/dist/message-sync.js +1358 -0
- package/dist/mime-walker.d.ts +102 -0
- package/dist/mime-walker.js +198 -0
- package/dist/organize-job.d.ts +20 -0
- package/dist/organize-job.js +37 -0
- package/dist/outbox-attachment-content.d.ts +37 -0
- package/dist/outbox-attachment-content.js +50 -0
- package/dist/outbox-attachment-filename.d.ts +19 -0
- package/dist/outbox-attachment-filename.js +64 -0
- package/dist/outbox-attachment.d.ts +148 -0
- package/dist/outbox-attachment.js +273 -0
- package/dist/outbox-queue.d.ts +73 -0
- package/dist/outbox-queue.js +280 -0
- package/{src/outbox-status.ts → dist/outbox-status.d.ts} +1 -6
- package/dist/outbox-status.js +18 -0
- package/dist/ownership.d.ts +24 -0
- package/dist/ownership.js +83 -0
- package/dist/placement-move-terminal.d.ts +67 -0
- package/dist/placement-move-terminal.js +88 -0
- package/dist/placement-move.d.ts +83 -0
- package/dist/placement-move.js +216 -0
- package/{src/placement-settled.ts → dist/placement-settled.d.ts} +13 -68
- package/dist/placement-settled.js +75 -0
- package/dist/quarantine.d.ts +95 -0
- package/dist/quarantine.js +131 -0
- package/dist/refusal.d.ts +31 -0
- package/dist/refusal.js +3 -0
- package/{src/restore-source-placement.ts → dist/restore-source-placement.d.ts} +29 -97
- package/dist/restore-source-placement.js +79 -0
- package/dist/search-text-backfill.d.ts +15 -0
- package/dist/search-text-backfill.js +33 -0
- package/dist/snippet.d.ts +61 -0
- package/dist/snippet.js +129 -0
- package/dist/spam-report.d.ts +86 -0
- package/dist/spam-report.js +241 -0
- package/dist/sync-warning.d.ts +21 -0
- package/dist/sync-warning.js +26 -0
- package/dist/sync-watermarks.d.ts +138 -0
- package/dist/sync-watermarks.js +205 -0
- package/dist/test-helpers/fake-imap.d.ts +151 -0
- package/dist/test-helpers/fake-imap.js +508 -0
- package/dist/test-helpers/folder-roles.d.ts +34 -0
- package/dist/test-helpers/folder-roles.js +47 -0
- package/dist/test-helpers/mime-tree-generator.d.ts +82 -0
- package/dist/test-helpers/mime-tree-generator.js +329 -0
- package/dist/test-helpers/test-store.d.ts +69 -0
- package/dist/test-helpers/test-store.js +193 -0
- package/dist/text/normalizer.d.ts +20 -0
- package/dist/text/normalizer.js +102 -0
- package/dist/thread-message-rows.d.ts +69 -0
- package/dist/thread-message-rows.js +40 -0
- package/dist/thread-root.d.ts +7 -0
- package/dist/thread-root.js +25 -0
- package/dist/types.d.ts +343 -0
- package/dist/types.js +19 -0
- package/package.json +86 -88
- package/src/account-credentials.ts +0 -210
- package/src/attribute-mapper.test.ts +0 -104
- package/src/attribute-mapper.ts +0 -148
- package/src/body-parse.test.ts +0 -55
- package/src/body-part-mapper.property.test.ts +0 -354
- package/src/body-part-mapper.ts +0 -410
- package/src/body-sync-queue.test.ts +0 -64
- package/src/body-sync-queue.ts +0 -131
- package/src/body-sync-terminal.test.ts +0 -148
- package/src/body-sync-terminal.ts +0 -174
- package/src/body-sync.test.ts +0 -925
- package/src/body-sync.ts +0 -1891
- package/src/calendar-parts.ts +0 -41
- package/src/classification-backfill.test.ts +0 -285
- package/src/classification-backfill.ts +0 -363
- package/src/classify-message.test.ts +0 -142
- package/src/classify-message.ts +0 -116
- package/src/connection-factory.ts +0 -102
- package/src/connection-test.ts +0 -208
- package/src/credentials.test.ts +0 -181
- package/src/external-move.test.ts +0 -262
- package/src/external-move.ts +0 -285
- package/src/filter-operations.test.ts +0 -319
- package/src/filter-operations.ts +0 -377
- package/src/filter-update.test.ts +0 -313
- package/src/filters/filter-config.test.ts +0 -87
- package/src/filters/filter-config.ts +0 -34
- package/src/filters/list-id.test.ts +0 -50
- package/src/filters/match.test.ts +0 -392
- package/src/filters/match.ts +0 -225
- package/src/filters/pipeline.test.ts +0 -199
- package/src/filters/pipeline.ts +0 -170
- package/src/flag-push.test.ts +0 -112
- package/src/flag-push.ts +0 -198
- package/src/flag-queue.test.ts +0 -197
- package/src/flag-queue.ts +0 -509
- package/src/folder-operations.test.ts +0 -273
- package/src/folder-operations.ts +0 -217
- package/src/folder-placement.ts +0 -56
- package/src/forwarded-message.test.ts +0 -122
- package/src/heuristics/authenticationResults.test.ts +0 -189
- package/src/heuristics/authenticationResults.ts +0 -212
- package/src/heuristics/classifyByHeaders.test.ts +0 -889
- package/src/heuristics/classifyByHeaders.ts +0 -467
- package/src/heuristics/classifyPlacement.test.ts +0 -366
- package/src/heuristics/classifyPlacement.ts +0 -211
- package/src/heuristics/classifyRealMail.test.ts +0 -242
- package/src/heuristics/machineSenders.test.ts +0 -101
- package/src/heuristics/senderMismatch.test.ts +0 -346
- package/src/heuristics/senderMismatch.ts +0 -259
- package/src/imapflow-connect-retry.test.ts +0 -97
- package/src/imapflow-connection.test.ts +0 -503
- package/src/imapflow-connection.ts +0 -1476
- package/src/index.ts +0 -397
- package/src/list-id-backfill.test.ts +0 -207
- package/src/list-id-backfill.ts +0 -274
- package/src/mailbox-cursor-rebuild.test.ts +0 -171
- package/src/mailbox-cursor-rebuild.ts +0 -146
- package/src/mailbox-cursor.test.ts +0 -141
- package/src/mailbox-delete-intent.test.ts +0 -119
- package/src/mailbox-management.test.ts +0 -243
- package/src/mailbox-management.ts +0 -838
- package/src/mailbox-presence.test.ts +0 -74
- package/src/mailbox-queue.ts +0 -397
- package/src/mailbox-rename-intent.test.ts +0 -220
- package/src/mailbox-rename-subtree.test.ts +0 -287
- package/src/mailbox-sync.test.ts +0 -286
- package/src/mailbox-sync.ts +0 -724
- package/src/message-move.test.ts +0 -726
- package/src/message-move.ts +0 -1518
- package/src/message-parser.ts +0 -26
- package/src/message-presence.test.ts +0 -50
- package/src/message-read.ts +0 -87
- package/src/message-save-retry.test.ts +0 -90
- package/src/message-sync-flags.test.ts +0 -167
- package/src/message-sync.test.ts +0 -762
- package/src/message-sync.ts +0 -2133
- package/src/mime-walker.test.ts +0 -311
- package/src/mime-walker.ts +0 -306
- package/src/organize-job.ts +0 -93
- package/src/outbox-attachment-content.test.ts +0 -153
- package/src/outbox-attachment-content.ts +0 -104
- package/src/outbox-attachment-filename.test.ts +0 -132
- package/src/outbox-attachment-filename.ts +0 -76
- package/src/outbox-attachment.test.ts +0 -626
- package/src/outbox-attachment.ts +0 -536
- package/src/outbox-failed-recovery.test.ts +0 -299
- package/src/outbox-queue.test.ts +0 -315
- package/src/outbox-queue.ts +0 -525
- package/src/ownership.ts +0 -140
- package/src/placement-move-terminal.ts +0 -145
- package/src/placement-move.ts +0 -363
- package/src/placement-settled.test.ts +0 -147
- package/src/placement.test.ts +0 -404
- package/src/quarantine.test.ts +0 -133
- package/src/quarantine.ts +0 -229
- package/src/refusal.ts +0 -29
- package/src/snippet.test.ts +0 -213
- package/src/snippet.ts +0 -152
- package/src/spam-report.test.ts +0 -329
- package/src/spam-report.ts +0 -357
- package/src/sync-warning.test.ts +0 -213
- package/src/sync-warning.ts +0 -56
- package/src/sync-watermarks.test.ts +0 -409
- package/src/sync-watermarks.ts +0 -319
- package/src/system-flag-wire-format.test.ts +0 -77
- package/src/test-helpers/fake-imap.ts +0 -690
- package/src/test-helpers/folder-roles.ts +0 -66
- package/src/test-helpers/mime-tree-generator.ts +0 -461
- package/src/test-helpers/test-queue.ts +0 -56
- package/src/test-helpers/test-store.ts +0 -299
- package/src/text/normalizer.test.ts +0 -223
- package/src/text/normalizer.ts +0 -140
- package/src/thread-message-rows.ts +0 -77
- package/src/thread-root.test.ts +0 -29
- package/src/thread-root.ts +0 -51
- package/src/types/mailparser-augment.d.ts +0 -23
- package/src/types/natural-porter-stemmer.d.ts +0 -50
- package/src/types/stopword.d.ts +0 -69
- package/src/types.ts +0 -371
- package/tsconfig.json +0 -8
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mailbox synchronization service
|
|
3
|
+
*
|
|
4
|
+
* Orchestrates syncing mailbox data from IMAP server to the data backend.
|
|
5
|
+
*/
|
|
6
|
+
import { ROLE_SPECIAL_USE, roleForFolderName, } from "@remit/data-ports/folder-role";
|
|
7
|
+
import { MailboxCursorState, MailboxSyncStatus, NamespaceType, } from "@remit/domain-enums";
|
|
8
|
+
import pMap from "p-map";
|
|
9
|
+
import { isNoSelect, parseImapAttributes } from "./attribute-mapper.js";
|
|
10
|
+
import { isCursorRebuildNeeded } from "./mailbox-cursor.js";
|
|
11
|
+
import { isFolderMutationInFlight, isMailboxMutationInFlight, } from "./mailbox-presence.js";
|
|
12
|
+
/**
|
|
13
|
+
* Compare two unordered special-use lists/sets for equality. Treats `undefined`
|
|
14
|
+
* and an empty array as equivalent (a mailbox with no flags).
|
|
15
|
+
*/
|
|
16
|
+
const areSpecialUseSetsEqual = (a, b) => {
|
|
17
|
+
const aArr = a ?? [];
|
|
18
|
+
const bArr = b ?? [];
|
|
19
|
+
if (aArr.length !== bArr.length)
|
|
20
|
+
return false;
|
|
21
|
+
const aSet = new Set(aArr);
|
|
22
|
+
for (const value of bArr) {
|
|
23
|
+
if (!aSet.has(value))
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Service for synchronizing mailbox metadata between IMAP and the data backend.
|
|
30
|
+
*/
|
|
31
|
+
export class MailboxSyncService {
|
|
32
|
+
mailboxService;
|
|
33
|
+
specialUseService;
|
|
34
|
+
log;
|
|
35
|
+
// Required, with no no-op default: the lines below are the only record that a
|
|
36
|
+
// mailbox was created or destroyed, and a default would let the next call site
|
|
37
|
+
// lose them with no error and no failing test.
|
|
38
|
+
constructor(mailboxService, specialUseService, logger) {
|
|
39
|
+
this.mailboxService = mailboxService;
|
|
40
|
+
this.specialUseService = specialUseService;
|
|
41
|
+
this.log = logger;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Sync all mailboxes for an account from IMAP server
|
|
45
|
+
*
|
|
46
|
+
* @param account - Account info including accountId
|
|
47
|
+
* @param connection - Active IMAP connection
|
|
48
|
+
*/
|
|
49
|
+
syncMailboxes = async (account, connection) => {
|
|
50
|
+
const result = {
|
|
51
|
+
created: 0,
|
|
52
|
+
updated: 0,
|
|
53
|
+
deleted: 0,
|
|
54
|
+
};
|
|
55
|
+
// Get existing mailboxes from database
|
|
56
|
+
const existingMailboxes = await this.getAllMailboxes(account.accountId);
|
|
57
|
+
const existingByPath = new Map(existingMailboxes.map((m) => [m.fullPath, m]));
|
|
58
|
+
// The paths a rename is on its way to (D14). Under D2 `fullPath` stays the
|
|
59
|
+
// path the server held when the intent was recorded, so between the server
|
|
60
|
+
// executing RENAME and the settle, LIST returns a path no row is keyed to
|
|
61
|
+
// — and the insert branch below has no state guard at all. Without this it
|
|
62
|
+
// inserts a second row under a fresh mailboxId and initial-syncs the
|
|
63
|
+
// folder's mail into it, leaving the original as a permanent phantom still
|
|
64
|
+
// holding that folder's messages, filters and role appointments. No unique
|
|
65
|
+
// index on (accountId, fullPath) exists to catch it; #386 adds one.
|
|
66
|
+
//
|
|
67
|
+
// `pending` only. A failed rename keeps its target so the UI can name what
|
|
68
|
+
// it was aiming at (T6), and nothing clears it until the user retries or
|
|
69
|
+
// dismisses — so honouring a `failed` row's claim would let one stuck
|
|
70
|
+
// folder block the sweep from ever discovering a real folder another
|
|
71
|
+
// client later creates at that path.
|
|
72
|
+
const claimedByPendingRename = new Set(existingMailboxes.flatMap((m) => m.syncStatus === MailboxSyncStatus.pending &&
|
|
73
|
+
m.pendingPath !== undefined
|
|
74
|
+
? [m.pendingPath]
|
|
75
|
+
: []));
|
|
76
|
+
// Get namespaces and mailboxes from IMAP
|
|
77
|
+
const namespaces = await connection.getNamespaces();
|
|
78
|
+
const remoteMailboxes = await this.fetchAllMailboxes(connection, namespaces);
|
|
79
|
+
// Build a map of special-use designations claimed by mailboxes with IMAP attributes.
|
|
80
|
+
// Used to:
|
|
81
|
+
// 1. Skip duplicate folders (e.g., "Trash" vs "[Gmail]/Trash") at sync time.
|
|
82
|
+
// 2. Skip duplicate localized folders (e.g., "Sent" vs "Verzonden items") when one
|
|
83
|
+
// of them carries the IMAP \Sent flag — issue #194.
|
|
84
|
+
const claimedSpecialUse = this.buildSpecialUseMap(remoteMailboxes);
|
|
85
|
+
// Track which paths we've seen from remote
|
|
86
|
+
const seenPaths = new Set();
|
|
87
|
+
// Process each remote mailbox (concurrency 3 for IMAP pipelining)
|
|
88
|
+
await pMap(remoteMailboxes, async (mailboxInfo) => {
|
|
89
|
+
// Skip non-selectable mailboxes (container folders that can't hold messages)
|
|
90
|
+
if (isNoSelect(mailboxInfo.attributes)) {
|
|
91
|
+
// Mark as seen so we don't try to delete again in cleanup loop
|
|
92
|
+
seenPaths.add(mailboxInfo.fullPath);
|
|
93
|
+
// If this mailbox exists in DB, delete it
|
|
94
|
+
const existing = existingByPath.get(mailboxInfo.fullPath);
|
|
95
|
+
// A folder mid-create can transiently LIST as `\Noselect`, and
|
|
96
|
+
// deleting the row then races the create exactly as #290
|
|
97
|
+
// documented. The state check goes before the delete, through the
|
|
98
|
+
// same predicate the rest of the sweep asks.
|
|
99
|
+
if (existing && !isFolderMutationInFlight(existing)) {
|
|
100
|
+
// The duplicate-special-use branch below clears the special-use
|
|
101
|
+
// entries too; this one skipped them, orphaning a row per
|
|
102
|
+
// deleted mailbox.
|
|
103
|
+
await this.specialUseService.deleteByMailboxId(existing.mailboxId);
|
|
104
|
+
await this.mailboxService.delete(account.accountId, existing.mailboxId);
|
|
105
|
+
this.log.info({
|
|
106
|
+
mailboxId: existing.mailboxId,
|
|
107
|
+
fullPath: existing.fullPath,
|
|
108
|
+
reason: "non-selectable",
|
|
109
|
+
}, "Deleted mailbox");
|
|
110
|
+
result.deleted++;
|
|
111
|
+
}
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
// Skip duplicate special-use folders (e.g., "Trash" when "[Gmail]/Trash" exists)
|
|
115
|
+
if (this.isDuplicateSpecialUse(mailboxInfo, claimedSpecialUse)) {
|
|
116
|
+
seenPaths.add(mailboxInfo.fullPath);
|
|
117
|
+
const existing = existingByPath.get(mailboxInfo.fullPath);
|
|
118
|
+
if (existing && !isFolderMutationInFlight(existing)) {
|
|
119
|
+
await this.specialUseService.deleteByMailboxId(existing.mailboxId);
|
|
120
|
+
await this.mailboxService.delete(account.accountId, existing.mailboxId);
|
|
121
|
+
this.log.info({
|
|
122
|
+
mailboxId: existing.mailboxId,
|
|
123
|
+
fullPath: existing.fullPath,
|
|
124
|
+
reason: "duplicate-special-use",
|
|
125
|
+
}, "Deleted mailbox");
|
|
126
|
+
result.deleted++;
|
|
127
|
+
}
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
seenPaths.add(mailboxInfo.fullPath);
|
|
131
|
+
const existing = existingByPath.get(mailboxInfo.fullPath);
|
|
132
|
+
if (existing) {
|
|
133
|
+
// A folder at either end of its lifecycle is left alone: the worker
|
|
134
|
+
// that establishes or removes it writes its own identity back, and
|
|
135
|
+
// reading its status meanwhile is work whose only possible outcome is
|
|
136
|
+
// a failure that fails this whole account's fan-out with it.
|
|
137
|
+
if (isFolderMutationInFlight(existing)) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
// The folder set can change under this sweep. A delete that lands
|
|
141
|
+
// between the LIST above and this mailbox's STATUS fails on the
|
|
142
|
+
// server or on the row, and that would abort the enumeration of every
|
|
143
|
+
// other folder and the SYNC_MESSAGES fan-out behind it — on a
|
|
144
|
+
// per-account FIFO queue, for the whole visibility window (issue
|
|
145
|
+
// #339). One folder leaving is not a failed account sync. Anything
|
|
146
|
+
// else still propagates.
|
|
147
|
+
const updated = await this.updateMailbox(account.accountId, existing, mailboxInfo, connection).catch(async (error) => {
|
|
148
|
+
// The read only classifies the failure in hand; one that cannot
|
|
149
|
+
// answer must not replace it.
|
|
150
|
+
const gone = await isMailboxMutationInFlight(this.mailboxService, account.accountId, existing.mailboxId).catch(() => false);
|
|
151
|
+
if (gone)
|
|
152
|
+
return null;
|
|
153
|
+
throw error;
|
|
154
|
+
});
|
|
155
|
+
if (updated) {
|
|
156
|
+
result.updated++;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
else if (!claimedByPendingRename.has(mailboxInfo.fullPath)) {
|
|
160
|
+
const created = await this.createMailbox(account.accountId, mailboxInfo, namespaces, connection);
|
|
161
|
+
if (created)
|
|
162
|
+
result.created++;
|
|
163
|
+
}
|
|
164
|
+
}, { concurrency: 3 });
|
|
165
|
+
// Handle deleted mailboxes (exist in DB but not on server)
|
|
166
|
+
for (const existing of existingMailboxes) {
|
|
167
|
+
if (seenPaths.has(existing.fullPath))
|
|
168
|
+
continue;
|
|
169
|
+
// A row with a mutation in flight is a folder whose absence from the
|
|
170
|
+
// LIST is expected, not a server-side deletion. Deleting it races the
|
|
171
|
+
// worker: the row vanishes, then MAILBOX_CREATE fails with NotFoundError
|
|
172
|
+
// trying to mark it synced, and — sharing this account's mailboxes FIFO
|
|
173
|
+
// group — that un-acked failure stalls every later mailbox sync for the
|
|
174
|
+
// queue's whole visibility window (#290).
|
|
175
|
+
//
|
|
176
|
+
// `deleting` is here for a second reason. That window is exactly when a
|
|
177
|
+
// delete has landed on the server and the worker has not written back,
|
|
178
|
+
// so hard-deleting the row removes it under the client holding its id —
|
|
179
|
+
// the "Mailbox not found: <old id>" of #333 — and removes it without the
|
|
180
|
+
// folder's mail, which is the worker's job (D8). Leave both to the flow
|
|
181
|
+
// that owns them.
|
|
182
|
+
if (isFolderMutationInFlight(existing))
|
|
183
|
+
continue;
|
|
184
|
+
await this.mailboxService.delete(account.accountId, existing.mailboxId);
|
|
185
|
+
this.log.info({
|
|
186
|
+
mailboxId: existing.mailboxId,
|
|
187
|
+
fullPath: existing.fullPath,
|
|
188
|
+
reason: "not-on-server",
|
|
189
|
+
}, "Deleted mailbox");
|
|
190
|
+
result.deleted++;
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* Sync metadata for a specific mailbox
|
|
196
|
+
*
|
|
197
|
+
* Opens the mailbox to get current UID validity, counts, etc.
|
|
198
|
+
*/
|
|
199
|
+
syncMailboxMetadata = async (accountId, mailboxId, connection) => {
|
|
200
|
+
const mailbox = await this.mailboxService.get(accountId, mailboxId);
|
|
201
|
+
const boxStatus = await connection.openBox(mailbox.fullPath, true);
|
|
202
|
+
// See `updateMailbox` above for why this trips (not skips) on a
|
|
203
|
+
// UIDVALIDITY change instead of silently overwriting the stored value.
|
|
204
|
+
const uidValidityChanged = mailbox.uidValidity !== boxStatus.uidvalidity;
|
|
205
|
+
const cursorTrip = uidValidityChanged && !isCursorRebuildNeeded(mailbox.cursorState)
|
|
206
|
+
? { cursorState: MailboxCursorState.cursor_invalid }
|
|
207
|
+
: {};
|
|
208
|
+
return this.mailboxService
|
|
209
|
+
.update(accountId, mailboxId, {
|
|
210
|
+
uidValidity: boxStatus.uidvalidity,
|
|
211
|
+
uidNext: boxStatus.uidnext,
|
|
212
|
+
messageCount: boxStatus.messages.total,
|
|
213
|
+
...cursorTrip,
|
|
214
|
+
})
|
|
215
|
+
.finally(() => connection.closeBox(false));
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* Get all mailboxes for an account, handling pagination
|
|
219
|
+
*/
|
|
220
|
+
getAllMailboxes = async (accountId) => {
|
|
221
|
+
const allMailboxes = [];
|
|
222
|
+
let continuationToken;
|
|
223
|
+
do {
|
|
224
|
+
const result = await this.mailboxService.listByAccount(accountId, {
|
|
225
|
+
continuationToken,
|
|
226
|
+
});
|
|
227
|
+
allMailboxes.push(...result.items);
|
|
228
|
+
continuationToken = result.continuationToken;
|
|
229
|
+
} while (continuationToken);
|
|
230
|
+
return allMailboxes;
|
|
231
|
+
};
|
|
232
|
+
/**
|
|
233
|
+
* Fetch all mailboxes from IMAP server across all namespaces.
|
|
234
|
+
* Uses listMailboxes() to preserve original paths from the server.
|
|
235
|
+
*/
|
|
236
|
+
fetchAllMailboxes = async (connection, namespaces) => {
|
|
237
|
+
// Flatten all namespaces with their types
|
|
238
|
+
const allNamespaces = [
|
|
239
|
+
...namespaces.personal.map((ns) => ({
|
|
240
|
+
type: NamespaceType.Personal,
|
|
241
|
+
prefix: ns.prefix || "",
|
|
242
|
+
})),
|
|
243
|
+
...namespaces.other.map((ns) => ({
|
|
244
|
+
type: NamespaceType.OtherUsers,
|
|
245
|
+
prefix: ns.prefix || "",
|
|
246
|
+
})),
|
|
247
|
+
...namespaces.shared.map((ns) => ({
|
|
248
|
+
type: NamespaceType.Shared,
|
|
249
|
+
prefix: ns.prefix || "",
|
|
250
|
+
})),
|
|
251
|
+
];
|
|
252
|
+
// Fetch mailboxes for each namespace and flatten
|
|
253
|
+
const nestedResults = await Promise.all(allNamespaces.map(async ({ type, prefix }) => {
|
|
254
|
+
const mailboxes = await connection.listMailboxes(prefix);
|
|
255
|
+
return mailboxes.map((mailbox) => ({
|
|
256
|
+
...mailbox,
|
|
257
|
+
namespaceType: type,
|
|
258
|
+
namespacePrefix: prefix,
|
|
259
|
+
}));
|
|
260
|
+
}));
|
|
261
|
+
const results = nestedResults.flat();
|
|
262
|
+
// INBOX is implicit in IMAP and may not be returned by LIST commands
|
|
263
|
+
const hasInbox = results.some((m) => m.fullPath.toUpperCase() === "INBOX");
|
|
264
|
+
if (!hasInbox) {
|
|
265
|
+
const nsDelimiter = namespaces.personal[0]?.delimiter;
|
|
266
|
+
const delimiter = typeof nsDelimiter === "string" ? nsDelimiter : "/";
|
|
267
|
+
results.unshift({
|
|
268
|
+
fullPath: "INBOX",
|
|
269
|
+
name: "INBOX",
|
|
270
|
+
delimiter,
|
|
271
|
+
attributes: [],
|
|
272
|
+
parentPath: null,
|
|
273
|
+
namespaceType: NamespaceType.Personal,
|
|
274
|
+
namespacePrefix: "",
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
return results;
|
|
278
|
+
};
|
|
279
|
+
/**
|
|
280
|
+
* Create a new mailbox in the database
|
|
281
|
+
*/
|
|
282
|
+
createMailbox = async (accountId, mailboxInfo, _namespaces, connection) => {
|
|
283
|
+
// Fetch mailbox status using STATUS command (doesn't require SELECT/EXAMINE)
|
|
284
|
+
// This gets us message counts including unseen without opening the mailbox
|
|
285
|
+
const status = await connection.getMailboxStatus(mailboxInfo.fullPath);
|
|
286
|
+
// Parse special-use attributes (RFC 6154) up front so the row stores a
|
|
287
|
+
// denormalized copy. Frontends list mailboxes by account; threading a join
|
|
288
|
+
// through MailboxSpecialUseEntry per row would be O(N) extra round-trips.
|
|
289
|
+
const parsed = parseImapAttributes(mailboxInfo.attributes);
|
|
290
|
+
const input = {
|
|
291
|
+
accountId,
|
|
292
|
+
namespaceType: mailboxInfo.namespaceType,
|
|
293
|
+
namespacePrefix: mailboxInfo.namespacePrefix,
|
|
294
|
+
hierarchyDelimiter: mailboxInfo.delimiter,
|
|
295
|
+
fullPath: mailboxInfo.fullPath,
|
|
296
|
+
uidValidity: status.uidValidity,
|
|
297
|
+
uidNext: status.uidNext,
|
|
298
|
+
// The message-sync cursor, not a status projection: it records how
|
|
299
|
+
// far THIS mailbox's messages have been applied, and nothing has
|
|
300
|
+
// been. Seeding it from the server's current HIGHESTMODSEQ would
|
|
301
|
+
// declare an unsynced folder already caught up and skip every
|
|
302
|
+
// message in it. Message sync seeds it once it has enumerated the
|
|
303
|
+
// folder.
|
|
304
|
+
highestModseq: "0",
|
|
305
|
+
messageCount: status.messages,
|
|
306
|
+
unseenCount: status.unseen,
|
|
307
|
+
deletedCount: status.deletedCount,
|
|
308
|
+
totalSize: 0,
|
|
309
|
+
lastSyncUid: 0,
|
|
310
|
+
highWaterMarkUid: 0,
|
|
311
|
+
lastMessageSyncAt: 0,
|
|
312
|
+
specialUse: parsed.specialUse.length > 0 ? parsed.specialUse : undefined,
|
|
313
|
+
// A folder the server just told us about is confirmed. The column
|
|
314
|
+
// defaults to this, so the row is right either way; the field is total
|
|
315
|
+
// now (D1) and the insert says what it means.
|
|
316
|
+
syncStatus: MailboxSyncStatus.synced,
|
|
317
|
+
// parentMailboxId would need to be resolved from parentPath
|
|
318
|
+
};
|
|
319
|
+
const created = await this.mailboxService.create(input);
|
|
320
|
+
if (created.outcome === "PathTaken") {
|
|
321
|
+
this.log.error({ accountId, fullPath: mailboxInfo.fullPath }, "Sweep found no row for a folder path the account already holds");
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
const { mailbox } = created;
|
|
325
|
+
// Keep the MailboxSpecialUseEntry table in sync — every backend and worker
|
|
326
|
+
// special-folder lookup reads it through MailboxSpecialUseRepo. Denormalized
|
|
327
|
+
// copy on Mailbox is the read-side optimization, the entries remain the
|
|
328
|
+
// authoritative join source for cross-mailbox lookups.
|
|
329
|
+
if (parsed.specialUse.length > 0) {
|
|
330
|
+
await this.specialUseService.createMany(mailbox.mailboxId, parsed.specialUse);
|
|
331
|
+
this.log.info({
|
|
332
|
+
mailboxId: mailbox.mailboxId,
|
|
333
|
+
fullPath: mailboxInfo.fullPath,
|
|
334
|
+
specialUse: parsed.specialUse,
|
|
335
|
+
}, "Created mailbox");
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
this.log.info({ mailboxId: mailbox.mailboxId, fullPath: mailboxInfo.fullPath }, "Created mailbox");
|
|
339
|
+
}
|
|
340
|
+
return mailbox;
|
|
341
|
+
};
|
|
342
|
+
/**
|
|
343
|
+
* Update an existing mailbox with fresh data.
|
|
344
|
+
* Skips DB write if nothing has changed.
|
|
345
|
+
*
|
|
346
|
+
* @returns The updated mailbox, or null if skipped due to no changes
|
|
347
|
+
*/
|
|
348
|
+
updateMailbox = async (accountId, existing, mailboxInfo, connection) => {
|
|
349
|
+
// Fetch mailbox status using STATUS command (doesn't require SELECT/EXAMINE)
|
|
350
|
+
const status = await connection.getMailboxStatus(mailboxInfo.fullPath);
|
|
351
|
+
const parsed = parseImapAttributes(mailboxInfo.attributes);
|
|
352
|
+
const specialUseChanged = !areSpecialUseSetsEqual(existing.specialUse, parsed.specialUse);
|
|
353
|
+
// UIDVALIDITY detection (#1272): this STATUS-based sweep persists a fresh
|
|
354
|
+
// uidValidity below regardless (harmless — it never touches a stored UID),
|
|
355
|
+
// but if the server's value disagrees with what's stored and the mailbox
|
|
356
|
+
// was still `normal`, the axis just changed. Trip the cursor here so the
|
|
357
|
+
// message-sync/flag-push/move/body-fetch paths pause outbound IMAP until
|
|
358
|
+
// the rebuild resolves it, instead of silently overwriting the old value
|
|
359
|
+
// and erasing the only evidence a bump happened.
|
|
360
|
+
const uidValidityChanged = existing.uidValidity !== status.uidValidity;
|
|
361
|
+
const cursorTrip = uidValidityChanged && !isCursorRebuildNeeded(existing.cursorState)
|
|
362
|
+
? { cursorState: MailboxCursorState.cursor_invalid }
|
|
363
|
+
: {};
|
|
364
|
+
// Check if anything actually changed
|
|
365
|
+
const hasChanges = existing.uidNext !== status.uidNext ||
|
|
366
|
+
existing.uidValidity !== status.uidValidity ||
|
|
367
|
+
existing.messageCount !== status.messages ||
|
|
368
|
+
existing.unseenCount !== status.unseen ||
|
|
369
|
+
existing.deletedCount !== status.deletedCount ||
|
|
370
|
+
specialUseChanged;
|
|
371
|
+
// Sync special-use attributes (handles migration of existing mailboxes)
|
|
372
|
+
await this.syncSpecialUseAttributes(existing.mailboxId, mailboxInfo);
|
|
373
|
+
if (!hasChanges) {
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
// Debug: log what changed
|
|
377
|
+
const changes = [];
|
|
378
|
+
if (existing.uidNext !== status.uidNext)
|
|
379
|
+
changes.push(`uidNext: ${existing.uidNext} -> ${status.uidNext}`);
|
|
380
|
+
if (existing.uidValidity !== status.uidValidity)
|
|
381
|
+
changes.push(`uidValidity: ${existing.uidValidity} -> ${status.uidValidity}`);
|
|
382
|
+
if (existing.messageCount !== status.messages)
|
|
383
|
+
changes.push(`messageCount: ${existing.messageCount} -> ${status.messages}`);
|
|
384
|
+
if (existing.unseenCount !== status.unseen)
|
|
385
|
+
changes.push(`unseenCount: ${existing.unseenCount} -> ${status.unseen}`);
|
|
386
|
+
if (existing.deletedCount !== status.deletedCount)
|
|
387
|
+
changes.push(`deletedCount: ${existing.deletedCount} -> ${status.deletedCount}`);
|
|
388
|
+
if (specialUseChanged)
|
|
389
|
+
changes.push(`specialUse: [${(existing.specialUse ?? []).join(",")}] -> [${parsed.specialUse.join(",")}]`);
|
|
390
|
+
this.log.info({
|
|
391
|
+
mailboxId: existing.mailboxId,
|
|
392
|
+
fullPath: mailboxInfo.fullPath,
|
|
393
|
+
changes,
|
|
394
|
+
}, "Updating mailbox");
|
|
395
|
+
// Update mailbox with fresh status. ElectroDB rejects empty sets, so we
|
|
396
|
+
// pass undefined when no flags are present rather than [].
|
|
397
|
+
return this.mailboxService.update(accountId, existing.mailboxId, {
|
|
398
|
+
hierarchyDelimiter: mailboxInfo.delimiter,
|
|
399
|
+
uidValidity: status.uidValidity,
|
|
400
|
+
uidNext: status.uidNext,
|
|
401
|
+
// `highestModseq` is deliberately absent: it is message sync's
|
|
402
|
+
// cursor over this mailbox's own applied changes, and overwriting it
|
|
403
|
+
// with the server's current value here would step it over every
|
|
404
|
+
// change message sync had not yet applied.
|
|
405
|
+
messageCount: status.messages,
|
|
406
|
+
unseenCount: status.unseen,
|
|
407
|
+
deletedCount: status.deletedCount,
|
|
408
|
+
specialUse: parsed.specialUse.length > 0 ? parsed.specialUse : undefined,
|
|
409
|
+
...cursorTrip,
|
|
410
|
+
});
|
|
411
|
+
};
|
|
412
|
+
/**
|
|
413
|
+
* Sync special-use attributes for a mailbox.
|
|
414
|
+
* Creates entries if they don't exist, updates if changed.
|
|
415
|
+
*/
|
|
416
|
+
syncSpecialUseAttributes = async (mailboxId, mailboxInfo) => {
|
|
417
|
+
const parsed = parseImapAttributes(mailboxInfo.attributes);
|
|
418
|
+
const existingEntries = await this.specialUseService.listByMailboxId(mailboxId);
|
|
419
|
+
const existingSpecialUses = new Set(existingEntries.map((e) => e.specialUse));
|
|
420
|
+
const newSpecialUses = new Set(parsed.specialUse);
|
|
421
|
+
// Check if sets are equal
|
|
422
|
+
const areEqual = existingSpecialUses.size === newSpecialUses.size &&
|
|
423
|
+
[...existingSpecialUses].every((use) => newSpecialUses.has(use));
|
|
424
|
+
if (areEqual)
|
|
425
|
+
return;
|
|
426
|
+
// Delete and recreate (simpler than diff)
|
|
427
|
+
if (existingEntries.length > 0) {
|
|
428
|
+
await this.specialUseService.deleteByMailboxId(mailboxId);
|
|
429
|
+
}
|
|
430
|
+
if (parsed.specialUse.length > 0) {
|
|
431
|
+
await this.specialUseService.createMany(mailboxId, parsed.specialUse);
|
|
432
|
+
this.log.debug({ fullPath: mailboxInfo.fullPath, specialUse: parsed.specialUse }, "Synced special-use");
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
/**
|
|
436
|
+
* Build a map of special-use designations to mailbox paths.
|
|
437
|
+
* Only includes mailboxes that have the IMAP special-use attribute.
|
|
438
|
+
*/
|
|
439
|
+
buildSpecialUseMap = (mailboxes) => {
|
|
440
|
+
const map = new Map();
|
|
441
|
+
for (const mailbox of mailboxes) {
|
|
442
|
+
const parsed = parseImapAttributes(mailbox.attributes);
|
|
443
|
+
for (const specialUse of parsed.specialUse) {
|
|
444
|
+
// First mailbox with this special-use wins (usually the canonical one)
|
|
445
|
+
if (!map.has(specialUse)) {
|
|
446
|
+
map.set(specialUse, mailbox.fullPath);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return map;
|
|
451
|
+
};
|
|
452
|
+
/**
|
|
453
|
+
* Check if a mailbox is a duplicate special-use folder.
|
|
454
|
+
* A folder is considered duplicate if:
|
|
455
|
+
* 1. Its name matches a common special-use folder name (e.g., "Trash")
|
|
456
|
+
* 2. It does NOT have the IMAP special-use attribute
|
|
457
|
+
* 3. Another folder already claimed that special-use designation
|
|
458
|
+
*/
|
|
459
|
+
isDuplicateSpecialUse = (mailbox, claimedSpecialUse) => {
|
|
460
|
+
// Get the folder name (last segment of path)
|
|
461
|
+
const folderName = mailbox.fullPath.split(mailbox.delimiter).pop() ?? "";
|
|
462
|
+
const normalizedName = folderName.toLowerCase();
|
|
463
|
+
// Check if this folder name is a conventional name for a role
|
|
464
|
+
const role = roleForFolderName(normalizedName);
|
|
465
|
+
if (!role) {
|
|
466
|
+
return false; // Not a special-use folder name
|
|
467
|
+
}
|
|
468
|
+
const expectedSpecialUse = ROLE_SPECIAL_USE[role];
|
|
469
|
+
if (!expectedSpecialUse) {
|
|
470
|
+
return false; // The role has no SPECIAL-USE flag to be a duplicate of
|
|
471
|
+
}
|
|
472
|
+
// Check if this mailbox has the special-use attribute
|
|
473
|
+
const parsed = parseImapAttributes(mailbox.attributes);
|
|
474
|
+
if (parsed.specialUse.some((use) => use === expectedSpecialUse)) {
|
|
475
|
+
return false; // This IS the canonical folder
|
|
476
|
+
}
|
|
477
|
+
// Check if another folder already claimed this special-use
|
|
478
|
+
const claimed = [...claimedSpecialUse].find(([specialUse]) => specialUse === expectedSpecialUse);
|
|
479
|
+
if (!claimed) {
|
|
480
|
+
return false; // No other folder has this special-use
|
|
481
|
+
}
|
|
482
|
+
// This is a duplicate - another folder has the attribute
|
|
483
|
+
this.log.debug({
|
|
484
|
+
fullPath: mailbox.fullPath,
|
|
485
|
+
claimedPath: claimed[1],
|
|
486
|
+
specialUse: expectedSpecialUse,
|
|
487
|
+
}, "Skipping duplicate folder");
|
|
488
|
+
return true;
|
|
489
|
+
};
|
|
490
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** The folder is not on the server: a delete has nothing left to do. */
|
|
2
|
+
export declare const isMailboxAbsentUpstream: (error: unknown) => boolean;
|
|
3
|
+
/** The folder is already on the server: a create has nothing left to do. */
|
|
4
|
+
export declare const isMailboxPresentUpstream: (error: unknown) => boolean;
|
|
5
|
+
export declare const isMailboxRefusedUpstream: (error: unknown) => boolean;
|
|
6
|
+
export declare const upstreamFailureReason: (error: unknown) => string;
|
|
7
|
+
/**
|
|
8
|
+
* Something after the server RENAME failed — the local settle, not the
|
|
9
|
+
* mutation. The rename landed, so this is never a refused rename: marking the
|
|
10
|
+
* rows `failed` would offer a retry of work the server has already done, and
|
|
11
|
+
* `fullPath` would go on naming a path the server no longer holds.
|
|
12
|
+
*
|
|
13
|
+
* It is rethrown rather than swallowed. The rows are still `pending` with their
|
|
14
|
+
* targets recorded, so the redelivery's guard passes and the settle runs again;
|
|
15
|
+
* swallowing it would ack the message and leave the subtree `pending` forever,
|
|
16
|
+
* skipped by message sync with no route out.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* The folder a rename was to move is confirmed gone: the server refused the
|
|
20
|
+
* RENAME as non-existent, and its own listing holds neither the path being left
|
|
21
|
+
* nor the path being aimed at.
|
|
22
|
+
*
|
|
23
|
+
* It is a distinct kind because it is the only answer that may remove a folder
|
|
24
|
+
* and its mail. Re-reading the server's response code at the call site would
|
|
25
|
+
* make every unclassifiable `NONEXISTENT` — a listing that could not be read, a
|
|
26
|
+
* path the comparison normalized wrongly — destroy the user's mail; here the
|
|
27
|
+
* decision is made once, where the evidence is.
|
|
28
|
+
*/
|
|
29
|
+
export declare class FolderGoneUpstreamError extends Error {
|
|
30
|
+
name: string;
|
|
31
|
+
readonly cause: unknown;
|
|
32
|
+
constructor(mailboxId: string, oldPath: string, cause: unknown);
|
|
33
|
+
}
|
|
34
|
+
export declare class FolderRenameSettleError extends Error {
|
|
35
|
+
name: string;
|
|
36
|
+
readonly cause: unknown;
|
|
37
|
+
constructor(mailboxId: string, confirmedPath: string, cause: unknown);
|
|
38
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
const stringField = (value
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
const stringField = (value, key) => {
|
|
2
|
+
if (!(value instanceof Object))
|
|
3
|
+
return "";
|
|
4
|
+
const field = Reflect.get(value, key);
|
|
5
|
+
return typeof field === "string" ? field : "";
|
|
5
6
|
};
|
|
6
|
-
|
|
7
7
|
/**
|
|
8
8
|
* Read a tagged-NO outcome out of an IMAP failure.
|
|
9
9
|
*
|
|
@@ -19,35 +19,32 @@ const stringField = (value: unknown, key: string): string => {
|
|
|
19
19
|
* with the code on the error, which is why matching the message alone never
|
|
20
20
|
* caught a real Dovecot answer.
|
|
21
21
|
*/
|
|
22
|
-
const saidByServer = (error
|
|
23
|
-
`${error.message} ${stringField(error, "responseText")}`;
|
|
24
|
-
|
|
22
|
+
const saidByServer = (error) => `${error.message} ${stringField(error, "responseText")}`;
|
|
25
23
|
/** The folder is not on the server: a delete has nothing left to do. */
|
|
26
|
-
export const isMailboxAbsentUpstream = (error
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
export const isMailboxAbsentUpstream = (error) => {
|
|
25
|
+
if (!(error instanceof Error))
|
|
26
|
+
return false;
|
|
27
|
+
if (stringField(error, "serverResponseCode") === "NONEXISTENT")
|
|
28
|
+
return true;
|
|
29
|
+
return /not found|does ?n.?t exist/i.test(saidByServer(error));
|
|
30
30
|
};
|
|
31
|
-
|
|
32
31
|
/** The folder is already on the server: a create has nothing left to do. */
|
|
33
|
-
export const isMailboxPresentUpstream = (error
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
export const isMailboxPresentUpstream = (error) => {
|
|
33
|
+
if (!(error instanceof Error))
|
|
34
|
+
return false;
|
|
35
|
+
if (stringField(error, "serverResponseCode") === "ALREADYEXISTS")
|
|
36
|
+
return true;
|
|
37
|
+
return /already exists/i.test(saidByServer(error));
|
|
37
38
|
};
|
|
38
|
-
|
|
39
39
|
const PERMANENT_REFUSALS = new Set(["NOPERM", "CANNOT"]);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (serverText.length > 0) return serverText;
|
|
48
|
-
return error instanceof Error ? error.message : String(error);
|
|
40
|
+
export const isMailboxRefusedUpstream = (error) => error instanceof Error &&
|
|
41
|
+
PERMANENT_REFUSALS.has(stringField(error, "serverResponseCode"));
|
|
42
|
+
export const upstreamFailureReason = (error) => {
|
|
43
|
+
const serverText = stringField(error, "responseText").trim();
|
|
44
|
+
if (serverText.length > 0)
|
|
45
|
+
return serverText;
|
|
46
|
+
return error instanceof Error ? error.message : String(error);
|
|
49
47
|
};
|
|
50
|
-
|
|
51
48
|
/**
|
|
52
49
|
* Something after the server RENAME failed — the local settle, not the
|
|
53
50
|
* mutation. The rename landed, so this is never a refused rename: marking the
|
|
@@ -71,25 +68,18 @@ export const upstreamFailureReason = (error: unknown): string => {
|
|
|
71
68
|
* decision is made once, where the evidence is.
|
|
72
69
|
*/
|
|
73
70
|
export class FolderGoneUpstreamError extends Error {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
);
|
|
81
|
-
this.cause = cause;
|
|
82
|
-
}
|
|
71
|
+
name = "FolderGoneUpstreamError";
|
|
72
|
+
cause;
|
|
73
|
+
constructor(mailboxId, oldPath, cause) {
|
|
74
|
+
super(`Folder ${mailboxId} is gone from the mail server: neither "${oldPath}" nor its rename target is listed`);
|
|
75
|
+
this.cause = cause;
|
|
76
|
+
}
|
|
83
77
|
}
|
|
84
|
-
|
|
85
78
|
export class FolderRenameSettleError extends Error {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
);
|
|
93
|
-
this.cause = cause;
|
|
94
|
-
}
|
|
79
|
+
name = "FolderRenameSettleError";
|
|
80
|
+
cause;
|
|
81
|
+
constructor(mailboxId, confirmedPath, cause) {
|
|
82
|
+
super(`Folder ${mailboxId} was renamed to "${confirmedPath}" but the local settle did not finish`);
|
|
83
|
+
this.cause = cause;
|
|
84
|
+
}
|
|
95
85
|
}
|