@remit/backend 0.0.1

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 (87) hide show
  1. package/.env.test +7 -0
  2. package/dev-server/content-auth.test.ts +113 -0
  3. package/dev-server/content-auth.ts +54 -0
  4. package/dev-server/content-handler.test.ts +105 -0
  5. package/dev-server/content-handler.ts +79 -0
  6. package/dev-server/content-path.test.ts +37 -0
  7. package/dev-server/content-path.ts +27 -0
  8. package/dev-server/cors.test.ts +48 -0
  9. package/dev-server/cors.ts +25 -0
  10. package/dev-server/lambda-helpers.ts +69 -0
  11. package/dev-server/server.ts +289 -0
  12. package/package.json +82 -0
  13. package/src/auth.ts +92 -0
  14. package/src/config/msoauth.ts +60 -0
  15. package/src/data-backend.test.ts +25 -0
  16. package/src/data-backend.ts +20 -0
  17. package/src/derive/autoMoved.ts +28 -0
  18. package/src/derive/contentSignature.test.ts +153 -0
  19. package/src/derive/contentSignature.ts +139 -0
  20. package/src/derive/contentUrl.test.ts +156 -0
  21. package/src/derive/contentUrl.ts +70 -0
  22. package/src/derive/enrichThreadRows.ts +155 -0
  23. package/src/derive/filterThreadCriteria.test.ts +119 -0
  24. package/src/derive/filterThreadCriteria.ts +60 -0
  25. package/src/derive/pendingMoveCounts.ts +90 -0
  26. package/src/derive/senderTrust.test.ts +67 -0
  27. package/src/derive/senderTrust.ts +23 -0
  28. package/src/error.ts +55 -0
  29. package/src/handlers/account-guards.ts +137 -0
  30. package/src/handlers/account-oauth.test.ts +383 -0
  31. package/src/handlers/account-oauth.ts +452 -0
  32. package/src/handlers/account-overrides.test.ts +69 -0
  33. package/src/handlers/account-overrides.ts +286 -0
  34. package/src/handlers/account-ownership.ts +25 -0
  35. package/src/handlers/account-signature.ts +129 -0
  36. package/src/handlers/account.ts +524 -0
  37. package/src/handlers/address.ts +136 -0
  38. package/src/handlers/config.test.ts +184 -0
  39. package/src/handlers/config.ts +219 -0
  40. package/src/handlers/ensure-account-config.ts +30 -0
  41. package/src/handlers/filter.ts +294 -0
  42. package/src/handlers/folder-role-appointments.test.ts +250 -0
  43. package/src/handlers/folder-role-appointments.ts +272 -0
  44. package/src/handlers/folder-role.ts +76 -0
  45. package/src/handlers/index.ts +48 -0
  46. package/src/handlers/mailbox.ts +411 -0
  47. package/src/handlers/me.ts +190 -0
  48. package/src/handlers/message.ts +886 -0
  49. package/src/handlers/organize.test.ts +43 -0
  50. package/src/handlers/organize.ts +196 -0
  51. package/src/handlers/outbox.ts +218 -0
  52. package/src/handlers/search.test.ts +182 -0
  53. package/src/handlers/search.ts +105 -0
  54. package/src/handlers/sync-progress.test.ts +158 -0
  55. package/src/handlers/sync-progress.ts +64 -0
  56. package/src/handlers/sync.test.ts +146 -0
  57. package/src/handlers/sync.ts +119 -0
  58. package/src/handlers/thread.ts +361 -0
  59. package/src/handlers/unified-threads.ts +196 -0
  60. package/src/handlers/vip-suggestions.ts +21 -0
  61. package/src/index.ts +170 -0
  62. package/src/json.ts +11 -0
  63. package/src/jwt-auth.test.ts +89 -0
  64. package/src/jwt-auth.ts +95 -0
  65. package/src/request-context.test.ts +61 -0
  66. package/src/request-context.ts +29 -0
  67. package/src/request.ts +10 -0
  68. package/src/response.test.ts +107 -0
  69. package/src/response.ts +80 -0
  70. package/src/service/compose-postgres.ts +72 -0
  71. package/src/service/compose-sqlite.ts +80 -0
  72. package/src/service/create-remit-client.ts +358 -0
  73. package/src/service/dynamodb.test.ts +100 -0
  74. package/src/service/dynamodb.ts +58 -0
  75. package/src/service/filter.ts +55 -0
  76. package/src/service/fire-and-forget.test.ts +126 -0
  77. package/src/service/fire-and-forget.ts +79 -0
  78. package/src/service/localhost-env-config.test.ts +40 -0
  79. package/src/service/organize.test.ts +384 -0
  80. package/src/service/organize.ts +354 -0
  81. package/src/service/semantic-capability.test.ts +64 -0
  82. package/src/service/semantic-capability.ts +62 -0
  83. package/src/service/sqs.ts +4 -0
  84. package/src/service/trigger-sync.test.ts +211 -0
  85. package/src/service/trigger-sync.ts +102 -0
  86. package/src/types.ts +161 -0
  87. package/tsconfig.json +7 -0
