@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.
Files changed (270) hide show
  1. package/dist/account-credentials.d.ts +83 -0
  2. package/dist/account-credentials.js +104 -0
  3. package/dist/attribute-mapper.d.ts +36 -0
  4. package/dist/attribute-mapper.js +119 -0
  5. package/dist/body-parse.d.ts +28 -0
  6. package/{src/body-parse.ts → dist/body-parse.js} +18 -26
  7. package/dist/body-part-mapper.d.ts +92 -0
  8. package/dist/body-part-mapper.js +301 -0
  9. package/dist/body-sync-queue.d.ts +59 -0
  10. package/dist/body-sync-queue.js +56 -0
  11. package/dist/body-sync-terminal.d.ts +58 -0
  12. package/dist/body-sync-terminal.js +95 -0
  13. package/dist/body-sync.d.ts +351 -0
  14. package/dist/body-sync.js +1204 -0
  15. package/dist/calendar-parts.d.ts +23 -0
  16. package/dist/calendar-parts.js +18 -0
  17. package/dist/classification-backfill.d.ts +91 -0
  18. package/dist/classification-backfill.js +180 -0
  19. package/dist/classify-message.d.ts +31 -0
  20. package/dist/classify-message.js +89 -0
  21. package/dist/connection-factory.d.ts +54 -0
  22. package/dist/connection-factory.js +54 -0
  23. package/dist/connection-test.d.ts +41 -0
  24. package/dist/connection-test.js +152 -0
  25. package/dist/external-move.d.ts +94 -0
  26. package/dist/external-move.js +173 -0
  27. package/dist/filter-operations.d.ts +42 -0
  28. package/dist/filter-operations.js +211 -0
  29. package/dist/filters/filter-config.d.ts +14 -0
  30. package/dist/filters/filter-config.js +10 -0
  31. package/{src/filters/list-id.ts → dist/filters/list-id.d.ts} +2 -16
  32. package/dist/filters/list-id.js +26 -0
  33. package/dist/filters/match.d.ts +69 -0
  34. package/dist/filters/match.js +165 -0
  35. package/dist/filters/pipeline.d.ts +64 -0
  36. package/dist/filters/pipeline.js +100 -0
  37. package/{src/flag-push-terminal.ts → dist/flag-push-terminal.d.ts} +16 -72
  38. package/dist/flag-push-terminal.js +63 -0
  39. package/dist/flag-push.d.ts +69 -0
  40. package/dist/flag-push.js +111 -0
  41. package/dist/flag-queue.d.ts +175 -0
  42. package/dist/flag-queue.js +291 -0
  43. package/dist/folder-operations.d.ts +39 -0
  44. package/dist/folder-operations.js +112 -0
  45. package/dist/folder-placement.d.ts +13 -0
  46. package/dist/folder-placement.js +23 -0
  47. package/dist/heuristics/authenticationResults.d.ts +10 -0
  48. package/dist/heuristics/authenticationResults.js +187 -0
  49. package/dist/heuristics/classifyByHeaders.d.ts +82 -0
  50. package/dist/heuristics/classifyByHeaders.js +365 -0
  51. package/dist/heuristics/classifyPlacement.d.ts +67 -0
  52. package/dist/heuristics/classifyPlacement.js +153 -0
  53. package/dist/heuristics/machineSenders.d.ts +21 -0
  54. package/{src/heuristics/machineSenders.ts → dist/heuristics/machineSenders.js} +34 -40
  55. package/dist/heuristics/senderMismatch.d.ts +81 -0
  56. package/dist/heuristics/senderMismatch.js +192 -0
  57. package/dist/heuristics/socialDomains.d.ts +14 -0
  58. package/{src/heuristics/socialDomains.ts → dist/heuristics/socialDomains.js} +9 -9
  59. package/dist/heuristics/transactionalDomains.d.ts +18 -0
  60. package/{src/heuristics/transactionalDomains.ts → dist/heuristics/transactionalDomains.js} +8 -8
  61. package/dist/imapflow-connection.d.ts +377 -0
  62. package/dist/imapflow-connection.js +1147 -0
  63. package/dist/index.d.ts +63 -0
  64. package/dist/index.js +65 -0
  65. package/dist/list-id-backfill.d.ts +83 -0
  66. package/dist/list-id-backfill.js +132 -0
  67. package/dist/mailbox-cursor-rebuild.d.ts +74 -0
  68. package/dist/mailbox-cursor-rebuild.js +71 -0
  69. package/{src/mailbox-cursor.ts → dist/mailbox-cursor.d.ts} +20 -98
  70. package/dist/mailbox-cursor.js +102 -0
  71. package/dist/mailbox-intent.d.ts +41 -0
  72. package/{src/mailbox-intent.ts → dist/mailbox-intent.js} +38 -64
  73. package/dist/mailbox-management.d.ts +240 -0
  74. package/dist/mailbox-management.js +576 -0
  75. package/dist/mailbox-presence.d.ts +52 -0
  76. package/{src/mailbox-presence.ts → dist/mailbox-presence.js} +19 -30
  77. package/dist/mailbox-queue.d.ts +129 -0
  78. package/dist/mailbox-queue.js +216 -0
  79. package/dist/mailbox-sync.d.ts +93 -0
  80. package/dist/mailbox-sync.js +490 -0
  81. package/dist/mailbox-upstream.d.ts +38 -0
  82. package/{src/mailbox-upstream.ts → dist/mailbox-upstream.js} +37 -47
  83. package/dist/message-category.d.ts +51 -0
  84. package/{src/message-category.ts → dist/message-category.js} +28 -71
  85. package/dist/message-move.d.ts +285 -0
  86. package/dist/message-move.js +858 -0
  87. package/dist/message-parser.d.ts +14 -0
  88. package/dist/message-parser.js +17 -0
  89. package/{src/message-presence.ts → dist/message-presence.d.ts} +1 -13
  90. package/dist/message-presence.js +29 -0
  91. package/dist/message-read.d.ts +51 -0
  92. package/dist/message-read.js +31 -0
  93. package/dist/message-sync.d.ts +345 -0
  94. package/dist/message-sync.js +1358 -0
  95. package/dist/mime-walker.d.ts +102 -0
  96. package/dist/mime-walker.js +198 -0
  97. package/dist/organize-job.d.ts +20 -0
  98. package/dist/organize-job.js +37 -0
  99. package/dist/outbox-attachment-content.d.ts +37 -0
  100. package/dist/outbox-attachment-content.js +50 -0
  101. package/dist/outbox-attachment-filename.d.ts +19 -0
  102. package/dist/outbox-attachment-filename.js +64 -0
  103. package/dist/outbox-attachment.d.ts +148 -0
  104. package/dist/outbox-attachment.js +273 -0
  105. package/dist/outbox-queue.d.ts +73 -0
  106. package/dist/outbox-queue.js +280 -0
  107. package/{src/outbox-status.ts → dist/outbox-status.d.ts} +1 -6
  108. package/dist/outbox-status.js +18 -0
  109. package/dist/ownership.d.ts +24 -0
  110. package/dist/ownership.js +83 -0
  111. package/dist/placement-move-terminal.d.ts +67 -0
  112. package/dist/placement-move-terminal.js +88 -0
  113. package/dist/placement-move.d.ts +83 -0
  114. package/dist/placement-move.js +216 -0
  115. package/{src/placement-settled.ts → dist/placement-settled.d.ts} +13 -68
  116. package/dist/placement-settled.js +75 -0
  117. package/dist/quarantine.d.ts +95 -0
  118. package/dist/quarantine.js +131 -0
  119. package/dist/refusal.d.ts +31 -0
  120. package/dist/refusal.js +3 -0
  121. package/{src/restore-source-placement.ts → dist/restore-source-placement.d.ts} +29 -97
  122. package/dist/restore-source-placement.js +79 -0
  123. package/dist/search-text-backfill.d.ts +15 -0
  124. package/dist/search-text-backfill.js +33 -0
  125. package/dist/snippet.d.ts +61 -0
  126. package/dist/snippet.js +129 -0
  127. package/dist/spam-report.d.ts +86 -0
  128. package/dist/spam-report.js +241 -0
  129. package/dist/sync-warning.d.ts +21 -0
  130. package/dist/sync-warning.js +26 -0
  131. package/dist/sync-watermarks.d.ts +138 -0
  132. package/dist/sync-watermarks.js +205 -0
  133. package/dist/test-helpers/fake-imap.d.ts +151 -0
  134. package/dist/test-helpers/fake-imap.js +508 -0
  135. package/dist/test-helpers/folder-roles.d.ts +34 -0
  136. package/dist/test-helpers/folder-roles.js +47 -0
  137. package/dist/test-helpers/mime-tree-generator.d.ts +82 -0
  138. package/dist/test-helpers/mime-tree-generator.js +329 -0
  139. package/dist/test-helpers/test-store.d.ts +69 -0
  140. package/dist/test-helpers/test-store.js +193 -0
  141. package/dist/text/normalizer.d.ts +20 -0
  142. package/dist/text/normalizer.js +102 -0
  143. package/dist/thread-message-rows.d.ts +69 -0
  144. package/dist/thread-message-rows.js +40 -0
  145. package/dist/thread-root.d.ts +7 -0
  146. package/dist/thread-root.js +25 -0
  147. package/dist/types.d.ts +343 -0
  148. package/dist/types.js +19 -0
  149. package/package.json +86 -88
  150. package/src/account-credentials.ts +0 -210
  151. package/src/attribute-mapper.test.ts +0 -104
  152. package/src/attribute-mapper.ts +0 -148
  153. package/src/body-parse.test.ts +0 -55
  154. package/src/body-part-mapper.property.test.ts +0 -354
  155. package/src/body-part-mapper.ts +0 -410
  156. package/src/body-sync-queue.test.ts +0 -64
  157. package/src/body-sync-queue.ts +0 -131
  158. package/src/body-sync-terminal.test.ts +0 -148
  159. package/src/body-sync-terminal.ts +0 -174
  160. package/src/body-sync.test.ts +0 -925
  161. package/src/body-sync.ts +0 -1891
  162. package/src/calendar-parts.ts +0 -41
  163. package/src/classification-backfill.test.ts +0 -285
  164. package/src/classification-backfill.ts +0 -363
  165. package/src/classify-message.test.ts +0 -142
  166. package/src/classify-message.ts +0 -116
  167. package/src/connection-factory.ts +0 -102
  168. package/src/connection-test.ts +0 -208
  169. package/src/credentials.test.ts +0 -181
  170. package/src/external-move.test.ts +0 -262
  171. package/src/external-move.ts +0 -285
  172. package/src/filter-operations.test.ts +0 -319
  173. package/src/filter-operations.ts +0 -377
  174. package/src/filter-update.test.ts +0 -313
  175. package/src/filters/filter-config.test.ts +0 -87
  176. package/src/filters/filter-config.ts +0 -34
  177. package/src/filters/list-id.test.ts +0 -50
  178. package/src/filters/match.test.ts +0 -392
  179. package/src/filters/match.ts +0 -225
  180. package/src/filters/pipeline.test.ts +0 -199
  181. package/src/filters/pipeline.ts +0 -170
  182. package/src/flag-push.test.ts +0 -112
  183. package/src/flag-push.ts +0 -198
  184. package/src/flag-queue.test.ts +0 -197
  185. package/src/flag-queue.ts +0 -509
  186. package/src/folder-operations.test.ts +0 -273
  187. package/src/folder-operations.ts +0 -217
  188. package/src/folder-placement.ts +0 -56
  189. package/src/forwarded-message.test.ts +0 -122
  190. package/src/heuristics/authenticationResults.test.ts +0 -189
  191. package/src/heuristics/authenticationResults.ts +0 -212
  192. package/src/heuristics/classifyByHeaders.test.ts +0 -889
  193. package/src/heuristics/classifyByHeaders.ts +0 -467
  194. package/src/heuristics/classifyPlacement.test.ts +0 -366
  195. package/src/heuristics/classifyPlacement.ts +0 -211
  196. package/src/heuristics/classifyRealMail.test.ts +0 -242
  197. package/src/heuristics/machineSenders.test.ts +0 -101
  198. package/src/heuristics/senderMismatch.test.ts +0 -346
  199. package/src/heuristics/senderMismatch.ts +0 -259
  200. package/src/imapflow-connect-retry.test.ts +0 -97
  201. package/src/imapflow-connection.test.ts +0 -503
  202. package/src/imapflow-connection.ts +0 -1476
  203. package/src/index.ts +0 -397
  204. package/src/list-id-backfill.test.ts +0 -207
  205. package/src/list-id-backfill.ts +0 -274
  206. package/src/mailbox-cursor-rebuild.test.ts +0 -171
  207. package/src/mailbox-cursor-rebuild.ts +0 -146
  208. package/src/mailbox-cursor.test.ts +0 -141
  209. package/src/mailbox-delete-intent.test.ts +0 -119
  210. package/src/mailbox-management.test.ts +0 -243
  211. package/src/mailbox-management.ts +0 -838
  212. package/src/mailbox-presence.test.ts +0 -74
  213. package/src/mailbox-queue.ts +0 -397
  214. package/src/mailbox-rename-intent.test.ts +0 -220
  215. package/src/mailbox-rename-subtree.test.ts +0 -287
  216. package/src/mailbox-sync.test.ts +0 -286
  217. package/src/mailbox-sync.ts +0 -724
  218. package/src/message-move.test.ts +0 -726
  219. package/src/message-move.ts +0 -1518
  220. package/src/message-parser.ts +0 -26
  221. package/src/message-presence.test.ts +0 -50
  222. package/src/message-read.ts +0 -87
  223. package/src/message-save-retry.test.ts +0 -90
  224. package/src/message-sync-flags.test.ts +0 -167
  225. package/src/message-sync.test.ts +0 -762
  226. package/src/message-sync.ts +0 -2133
  227. package/src/mime-walker.test.ts +0 -311
  228. package/src/mime-walker.ts +0 -306
  229. package/src/organize-job.ts +0 -93
  230. package/src/outbox-attachment-content.test.ts +0 -153
  231. package/src/outbox-attachment-content.ts +0 -104
  232. package/src/outbox-attachment-filename.test.ts +0 -132
  233. package/src/outbox-attachment-filename.ts +0 -76
  234. package/src/outbox-attachment.test.ts +0 -626
  235. package/src/outbox-attachment.ts +0 -536
  236. package/src/outbox-failed-recovery.test.ts +0 -299
  237. package/src/outbox-queue.test.ts +0 -315
  238. package/src/outbox-queue.ts +0 -525
  239. package/src/ownership.ts +0 -140
  240. package/src/placement-move-terminal.ts +0 -145
  241. package/src/placement-move.ts +0 -363
  242. package/src/placement-settled.test.ts +0 -147
  243. package/src/placement.test.ts +0 -404
  244. package/src/quarantine.test.ts +0 -133
  245. package/src/quarantine.ts +0 -229
  246. package/src/refusal.ts +0 -29
  247. package/src/snippet.test.ts +0 -213
  248. package/src/snippet.ts +0 -152
  249. package/src/spam-report.test.ts +0 -329
  250. package/src/spam-report.ts +0 -357
  251. package/src/sync-warning.test.ts +0 -213
  252. package/src/sync-warning.ts +0 -56
  253. package/src/sync-watermarks.test.ts +0 -409
  254. package/src/sync-watermarks.ts +0 -319
  255. package/src/system-flag-wire-format.test.ts +0 -77
  256. package/src/test-helpers/fake-imap.ts +0 -690
  257. package/src/test-helpers/folder-roles.ts +0 -66
  258. package/src/test-helpers/mime-tree-generator.ts +0 -461
  259. package/src/test-helpers/test-queue.ts +0 -56
  260. package/src/test-helpers/test-store.ts +0 -299
  261. package/src/text/normalizer.test.ts +0 -223
  262. package/src/text/normalizer.ts +0 -140
  263. package/src/thread-message-rows.ts +0 -77
  264. package/src/thread-root.test.ts +0 -29
  265. package/src/thread-root.ts +0 -51
  266. package/src/types/mailparser-augment.d.ts +0 -23
  267. package/src/types/natural-porter-stemmer.d.ts +0 -50
  268. package/src/types/stopword.d.ts +0 -69
  269. package/src/types.ts +0 -371
  270. package/tsconfig.json +0 -8
