@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,1518 +0,0 @@
1
- import { randomUUID } from "node:crypto";
2
- import {
3
- SendMessageBatchCommand,
4
- SendMessageCommand,
5
- type SQSClient,
6
- } from "@aws-sdk/client-sqs";
7
- import type {
8
- IAddressRepository,
9
- IMailboxRepository,
10
- IMailboxSpecialUseRepository,
11
- IMessageRepository,
12
- IThreadMessageRepository,
13
- MessageItem,
14
- } from "@remit/data-ports";
15
- import {
16
- FolderRoleUnresolvedError,
17
- MessagePlacementUnsettledError,
18
- } from "@remit/data-ports/errors";
19
- import {
20
- type FolderRoleUnresolvedReason,
21
- NO_TRASH_FOLDER_REASON,
22
- type RoleResolution,
23
- STALE_TRASH_FOLDER_REASON,
24
- type TrashAssuranceLevel,
25
- trashMailboxAt,
26
- UNCONFIRMED_TRASH_FOLDER_REASON,
27
- } from "@remit/data-ports/folder-role";
28
- import { deriveCopyMessageId } from "@remit/data-ports/id";
29
- import { MUTATION_EVENT_SCHEMA_VERSION } from "@remit/data-ports/mutation-events";
30
- import {
31
- CanonicalMailboxRole,
32
- MessageMutation,
33
- MessageStatus,
34
- MessageSyncStatus,
35
- } from "@remit/domain-enums";
36
- import { createQueueProducer } from "@remit/sqs-client/producer";
37
- import {
38
- carriesForeignUid,
39
- placementBindingOf,
40
- waitForPlacementToSettle,
41
- } from "./placement-settled.js";
42
- import { restoreSourcePlacement } from "./restore-source-placement.js";
43
-
44
- /**
45
- * What a delete batch did not claim. A row whose placement changed between the
46
- * read that decided this delete and the mark that recorded it belongs to
47
- * whoever changed it (docs/architecture/imap-mutations.md R3), so this delete
48
- * leaves it alone — and says which, because the client removed it optimistically
49
- * and a row that reappears unexplained is a failure the user cannot act on.
50
- */
51
- export interface DeleteMessagesOutcome {
52
- refusedMessageIds: string[];
53
- }
54
-
55
- /**
56
- * Event types for message move/delete operations.
57
- * These match the worker event types in remit-imap-worker/events.ts.
58
- */
59
- interface MessageDeleteEvent {
60
- type: "MESSAGE_DELETE";
61
- schemaVersion: typeof MUTATION_EVENT_SCHEMA_VERSION;
62
- eventId: string;
63
- timestamp: number;
64
- accountId: string;
65
- messageId: string;
66
- mailboxId: string;
67
- mailboxPath: string;
68
- uid: number;
69
- operation: "move_to_trash" | "permanent_delete";
70
- destinationMailboxId?: string;
71
- destinationMailboxPath?: string;
72
- }
73
-
74
- interface MessageMoveEvent {
75
- type: "MESSAGE_MOVE";
76
- eventId: string;
77
- timestamp: number;
78
- accountId: string;
79
- messageId: string;
80
- sourceMailboxId: string;
81
- sourceMailboxPath: string;
82
- destinationMailboxId: string;
83
- destinationMailboxPath: string;
84
- uid: number;
85
- }
86
-
87
- interface EmptyTrashEvent {
88
- type: "EMPTY_TRASH";
89
- schemaVersion: typeof MUTATION_EVENT_SCHEMA_VERSION;
90
- eventId: string;
91
- timestamp: number;
92
- accountId: string;
93
- trashMailboxId: string;
94
- trashMailboxPath: string;
95
- /** The folder's UIDVALIDITY at consent time; the worker re-checks it. */
96
- trashUidValidity: number;
97
- }
98
-
99
- interface MessageCopyEvent {
100
- type: "MESSAGE_COPY";
101
- eventId: string;
102
- timestamp: number;
103
- accountId: string;
104
- sourceMessageId: string;
105
- newMessageId: string;
106
- sourceMailboxId: string;
107
- sourceMailboxPath: string;
108
- destinationMailboxId: string;
109
- destinationMailboxPath: string;
110
- uid: number;
111
- }
112
-
113
- /** An event, and the undo of the local write it was enqueued for. */
114
- interface UndoableEvent<T> {
115
- event: T;
116
- handBack: () => Promise<void>;
117
- }
118
-
119
- type BatchEnqueueOutcome =
120
- | { outcome: "sent" }
121
- | {
122
- outcome: "unsent";
123
- unsent: UndoableEvent<MessageDeleteEvent>[];
124
- fault: unknown;
125
- };
126
-
127
- /** Undo every write whose event never left, then fail with the queue's error. */
128
- const handBackThenThrow = async (
129
- fault: unknown,
130
- handBacks: ReadonlyArray<() => Promise<void>>,
131
- ): Promise<never> => {
132
- const undoFaults: unknown[] = [];
133
- for (const handBack of handBacks) {
134
- await handBack().catch((undoFault: unknown) => {
135
- undoFaults.push(undoFault);
136
- });
137
- }
138
- if (undoFaults.length > 0) {
139
- throw new AggregateError(
140
- [fault, ...undoFaults],
141
- "The queue refused the event, and handing the local write back failed too",
142
- );
143
- }
144
- throw fault;
145
- };
146
-
147
- type MessageMoveQueueEvent =
148
- | MessageDeleteEvent
149
- | MessageMoveEvent
150
- | EmptyTrashEvent
151
- | MessageCopyEvent;
152
-
153
- /**
154
- * Logger interface
155
- */
156
- export interface MessageMoveLogger {
157
- info(obj: Record<string, unknown>, msg: string): void;
158
- error(obj: Record<string, unknown>, msg: string): void;
159
- warn?(obj: Record<string, unknown>, msg: string): void;
160
- }
161
-
162
- const noopLogger: MessageMoveLogger = {
163
- info: () => {},
164
- error: () => {},
165
- };
166
-
167
- /**
168
- * Configuration for MessageMoveService
169
- */
170
- export interface MessageMoveConfig {
171
- messageService: IMessageRepository;
172
- mailboxService: IMailboxRepository;
173
- mailboxSpecialUseService: IMailboxSpecialUseRepository;
174
- threadMessageService: IThreadMessageRepository;
175
- addressService: IAddressRepository;
176
- sqsQueueUrl: string;
177
- sqsEndpoint?: string;
178
- logger?: MessageMoveLogger;
179
- moveSettleTimeoutMs?: number;
180
- moveSettlePollMs?: number;
181
- }
182
-
183
- const DEFAULT_MOVE_SETTLE_TIMEOUT_MS = 5_000;
184
- const DEFAULT_MOVE_SETTLE_POLL_MS = 250;
185
-
186
- /**
187
- * The account resolves no Trash folder, so a move-to-Trash delete has nowhere
188
- * to put the mail and an Empty Trash has nothing it may empty. A designed,
189
- * user-facing outcome carrying its own text and a 409 the backend returns
190
- * verbatim: the absence of a Trash folder is a blocker the user resolves by
191
- * appointing one, never a licence to expunge.
192
- */
193
- export class NoTrashMailboxError extends FolderRoleUnresolvedError {
194
- name = "NoTrashMailboxError";
195
-
196
- constructor(accountId: string) {
197
- super(
198
- NO_TRASH_FOLDER_REASON,
199
- CanonicalMailboxRole.Trash,
200
- "none",
201
- accountId,
202
- );
203
- }
204
- }
205
-
206
- /**
207
- * The folder the user appointed as Trash is gone from the server. Distinct from
208
- * having no Trash at all: there is a decision on file, and the repair is to
209
- * point it at a folder that exists — not to let reader pick one instead.
210
- */
211
- export class StaleTrashAppointmentError extends FolderRoleUnresolvedError {
212
- name = "StaleTrashAppointmentError";
213
-
214
- constructor(accountId: string) {
215
- super(
216
- STALE_TRASH_FOLDER_REASON,
217
- CanonicalMailboxRole.Trash,
218
- "stale",
219
- accountId,
220
- );
221
- }
222
- }
223
-
224
- /**
225
- * A folder resolves as Trash by its name alone. Enough to move mail into,
226
- * never enough to expunge — raised by Empty Trash and by a delete that would
227
- * expunge a message already sitting in that folder.
228
- */
229
- export class UnconfirmedTrashMailboxError extends FolderRoleUnresolvedError {
230
- name = "UnconfirmedTrashMailboxError";
231
-
232
- constructor(accountId: string) {
233
- super(
234
- UNCONFIRMED_TRASH_FOLDER_REASON,
235
- CanonicalMailboxRole.Trash,
236
- "unconfirmed",
237
- accountId,
238
- );
239
- }
240
- }
241
-
242
- const REFUSAL_BY_REASON: Record<
243
- FolderRoleUnresolvedReason,
244
- new (
245
- accountId: string,
246
- ) => FolderRoleUnresolvedError
247
- > = {
248
- none: NoTrashMailboxError,
249
- stale: StaleTrashAppointmentError,
250
- unconfirmed: UnconfirmedTrashMailboxError,
251
- };
252
-
253
- /** The coded 409 for one refusal reason. The only place a reason becomes an error. */
254
- const trashRefusal = (
255
- reason: FolderRoleUnresolvedReason,
256
- accountId: string,
257
- ): FolderRoleUnresolvedError => new REFUSAL_BY_REASON[reason](accountId);
258
-
259
- /**
260
- * The Trash folder a verb may act on at the assurance it demands, or the coded
261
- * refusal naming which of the three reasons stopped it. Every Trash gate goes
262
- * through here — the delete and the expunge — so no two of them can tell a
263
- * user different things about the same account.
264
- */
265
- export const requireTrashMailbox = <T>(
266
- resolution: RoleResolution<T>,
267
- level: TrashAssuranceLevel,
268
- accountId: string,
269
- ): T => {
270
- const outcome = trashMailboxAt(resolution, level);
271
- if (outcome.allowed) return outcome.mailbox;
272
- throw trashRefusal(outcome.reason, accountId);
273
- };
274
-
275
- /**
276
- * Options for delete operations
277
- */
278
- export interface DeleteOptions {
279
- /** Move to Trash instead of permanent delete. Default: true */
280
- toTrash?: boolean;
281
- /** Permanently delete even if in Trash. Default: false */
282
- permanent?: boolean;
283
- }
284
-
285
- /**
286
- * Service for moving, copying, and deleting messages.
287
- *
288
- * Implements optimistic local-first pattern:
289
- * 1. Updates local state immediately (Message + ThreadMessage)
290
- * 2. Enqueues event to SQS for worker to sync to IMAP
291
- *
292
- * Following RFC 016 for message deletion and moving.
293
- */
294
- export class MessageMoveService {
295
- private messageService: IMessageRepository;
296
- private mailboxService: IMailboxRepository;
297
- private mailboxSpecialUseService: IMailboxSpecialUseRepository;
298
- private threadMessageService: IThreadMessageRepository;
299
- private addressService: IAddressRepository;
300
- private sqs: SQSClient;
301
- private queueUrl: string;
302
- private log: MessageMoveLogger;
303
- private moveSettleTimeoutMs: number;
304
- private moveSettlePollMs: number;
305
-
306
- constructor(config: MessageMoveConfig) {
307
- this.messageService = config.messageService;
308
- this.mailboxService = config.mailboxService;
309
- this.mailboxSpecialUseService = config.mailboxSpecialUseService;
310
- this.threadMessageService = config.threadMessageService;
311
- this.addressService = config.addressService;
312
- this.queueUrl = config.sqsQueueUrl;
313
- this.log = config.logger ?? noopLogger;
314
- this.moveSettleTimeoutMs =
315
- config.moveSettleTimeoutMs ?? DEFAULT_MOVE_SETTLE_TIMEOUT_MS;
316
- this.moveSettlePollMs =
317
- config.moveSettlePollMs ?? DEFAULT_MOVE_SETTLE_POLL_MS;
318
-
319
- this.sqs = createQueueProducer({
320
- queueUrl: config.sqsQueueUrl,
321
- endpoint: config.sqsEndpoint,
322
- });
323
- }
324
-
325
- /**
326
- * Delete a message. By default moves to Trash.
327
- *
328
- * @param messageId - Message to delete
329
- * @param accountId - Account ID
330
- * @param options - Delete options (toTrash, permanent)
331
- */
332
- deleteMessage = async (
333
- accountConfigId: string,
334
- messageId: string,
335
- accountId: string,
336
- options: DeleteOptions = { toTrash: true },
337
- ): Promise<DeleteMessagesOutcome> =>
338
- this.deleteMessages(accountConfigId, [messageId], accountId, options);
339
-
340
- /**
341
- * Delete multiple messages using batch operations.
342
- * By default moves to Trash, unless permanent is specified or already in Trash.
343
- *
344
- * @param messageIds - Messages to delete
345
- * @param accountId - Account ID
346
- * @param options - Delete options (toTrash, permanent)
347
- */
348
- deleteMessages = async (
349
- accountConfigId: string,
350
- messageIds: string[],
351
- accountId: string,
352
- options: DeleteOptions = { toTrash: true },
353
- ): Promise<DeleteMessagesOutcome> => {
354
- if (messageIds.length === 0) return { refusedMessageIds: [] };
355
-
356
- // Batch get all messages
357
- const rows = await this.messageService.get(messageIds);
358
- if (rows.length === 0) return { refusedMessageIds: [] };
359
-
360
- // Ahead of the Trash gates below, which compare `mailboxId` against the
361
- // Trash folder: an in-flight move makes that comparison answer for a
362
- // folder the message has not reached.
363
- const messages = await this.settledPlacements(rows, accountId);
364
-
365
- // Get unique mailbox IDs and batch fetch mailboxes
366
- const uniqueMailboxIds = [...new Set(messages.map((m) => m.mailboxId))];
367
- const mailboxes = await this.mailboxService.get(
368
- accountId,
369
- uniqueMailboxIds,
370
- );
371
- const mailboxMap = new Map(mailboxes.map((m) => [m.mailboxId, m]));
372
-
373
- // A permanent delete is only ever what the caller explicitly asked for.
374
- // Everything else is a move to Trash, and a Trash folder that does not
375
- // resolve blocks it — the user was asked "move to Trash?" and expunging
376
- // their mail instead is unrecoverable and answers a question nobody put
377
- // to them. Thrown before any local write or event, so the server and the
378
- // projection are both left untouched.
379
- const wantsPermanentDelete =
380
- options.permanent === true || options.toTrash === false;
381
-
382
- const trashResolution = wantsPermanentDelete
383
- ? null
384
- : await this.mailboxSpecialUseService.resolveTrashRole(accountId);
385
-
386
- const trashGate = trashResolution
387
- ? trashMailboxAt(trashResolution, "resolved")
388
- : null;
389
-
390
- if (trashGate && !trashGate.allowed) {
391
- this.log.error(
392
- { accountId, messageCount: messages.length, reason: trashGate.reason },
393
- "Refused to delete: this account's Trash is unresolved",
394
- );
395
- throw trashRefusal(trashGate.reason, accountId);
396
- }
397
-
398
- const trashMailbox = trashGate ? trashGate.mailbox : null;
399
-
400
- // A message already sitting in the folder resolved as Trash deletes by
401
- // expunging it, not by moving it — the same unrecoverable act Empty
402
- // Trash performs, so it demands the same confirmed evidence. The name
403
- // guess ("resolved") is enough to file mail into; it is never enough to
404
- // destroy mail that is already there. Checked once, before any local
405
- // write, so a batch that touches such a message is refused whole rather
406
- // than partially expunged.
407
- if (!wantsPermanentDelete && trashResolution && trashMailbox) {
408
- const deletesInPlace = messages.some(
409
- (message) => message.mailboxId === trashMailbox.mailboxId,
410
- );
411
-
412
- if (deletesInPlace) {
413
- const confirmedTrashGate = trashMailboxAt(trashResolution, "confirmed");
414
- if (!confirmedTrashGate.allowed) {
415
- this.log.error(
416
- {
417
- accountId,
418
- messageCount: messages.length,
419
- reason: confirmedTrashGate.reason,
420
- },
421
- "Refused to delete: this account's Trash is not confirmed",
422
- );
423
- throw trashRefusal(confirmedTrashGate.reason, accountId);
424
- }
425
- }
426
- }
427
-
428
- // Group messages by operation type
429
- type DeleteEntry = {
430
- messageId: string;
431
- message: { mailboxId: string; uid: number };
432
- sourceMailbox: { mailboxId: string; fullPath: string };
433
- };
434
- const moveToTrashMessages: DeleteEntry[] = [];
435
- const permanentDeleteMessages: DeleteEntry[] = [];
436
-
437
- // Rows whose placement changed between the read above and the mark below,
438
- // so this delete never claimed them. Reported rather than skipped in
439
- // silence: the client removed them optimistically, and a row that
440
- // reappears with nothing said is the dead-button failure again (#1229).
441
- const refused: string[] = [];
442
-
443
- for (const message of messages) {
444
- const sourceMailbox = mailboxMap.get(message.mailboxId);
445
- if (!sourceMailbox) {
446
- // The folder lookup is scoped to one account, so a selection that
447
- // spans accounts — the daily brief lists every account's mail —
448
- // leaves this row's mailbox unresolved. Nothing about it was
449
- // written and no event was enqueued, so it is a refusal like any
450
- // other; dropping it silently answered "deleted" over a message
451
- // still sitting where it was.
452
- refused.push(message.messageId);
453
- this.log.info(
454
- { accountId, messageId: message.messageId },
455
- "Refused delete: this message's folder does not belong to this account",
456
- );
457
- continue;
458
- }
459
-
460
- const isInTrash =
461
- trashMailbox !== null && message.mailboxId === trashMailbox.mailboxId;
462
- const shouldMoveToTrash = trashMailbox !== null && !isInTrash;
463
-
464
- const entry = {
465
- messageId: message.messageId,
466
- message: { mailboxId: message.mailboxId, uid: message.uid },
467
- sourceMailbox: {
468
- mailboxId: sourceMailbox.mailboxId,
469
- fullPath: sourceMailbox.fullPath,
470
- },
471
- };
472
-
473
- if (shouldMoveToTrash) {
474
- moveToTrashMessages.push(entry);
475
- } else {
476
- permanentDeleteMessages.push(entry);
477
- }
478
- }
479
-
480
- // Collect all events for batch SQS send
481
- const events: UndoableEvent<MessageDeleteEvent>[] = [];
482
-
483
- // Process move to trash
484
- if (trashMailbox && moveToTrashMessages.length > 0) {
485
- // Every message in the batch shares the config, so its Junk and Trash
486
- // folders resolve once rather than once per message.
487
- const configJunkRoles =
488
- await this.mailboxSpecialUseService.resolveJunkRolesForConfig(
489
- accountConfigId,
490
- );
491
- for (const { messageId, message, sourceMailbox } of moveToTrashMessages) {
492
- // The optimistic write is a transition off the row this call read
493
- // (imap-mutations R3). Several folder lookups stand between the read
494
- // and here, and PLACEMENT_MOVE_PUSH runs on a queue this account's
495
- // FIFO group does not order, so the row can settle onto a different
496
- // folder and uid in between. Writing anyway would stamp `moving` and
497
- // the OLD uid over the new placement, and the event enqueued below
498
- // would name a uid the source no longer holds — a delete aimed at
499
- // whatever message has since taken that number.
500
- const marked = await this.messageService.transitionPlacement(
501
- messageId,
502
- {
503
- // `active`, not whatever the row was read as: a row already
504
- // `moving` or `deleting` is mid-mutation, and marking it again
505
- // would enqueue a second event against the same uid.
506
- status: MessageStatus.active,
507
- mailboxId: message.mailboxId,
508
- uid: message.uid,
509
- },
510
- {
511
- mailboxId: trashMailbox.mailboxId,
512
- status: MessageStatus.moving,
513
- syncStatus: MessageSyncStatus.pending,
514
- abandonedMutation: MessageMutation.none,
515
- originalMailboxId: sourceMailbox.mailboxId,
516
- originalUid: message.uid,
517
- },
518
- );
519
- if (!marked) {
520
- refused.push(messageId);
521
- this.log.info(
522
- { accountId, messageId },
523
- "Refused delete: this message's placement changed after it was read",
524
- );
525
- continue;
526
- }
527
-
528
- // Update ThreadMessage
529
- await this.updateThreadMessageForMove(
530
- accountConfigId,
531
- messageId,
532
- trashMailbox.mailboxId,
533
- true,
534
- );
535
-
536
- await this.addressService.reconcileJunkOnlyForMessage(
537
- messageId,
538
- configJunkRoles,
539
- );
540
-
541
- events.push({
542
- event: {
543
- type: "MESSAGE_DELETE",
544
- schemaVersion: MUTATION_EVENT_SCHEMA_VERSION,
545
- eventId: randomUUID(),
546
- timestamp: Date.now(),
547
- accountId,
548
- messageId,
549
- mailboxId: sourceMailbox.mailboxId,
550
- mailboxPath: sourceMailbox.fullPath,
551
- uid: message.uid,
552
- operation: "move_to_trash",
553
- destinationMailboxId: trashMailbox.mailboxId,
554
- destinationMailboxPath: trashMailbox.fullPath,
555
- },
556
- handBack: () =>
557
- this.handBackUnsentMove(
558
- accountConfigId,
559
- messageId,
560
- sourceMailbox.mailboxId,
561
- message.uid,
562
- ),
563
- });
564
- }
565
-
566
- this.log.info(
567
- {
568
- count: moveToTrashMessages.length,
569
- trashMailboxId: trashMailbox.mailboxId,
570
- },
571
- "Moved messages to trash (local)",
572
- );
573
- }
574
-
575
- // Process permanent deletes
576
- for (const {
577
- messageId,
578
- message,
579
- sourceMailbox,
580
- } of permanentDeleteMessages) {
581
- // A transition off the row this call read, like the trash path above
582
- // (imap-mutations R3). The expunge below is aimed at a specific uid in
583
- // a specific folder, so a row that has moved since the read must not be
584
- // marked for it: a restore out of Trash settling in between would be
585
- // overwritten with `deleting`, and the event would carry the uid Trash
586
- // no longer holds.
587
- const marked = await this.messageService.transitionPlacement(
588
- messageId,
589
- {
590
- // `active` for the same reason as the trash path above: a second
591
- // press on a row already `deleting` would enqueue a duplicate
592
- // expunge rather than refusing.
593
- status: MessageStatus.active,
594
- mailboxId: message.mailboxId,
595
- uid: message.uid,
596
- },
597
- {
598
- status: MessageStatus.deleting,
599
- syncStatus: MessageSyncStatus.pending,
600
- abandonedMutation: MessageMutation.none,
601
- },
602
- );
603
- if (!marked) {
604
- refused.push(messageId);
605
- this.log.info(
606
- { accountId, messageId },
607
- "Refused permanent delete: this message's placement changed after it was read",
608
- );
609
- continue;
610
- }
611
-
612
- // Delete ThreadMessage rows up-front (one row per mailbox copy).
613
- // The IMAP worker also deletes them once the IMAP DELETE succeeds —
614
- // doing it eagerly here closes the visibility window where the inbox
615
- // list shows a row whose backing Message is being deleted, leading
616
- // to "Message not found: <id>" on click. See issue #212.
617
- await this.deleteThreadMessagesForMessage(accountConfigId, messageId);
618
-
619
- events.push({
620
- event: {
621
- type: "MESSAGE_DELETE",
622
- schemaVersion: MUTATION_EVENT_SCHEMA_VERSION,
623
- eventId: randomUUID(),
624
- timestamp: Date.now(),
625
- accountId,
626
- messageId,
627
- mailboxId: sourceMailbox.mailboxId,
628
- mailboxPath: sourceMailbox.fullPath,
629
- uid: message.uid,
630
- operation: "permanent_delete",
631
- },
632
- // The listing rows went above, so the local removal finishes, as the
633
- // worker's `handBackDelete` does; the folder's next sync re-projects it.
634
- handBack: async () => {
635
- const claimed = await this.messageService.transitionPlacement(
636
- messageId,
637
- {
638
- status: MessageStatus.deleting,
639
- mailboxId: message.mailboxId,
640
- uid: message.uid,
641
- },
642
- { status: MessageStatus.deleted },
643
- );
644
- if (claimed) await this.messageService.delete(messageId);
645
- },
646
- });
647
- }
648
-
649
- if (permanentDeleteMessages.length > 0) {
650
- this.log.info(
651
- { count: permanentDeleteMessages.length },
652
- "Marked messages for permanent deletion (local)",
653
- );
654
- }
655
-
656
- await this.enqueueEventsBatch(events);
657
-
658
- return { refusedMessageIds: refused };
659
- };
660
-
661
- /**
662
- * Move a message to another mailbox.
663
- *
664
- * Waits, never reconciles (imap-mutations R2): the folder and uid go to the
665
- * server as one pair, and once the worker has moved the destination's own
666
- * message at that uid there is no reference left to repair. The person is
667
- * looking at the result, so blocking for the sub-second a move takes is the
668
- * cheap half; on timeout the move refuses ahead of any local write.
669
- *
670
- * @param messageId - Message to move
671
- * @param destinationMailboxId - Destination mailbox ID
672
- * @param accountId - Account ID
673
- */
674
- moveMessage = async (
675
- accountConfigId: string,
676
- messageId: string,
677
- destinationMailboxId: string,
678
- accountId: string,
679
- ): Promise<void> => {
680
- await this.moveSettledMessage(
681
- accountConfigId,
682
- await this.settledPlacement(
683
- await this.messageService.get(messageId),
684
- accountId,
685
- ),
686
- destinationMailboxId,
687
- accountId,
688
- );
689
- };
690
-
691
- private moveSettledMessage = async (
692
- accountConfigId: string,
693
- message: MessageItem,
694
- destinationMailboxId: string,
695
- accountId: string,
696
- ): Promise<void> => {
697
- const messageId = message.messageId;
698
-
699
- // IMAP has no same-mailbox MOVE: the server copies the message, expunges
700
- // the original and hands back a fresh UID, so a request that asks for the
701
- // mailbox the message is already in destroys its identity to reach the
702
- // state it was already in. The requested end state holds, so there is
703
- // nothing to record as pending and nothing to enqueue. The row is settled
704
- // by the time this compares, so the folder it names is one the message has
705
- // actually reached rather than one an in-flight move wrote optimistically.
706
- if (message.mailboxId === destinationMailboxId) {
707
- this.log.info(
708
- { messageId, mailboxId: destinationMailboxId },
709
- "Skipping move: message is already in the destination mailbox",
710
- );
711
- return;
712
- }
713
-
714
- const sourceMailbox = await this.mailboxService.get(
715
- accountId,
716
- message.mailboxId,
717
- );
718
- const destinationMailbox = await this.mailboxService.get(
719
- accountId,
720
- destinationMailboxId,
721
- );
722
-
723
- // Check if moving to/from Trash
724
- const trashMailbox =
725
- await this.mailboxSpecialUseService.findTrashMailbox(accountId);
726
- const isMovingToTrash = Boolean(
727
- trashMailbox && destinationMailboxId === trashMailbox.mailboxId,
728
- );
729
- const isMovingFromTrash = Boolean(
730
- trashMailbox && message.mailboxId === trashMailbox.mailboxId,
731
- );
732
-
733
- // The optimistic write is a transition off the row this call read
734
- // (imap-mutations R3). Three mailbox lookups stand between the read and
735
- // here, and PLACEMENT_MOVE_PUSH runs on a queue this account's FIFO group
736
- // does not order, so the row can settle onto a different folder and uid in
737
- // between. A blind write would stamp `moving` and the OLD uid over that
738
- // placement, leaving a pair `carriesForeignUid` calls consistent, and the
739
- // event below would send the worker after whatever now holds the old uid.
740
- const moved = await this.messageService.transitionPlacement(
741
- messageId,
742
- {
743
- status: MessageStatus.active,
744
- mailboxId: message.mailboxId,
745
- uid: message.uid,
746
- },
747
- {
748
- mailboxId: destinationMailboxId,
749
- status: MessageStatus.moving,
750
- syncStatus: MessageSyncStatus.pending,
751
- abandonedMutation: MessageMutation.none,
752
- originalMailboxId: sourceMailbox.mailboxId,
753
- originalUid: message.uid,
754
- },
755
- );
756
- if (!moved) {
757
- this.log.error(
758
- { accountId, messageId, destinationMailboxId },
759
- "Refused: this message's placement changed while the move was being prepared",
760
- );
761
- throw new MessagePlacementUnsettledError(
762
- `Message ${messageId} was not moved: something else changed where it is while this move was being prepared`,
763
- accountId,
764
- messageId,
765
- "in_flight",
766
- );
767
- }
768
-
769
- // Update ThreadMessage
770
- await this.updateThreadMessageForMove(
771
- accountConfigId,
772
- messageId,
773
- destinationMailboxId,
774
- isMovingToTrash,
775
- );
776
-
777
- await this.addressService.reconcileJunkOnlyForMessage(
778
- messageId,
779
- await this.mailboxSpecialUseService.resolveJunkRolesForConfig(
780
- accountConfigId,
781
- ),
782
- );
783
-
784
- // If moving FROM Trash, clear isDeleted
785
- if (isMovingFromTrash) {
786
- await this.updateThreadMessageDeleted(accountConfigId, messageId, false);
787
- }
788
-
789
- this.log.info(
790
- {
791
- messageId,
792
- from: sourceMailbox.fullPath,
793
- to: destinationMailbox.fullPath,
794
- },
795
- "Moved message (local)",
796
- );
797
-
798
- // Enqueue IMAP sync
799
- const event: MessageMoveEvent = {
800
- type: "MESSAGE_MOVE",
801
- eventId: randomUUID(),
802
- timestamp: Date.now(),
803
- accountId,
804
- messageId,
805
- sourceMailboxId: sourceMailbox.mailboxId,
806
- sourceMailboxPath: sourceMailbox.fullPath,
807
- destinationMailboxId,
808
- destinationMailboxPath: destinationMailbox.fullPath,
809
- uid: message.uid,
810
- };
811
-
812
- await this.enqueueEvent(event, () =>
813
- this.handBackUnsentMove(
814
- accountConfigId,
815
- messageId,
816
- sourceMailbox.mailboxId,
817
- message.uid,
818
- ),
819
- );
820
- };
821
-
822
- /**
823
- * Move multiple messages to another mailbox.
824
- *
825
- * @param messageIds - Messages to move
826
- * @param destinationMailboxId - Destination mailbox ID
827
- * @param accountId - Account ID
828
- */
829
- moveMessages = async (
830
- accountConfigId: string,
831
- messageIds: string[],
832
- destinationMailboxId: string,
833
- accountId: string,
834
- ): Promise<void> => {
835
- // Every row is settled before the first move is written, so the batch's
836
- // wait is one row's ceiling rather than the sum across it, and a refusal
837
- // lands before any part of the batch has committed.
838
- const sources = await this.settledPlacements(
839
- await this.messageService.get(messageIds),
840
- accountId,
841
- );
842
-
843
- for (const source of sources) {
844
- await this.moveSettledMessage(
845
- accountConfigId,
846
- source,
847
- destinationMailboxId,
848
- accountId,
849
- );
850
- }
851
- };
852
-
853
- /**
854
- * Copy a message to another mailbox.
855
- * Creates a new message record locally and enqueues IMAP COPY.
856
- *
857
- * @param messageId - Message to copy
858
- * @param destinationMailboxId - Destination mailbox ID
859
- * @param accountId - Account ID
860
- * @returns The new message ID for the copy
861
- */
862
- copyMessage = async (
863
- accountConfigId: string,
864
- messageId: string,
865
- destinationMailboxId: string,
866
- accountId: string,
867
- ): Promise<string> => {
868
- return this.copySettledMessage(
869
- accountConfigId,
870
- await this.settledPlacement(
871
- await this.messageService.get(messageId),
872
- accountId,
873
- ),
874
- destinationMailboxId,
875
- accountId,
876
- );
877
- };
878
-
879
- private copySettledMessage = async (
880
- accountConfigId: string,
881
- sourceMessage: MessageItem,
882
- destinationMailboxId: string,
883
- accountId: string,
884
- ): Promise<string> => {
885
- const messageId = sourceMessage.messageId;
886
- const sourceMailbox = await this.mailboxService.get(
887
- accountId,
888
- sourceMessage.mailboxId,
889
- );
890
- const destinationMailbox = await this.mailboxService.get(
891
- accountId,
892
- destinationMailboxId,
893
- );
894
-
895
- // Deterministic identity for the copy: derived from the source message and
896
- // the destination mailbox, so a replayed COPY event or a repeated copy
897
- // resolves to the same row instead of a fresh unreachable duplicate. A
898
- // random id (the prior behaviour) left rows no sync or delete could reach
899
- // (issue #75).
900
- const newMessageId = deriveCopyMessageId(messageId, destinationMailboxId);
901
-
902
- // Upsert, not create: the id is deterministic, so a repeated copy or a
903
- // retry after a partial failure (row written, event never delivered)
904
- // re-derives the same id. create throws on the existing row and strands
905
- // the copy at uid=0 with an undelivered event; upsert no-ops on the row
906
- // and lets the event be re-enqueued below, keeping the operation
907
- // idempotent and the copy re-drivable (issue #75).
908
- const copyRow = await this.messageService.upsertWithStatus({
909
- messageId: newMessageId,
910
- mailboxId: destinationMailboxId,
911
- uid: 0, // Will be updated by worker after IMAP COPY
912
- sequenceNumber: 0, // Will be updated by worker
913
- rfc822Size: sourceMessage.rfc822Size,
914
- internalDate: sourceMessage.internalDate,
915
- envelopeId: sourceMessage.envelopeId, // Share envelope with source
916
- rootBodyPartId: sourceMessage.rootBodyPartId, // Share body parts with source
917
- status: MessageStatus.moving,
918
- syncStatus: MessageSyncStatus.pending,
919
- bodyStorageKey: sourceMessage.bodyStorageKey,
920
- // The copy inherits the source's stored body, so body-sync's skip guard
921
- // will never re-derive these. Carry them across or the copy is
922
- // permanently `uncategorized` while its body says it is fully synced
923
- // (issue #45).
924
- category: sourceMessage.category,
925
- // The record of whether that category was the classifier's answer travels
926
- // with it (issue #331). Defaulting the copy to `NotExamined` would put a
927
- // row no classifier will ever reach — its body is already stored — into
928
- // the cohort a backfill selects as never examined.
929
- classificationState: sourceMessage.classificationState,
930
- hasListUnsubscribe: sourceMessage.hasListUnsubscribe,
931
- });
932
-
933
- // Copy ThreadMessage entry
934
- const sourceThreadMessage = await this.threadMessageService.getByMessageId(
935
- accountConfigId,
936
- messageId,
937
- );
938
-
939
- const copyListing = await this.threadMessageService.create({
940
- accountConfigId: sourceThreadMessage.accountConfigId,
941
- threadId: sourceThreadMessage.threadId,
942
- messageId: newMessageId,
943
- mailboxId: destinationMailboxId,
944
- uid: 0, // Will be updated by worker
945
- messageIdHeader: sourceThreadMessage.messageIdHeader,
946
- inReplyTo: sourceThreadMessage.inReplyTo,
947
- referenceOrder: sourceThreadMessage.referenceOrder,
948
- fromEmail: sourceThreadMessage.fromEmail,
949
- fromName: sourceThreadMessage.fromName,
950
- subject: sourceThreadMessage.subject,
951
- internalDate: sourceThreadMessage.internalDate,
952
- sentDate: sourceThreadMessage.sentDate,
953
- isRead: sourceThreadMessage.isRead,
954
- hasAttachment: sourceThreadMessage.hasAttachment,
955
- star: sourceThreadMessage.star,
956
- hasStars: sourceThreadMessage.hasStars,
957
- isDeleted: false,
958
- snippet: sourceThreadMessage.snippet,
959
- category: sourceThreadMessage.category,
960
- // Same reason as `category` above, and the same source row: the copy
961
- // inherits a stored body, so no classifying pass ever runs over it to
962
- // derive a `listId` of its own (issue #331).
963
- listId: sourceThreadMessage.listId,
964
- });
965
-
966
- this.log.info(
967
- {
968
- sourceMessageId: messageId,
969
- newMessageId,
970
- from: sourceMailbox.fullPath,
971
- to: destinationMailbox.fullPath,
972
- },
973
- "Created message copy (local)",
974
- );
975
-
976
- // Enqueue IMAP sync
977
- const event: MessageCopyEvent = {
978
- type: "MESSAGE_COPY",
979
- eventId: randomUUID(),
980
- timestamp: Date.now(),
981
- accountId,
982
- sourceMessageId: messageId,
983
- newMessageId,
984
- sourceMailboxId: sourceMailbox.mailboxId,
985
- sourceMailboxPath: sourceMailbox.fullPath,
986
- destinationMailboxId,
987
- destinationMailboxPath: destinationMailbox.fullPath,
988
- uid: sourceMessage.uid,
989
- };
990
-
991
- await this.enqueueEvent(event, async () => {
992
- await this.threadMessageService.deleteMany([
993
- {
994
- accountConfigId: copyListing.accountConfigId,
995
- threadMessageId: copyListing.threadMessageId,
996
- },
997
- ]);
998
- if (!copyRow.created) return;
999
- await this.messageService.deleteIfPlacement(newMessageId, {
1000
- status: MessageStatus.moving,
1001
- mailboxId: destinationMailboxId,
1002
- uid: 0,
1003
- });
1004
- });
1005
-
1006
- return newMessageId;
1007
- };
1008
-
1009
- /**
1010
- * Copy multiple messages to another mailbox.
1011
- *
1012
- * @param messageIds - Messages to copy
1013
- * @param destinationMailboxId - Destination mailbox ID
1014
- * @param accountId - Account ID
1015
- * @returns Array of new message IDs for the copies
1016
- */
1017
- copyMessages = async (
1018
- accountConfigId: string,
1019
- messageIds: string[],
1020
- destinationMailboxId: string,
1021
- accountId: string,
1022
- ): Promise<string[]> => {
1023
- // Every row is settled before the first copy is written, so the batch's
1024
- // wait is one row's ceiling rather than the sum across it, and a refusal
1025
- // lands before any part of the batch has committed.
1026
- const sources = await this.settledPlacements(
1027
- await this.messageService.get(messageIds),
1028
- accountId,
1029
- );
1030
-
1031
- const newMessageIds: string[] = [];
1032
- for (const source of sources) {
1033
- newMessageIds.push(
1034
- await this.copySettledMessage(
1035
- accountConfigId,
1036
- source,
1037
- destinationMailboxId,
1038
- accountId,
1039
- ),
1040
- );
1041
- }
1042
- return newMessageIds;
1043
- };
1044
-
1045
- /**
1046
- * Restore a message from Trash to its original mailbox.
1047
- *
1048
- * @param messageId - Message to restore
1049
- * @param accountId - Account ID
1050
- */
1051
- restoreMessage = async (
1052
- accountConfigId: string,
1053
- messageId: string,
1054
- accountId: string,
1055
- ): Promise<void> => {
1056
- const message = await this.messageService.get(messageId);
1057
-
1058
- if (!message.originalMailboxId) {
1059
- throw new Error("Message has no original mailbox to restore to");
1060
- }
1061
-
1062
- await this.moveMessage(
1063
- accountConfigId,
1064
- messageId,
1065
- message.originalMailboxId,
1066
- accountId,
1067
- );
1068
- };
1069
-
1070
- /**
1071
- * Empty the Trash mailbox: an EXPUNGE of everything in it, with no undo.
1072
- *
1073
- * Demands confirmed evidence: the folder the user appointed or the one the
1074
- * server flagged \Trash — never one that merely reads like a trash folder,
1075
- * and never the fallback behind an appointment that went missing. A user
1076
- * with an ordinary folder called `Deleted` would otherwise lose its contents
1077
- * permanently. Each of the three ways that evidence can be absent refuses
1078
- * under its own reason, so the surface can name the remedy instead of
1079
- * offering a silent no-op.
1080
- *
1081
- * Reports the rows it marked, read once here, so the number the caller shows
1082
- * a user is the number this operation acted on. Pressed twice before the
1083
- * worker runs it re-marks the same rows and reports the same number: the
1084
- * folder really does still hold them, and the second expunge finds nothing
1085
- * to do.
1086
- */
1087
- emptyTrash = async (
1088
- accountConfigId: string,
1089
- accountId: string,
1090
- ): Promise<{ deletedCount: number }> => {
1091
- const trashMailbox = requireTrashMailbox(
1092
- await this.mailboxSpecialUseService.resolveTrashRole(accountId),
1093
- "confirmed",
1094
- accountId,
1095
- );
1096
-
1097
- // The identity the user consented to, carried on the event so the worker
1098
- // can tell this folder from a different one that has since taken over its
1099
- // path. Read here rather than re-read there: the value at consent time is
1100
- // the whole point, and the stored row is rewritten by every sync.
1101
- const { uidValidity: trashUidValidity } = await this.mailboxService.get(
1102
- accountId,
1103
- trashMailbox.mailboxId,
1104
- );
1105
-
1106
- const rows = await this.messageService.listAllByMailbox(
1107
- trashMailbox.mailboxId,
1108
- );
1109
-
1110
- // Reconciles, never waits (imap-mutations R2). A row whose move into
1111
- // Trash has not settled names this folder while still carrying the SOURCE
1112
- // folder's uid, and marking it `deleting` overwrites the only marker that
1113
- // says so: its own MESSAGE_MOVE then reads the row as settled and returns
1114
- // without moving anything, and the worker's expunge binds that borrowed
1115
- // uid against whatever Trash really holds at it. Left as it stands, that
1116
- // move settles the row on its own, so the message is in Trash by the time
1117
- // the sweep reaches it and goes with the rest of this same press — only
1118
- // the count below is one short of what the sweep ends up removing.
1119
- //
1120
- // `carriesForeignUid`, the same predicate the sweep applies (#1217).
1121
- // The binding form reads `status` too, and `status` is exactly what this
1122
- // mark is about to overwrite, so the two halves of one gate would have
1123
- // disagreed the moment the mark landed.
1124
- const candidates = rows.filter((message) => !carriesForeignUid(message));
1125
-
1126
- // Each mark is a transition off the row as it was just read
1127
- // (imap-mutations R3), not a blind write onto a snapshot. The rows are
1128
- // held by nothing between the listing and here, and PLACEMENT_MOVE_PUSH
1129
- // runs on a standard queue that this account's FIFO group does not order,
1130
- // so a row can move out from under the sweep. A lost predicate means
1131
- // another lane won: the row is left alone and not counted, rather than
1132
- // marked `deleting` for an expunge that would bind somebody else's uid.
1133
- const messages: MessageItem[] = [];
1134
- const undo: Array<() => Promise<void>> = [];
1135
- for (const message of candidates) {
1136
- const marked = await this.messageService.transitionPlacement(
1137
- message.messageId,
1138
- {
1139
- status: message.status,
1140
- mailboxId: message.mailboxId,
1141
- uid: message.uid,
1142
- },
1143
- {
1144
- status: MessageStatus.deleting,
1145
- syncStatus: MessageSyncStatus.pending,
1146
- },
1147
- );
1148
- if (!marked) {
1149
- this.log.info(
1150
- { accountId, messageId: message.messageId },
1151
- "Empty Trash skipped a message whose placement changed while the folder was being read",
1152
- );
1153
- continue;
1154
- }
1155
- messages.push(marked);
1156
- const wasDeleted = await this.updateThreadMessageDeleted(
1157
- accountConfigId,
1158
- message.messageId,
1159
- true,
1160
- );
1161
- undo.push(async () => {
1162
- const restored = await this.messageService.transitionPlacement(
1163
- message.messageId,
1164
- {
1165
- status: MessageStatus.deleting,
1166
- mailboxId: message.mailboxId,
1167
- uid: message.uid,
1168
- },
1169
- { status: message.status, syncStatus: message.syncStatus },
1170
- );
1171
- if (restored && !wasDeleted) {
1172
- await this.updateThreadMessageDeleted(
1173
- accountConfigId,
1174
- message.messageId,
1175
- false,
1176
- );
1177
- }
1178
- });
1179
- }
1180
-
1181
- this.log.info(
1182
- {
1183
- accountId,
1184
- trashMailboxId: trashMailbox.mailboxId,
1185
- count: messages.length,
1186
- skipped: rows.length - messages.length,
1187
- },
1188
- "Marked all trash messages for deletion (local)",
1189
- );
1190
-
1191
- // Enqueue single event for worker to handle batch
1192
- const event: EmptyTrashEvent = {
1193
- type: "EMPTY_TRASH",
1194
- schemaVersion: MUTATION_EVENT_SCHEMA_VERSION,
1195
- eventId: randomUUID(),
1196
- timestamp: Date.now(),
1197
- accountId,
1198
- trashMailboxId: trashMailbox.mailboxId,
1199
- trashMailboxPath: trashMailbox.fullPath,
1200
- trashUidValidity,
1201
- };
1202
-
1203
- await this.enqueueEvent(event, async () => {
1204
- for (const restore of undo) await restore();
1205
- });
1206
-
1207
- return { deletedCount: messages.length };
1208
- };
1209
-
1210
- private refusePlacement = (
1211
- message: MessageItem,
1212
- accountId: string,
1213
- ): never => {
1214
- this.log.error(
1215
- { accountId, messageId: message.messageId },
1216
- "Refused: this message's folder and uid do not name the same message",
1217
- );
1218
- throw new MessagePlacementUnsettledError(
1219
- `Message ${message.messageId} was not acted on: its folder and uid do not name the same message`,
1220
- accountId,
1221
- message.messageId,
1222
- "in_flight",
1223
- );
1224
- };
1225
-
1226
- private settledPlacement = async (
1227
- message: MessageItem,
1228
- accountId: string,
1229
- ): Promise<MessageItem> => {
1230
- if (placementBindingOf(message) === "consistent") return message;
1231
-
1232
- const settled = await waitForPlacementToSettle(
1233
- this.messageService,
1234
- message.messageId,
1235
- {
1236
- timeoutMs: this.moveSettleTimeoutMs,
1237
- pollMs: this.moveSettlePollMs,
1238
- },
1239
- );
1240
- if (placementBindingOf(settled) === "consistent") return settled;
1241
- return this.refusePlacement(settled, accountId);
1242
- };
1243
-
1244
- /**
1245
- * Settle a whole batch before any of it is acted on, so one unsettled row
1246
- * refuses the batch whole rather than leaving part of it expunged, and the
1247
- * batch's total wait is one row's ceiling rather than the sum across it.
1248
- */
1249
- private settledPlacements = async (
1250
- messages: MessageItem[],
1251
- accountId: string,
1252
- ): Promise<MessageItem[]> =>
1253
- Promise.all(
1254
- messages.map((message) => this.settledPlacement(message, accountId)),
1255
- );
1256
-
1257
- /**
1258
- * Update ThreadMessage for move operations.
1259
- * Updates mailboxId and optionally isDeleted.
1260
- */
1261
- private updateThreadMessageForMove = async (
1262
- accountConfigId: string,
1263
- messageId: string,
1264
- newMailboxId: string,
1265
- isDeleted: boolean,
1266
- ): Promise<void> => {
1267
- const threadMessage = await this.threadMessageService.getByMessageId(
1268
- accountConfigId,
1269
- messageId,
1270
- );
1271
-
1272
- this.log.info(
1273
- {
1274
- messageId,
1275
- accountConfigId: threadMessage.accountConfigId,
1276
- threadMessageId: threadMessage.threadMessageId,
1277
- sentDate: threadMessage.sentDate,
1278
- newMailboxId,
1279
- isDeleted,
1280
- },
1281
- "Updating ThreadMessage for move",
1282
- );
1283
-
1284
- await this.threadMessageService.update(
1285
- threadMessage.accountConfigId,
1286
- threadMessage.threadMessageId,
1287
- { mailboxId: newMailboxId, isDeleted },
1288
- {
1289
- // Composites contain CURRENT values for condition checking
1290
- // ElectroDB uses these to verify the item state before updating
1291
- composites: {
1292
- mailboxId: threadMessage.mailboxId,
1293
- sentDate: threadMessage.sentDate,
1294
- isRead: threadMessage.isRead,
1295
- isDeleted: threadMessage.isDeleted,
1296
- hasStars: threadMessage.hasStars,
1297
- hasAttachment: threadMessage.hasAttachment,
1298
- },
1299
- },
1300
- );
1301
-
1302
- this.log.info(
1303
- {
1304
- messageId,
1305
- threadMessageId: threadMessage.threadMessageId,
1306
- newMailboxId,
1307
- isDeleted,
1308
- },
1309
- "Updated ThreadMessage for move",
1310
- );
1311
- };
1312
-
1313
- /**
1314
- * Delete every ThreadMessage row that points at this messageId.
1315
- *
1316
- * A row is keyed by (threadId, messageId), so this is one row in practice —
1317
- * a message filed in several folders keeps a single row, because its
1318
- * messageId is derived from the Message-ID header and not from the mailbox.
1319
- * The query stays a list because the key permits more than one thread per
1320
- * message and nothing enforces otherwise.
1321
- *
1322
- * Deleting up-front in the permanent-delete optimistic step prevents stale
1323
- * rows from leaking into mailbox listings while IMAP catches up (#212).
1324
- */
1325
- private deleteThreadMessagesForMessage = async (
1326
- accountConfigId: string,
1327
- messageId: string,
1328
- ): Promise<void> => {
1329
- const rows = await this.threadMessageService.findAllByMessageId(
1330
- accountConfigId,
1331
- messageId,
1332
- );
1333
- for (const row of rows) {
1334
- await this.threadMessageService.delete(
1335
- row.accountConfigId,
1336
- row.threadMessageId,
1337
- );
1338
- }
1339
- this.log.info(
1340
- { messageId, deletedRows: rows.length },
1341
- "Deleted ThreadMessage rows for permanent-delete",
1342
- );
1343
- };
1344
-
1345
- /**
1346
- * Update ThreadMessage.isDeleted flag only.
1347
- */
1348
- private updateThreadMessageDeleted = async (
1349
- accountConfigId: string,
1350
- messageId: string,
1351
- isDeleted: boolean,
1352
- ): Promise<boolean> => {
1353
- const threadMessage = await this.threadMessageService.getByMessageId(
1354
- accountConfigId,
1355
- messageId,
1356
- );
1357
-
1358
- await this.threadMessageService.update(
1359
- threadMessage.accountConfigId,
1360
- threadMessage.threadMessageId,
1361
- { isDeleted },
1362
- {
1363
- // Composites contain CURRENT values for condition checking
1364
- composites: {
1365
- sentDate: threadMessage.sentDate,
1366
- mailboxId: threadMessage.mailboxId,
1367
- isRead: threadMessage.isRead,
1368
- isDeleted: threadMessage.isDeleted,
1369
- hasStars: threadMessage.hasStars,
1370
- hasAttachment: threadMessage.hasAttachment,
1371
- },
1372
- },
1373
- );
1374
-
1375
- this.log.info(
1376
- {
1377
- messageId,
1378
- threadMessageId: threadMessage.threadMessageId,
1379
- isDeleted,
1380
- },
1381
- "Updated ThreadMessage.isDeleted",
1382
- );
1383
- return threadMessage.isDeleted;
1384
- };
1385
-
1386
- /**
1387
- * Enqueue a message move/delete event to SQS.
1388
- *
1389
- * FIFO queues require MessageGroupId; standard queues reject it. We detect
1390
- * FIFO queues by the `.fifo` suffix and group by accountId so events for
1391
- * different accounts can be processed in parallel.
1392
- */
1393
- private enqueueEvent = async (
1394
- event: MessageMoveQueueEvent,
1395
- handBack: () => Promise<void>,
1396
- ): Promise<void> => {
1397
- const useFifo = this.queueUrl.endsWith(".fifo");
1398
- await this.sqs
1399
- .send(
1400
- new SendMessageCommand({
1401
- QueueUrl: this.queueUrl,
1402
- MessageBody: JSON.stringify(event),
1403
- ...(useFifo && {
1404
- MessageGroupId: event.accountId,
1405
- MessageDeduplicationId: event.eventId,
1406
- }),
1407
- }),
1408
- )
1409
- .catch((fault: unknown) => handBackThenThrow(fault, [handBack]));
1410
-
1411
- this.log.info(
1412
- { eventId: event.eventId, type: event.type },
1413
- "Enqueued message event",
1414
- );
1415
- };
1416
-
1417
- /**
1418
- * Enqueue multiple events to SQS using batch send.
1419
- * SQS batch limit is 10 messages, so we chunk if needed.
1420
- *
1421
- * FIFO queues require MessageGroupId on each batch entry; standard queues
1422
- * reject it. We detect FIFO queues by the `.fifo` suffix.
1423
- */
1424
- private enqueueEventsBatch = async (
1425
- events: UndoableEvent<MessageDeleteEvent>[],
1426
- ): Promise<void> => {
1427
- const enqueued = await this.sendEventsBatch(events);
1428
- if (enqueued.outcome === "sent") return;
1429
- await handBackThenThrow(
1430
- enqueued.fault,
1431
- enqueued.unsent.map(({ handBack }) => handBack),
1432
- );
1433
- };
1434
-
1435
- // A batch send answers 200 with per-entry refusals in `Failed`.
1436
- private sendEventsBatch = async (
1437
- events: UndoableEvent<MessageDeleteEvent>[],
1438
- ): Promise<BatchEnqueueOutcome> => {
1439
- const SQS_BATCH_SIZE = 10;
1440
- const useFifo = this.queueUrl.endsWith(".fifo");
1441
-
1442
- for (let i = 0; i < events.length; i += SQS_BATCH_SIZE) {
1443
- const batch = events.slice(i, i + SQS_BATCH_SIZE);
1444
- const sent = await this.sqs
1445
- .send(
1446
- new SendMessageBatchCommand({
1447
- QueueUrl: this.queueUrl,
1448
- Entries: batch.map(({ event }, idx) => ({
1449
- Id: `${i + idx}`,
1450
- MessageBody: JSON.stringify(event),
1451
- ...(useFifo && {
1452
- MessageGroupId: event.accountId,
1453
- MessageDeduplicationId: event.eventId,
1454
- }),
1455
- })),
1456
- }),
1457
- )
1458
- .then(
1459
- (result) => ({
1460
- refusedIds: new Set((result.Failed ?? []).map((entry) => entry.Id)),
1461
- }),
1462
- (fault: unknown) => ({ fault }),
1463
- );
1464
- if ("fault" in sent) {
1465
- return {
1466
- outcome: "unsent",
1467
- unsent: events.slice(i),
1468
- fault: sent.fault,
1469
- };
1470
- }
1471
- if (sent.refusedIds.size > 0) {
1472
- const refused = batch.filter((_, idx) =>
1473
- sent.refusedIds.has(`${i + idx}`),
1474
- );
1475
- return {
1476
- outcome: "unsent",
1477
- unsent: [...refused, ...events.slice(i + SQS_BATCH_SIZE)],
1478
- fault: new Error(
1479
- `The queue refused ${refused.length} of ${events.length} delete event(s)`,
1480
- ),
1481
- };
1482
- }
1483
- }
1484
-
1485
- if (events.length > 0) {
1486
- this.log.info({ count: events.length }, "Enqueued message events batch");
1487
- }
1488
- return { outcome: "sent" };
1489
- };
1490
-
1491
- private handBackUnsentMove = async (
1492
- accountConfigId: string,
1493
- messageId: string,
1494
- sourceMailboxId: string,
1495
- uid: number,
1496
- ): Promise<void> => {
1497
- await restoreSourcePlacement(
1498
- {
1499
- messageService: this.messageService,
1500
- threadMessageService: this.threadMessageService,
1501
- },
1502
- {
1503
- accountConfigId,
1504
- messageId,
1505
- sourceMailboxId,
1506
- uid,
1507
- syncStatus: MessageSyncStatus.synced,
1508
- abandonedMutation: MessageMutation.none,
1509
- },
1510
- );
1511
- await this.addressService.reconcileJunkOnlyForMessage(
1512
- messageId,
1513
- await this.mailboxSpecialUseService.resolveJunkRolesForConfig(
1514
- accountConfigId,
1515
- ),
1516
- );
1517
- };
1518
- }