@@ -0,0 +1,886 @@
1
+ import type {
2
+ BodyPartResponse,
3
+ EnvelopeAddressResponse,
4
+ EnvelopeResponse,
5
+ MessageSummaryResponse,
6
+ } from "@remit/api-openapi-types";
7
+ import type { MailboxItem } from "@remit/data-ports";
8
+ import {
9
+ BadRequestError,
10
+ ForbiddenError,
11
+ NotFoundError,
12
+ UnrecoverableBodyError,
13
+ } from "@remit/data-ports/errors";
14
+ import {
15
+ ContentDisposition,
16
+ MediaType,
17
+ MessageCategory,
18
+ SenderTrust,
19
+ type StarColor,
20
+ } from "@remit/domain-enums";
21
+ import { logger } from "@remit/logger-lambda";
22
+ import {
23
+ guardConnectionCursor,
24
+ isCursorRebuildNeeded,
25
+ isMessageBodySyncBroken,
26
+ MailboxCursorPausedError,
27
+ } from "@remit/mailbox-service";
28
+ import {
29
+ isStorageNotFoundError as isStorageNotFoundErrorFromService,
30
+ parseStorageUri,
31
+ } from "@remit/storage-service";
32
+ import type { APIGatewayProxyEvent } from "aws-lambda";
33
+ import type { Context } from "openapi-backend";
34
+ import { getAccountConfigIdFromEvent } from "../auth.js";
35
+ import { deriveAutoMoved } from "../derive/autoMoved.js";
36
+ import {
37
+ type ContentSigner,
38
+ getContentSigner,
39
+ } from "../derive/contentSignature.js";
40
+ import {
41
+ buildContentUrl,
42
+ getContentDeliveryDomain,
43
+ } from "../derive/contentUrl.js";
44
+ import { deriveSenderTrust } from "../derive/senderTrust.js";
45
+ import { getClient } from "../service/dynamodb.js";
46
+ import type {
47
+ MessageBulkOperationIds,
48
+ MessageOperationIds,
49
+ OperationHandler,
50
+ } from "../types.js";
51
+
52
+ type StarColorValue = (typeof StarColor)[keyof typeof StarColor];
53
+
54
+ interface MessageOwnershipClient {
55
+ message: { get(messageId: string): Promise<{ mailboxId: string }> };
56
+ mailbox: {
57
+ get(
58
+ accountId: string,
59
+ mailboxIds: string[],
60
+ ): Promise<{ mailboxId: string; accountId: string }[]>;
61
+ };
62
+ account: {
63
+ listAllByAccountConfig(
64
+ accountConfigId: string,
65
+ ): Promise<{ accountId: string }[]>;
66
+ };
67
+ }
68
+
69
+ /**
70
+ * Resolve the account that owns every message in `messageIds` and assert the
71
+ * caller owns it. Messages have no tenant column, so ownership resolves through
72
+ * the mailbox: the caller's own accounts (from their accountConfigId) scope a
73
+ * mailbox lookup, and a mailbox resolves only under the account that owns it —
74
+ * a foreign message never resolves and throws before any content is returned or
75
+ * mutated (`read` -> 404, no existence leak; `act` -> 403). The tenant comes
76
+ * from the caller's config, never from the row being read. The whole batch must
77
+ * resolve to a single account — the downstream flag/move/delete services take
78
+ * one accountId — so a batch spanning accounts is rejected. Returns that
79
+ * accountId so callers reuse it without another lookup.
80
+ */
81
+ export const assertMessagesOwned = async (
82
+ client: MessageOwnershipClient,
83
+ messageIds: string[],
84
+ callerAccountConfigId: string,
85
+ mode: "read" | "act",
86
+ ): Promise<string> => {
87
+ const messages = await Promise.all(
88
+ messageIds.map((id) => client.message.get(id)),
89
+ );
90
+ const mailboxIds = [...new Set(messages.map((m) => m.mailboxId))];
91
+
92
+ const ownedAccounts = await client.account.listAllByAccountConfig(
93
+ callerAccountConfigId,
94
+ );
95
+
96
+ const accountByMailbox = new Map<string, string>();
97
+ for (const { accountId } of ownedAccounts) {
98
+ const rows = await client.mailbox.get(accountId, mailboxIds);
99
+ for (const row of rows) {
100
+ accountByMailbox.set(row.mailboxId, row.accountId);
101
+ }
102
+ }
103
+
104
+ if (mailboxIds.some((id) => !accountByMailbox.has(id))) {
105
+ if (mode === "read") {
106
+ throw new NotFoundError("Message not found");
107
+ }
108
+ throw new ForbiddenError("Message not in account config");
109
+ }
110
+
111
+ const accountIds = [...new Set(accountByMailbox.values())];
112
+ if (accountIds.length !== 1) {
113
+ throw new BadRequestError("All messages must belong to the same account");
114
+ }
115
+
116
+ return accountIds[0];
117
+ };
118
+
119
+ export const isStorageNotFoundError = (error: unknown): boolean =>
120
+ isStorageNotFoundErrorFromService(error);
121
+
122
+ /**
123
+ * Decode the raw `.eml` bytes into a string for inspection. RFC822/MIME
124
+ * sources are 7-bit/8-bit ASCII-compatible (non-ASCII bytes are encoded via
125
+ * quoted-printable / base64 inside the body), so `latin1` round-trips every
126
+ * byte 1:1 without loss or replacement characters — unlike `utf8`, which would
127
+ * mangle raw 8-bit bytes. Headers and structure stay byte-accurate.
128
+ */
129
+ export const decodeRawEml = (body: Buffer): string => body.toString("latin1");
130
+
131
+ /**
132
+ * Extract the accountConfigId + accountId segments from a bodyStorageKey URI
133
+ * such as
134
+ * `s3://bucket/accounts/{accountConfigId}/{accountId}/messages/{messageId}/body.eml`.
135
+ * Returns null when the URI shape doesn't match — caller falls back to the
136
+ * slow path without writing a parsed-body cache.
137
+ */
138
+ export const extractAccountIdsFromBodyKey = (
139
+ uri: string,
140
+ ): { accountConfigId: string; accountId: string } | null => {
141
+ const parsed = parseStorageUri(uri);
142
+ const match = parsed.storageKey.match(
143
+ /^accounts\/([^/]+)\/([^/]+)\/messages\//,
144
+ );
145
+ if (!match) return null;
146
+ return { accountConfigId: match[1], accountId: match[2] };
147
+ };
148
+
149
+ /**
150
+ * Map a list of stored `BodyPart` rows to API `BodyPartResponse` objects,
151
+ * populating `contentUrl` from the CloudFront distribution domain. Pure
152
+ * function so the URL-construction contract can be pinned in tests without
153
+ * standing up the full handler. The contract requires every `contentUrl`
154
+ * to be a real URL (#299) — `getContentDeliveryDomain` throws when the
155
+ * Lambda env is missing the domain, and this function expects a non-empty
156
+ * string from the caller.
157
+ */
158
+ export interface BodyPartLike {
159
+ bodyPartId: string;
160
+ mediaType: string;
161
+ mediaSubtype: string;
162
+ sizeOctets: number;
163
+ disposition?: string;
164
+ dispositionFilename?: string;
165
+ isMultipart: boolean;
166
+ contentId?: string;
167
+ partPath: string;
168
+ }
169
+
170
+ const MEDIA_TYPE_VALUES: ReadonlySet<string> = new Set(
171
+ Object.values(MediaType),
172
+ );
173
+ const CONTENT_DISPOSITION_VALUES: ReadonlySet<string> = new Set(
174
+ Object.values(ContentDisposition),
175
+ );
176
+
177
+ export const isMediaType = (
178
+ value: unknown,
179
+ ): value is BodyPartResponse["mediaType"] =>
180
+ typeof value === "string" && MEDIA_TYPE_VALUES.has(value);
181
+
182
+ export const isContentDisposition = (
183
+ value: unknown,
184
+ ): value is NonNullable<BodyPartResponse["disposition"]> =>
185
+ typeof value === "string" && CONTENT_DISPOSITION_VALUES.has(value);
186
+
187
+ const assertMediaType = (
188
+ value: string,
189
+ bodyPartId: string,
190
+ ): BodyPartResponse["mediaType"] => {
191
+ if (!isMediaType(value)) {
192
+ throw new Error(
193
+ `Invalid mediaType "${value}" on BodyPart ${bodyPartId}; expected one of ${[...MEDIA_TYPE_VALUES].join(", ")}`,
194
+ );
195
+ }
196
+ return value;
197
+ };
198
+
199
+ const assertDisposition = (
200
+ value: string | undefined,
201
+ bodyPartId: string,
202
+ ): BodyPartResponse["disposition"] => {
203
+ if (value === undefined) return undefined;
204
+ if (!isContentDisposition(value)) {
205
+ throw new Error(
206
+ `Invalid disposition "${value}" on BodyPart ${bodyPartId}; expected one of ${[...CONTENT_DISPOSITION_VALUES].join(", ")}`,
207
+ );
208
+ }
209
+ return value;
210
+ };
211
+
212
+ export const buildBodyPartResponses = (
213
+ parts: readonly BodyPartLike[],
214
+ context: {
215
+ contentDeliveryDomain: string;
216
+ accountConfigId: string;
217
+ accountId: string;
218
+ messageId: string;
219
+ sign?: ContentSigner;
220
+ },
221
+ ): BodyPartResponse[] => {
222
+ return parts.map((part) => ({
223
+ bodyPartId: part.bodyPartId,
224
+ mediaType: assertMediaType(part.mediaType, part.bodyPartId),
225
+ mediaSubtype: part.mediaSubtype,
226
+ sizeOctets: part.sizeOctets,
227
+ disposition: assertDisposition(part.disposition, part.bodyPartId),
228
+ dispositionFilename: part.dispositionFilename,
229
+ isMultipart: part.isMultipart,
230
+ contentId: part.contentId,
231
+ contentUrl: buildContentUrl({
232
+ domain: context.contentDeliveryDomain,
233
+ accountConfigId: context.accountConfigId,
234
+ accountId: context.accountId,
235
+ messageId: context.messageId,
236
+ partPath: part.partPath,
237
+ sign: context.sign,
238
+ }),
239
+ }));
240
+ };
241
+
242
+ /**
243
+ * The slice of `BodySyncService` the read path needs to lazily materialize
244
+ * deferred per-part objects. Narrowed to one method so the orchestration below
245
+ * can be unit-tested without standing up the whole client.
246
+ */
247
+ export interface BodyPartMaterializer {
248
+ ensureBodyPartsStored(
249
+ accountConfigId: string,
250
+ accountId: string,
251
+ messageId: string,
252
+ bodyStorageKey: string,
253
+ ): Promise<unknown>;
254
+ }
255
+
256
+ export interface MaterializeLogger {
257
+ warn(obj: Record<string, unknown>, msg: string): void;
258
+ error(obj: Record<string, unknown>, msg: string): void;
259
+ }
260
+
261
+ /**
262
+ * Re-arms the body-sync cue for one message (the read-path slice of
263
+ * `BodySyncQueueService`). Narrowed to one method so the orchestration below can
264
+ * be unit-tested without an SQS client.
265
+ */
266
+ export interface BodySyncCue {
267
+ requestBodySync(input: {
268
+ accountId: string;
269
+ mailboxId: string;
270
+ messageId: string;
271
+ uid?: number;
272
+ }): Promise<void>;
273
+ }
274
+
275
+ export interface MaterializeResult {
276
+ /** `false` when the body object was missing — the caller must not 200 the body. */
277
+ ready: boolean;
278
+ }
279
+
280
+ /**
281
+ * Materialize the deferred per-part storage objects for a message before the
282
+ * SPA fetches any `contentUrl`. Distinguishes the two failure modes the body
283
+ * contract cares about:
284
+ *
285
+ * - Missing storage object (never synced or lost) — re-arm the SYNC_MESSAGE_BODY
286
+ * cue so the worker (re)stores the body, and report `ready: false`. Not a
287
+ * 500: a retry succeeds once the worker catches up.
288
+ * - Any other error — rethrow so the request 500s and the bug is observable.
289
+ * Never swallowed, never "best-effort".
290
+ *
291
+ * No-op (`ready: true`) when the body isn't stored yet (no `bodyStorageKey`) or
292
+ * when the parts were written eagerly.
293
+ */
294
+ export const materializeBodyParts = async (
295
+ materializer: BodyPartMaterializer,
296
+ args: {
297
+ accountConfigId: string;
298
+ accountId: string;
299
+ mailboxId: string;
300
+ messageId: string;
301
+ uid?: number;
302
+ bodyStorageKey: string | undefined;
303
+ cue: BodySyncCue | undefined;
304
+ logger: MaterializeLogger;
305
+ },
306
+ ): Promise<MaterializeResult> => {
307
+ const { accountConfigId, accountId, mailboxId, messageId, uid } = args;
308
+ if (!args.bodyStorageKey) return { ready: true };
309
+
310
+ try {
311
+ await materializer.ensureBodyPartsStored(
312
+ accountConfigId,
313
+ accountId,
314
+ messageId,
315
+ args.bodyStorageKey,
316
+ );
317
+ return { ready: true };
318
+ } catch (error: unknown) {
319
+ if (!isStorageNotFoundError(error)) throw error;
320
+
321
+ args.logger.warn(
322
+ { messageId, accountId, mailboxId },
323
+ "Body object missing on read; re-arming body-sync cue and signalling retry",
324
+ );
325
+ await args.cue?.requestBodySync({ accountId, mailboxId, messageId, uid });
326
+ return { ready: false };
327
+ }
328
+ };
329
+
330
+ export const MessageOperations: Record<
331
+ MessageOperationIds,
332
+ OperationHandler<MessageOperationIds>
333
+ > = {
334
+ MessageOperations_describeMessage: async (
335
+ context: Context,
336
+ ...args: unknown[]
337
+ ) => {
338
+ const event = args[0] as APIGatewayProxyEvent;
339
+ const accountConfigId = getAccountConfigIdFromEvent(event);
340
+ const { messageId } = context.request.params as { messageId: string };
341
+ const client = await getClient();
342
+ const ownedAccountId = await assertMessagesOwned(
343
+ client,
344
+ [messageId],
345
+ accountConfigId,
346
+ "read",
347
+ );
348
+ const description = await client.message.describe(messageId);
349
+
350
+ const message = description.message[0];
351
+ const envelope = description.envelope[0];
352
+
353
+ const autoMoved = deriveAutoMoved(message);
354
+ const messageSummary: MessageSummaryResponse = {
355
+ messageId: message.messageId,
356
+ mailboxId: message.mailboxId,
357
+ uid: message.uid,
358
+ rfc822Size: message.rfc822Size,
359
+ internalDate: message.internalDate,
360
+ messageIdHeader: message.messageIdHeader,
361
+ authenticity: message.authenticity,
362
+ ...(autoMoved ? { autoMoved } : {}),
363
+ };
364
+
365
+ // Batch-fetch the resolved Address rows so each EnvelopeAddressResponse can
366
+ // carry the sender-level `flags` map. Without this the From-line UI would
367
+ // need a second round-trip to render trust state on every message open.
368
+ const uniqueAddressIds = Array.from(
369
+ new Set(description.envelopeAddress.map((a) => a.addressId)),
370
+ );
371
+ const addresses = uniqueAddressIds.length
372
+ ? await client.address.getAddress(accountConfigId, uniqueAddressIds)
373
+ : [];
374
+ const flagsByAddressId = new Map(
375
+ addresses.map((a) => [a.addressId, a.flags]),
376
+ );
377
+
378
+ const groupedAddresses = description.envelopeAddress.reduce(
379
+ (acc, addr) => {
380
+ const role = addr.addressRole;
381
+ if (!acc[role]) acc[role] = [];
382
+ acc[role].push({
383
+ addressId: addr.addressId,
384
+ displayName: addr.displayName,
385
+ normalizedEmail: addr.normalizedEmail,
386
+ addressRole: addr.addressRole,
387
+ addressOrder: addr.addressOrder,
388
+ flags: flagsByAddressId.get(addr.addressId),
389
+ });
390
+ return acc;
391
+ },
392
+ {} as Record<string, EnvelopeAddressResponse[]>,
393
+ );
394
+
395
+ const fromAddress = groupedAddresses.from?.[0];
396
+ const fromFlags = fromAddress
397
+ ? flagsByAddressId.get(fromAddress.addressId)
398
+ : undefined;
399
+ const senderTrust = fromAddress
400
+ ? deriveSenderTrust(fromFlags)
401
+ : SenderTrust.Unknown;
402
+
403
+ const envelopeResponse: EnvelopeResponse = {
404
+ messageId: envelope?.messageId ?? messageId,
405
+ date: envelope?.dateValue ?? message.internalDate,
406
+ subject: envelope?.subject,
407
+ messageIdValue: envelope?.messageIdValue,
408
+ from: groupedAddresses.from ?? [],
409
+ to: groupedAddresses.to ?? [],
410
+ cc: groupedAddresses.cc ?? [],
411
+ bcc: groupedAddresses.bcc ?? [],
412
+ replyTo: groupedAddresses.reply_to ?? [],
413
+ category: message.category ?? MessageCategory.uncategorized,
414
+ senderTrust,
415
+ };
416
+
417
+ const flags = description.messageFlag.map((f) => f.flagName);
418
+
419
+ // `accountId` is needed to build per-part `contentUrl` values for the
420
+ // BodyPartResponse list and on the IMAP-backfill path. Resolve it from
421
+ // the bodyStorageKey when the body has already been synced (free — the
422
+ // URI encodes both ids), or fall back to a single Mailbox.get() lookup
423
+ // to avoid a redundant query on the fast path.
424
+ const idsFromKey = message.bodyStorageKey
425
+ ? extractAccountIdsFromBodyKey(message.bodyStorageKey)
426
+ : null;
427
+ let accountId = idsFromKey?.accountId;
428
+ let mailbox: MailboxItem | undefined;
429
+ if (!accountId) {
430
+ mailbox = await client.mailbox.get(ownedAccountId, message.mailboxId);
431
+ accountId = mailbox.accountId;
432
+ }
433
+
434
+ // Trigger an IMAP backfill when the body has never been stored. The
435
+ // fetcher writes the raw .eml + parsed.json.gz cache + per-part rows
436
+ // as a side-effect; the SPA then renders body content via each
437
+ // BodyPartResponse.contentUrl (CloudFront-fronted, JWT-authorized at
438
+ // the edge). The handler no longer returns body text/html — that
439
+ // payload moved to per-part fetches once content delivery cut over
440
+ // (#224 PR 3/3). When bodyStorageKey is already set we skip IMAP
441
+ // entirely; the worker has already populated parts.
442
+ let bodyPartRows = description.bodyPart;
443
+ let bodyStorageKey = message.bodyStorageKey;
444
+ if (!bodyStorageKey) {
445
+ // A message whose body-sync exhausted every retry against real
446
+ // (broken) content never gets a bodyStorageKey (issue #1270 / epic
447
+ // #1281 invariant 3) — this branch is the ONLY place that condition
448
+ // can occur, so the sentinel check lives here instead of unconditionally
449
+ // at the top of the handler, which would tax every already-synced
450
+ // read with an S3 HEAD it can never need. Checking before the backfill
451
+ // attempt stops a client from looping through repeated 202s
452
+ // (materializeBodyParts) or repeated synchronous IMAP round-trips
453
+ // (fetchAndGetBody) on a message that can never succeed.
454
+ if (
455
+ await isMessageBodySyncBroken(
456
+ client.storage,
457
+ accountConfigId,
458
+ accountId,
459
+ messageId,
460
+ )
461
+ ) {
462
+ throw new UnrecoverableBodyError(
463
+ "This message's content could not be retrieved. Please report this issue.",
464
+ );
465
+ }
466
+
467
+ if (!mailbox) {
468
+ mailbox = await client.mailbox.get(accountId, message.mailboxId);
469
+ }
470
+
471
+ // Cheap frugal skip (epic #1281 invariant 6): a mailbox already known
472
+ // paused never even opens a connection. Optimization only —
473
+ // guardConnectionCursor's openBox wrap below is the structural
474
+ // guarantee (issue #1272's read-path detection point).
475
+ if (isCursorRebuildNeeded(mailbox.cursorState)) {
476
+ logger.debug(
477
+ {
478
+ accountId,
479
+ mailboxId: mailbox.mailboxId,
480
+ messageId,
481
+ cursorState: mailbox.cursorState,
482
+ },
483
+ "Mailbox cursor not normal; skipping IMAP body backfill for this request",
484
+ );
485
+ } else {
486
+ const scope = await client.createConnectionScope(accountId);
487
+ // Guard at the one choke point every UID-based IMAP op already
488
+ // passes through: openBox. A mismatch trips the mailbox to
489
+ // cursor_invalid and throws; caught below as a routine skip (epic
490
+ // #1281 invariant 3) — the request just returns without a fresh
491
+ // backfill this time.
492
+ const connection = guardConnectionCursor(
493
+ await scope.getConnection(),
494
+ { mailboxService: client.mailbox },
495
+ accountId,
496
+ mailbox,
497
+ );
498
+
499
+ await client.bodySync
500
+ .fetchAndGetBody(
501
+ messageId,
502
+ accountId,
503
+ accountConfigId,
504
+ mailbox.fullPath,
505
+ () => Promise.resolve(connection),
506
+ )
507
+ .catch((error: unknown) => {
508
+ if (error instanceof MailboxCursorPausedError) {
509
+ logger.debug(
510
+ {
511
+ accountId,
512
+ mailboxId: mailbox?.mailboxId,
513
+ messageId,
514
+ cursorState: error.state,
515
+ },
516
+ "Mailbox cursor not normal; skipping IMAP body backfill for this request",
517
+ );
518
+ return;
519
+ }
520
+ throw error;
521
+ })
522
+ .finally(() => scope.disconnect());
523
+
524
+ // Body just landed via IMAP — re-read so the response includes the
525
+ // parts the worker just wrote. Without this the first describe of a
526
+ // never-synced message would return an empty bodyParts array.
527
+ const refreshed = await client.message.describe(messageId);
528
+ bodyPartRows = refreshed.bodyPart;
529
+ bodyStorageKey = refreshed.message[0]?.bodyStorageKey;
530
+ }
531
+ }
532
+
533
+ // Per-part storage objects are deferred during bulk sync (DEFER_BODY_PARTS)
534
+ // to cut write amplification. Content delivery serves each part straight
535
+ // from storage with no Lambda in the request path, so a deferred (missing)
536
+ // object would surface to the SPA as a hard failure. Materialize them here —
537
+ // idempotent, skips parts already stored — so every contentUrl below
538
+ // resolves on first open. No-op when parts were written eagerly.
539
+ //
540
+ // A missing body object re-arms the SYNC_MESSAGE_BODY cue (so the worker
541
+ // (re)stores it) and leaves the contentUrls to surface a retryable 202 on
542
+ // the content route; any other error is rethrown and 500s loudly.
543
+ await materializeBodyParts(client.bodySync, {
544
+ accountConfigId,
545
+ accountId,
546
+ mailboxId: message.mailboxId,
547
+ messageId,
548
+ uid: message.uid,
549
+ bodyStorageKey,
550
+ cue: client.bodySyncQueue,
551
+ logger,
552
+ });
553
+
554
+ const bodyParts = buildBodyPartResponses(bodyPartRows, {
555
+ contentDeliveryDomain: getContentDeliveryDomain(),
556
+ accountConfigId,
557
+ accountId,
558
+ messageId,
559
+ sign: getContentSigner(),
560
+ });
561
+
562
+ const references = description.messageReference.map((ref) => ({
563
+ messageIdValue: ref.messageIdValue,
564
+ referenceType: ref.referenceType,
565
+ referenceOrder: ref.referenceOrder,
566
+ }));
567
+
568
+ return {
569
+ message: messageSummary,
570
+ envelope: envelopeResponse,
571
+ flags,
572
+ bodyParts,
573
+ references,
574
+ };
575
+ },
576
+
577
+ MessageOperations_getRawMessage: async (
578
+ context: Context,
579
+ ...args: unknown[]
580
+ ) => {
581
+ const event = args[0] as APIGatewayProxyEvent;
582
+ const accountConfigId = getAccountConfigIdFromEvent(event);
583
+ const { messageId } = context.request.params as { messageId: string };
584
+ const client = await getClient();
585
+
586
+ // Cross-tenant ownership guard. Returning raw RFC822 bytes is sensitive,
587
+ // so resolve the owning account and prove the caller owns it BEFORE any
588
+ // storage read or backfill. `read` mode throws NotFoundError (→ 404) on a
589
+ // foreign message and never leaks the owner's accountConfigId.
590
+ const accountId = await assertMessagesOwned(
591
+ client,
592
+ [messageId],
593
+ accountConfigId,
594
+ "read",
595
+ );
596
+
597
+ // Resolve the message row for its mailboxId / bodyStorageKey.
598
+ const message = await client.message.get(messageId);
599
+
600
+ // Backfill from IMAP when the body has never been stored, reusing the
601
+ // exact path describeMessage uses. fetchAndGetBody writes the raw .eml +
602
+ // parsed cache + per-part rows and sets message.bodyStorageKey as a
603
+ // side-effect; we then re-read the row to pick up the freshly written key.
604
+ if (!message.bodyStorageKey) {
605
+ const mailbox = await client.mailbox.get(accountId, message.mailboxId);
606
+
607
+ // Cheap frugal skip (epic #1281 invariant 6): a mailbox already known
608
+ // paused never even opens a connection. The existing "no
609
+ // bodyStorageKey after backfill" check just past this block is
610
+ // already the right signal for that case, so no new response shape
611
+ // is needed here.
612
+ if (isCursorRebuildNeeded(mailbox.cursorState)) {
613
+ logger.debug(
614
+ {
615
+ accountId,
616
+ mailboxId: mailbox.mailboxId,
617
+ messageId,
618
+ cursorState: mailbox.cursorState,
619
+ },
620
+ "Mailbox cursor not normal; skipping IMAP body backfill for this request",
621
+ );
622
+ } else {
623
+ const scope = await client.createConnectionScope(accountId);
624
+ // Guard at the openBox choke point — a fresh mismatch trips the
625
+ // mailbox and throws once the SELECT reveals it; caught below.
626
+ const connection = guardConnectionCursor(
627
+ await scope.getConnection(),
628
+ { mailboxService: client.mailbox },
629
+ accountId,
630
+ mailbox,
631
+ );
632
+ await client.bodySync
633
+ .fetchAndGetBody(
634
+ messageId,
635
+ accountId,
636
+ accountConfigId,
637
+ mailbox.fullPath,
638
+ () => Promise.resolve(connection),
639
+ )
640
+ .catch((error: unknown) => {
641
+ if (error instanceof MailboxCursorPausedError) {
642
+ logger.debug(
643
+ {
644
+ accountId,
645
+ mailboxId: mailbox.mailboxId,
646
+ messageId,
647
+ cursorState: error.state,
648
+ },
649
+ "Mailbox cursor not normal; skipping IMAP body backfill for this request",
650
+ );
651
+ return;
652
+ }
653
+ throw error;
654
+ })
655
+ .finally(() => scope.disconnect());
656
+ }
657
+ }
658
+
659
+ // Re-read so we have the bodyStorageKey the backfill just wrote (or the
660
+ // key that was already present), then pull the raw bytes from storage.
661
+ const stored = await client.message.get(messageId);
662
+ if (!stored.bodyStorageKey) {
663
+ throw new Error(
664
+ `Raw source unavailable for message ${messageId}: no bodyStorageKey after backfill`,
665
+ );
666
+ }
667
+
668
+ const body = await client.storage.retrieve(stored.bodyStorageKey);
669
+
670
+ return { raw: decodeRawEml(body) };
671
+ },
672
+
673
+ MessageOperations_updateMessageFlags: async (context, ...args: unknown[]) => {
674
+ const event = args[0] as APIGatewayProxyEvent;
675
+ const accountConfigId = getAccountConfigIdFromEvent(event);
676
+ const { messageId } = context.request.params as { messageId: string };
677
+ const { isRead, isStarred, starColor } = context.request.requestBody as {
678
+ isRead?: boolean;
679
+ isStarred?: boolean;
680
+ starColor?: string;
681
+ };
682
+
683
+ const client = await getClient();
684
+ const accountId = await assertMessagesOwned(
685
+ client,
686
+ [messageId],
687
+ accountConfigId,
688
+ "act",
689
+ );
690
+
691
+ // FlagQueueService handles: MessageFlag + ThreadMessage updates + SQS event
692
+ const result = await client.flagQueue.updateFlags(
693
+ accountConfigId,
694
+ messageId,
695
+ accountId,
696
+ {
697
+ isRead,
698
+ isStarred,
699
+ starColor: starColor as StarColorValue | undefined,
700
+ },
701
+ );
702
+
703
+ return {
704
+ messageId: result.messageId,
705
+ isRead: result.isRead,
706
+ isStarred: result.isStarred,
707
+ };
708
+ },
709
+ };
710
+
711
+ export const MessageBulkOperations: Record<
712
+ MessageBulkOperationIds,
713
+ OperationHandler<MessageBulkOperationIds>
714
+ > = {
715
+ MessageBulkOperations_updateFlags: async (context, ...args: unknown[]) => {
716
+ const event = args[0] as APIGatewayProxyEvent;
717
+ const accountConfigId = getAccountConfigIdFromEvent(event);
718
+ const { messageIds, isRead, isStarred, starColor } = context.request
719
+ .requestBody as {
720
+ messageIds: string[];
721
+ isRead?: boolean;
722
+ isStarred?: boolean;
723
+ starColor?: string;
724
+ };
725
+
726
+ if (messageIds.length === 0) {
727
+ return { successCount: 0, failureCount: 0 };
728
+ }
729
+
730
+ const client = await getClient();
731
+ const accountId = await assertMessagesOwned(
732
+ client,
733
+ messageIds,
734
+ accountConfigId,
735
+ "act",
736
+ );
737
+
738
+ // FlagQueueService handles: MessageFlag + ThreadMessage updates + SQS events
739
+ // Process each message individually (same pattern as moveMessages)
740
+ for (const messageId of messageIds) {
741
+ await client.flagQueue.updateFlags(
742
+ accountConfigId,
743
+ messageId,
744
+ accountId,
745
+ {
746
+ isRead,
747
+ isStarred,
748
+ starColor: starColor as StarColorValue | undefined,
749
+ },
750
+ );
751
+ }
752
+
753
+ return {
754
+ successCount: messageIds.length,
755
+ failureCount: 0,
756
+ };
757
+ },
758
+
759
+ MessageBulkOperations_deleteMessages: async (context, ...args: unknown[]) => {
760
+ const event = args[0] as APIGatewayProxyEvent;
761
+ const accountConfigId = getAccountConfigIdFromEvent(event);
762
+ const { messageIds, permanent } = context.request.requestBody as {
763
+ messageIds: string[];
764
+ permanent?: boolean;
765
+ };
766
+
767
+ if (messageIds.length === 0) {
768
+ return { successCount: 0, failureCount: 0 };
769
+ }
770
+
771
+ const client = await getClient();
772
+ const accountId = await assertMessagesOwned(
773
+ client,
774
+ messageIds,
775
+ accountConfigId,
776
+ "act",
777
+ );
778
+
779
+ // MessageMoveService handles: Message + ThreadMessage updates + SQS events
780
+ await client.messageMove.deleteMessages(
781
+ accountConfigId,
782
+ messageIds,
783
+ accountId,
784
+ {
785
+ permanent,
786
+ },
787
+ );
788
+
789
+ return {
790
+ successCount: messageIds.length,
791
+ failureCount: 0,
792
+ };
793
+ },
794
+
795
+ MessageBulkOperations_moveMessages: async (context, ...args: unknown[]) => {
796
+ const event = args[0] as APIGatewayProxyEvent;
797
+ const accountConfigId = getAccountConfigIdFromEvent(event);
798
+ const { messageIds, destinationMailboxId } = context.request
799
+ .requestBody as {
800
+ messageIds: string[];
801
+ destinationMailboxId: string;
802
+ };
803
+
804
+ if (messageIds.length === 0) {
805
+ return { successCount: 0, failureCount: 0 };
806
+ }
807
+
808
+ const client = await getClient();
809
+ const accountId = await assertMessagesOwned(
810
+ client,
811
+ messageIds,
812
+ accountConfigId,
813
+ "act",
814
+ );
815
+
816
+ // Destination must belong to the same (caller-owned) account.
817
+ const destination = await client.mailbox.get(
818
+ accountId,
819
+ destinationMailboxId,
820
+ );
821
+ if (destination.accountId !== accountId) {
822
+ throw new ForbiddenError(
823
+ `Destination mailbox ${destinationMailboxId} not in account`,
824
+ );
825
+ }
826
+
827
+ // MessageMoveService handles: Message + ThreadMessage updates + SQS events
828
+ await client.messageMove.moveMessages(
829
+ accountConfigId,
830
+ messageIds,
831
+ destinationMailboxId,
832
+ accountId,
833
+ );
834
+
835
+ return {
836
+ successCount: messageIds.length,
837
+ failureCount: 0,
838
+ };
839
+ },
840
+
841
+ MessageBulkOperations_copyMessages: async (context, ...args: unknown[]) => {
842
+ const event = args[0] as APIGatewayProxyEvent;
843
+ const accountConfigId = getAccountConfigIdFromEvent(event);
844
+ const { messageIds, destinationMailboxId } = context.request
845
+ .requestBody as {
846
+ messageIds: string[];
847
+ destinationMailboxId: string;
848
+ };
849
+
850
+ if (messageIds.length === 0) {
851
+ return { successCount: 0, failureCount: 0 };
852
+ }
853
+
854
+ const client = await getClient();
855
+ const accountId = await assertMessagesOwned(
856
+ client,
857
+ messageIds,
858
+ accountConfigId,
859
+ "act",
860
+ );
861
+
862
+ // Destination must belong to the same (caller-owned) account.
863
+ const destination = await client.mailbox.get(
864
+ accountId,
865
+ destinationMailboxId,
866
+ );
867
+ if (destination.accountId !== accountId) {
868
+ throw new ForbiddenError(
869
+ `Destination mailbox ${destinationMailboxId} not in account`,
870
+ );
871
+ }
872
+
873
+ // MessageMoveService handles: Message copies + ThreadMessage creation + SQS events
874
+ await client.messageMove.copyMessages(
875
+ accountConfigId,
876
+ messageIds,
877
+ destinationMailboxId,
878
+ accountId,
879
+ );
880
+
881
+ return {
882
+ successCount: messageIds.length,
883
+ failureCount: 0,
884
+ };
885
+ },
886
+ };