@@ -1,724 +0,0 @@
1
- /**
2
- * Mailbox synchronization service
3
- *
4
- * Orchestrates syncing mailbox data from IMAP server to the data backend.
5
- */
6
-
7
- import type {
8
- CreateMailboxInput,
9
- IMailboxRepository,
10
- IMailboxSpecialUseRepository,
11
- MailboxItem,
12
- } from "@remit/data-ports";
13
- import {
14
- ROLE_SPECIAL_USE,
15
- roleForFolderName,
16
- } from "@remit/data-ports/folder-role";
17
- import {
18
- MailboxCursorState,
19
- type MailboxSpecialUse,
20
- MailboxSyncStatus,
21
- NamespaceType,
22
- } from "@remit/domain-enums";
23
- import pMap from "p-map";
24
- import { isNoSelect, parseImapAttributes } from "./attribute-mapper.js";
25
- import { isCursorRebuildNeeded } from "./mailbox-cursor.js";
26
- import {
27
- isFolderMutationInFlight,
28
- isMailboxMutationInFlight,
29
- } from "./mailbox-presence.js";
30
- import type {
31
- FlatMailboxInfo,
32
- IImapConnection,
33
- ImapNamespaces,
34
- MailboxSyncResult,
35
- } from "./types.js";
36
-
37
- // Type for namespace type values
38
- type NamespaceTypeValue = (typeof NamespaceType)[keyof typeof NamespaceType];
39
- type MailboxSpecialUseValue =
40
- (typeof MailboxSpecialUse)[keyof typeof MailboxSpecialUse];
41
-
42
- /**
43
- * Compare two unordered special-use lists/sets for equality. Treats `undefined`
44
- * and an empty array as equivalent (a mailbox with no flags).
45
- */
46
- const areSpecialUseSetsEqual = (
47
- a: readonly MailboxSpecialUseValue[] | undefined,
48
- b: readonly MailboxSpecialUseValue[] | undefined,
49
- ): boolean => {
50
- const aArr = a ?? [];
51
- const bArr = b ?? [];
52
- if (aArr.length !== bArr.length) return false;
53
- const aSet = new Set<string>(aArr);
54
- for (const value of bArr) {
55
- if (!aSet.has(value)) return false;
56
- }
57
- return true;
58
- };
59
-
60
- /**
61
- * Account info needed for mailbox sync
62
- */
63
- export interface SyncAccountInfo {
64
- accountId: string;
65
- }
66
-
67
- /**
68
- * Logger interface for MailboxSyncService.
69
- *
70
- * `info` is reserved here for the three mailbox lifecycle events — created,
71
- * updated, deleted — and is the argued exception to `plugins/no-logger-info.grit`
72
- * that the rest of this repo writes as a suppression. (The plugin matches the
73
- * bare `logger` identifier, so it does not see an injected logger; the reasoning
74
- * is written out instead of being silently absent.) A folder disappearing from
75
- * someone's mail is destructive and irreversible from this service's side, and
76
- * the only record that this process did it is the line it writes. That must not
77
- * sit below the default threshold.
78
- *
79
- * Everything else this service has to say — a special-use flag it synced, a
80
- * duplicate folder it skipped — is a routine trace and goes to `debug`.
81
- */
82
- export interface MailboxSyncLogger {
83
- info(obj: Record<string, unknown>, msg: string): void;
84
- debug(obj: Record<string, unknown>, msg: string): void;
85
- error(obj: Record<string, unknown>, msg: string): void;
86
- }
87
-
88
- /**
89
- * Service for synchronizing mailbox metadata between IMAP and the data backend.
90
- */
91
- export class MailboxSyncService {
92
- private mailboxService: IMailboxRepository;
93
- private specialUseService: IMailboxSpecialUseRepository;
94
- private log: MailboxSyncLogger;
95
-
96
- // Required, with no no-op default: the lines below are the only record that a
97
- // mailbox was created or destroyed, and a default would let the next call site
98
- // lose them with no error and no failing test.
99
- constructor(
100
- mailboxService: IMailboxRepository,
101
- specialUseService: IMailboxSpecialUseRepository,
102
- logger: MailboxSyncLogger,
103
- ) {
104
- this.mailboxService = mailboxService;
105
- this.specialUseService = specialUseService;
106
- this.log = logger;
107
- }
108
-
109
- /**
110
- * Sync all mailboxes for an account from IMAP server
111
- *
112
- * @param account - Account info including accountId
113
- * @param connection - Active IMAP connection
114
- */
115
- syncMailboxes = async (
116
- account: SyncAccountInfo,
117
- connection: IImapConnection,
118
- ): Promise<MailboxSyncResult> => {
119
- const result: MailboxSyncResult = {
120
- created: 0,
121
- updated: 0,
122
- deleted: 0,
123
- };
124
-
125
- // Get existing mailboxes from database
126
- const existingMailboxes = await this.getAllMailboxes(account.accountId);
127
- const existingByPath = new Map(
128
- existingMailboxes.map((m) => [m.fullPath, m]),
129
- );
130
- // The paths a rename is on its way to (D14). Under D2 `fullPath` stays the
131
- // path the server held when the intent was recorded, so between the server
132
- // executing RENAME and the settle, LIST returns a path no row is keyed to
133
- // — and the insert branch below has no state guard at all. Without this it
134
- // inserts a second row under a fresh mailboxId and initial-syncs the
135
- // folder's mail into it, leaving the original as a permanent phantom still
136
- // holding that folder's messages, filters and role appointments. No unique
137
- // index on (accountId, fullPath) exists to catch it; #386 adds one.
138
- //
139
- // `pending` only. A failed rename keeps its target so the UI can name what
140
- // it was aiming at (T6), and nothing clears it until the user retries or
141
- // dismisses — so honouring a `failed` row's claim would let one stuck
142
- // folder block the sweep from ever discovering a real folder another
143
- // client later creates at that path.
144
- const claimedByPendingRename = new Set(
145
- existingMailboxes.flatMap((m) =>
146
- m.syncStatus === MailboxSyncStatus.pending &&
147
- m.pendingPath !== undefined
148
- ? [m.pendingPath]
149
- : [],
150
- ),
151
- );
152
-
153
- // Get namespaces and mailboxes from IMAP
154
- const namespaces = await connection.getNamespaces();
155
- const remoteMailboxes = await this.fetchAllMailboxes(
156
- connection,
157
- namespaces,
158
- );
159
-
160
- // Build a map of special-use designations claimed by mailboxes with IMAP attributes.
161
- // Used to:
162
- // 1. Skip duplicate folders (e.g., "Trash" vs "[Gmail]/Trash") at sync time.
163
- // 2. Skip duplicate localized folders (e.g., "Sent" vs "Verzonden items") when one
164
- // of them carries the IMAP \Sent flag — issue #194.
165
- const claimedSpecialUse = this.buildSpecialUseMap(remoteMailboxes);
166
-
167
- // Track which paths we've seen from remote
168
- const seenPaths = new Set<string>();
169
-
170
- // Process each remote mailbox (concurrency 3 for IMAP pipelining)
171
- await pMap(
172
- remoteMailboxes,
173
- async (mailboxInfo) => {
174
- // Skip non-selectable mailboxes (container folders that can't hold messages)
175
- if (isNoSelect(mailboxInfo.attributes)) {
176
- // Mark as seen so we don't try to delete again in cleanup loop
177
- seenPaths.add(mailboxInfo.fullPath);
178
- // If this mailbox exists in DB, delete it
179
- const existing = existingByPath.get(mailboxInfo.fullPath);
180
- // A folder mid-create can transiently LIST as `\Noselect`, and
181
- // deleting the row then races the create exactly as #290
182
- // documented. The state check goes before the delete, through the
183
- // same predicate the rest of the sweep asks.
184
- if (existing && !isFolderMutationInFlight(existing)) {
185
- // The duplicate-special-use branch below clears the special-use
186
- // entries too; this one skipped them, orphaning a row per
187
- // deleted mailbox.
188
- await this.specialUseService.deleteByMailboxId(existing.mailboxId);
189
- await this.mailboxService.delete(
190
- account.accountId,
191
- existing.mailboxId,
192
- );
193
- this.log.info(
194
- {
195
- mailboxId: existing.mailboxId,
196
- fullPath: existing.fullPath,
197
- reason: "non-selectable",
198
- },
199
- "Deleted mailbox",
200
- );
201
- result.deleted++;
202
- }
203
- return;
204
- }
205
-
206
- // Skip duplicate special-use folders (e.g., "Trash" when "[Gmail]/Trash" exists)
207
- if (this.isDuplicateSpecialUse(mailboxInfo, claimedSpecialUse)) {
208
- seenPaths.add(mailboxInfo.fullPath);
209
- const existing = existingByPath.get(mailboxInfo.fullPath);
210
- if (existing && !isFolderMutationInFlight(existing)) {
211
- await this.specialUseService.deleteByMailboxId(existing.mailboxId);
212
- await this.mailboxService.delete(
213
- account.accountId,
214
- existing.mailboxId,
215
- );
216
- this.log.info(
217
- {
218
- mailboxId: existing.mailboxId,
219
- fullPath: existing.fullPath,
220
- reason: "duplicate-special-use",
221
- },
222
- "Deleted mailbox",
223
- );
224
- result.deleted++;
225
- }
226
- return;
227
- }
228
-
229
- seenPaths.add(mailboxInfo.fullPath);
230
- const existing = existingByPath.get(mailboxInfo.fullPath);
231
-
232
- if (existing) {
233
- // A folder at either end of its lifecycle is left alone: the worker
234
- // that establishes or removes it writes its own identity back, and
235
- // reading its status meanwhile is work whose only possible outcome is
236
- // a failure that fails this whole account's fan-out with it.
237
- if (isFolderMutationInFlight(existing)) {
238
- return;
239
- }
240
- // The folder set can change under this sweep. A delete that lands
241
- // between the LIST above and this mailbox's STATUS fails on the
242
- // server or on the row, and that would abort the enumeration of every
243
- // other folder and the SYNC_MESSAGES fan-out behind it — on a
244
- // per-account FIFO queue, for the whole visibility window (issue
245
- // #339). One folder leaving is not a failed account sync. Anything
246
- // else still propagates.
247
- const updated = await this.updateMailbox(
248
- account.accountId,
249
- existing,
250
- mailboxInfo,
251
- connection,
252
- ).catch(async (error: unknown) => {
253
- // The read only classifies the failure in hand; one that cannot
254
- // answer must not replace it.
255
- const gone = await isMailboxMutationInFlight(
256
- this.mailboxService,
257
- account.accountId,
258
- existing.mailboxId,
259
- ).catch(() => false);
260
- if (gone) return null;
261
- throw error;
262
- });
263
- if (updated) {
264
- result.updated++;
265
- }
266
- } else if (!claimedByPendingRename.has(mailboxInfo.fullPath)) {
267
- const created = await this.createMailbox(
268
- account.accountId,
269
- mailboxInfo,
270
- namespaces,
271
- connection,
272
- );
273
- if (created) result.created++;
274
- }
275
- },
276
- { concurrency: 3 },
277
- );
278
-
279
- // Handle deleted mailboxes (exist in DB but not on server)
280
- for (const existing of existingMailboxes) {
281
- if (seenPaths.has(existing.fullPath)) continue;
282
- // A row with a mutation in flight is a folder whose absence from the
283
- // LIST is expected, not a server-side deletion. Deleting it races the
284
- // worker: the row vanishes, then MAILBOX_CREATE fails with NotFoundError
285
- // trying to mark it synced, and — sharing this account's mailboxes FIFO
286
- // group — that un-acked failure stalls every later mailbox sync for the
287
- // queue's whole visibility window (#290).
288
- //
289
- // `deleting` is here for a second reason. That window is exactly when a
290
- // delete has landed on the server and the worker has not written back,
291
- // so hard-deleting the row removes it under the client holding its id —
292
- // the "Mailbox not found: <old id>" of #333 — and removes it without the
293
- // folder's mail, which is the worker's job (D8). Leave both to the flow
294
- // that owns them.
295
- if (isFolderMutationInFlight(existing)) continue;
296
- await this.mailboxService.delete(account.accountId, existing.mailboxId);
297
- this.log.info(
298
- {
299
- mailboxId: existing.mailboxId,
300
- fullPath: existing.fullPath,
301
- reason: "not-on-server",
302
- },
303
- "Deleted mailbox",
304
- );
305
- result.deleted++;
306
- }
307
-
308
- return result;
309
- };
310
-
311
- /**
312
- * Sync metadata for a specific mailbox
313
- *
314
- * Opens the mailbox to get current UID validity, counts, etc.
315
- */
316
- syncMailboxMetadata = async (
317
- accountId: string,
318
- mailboxId: string,
319
- connection: IImapConnection,
320
- ): Promise<MailboxItem> => {
321
- const mailbox = await this.mailboxService.get(accountId, mailboxId);
322
- const boxStatus = await connection.openBox(mailbox.fullPath, true);
323
-
324
- // See `updateMailbox` above for why this trips (not skips) on a
325
- // UIDVALIDITY change instead of silently overwriting the stored value.
326
- const uidValidityChanged = mailbox.uidValidity !== boxStatus.uidvalidity;
327
- const cursorTrip =
328
- uidValidityChanged && !isCursorRebuildNeeded(mailbox.cursorState)
329
- ? { cursorState: MailboxCursorState.cursor_invalid }
330
- : {};
331
-
332
- return this.mailboxService
333
- .update(accountId, mailboxId, {
334
- uidValidity: boxStatus.uidvalidity,
335
- uidNext: boxStatus.uidnext,
336
- messageCount: boxStatus.messages.total,
337
- ...cursorTrip,
338
- })
339
- .finally(() => connection.closeBox(false));
340
- };
341
-
342
- /**
343
- * Get all mailboxes for an account, handling pagination
344
- */
345
- private getAllMailboxes = async (
346
- accountId: string,
347
- ): Promise<MailboxItem[]> => {
348
- const allMailboxes: MailboxItem[] = [];
349
- let continuationToken: string | undefined;
350
-
351
- do {
352
- const result = await this.mailboxService.listByAccount(accountId, {
353
- continuationToken,
354
- });
355
- allMailboxes.push(...result.items);
356
- continuationToken = result.continuationToken;
357
- } while (continuationToken);
358
-
359
- return allMailboxes;
360
- };
361
-
362
- /**
363
- * Fetch all mailboxes from IMAP server across all namespaces.
364
- * Uses listMailboxes() to preserve original paths from the server.
365
- */
366
- private fetchAllMailboxes = async (
367
- connection: IImapConnection,
368
- namespaces: ImapNamespaces,
369
- ): Promise<
370
- Array<
371
- FlatMailboxInfo & {
372
- namespaceType: NamespaceTypeValue;
373
- namespacePrefix: string;
374
- }
375
- >
376
- > => {
377
- // Flatten all namespaces with their types
378
- const allNamespaces = [
379
- ...namespaces.personal.map((ns) => ({
380
- type: NamespaceType.Personal as NamespaceTypeValue,
381
- prefix: ns.prefix || "",
382
- })),
383
- ...namespaces.other.map((ns) => ({
384
- type: NamespaceType.OtherUsers as NamespaceTypeValue,
385
- prefix: ns.prefix || "",
386
- })),
387
- ...namespaces.shared.map((ns) => ({
388
- type: NamespaceType.Shared as NamespaceTypeValue,
389
- prefix: ns.prefix || "",
390
- })),
391
- ];
392
-
393
- // Fetch mailboxes for each namespace and flatten
394
- const nestedResults = await Promise.all(
395
- allNamespaces.map(async ({ type, prefix }) => {
396
- const mailboxes = await connection.listMailboxes(prefix);
397
- return mailboxes.map((mailbox) => ({
398
- ...mailbox,
399
- namespaceType: type,
400
- namespacePrefix: prefix,
401
- }));
402
- }),
403
- );
404
- const results = nestedResults.flat();
405
-
406
- // INBOX is implicit in IMAP and may not be returned by LIST commands
407
- const hasInbox = results.some((m) => m.fullPath.toUpperCase() === "INBOX");
408
- if (!hasInbox) {
409
- const nsDelimiter = namespaces.personal[0]?.delimiter;
410
- const delimiter = typeof nsDelimiter === "string" ? nsDelimiter : "/";
411
- results.unshift({
412
- fullPath: "INBOX",
413
- name: "INBOX",
414
- delimiter,
415
- attributes: [],
416
- parentPath: null,
417
- namespaceType: NamespaceType.Personal,
418
- namespacePrefix: "",
419
- });
420
- }
421
-
422
- return results;
423
- };
424
-
425
- /**
426
- * Create a new mailbox in the database
427
- */
428
- private createMailbox = async (
429
- accountId: string,
430
- mailboxInfo: FlatMailboxInfo & {
431
- namespaceType: NamespaceTypeValue;
432
- namespacePrefix: string;
433
- },
434
- _namespaces: ImapNamespaces,
435
- connection: IImapConnection,
436
- ): Promise<MailboxItem | null> => {
437
- // Fetch mailbox status using STATUS command (doesn't require SELECT/EXAMINE)
438
- // This gets us message counts including unseen without opening the mailbox
439
- const status = await connection.getMailboxStatus(mailboxInfo.fullPath);
440
-
441
- // Parse special-use attributes (RFC 6154) up front so the row stores a
442
- // denormalized copy. Frontends list mailboxes by account; threading a join
443
- // through MailboxSpecialUseEntry per row would be O(N) extra round-trips.
444
- const parsed = parseImapAttributes(mailboxInfo.attributes);
445
-
446
- const input: CreateMailboxInput = {
447
- accountId,
448
- namespaceType: mailboxInfo.namespaceType,
449
- namespacePrefix: mailboxInfo.namespacePrefix,
450
- hierarchyDelimiter: mailboxInfo.delimiter,
451
- fullPath: mailboxInfo.fullPath,
452
- uidValidity: status.uidValidity,
453
- uidNext: status.uidNext,
454
- // The message-sync cursor, not a status projection: it records how
455
- // far THIS mailbox's messages have been applied, and nothing has
456
- // been. Seeding it from the server's current HIGHESTMODSEQ would
457
- // declare an unsynced folder already caught up and skip every
458
- // message in it. Message sync seeds it once it has enumerated the
459
- // folder.
460
- highestModseq: "0",
461
- messageCount: status.messages,
462
- unseenCount: status.unseen,
463
- deletedCount: status.deletedCount,
464
- totalSize: 0,
465
- lastSyncUid: 0,
466
- highWaterMarkUid: 0,
467
- lastMessageSyncAt: 0,
468
- specialUse: parsed.specialUse.length > 0 ? parsed.specialUse : undefined,
469
- // A folder the server just told us about is confirmed. The column
470
- // defaults to this, so the row is right either way; the field is total
471
- // now (D1) and the insert says what it means.
472
- syncStatus: MailboxSyncStatus.synced,
473
- // parentMailboxId would need to be resolved from parentPath
474
- };
475
-
476
- const created = await this.mailboxService.create(input);
477
- if (created.outcome === "PathTaken") {
478
- this.log.error(
479
- { accountId, fullPath: mailboxInfo.fullPath },
480
- "Sweep found no row for a folder path the account already holds",
481
- );
482
- return null;
483
- }
484
- const { mailbox } = created;
485
-
486
- // Keep the MailboxSpecialUseEntry table in sync — every backend and worker
487
- // special-folder lookup reads it through MailboxSpecialUseRepo. Denormalized
488
- // copy on Mailbox is the read-side optimization, the entries remain the
489
- // authoritative join source for cross-mailbox lookups.
490
- if (parsed.specialUse.length > 0) {
491
- await this.specialUseService.createMany(
492
- mailbox.mailboxId,
493
- parsed.specialUse,
494
- );
495
- this.log.info(
496
- {
497
- mailboxId: mailbox.mailboxId,
498
- fullPath: mailboxInfo.fullPath,
499
- specialUse: parsed.specialUse,
500
- },
501
- "Created mailbox",
502
- );
503
- } else {
504
- this.log.info(
505
- { mailboxId: mailbox.mailboxId, fullPath: mailboxInfo.fullPath },
506
- "Created mailbox",
507
- );
508
- }
509
-
510
- return mailbox;
511
- };
512
-
513
- /**
514
- * Update an existing mailbox with fresh data.
515
- * Skips DB write if nothing has changed.
516
- *
517
- * @returns The updated mailbox, or null if skipped due to no changes
518
- */
519
- private updateMailbox = async (
520
- accountId: string,
521
- existing: MailboxItem,
522
- mailboxInfo: FlatMailboxInfo,
523
- connection: IImapConnection,
524
- ): Promise<MailboxItem | null> => {
525
- // Fetch mailbox status using STATUS command (doesn't require SELECT/EXAMINE)
526
- const status = await connection.getMailboxStatus(mailboxInfo.fullPath);
527
-
528
- const parsed = parseImapAttributes(mailboxInfo.attributes);
529
- const specialUseChanged = !areSpecialUseSetsEqual(
530
- existing.specialUse,
531
- parsed.specialUse,
532
- );
533
-
534
- // UIDVALIDITY detection (#1272): this STATUS-based sweep persists a fresh
535
- // uidValidity below regardless (harmless — it never touches a stored UID),
536
- // but if the server's value disagrees with what's stored and the mailbox
537
- // was still `normal`, the axis just changed. Trip the cursor here so the
538
- // message-sync/flag-push/move/body-fetch paths pause outbound IMAP until
539
- // the rebuild resolves it, instead of silently overwriting the old value
540
- // and erasing the only evidence a bump happened.
541
- const uidValidityChanged = existing.uidValidity !== status.uidValidity;
542
- const cursorTrip =
543
- uidValidityChanged && !isCursorRebuildNeeded(existing.cursorState)
544
- ? { cursorState: MailboxCursorState.cursor_invalid }
545
- : {};
546
-
547
- // Check if anything actually changed
548
- const hasChanges =
549
- existing.uidNext !== status.uidNext ||
550
- existing.uidValidity !== status.uidValidity ||
551
- existing.messageCount !== status.messages ||
552
- existing.unseenCount !== status.unseen ||
553
- existing.deletedCount !== status.deletedCount ||
554
- specialUseChanged;
555
-
556
- // Sync special-use attributes (handles migration of existing mailboxes)
557
- await this.syncSpecialUseAttributes(existing.mailboxId, mailboxInfo);
558
-
559
- if (!hasChanges) {
560
- return null;
561
- }
562
-
563
- // Debug: log what changed
564
- const changes: string[] = [];
565
- if (existing.uidNext !== status.uidNext)
566
- changes.push(`uidNext: ${existing.uidNext} -> ${status.uidNext}`);
567
- if (existing.uidValidity !== status.uidValidity)
568
- changes.push(
569
- `uidValidity: ${existing.uidValidity} -> ${status.uidValidity}`,
570
- );
571
- if (existing.messageCount !== status.messages)
572
- changes.push(
573
- `messageCount: ${existing.messageCount} -> ${status.messages}`,
574
- );
575
- if (existing.unseenCount !== status.unseen)
576
- changes.push(`unseenCount: ${existing.unseenCount} -> ${status.unseen}`);
577
- if (existing.deletedCount !== status.deletedCount)
578
- changes.push(
579
- `deletedCount: ${existing.deletedCount} -> ${status.deletedCount}`,
580
- );
581
- if (specialUseChanged)
582
- changes.push(
583
- `specialUse: [${(existing.specialUse ?? []).join(",")}] -> [${parsed.specialUse.join(",")}]`,
584
- );
585
-
586
- this.log.info(
587
- {
588
- mailboxId: existing.mailboxId,
589
- fullPath: mailboxInfo.fullPath,
590
- changes,
591
- },
592
- "Updating mailbox",
593
- );
594
-
595
- // Update mailbox with fresh status. ElectroDB rejects empty sets, so we
596
- // pass undefined when no flags are present rather than [].
597
- return this.mailboxService.update(accountId, existing.mailboxId, {
598
- hierarchyDelimiter: mailboxInfo.delimiter,
599
- uidValidity: status.uidValidity,
600
- uidNext: status.uidNext,
601
- // `highestModseq` is deliberately absent: it is message sync's
602
- // cursor over this mailbox's own applied changes, and overwriting it
603
- // with the server's current value here would step it over every
604
- // change message sync had not yet applied.
605
- messageCount: status.messages,
606
- unseenCount: status.unseen,
607
- deletedCount: status.deletedCount,
608
- specialUse: parsed.specialUse.length > 0 ? parsed.specialUse : undefined,
609
- ...cursorTrip,
610
- });
611
- };
612
-
613
- /**
614
- * Sync special-use attributes for a mailbox.
615
- * Creates entries if they don't exist, updates if changed.
616
- */
617
- private syncSpecialUseAttributes = async (
618
- mailboxId: string,
619
- mailboxInfo: FlatMailboxInfo,
620
- ): Promise<void> => {
621
- const parsed = parseImapAttributes(mailboxInfo.attributes);
622
- const existingEntries =
623
- await this.specialUseService.listByMailboxId(mailboxId);
624
-
625
- const existingSpecialUses = new Set(
626
- existingEntries.map((e) => e.specialUse),
627
- );
628
- const newSpecialUses = new Set(parsed.specialUse);
629
-
630
- // Check if sets are equal
631
- const areEqual =
632
- existingSpecialUses.size === newSpecialUses.size &&
633
- [...existingSpecialUses].every((use) => newSpecialUses.has(use));
634
-
635
- if (areEqual) return;
636
-
637
- // Delete and recreate (simpler than diff)
638
- if (existingEntries.length > 0) {
639
- await this.specialUseService.deleteByMailboxId(mailboxId);
640
- }
641
-
642
- if (parsed.specialUse.length > 0) {
643
- await this.specialUseService.createMany(mailboxId, parsed.specialUse);
644
- this.log.debug(
645
- { fullPath: mailboxInfo.fullPath, specialUse: parsed.specialUse },
646
- "Synced special-use",
647
- );
648
- }
649
- };
650
-
651
- /**
652
- * Build a map of special-use designations to mailbox paths.
653
- * Only includes mailboxes that have the IMAP special-use attribute.
654
- */
655
- private buildSpecialUseMap = (
656
- mailboxes: FlatMailboxInfo[],
657
- ): Map<MailboxSpecialUseValue, string> => {
658
- const map = new Map<MailboxSpecialUseValue, string>();
659
-
660
- for (const mailbox of mailboxes) {
661
- const parsed = parseImapAttributes(mailbox.attributes);
662
- for (const specialUse of parsed.specialUse) {
663
- // First mailbox with this special-use wins (usually the canonical one)
664
- if (!map.has(specialUse)) {
665
- map.set(specialUse, mailbox.fullPath);
666
- }
667
- }
668
- }
669
-
670
- return map;
671
- };
672
-
673
- /**
674
- * Check if a mailbox is a duplicate special-use folder.
675
- * A folder is considered duplicate if:
676
- * 1. Its name matches a common special-use folder name (e.g., "Trash")
677
- * 2. It does NOT have the IMAP special-use attribute
678
- * 3. Another folder already claimed that special-use designation
679
- */
680
- private isDuplicateSpecialUse = (
681
- mailbox: FlatMailboxInfo,
682
- claimedSpecialUse: Map<MailboxSpecialUseValue, string>,
683
- ): boolean => {
684
- // Get the folder name (last segment of path)
685
- const folderName = mailbox.fullPath.split(mailbox.delimiter).pop() ?? "";
686
- const normalizedName = folderName.toLowerCase();
687
-
688
- // Check if this folder name is a conventional name for a role
689
- const role = roleForFolderName(normalizedName);
690
- if (!role) {
691
- return false; // Not a special-use folder name
692
- }
693
-
694
- const expectedSpecialUse = ROLE_SPECIAL_USE[role];
695
- if (!expectedSpecialUse) {
696
- return false; // The role has no SPECIAL-USE flag to be a duplicate of
697
- }
698
-
699
- // Check if this mailbox has the special-use attribute
700
- const parsed = parseImapAttributes(mailbox.attributes);
701
- if (parsed.specialUse.some((use) => use === expectedSpecialUse)) {
702
- return false; // This IS the canonical folder
703
- }
704
-
705
- // Check if another folder already claimed this special-use
706
- const claimed = [...claimedSpecialUse].find(
707
- ([specialUse]) => specialUse === expectedSpecialUse,
708
- );
709
- if (!claimed) {
710
- return false; // No other folder has this special-use
711
- }
712
-
713
- // This is a duplicate - another folder has the attribute
714
- this.log.debug(
715
- {
716
- fullPath: mailbox.fullPath,
717
- claimedPath: claimed[1],
718
- specialUse: expectedSpecialUse,
719
- },
720
- "Skipping duplicate folder",
721
- );
722
- return true;
723
- };
724
- }