@remit/backend 0.0.143 → 0.0.145

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 (279) hide show
  1. package/dist/dev-server/content-handler.d.ts +38 -0
  2. package/dist/dev-server/content-handler.js +46 -0
  3. package/dist/src/api.d.ts +5 -0
  4. package/dist/src/api.js +73 -0
  5. package/dist/src/auth.d.ts +24 -0
  6. package/dist/src/auth.js +81 -0
  7. package/dist/src/config/api-config.d.ts +20 -0
  8. package/dist/src/config/api-config.js +1 -0
  9. package/dist/src/config/from-env.d.ts +2 -0
  10. package/dist/src/config/from-env.js +65 -0
  11. package/dist/src/config/msoauth-secrets-manager.d.ts +2 -0
  12. package/dist/src/config/msoauth-secrets-manager.js +28 -0
  13. package/dist/src/config/msoauth.d.ts +19 -0
  14. package/dist/src/config/msoauth.js +6 -0
  15. package/{src/derive/autoMoved.ts → dist/src/derive/autoMoved.d.ts} +1 -29
  16. package/dist/src/derive/autoMoved.js +49 -0
  17. package/dist/src/derive/contentSignature.d.ts +40 -0
  18. package/{src/derive/contentSignature.ts → dist/src/derive/contentSignature.js} +23 -77
  19. package/dist/src/derive/contentUrl.d.ts +33 -0
  20. package/dist/src/derive/contentUrl.js +27 -0
  21. package/{src/derive/deriveMuted.ts → dist/src/derive/deriveMuted.d.ts} +1 -3
  22. package/dist/src/derive/deriveMuted.js +7 -0
  23. package/dist/src/derive/enrichThreadRows.d.ts +118 -0
  24. package/dist/src/derive/enrichThreadRows.js +151 -0
  25. package/dist/src/derive/filterThreadCriteria.d.ts +23 -0
  26. package/dist/src/derive/filterThreadCriteria.js +24 -0
  27. package/{src/derive/pendingMoveCounts.ts → dist/src/derive/pendingMoveCounts.d.ts} +5 -53
  28. package/dist/src/derive/pendingMoveCounts.js +53 -0
  29. package/{src/derive/senderTrust.ts → dist/src/derive/senderTrust.d.ts} +1 -9
  30. package/dist/src/derive/senderTrust.js +19 -0
  31. package/dist/src/error.d.ts +21 -0
  32. package/dist/src/error.js +85 -0
  33. package/dist/src/handlers/account-guards.d.ts +36 -0
  34. package/dist/src/handlers/account-guards.js +107 -0
  35. package/dist/src/handlers/account-oauth.d.ts +15 -0
  36. package/dist/src/handlers/account-oauth.js +334 -0
  37. package/dist/src/handlers/account-overrides.d.ts +53 -0
  38. package/dist/src/handlers/account-overrides.js +233 -0
  39. package/dist/src/handlers/account-signature.d.ts +28 -0
  40. package/dist/src/handlers/account-signature.js +85 -0
  41. package/dist/src/handlers/account.d.ts +7 -0
  42. package/dist/src/handlers/account.js +337 -0
  43. package/dist/src/handlers/address.d.ts +35 -0
  44. package/dist/src/handlers/address.js +133 -0
  45. package/dist/src/handlers/calendar-event.d.ts +70 -0
  46. package/dist/src/handlers/calendar-event.js +417 -0
  47. package/dist/src/handlers/calendar-feed.d.ts +47 -0
  48. package/dist/src/handlers/calendar-feed.js +184 -0
  49. package/dist/src/handlers/calendar-suggestion.d.ts +47 -0
  50. package/dist/src/handlers/calendar-suggestion.js +204 -0
  51. package/dist/src/handlers/calendar.d.ts +116 -0
  52. package/dist/src/handlers/calendar.js +323 -0
  53. package/dist/src/handlers/config.d.ts +43 -0
  54. package/dist/src/handlers/config.js +164 -0
  55. package/dist/src/handlers/ensure-account-config.d.ts +9 -0
  56. package/dist/src/handlers/ensure-account-config.js +27 -0
  57. package/dist/src/handlers/filter.d.ts +3 -0
  58. package/dist/src/handlers/filter.js +88 -0
  59. package/dist/src/handlers/folder-role-appointments.d.ts +68 -0
  60. package/dist/src/handlers/folder-role-appointments.js +168 -0
  61. package/dist/src/handlers/folder-role-labels.d.ts +26 -0
  62. package/dist/src/handlers/folder-role-labels.js +40 -0
  63. package/dist/src/handlers/folder-role.d.ts +9 -0
  64. package/dist/src/handlers/folder-role.js +37 -0
  65. package/dist/src/handlers/index.d.ts +88 -0
  66. package/dist/src/handlers/index.js +62 -0
  67. package/dist/src/handlers/label.d.ts +43 -0
  68. package/dist/src/handlers/label.js +107 -0
  69. package/dist/src/handlers/mailbox-bindings.d.ts +41 -0
  70. package/dist/src/handlers/mailbox-bindings.js +61 -0
  71. package/dist/src/handlers/mailbox.d.ts +23 -0
  72. package/dist/src/handlers/mailbox.js +211 -0
  73. package/dist/src/handlers/me.d.ts +2 -0
  74. package/dist/src/handlers/me.js +117 -0
  75. package/dist/src/handlers/message.d.ts +82 -0
  76. package/dist/src/handlers/message.js +595 -0
  77. package/dist/src/handlers/organize.d.ts +10 -0
  78. package/dist/src/handlers/organize.js +103 -0
  79. package/dist/src/handlers/outbox-attachment.d.ts +25 -0
  80. package/dist/src/handlers/outbox-attachment.js +77 -0
  81. package/dist/src/handlers/outbox.d.ts +4 -0
  82. package/dist/src/handlers/outbox.js +136 -0
  83. package/dist/src/handlers/sync-progress.d.ts +46 -0
  84. package/dist/src/handlers/sync-progress.js +63 -0
  85. package/dist/src/handlers/sync.d.ts +28 -0
  86. package/dist/src/handlers/sync.js +73 -0
  87. package/dist/src/handlers/system-update.d.ts +2 -0
  88. package/dist/src/handlers/system-update.js +159 -0
  89. package/dist/src/handlers/thread.d.ts +148 -0
  90. package/dist/src/handlers/thread.js +228 -0
  91. package/dist/src/handlers/unified-threads.d.ts +247 -0
  92. package/dist/src/handlers/unified-threads.js +406 -0
  93. package/{src/handlers/vip-suggestions.ts → dist/src/handlers/vip-suggestions.d.ts} +1 -13
  94. package/dist/src/handlers/vip-suggestions.js +16 -0
  95. package/dist/src/index.d.ts +5 -0
  96. package/dist/src/index.js +53 -0
  97. package/{src/json.ts → dist/src/json.d.ts} +1 -4
  98. package/dist/src/json.js +10 -0
  99. package/dist/src/jwt-auth.d.ts +15 -0
  100. package/dist/src/jwt-auth.js +45 -0
  101. package/dist/src/lambda.d.ts +13 -0
  102. package/dist/src/lambda.js +33 -0
  103. package/dist/src/request.d.ts +9 -0
  104. package/dist/src/request.js +17 -0
  105. package/dist/src/response.d.ts +44 -0
  106. package/dist/src/response.js +80 -0
  107. package/dist/src/serve.d.ts +31 -0
  108. package/dist/src/serve.js +117 -0
  109. package/dist/src/service/account-deletion.d.ts +20 -0
  110. package/dist/src/service/account-deletion.js +33 -0
  111. package/dist/src/service/compose-sqlite.d.ts +3 -0
  112. package/dist/src/service/compose-sqlite.js +13 -0
  113. package/dist/src/service/create-remit-client.d.ts +109 -0
  114. package/dist/src/service/create-remit-client.js +188 -0
  115. package/dist/src/service/data-client.d.ts +12 -0
  116. package/dist/src/service/data-client.js +42 -0
  117. package/dist/src/service/export-identity.d.ts +4 -0
  118. package/dist/src/service/export-identity.js +33 -0
  119. package/dist/src/service/fire-and-forget.d.ts +54 -0
  120. package/dist/src/service/fire-and-forget.js +37 -0
  121. package/dist/src/service/organize.d.ts +181 -0
  122. package/dist/src/service/organize.js +367 -0
  123. package/dist/src/service/queues.d.ts +12 -0
  124. package/dist/src/service/queues.js +1 -0
  125. package/dist/src/service/refusal.d.ts +4 -0
  126. package/dist/src/service/refusal.js +20 -0
  127. package/dist/src/service/sender-category.d.ts +51 -0
  128. package/{src/service/sender-category.ts → dist/src/service/sender-category.js} +40 -81
  129. package/dist/src/service/shared-deps.d.ts +6 -0
  130. package/dist/src/service/shared-deps.js +24 -0
  131. package/dist/src/service/trigger-sync.d.ts +57 -0
  132. package/dist/src/service/trigger-sync.js +31 -0
  133. package/dist/src/types.d.ts +72 -0
  134. package/dist/src/types.js +1 -0
  135. package/package.json +125 -98
  136. package/.env.test +0 -4
  137. package/dev-server/content-auth.test.ts +0 -113
  138. package/dev-server/content-auth.ts +0 -54
  139. package/dev-server/content-handler.test.ts +0 -105
  140. package/dev-server/content-handler.ts +0 -79
  141. package/dev-server/content-path.test.ts +0 -37
  142. package/dev-server/content-path.ts +0 -27
  143. package/dev-server/cors.test.ts +0 -48
  144. package/dev-server/cors.ts +0 -25
  145. package/dev-server/keep-alive.test.ts +0 -24
  146. package/dev-server/lambda-helpers.ts +0 -69
  147. package/dev-server/log-output.test.ts +0 -173
  148. package/dev-server/oauth-callback-route.test.ts +0 -66
  149. package/dev-server/relational-health.test.ts +0 -83
  150. package/dev-server/relational-health.ts +0 -39
  151. package/dev-server/server.ts +0 -384
  152. package/dev-server/sync-age.test.ts +0 -91
  153. package/dev-server/sync-age.ts +0 -55
  154. package/dev-server/upload-handler.ts +0 -132
  155. package/dev-server/upload-route.test.ts +0 -196
  156. package/scripts/backfill-classification.ts +0 -91
  157. package/scripts/backfill-list-id.ts +0 -83
  158. package/scripts/config-save.ts +0 -128
  159. package/src/auth.test.ts +0 -12
  160. package/src/auth.ts +0 -92
  161. package/src/config/msoauth.test.ts +0 -48
  162. package/src/config/msoauth.ts +0 -48
  163. package/src/data-backend.test.ts +0 -23
  164. package/src/data-backend.ts +0 -14
  165. package/src/derive/autoMoved.test.ts +0 -104
  166. package/src/derive/contentSignature.test.ts +0 -161
  167. package/src/derive/contentUrl.test.ts +0 -172
  168. package/src/derive/contentUrl.ts +0 -86
  169. package/src/derive/deriveMuted.test.ts +0 -34
  170. package/src/derive/enrichThreadRows.test.ts +0 -360
  171. package/src/derive/enrichThreadRows.ts +0 -234
  172. package/src/derive/filterThreadCriteria.test.ts +0 -126
  173. package/src/derive/filterThreadCriteria.ts +0 -51
  174. package/src/derive/senderTrust.test.ts +0 -67
  175. package/src/error.test.ts +0 -201
  176. package/src/error.ts +0 -123
  177. package/src/handlers/account-guards.test.ts +0 -67
  178. package/src/handlers/account-guards.ts +0 -169
  179. package/src/handlers/account-oauth-callback.test.ts +0 -155
  180. package/src/handlers/account-oauth-consent.test.ts +0 -336
  181. package/src/handlers/account-oauth-start.test.ts +0 -191
  182. package/src/handlers/account-oauth.test.ts +0 -439
  183. package/src/handlers/account-oauth.ts +0 -635
  184. package/src/handlers/account-overrides.test.ts +0 -88
  185. package/src/handlers/account-overrides.ts +0 -323
  186. package/src/handlers/account-ownership.ts +0 -25
  187. package/src/handlers/account-signature.ts +0 -117
  188. package/src/handlers/account-synced-services.test.ts +0 -245
  189. package/src/handlers/account.ts +0 -552
  190. package/src/handlers/address.test.ts +0 -381
  191. package/src/handlers/address.ts +0 -200
  192. package/src/handlers/calendar-event.test.ts +0 -685
  193. package/src/handlers/calendar-event.ts +0 -738
  194. package/src/handlers/calendar-feed-validators.test.ts +0 -228
  195. package/src/handlers/calendar-feed.test.ts +0 -439
  196. package/src/handlers/calendar-feed.ts +0 -291
  197. package/src/handlers/calendar-sqlite-fixture.ts +0 -36
  198. package/src/handlers/calendar-suggestion.test.ts +0 -1269
  199. package/src/handlers/calendar-suggestion.ts +0 -425
  200. package/src/handlers/calendar.test.ts +0 -1080
  201. package/src/handlers/calendar.ts +0 -517
  202. package/src/handlers/config.test.ts +0 -245
  203. package/src/handlers/config.ts +0 -332
  204. package/src/handlers/ensure-account-config.ts +0 -30
  205. package/src/handlers/filter.test.ts +0 -644
  206. package/src/handlers/filter.ts +0 -518
  207. package/src/handlers/folder-role-appointments.test.ts +0 -438
  208. package/src/handlers/folder-role-appointments.ts +0 -249
  209. package/src/handlers/folder-role-labels.ts +0 -63
  210. package/src/handlers/folder-role.test.ts +0 -209
  211. package/src/handlers/folder-role.ts +0 -80
  212. package/src/handlers/index.ts +0 -83
  213. package/src/handlers/label.test.ts +0 -112
  214. package/src/handlers/label.ts +0 -210
  215. package/src/handlers/mailbox-bindings.ts +0 -111
  216. package/src/handlers/mailbox-rename-refusal.test.ts +0 -247
  217. package/src/handlers/mailbox-settled-target.test.ts +0 -251
  218. package/src/handlers/mailbox.test.ts +0 -280
  219. package/src/handlers/mailbox.ts +0 -581
  220. package/src/handlers/me.test.ts +0 -81
  221. package/src/handlers/me.ts +0 -203
  222. package/src/handlers/message-read-backfill.test.ts +0 -103
  223. package/src/handlers/message.test.ts +0 -131
  224. package/src/handlers/message.ts +0 -1131
  225. package/src/handlers/organize.test.ts +0 -277
  226. package/src/handlers/organize.ts +0 -197
  227. package/src/handlers/outbox-attachment.test.ts +0 -765
  228. package/src/handlers/outbox-attachment.ts +0 -126
  229. package/src/handlers/outbox-list.test.ts +0 -179
  230. package/src/handlers/outbox.test.ts +0 -772
  231. package/src/handlers/outbox.ts +0 -258
  232. package/src/handlers/search.test.ts +0 -161
  233. package/src/handlers/search.ts +0 -105
  234. package/src/handlers/sync-progress.test.ts +0 -205
  235. package/src/handlers/sync-progress.ts +0 -99
  236. package/src/handlers/sync.test.ts +0 -168
  237. package/src/handlers/sync.ts +0 -118
  238. package/src/handlers/system-update.test.ts +0 -497
  239. package/src/handlers/system-update.ts +0 -230
  240. package/src/handlers/thread.test.ts +0 -396
  241. package/src/handlers/thread.ts +0 -427
  242. package/src/handlers/unified-threads-mail-sources.sqlite.test.ts +0 -197
  243. package/src/handlers/unified-threads.test.ts +0 -785
  244. package/src/handlers/unified-threads.ts +0 -669
  245. package/src/index.test.ts +0 -83
  246. package/src/index.ts +0 -228
  247. package/src/jwt-auth.test.ts +0 -152
  248. package/src/jwt-auth.ts +0 -132
  249. package/src/log-redaction.test.ts +0 -144
  250. package/src/openapi-spec.test.ts +0 -118
  251. package/src/request-context.test.ts +0 -61
  252. package/src/request-context.ts +0 -33
  253. package/src/request-pipeline.test.ts +0 -88
  254. package/src/request.ts +0 -10
  255. package/src/response.test.ts +0 -130
  256. package/src/response.ts +0 -144
  257. package/src/service/compose-sqlite.ts +0 -98
  258. package/src/service/config-import.ts +0 -39
  259. package/src/service/create-remit-client.ts +0 -522
  260. package/src/service/data-client.test.ts +0 -97
  261. package/src/service/data-client.ts +0 -58
  262. package/src/service/export-identity.ts +0 -39
  263. package/src/service/filter.ts +0 -40
  264. package/src/service/fire-and-forget.test.ts +0 -126
  265. package/src/service/fire-and-forget.ts +0 -79
  266. package/src/service/localhost-env-config.test.ts +0 -36
  267. package/src/service/organize-queue.ts +0 -98
  268. package/src/service/organize.test.ts +0 -1451
  269. package/src/service/organize.ts +0 -1025
  270. package/src/service/read-path-filter-config.test.ts +0 -243
  271. package/src/service/semantic-capability.test.ts +0 -78
  272. package/src/service/semantic-capability.ts +0 -95
  273. package/src/service/sender-category.test.ts +0 -235
  274. package/src/service/sqs.ts +0 -4
  275. package/src/service/trigger-sync.test.ts +0 -205
  276. package/src/service/trigger-sync.ts +0 -100
  277. package/src/stdout-capture-fixture.ts +0 -40
  278. package/src/types.ts +0 -271
  279. package/tsconfig.json +0 -7
@@ -0,0 +1,595 @@
1
+ import { ForbiddenError, MessagePlacementUnsettledError, UnrecoverableBodyError, } from "@remit/data-ports/errors";
2
+ import { isEncapsulatedMessage } from "@remit/data-ports/message-parts";
3
+ import { ContentDisposition, MediaType, MessageCategory, SenderTrust, } from "@remit/domain-enums";
4
+ import { logger } from "@remit/logger";
5
+ import { guardConnectionCursor, isCursorRebuildNeeded, isMessageBodySyncBroken, MailboxCursorPausedError, MoveNotSettledError, materializeBodyParts, NoJunkMailboxError, ownedMessages, resolveFolderPlacement, settledFolder, } from "@remit/mailbox-service";
6
+ import { isStorageNotFoundError as isStorageNotFoundErrorFromService, parseStorageUri, } from "@remit/storage-service";
7
+ import { deriveAutoMoved } from "../derive/autoMoved.js";
8
+ import { getContentSigner, } from "../derive/contentSignature.js";
9
+ import { buildContentUrl } from "../derive/contentUrl.js";
10
+ import { deriveSenderTrust } from "../derive/senderTrust.js";
11
+ import { unwrap } from "../service/refusal.js";
12
+ export const isStorageNotFoundError = (error) => isStorageNotFoundErrorFromService(error);
13
+ /**
14
+ * Decode the raw `.eml` bytes into a string for inspection. RFC822/MIME
15
+ * sources are 7-bit/8-bit ASCII-compatible (non-ASCII bytes are encoded via
16
+ * quoted-printable / base64 inside the body), so `latin1` round-trips every
17
+ * byte 1:1 without loss or replacement characters — unlike `utf8`, which would
18
+ * mangle raw 8-bit bytes. Headers and structure stay byte-accurate.
19
+ */
20
+ export const decodeRawEml = (body) => body.toString("latin1");
21
+ /**
22
+ * Extract the accountConfigId + accountId segments from a bodyStorageKey URI
23
+ * such as
24
+ * `s3://bucket/accounts/{accountConfigId}/{accountId}/messages/{messageId}/body.eml`.
25
+ * Returns null when the URI shape doesn't match — caller falls back to the
26
+ * slow path without writing a parsed-body cache.
27
+ */
28
+ export const extractAccountIdsFromBodyKey = (uri) => {
29
+ const parsed = parseStorageUri(uri);
30
+ const match = parsed.storageKey.match(/^accounts\/([^/]+)\/([^/]+)\/messages\//);
31
+ if (!match)
32
+ return null;
33
+ return { accountConfigId: match[1], accountId: match[2] };
34
+ };
35
+ const MEDIA_TYPE_VALUES = new Set(Object.values(MediaType));
36
+ const CONTENT_DISPOSITION_VALUES = new Set(Object.values(ContentDisposition));
37
+ export const isMediaType = (value) => typeof value === "string" && MEDIA_TYPE_VALUES.has(value);
38
+ export const isContentDisposition = (value) => typeof value === "string" && CONTENT_DISPOSITION_VALUES.has(value);
39
+ const assertMediaType = (value, bodyPartId) => {
40
+ if (!isMediaType(value)) {
41
+ throw new Error(`Invalid mediaType "${value}" on BodyPart ${bodyPartId}; expected one of ${[...MEDIA_TYPE_VALUES].join(", ")}`);
42
+ }
43
+ return value;
44
+ };
45
+ const assertDisposition = (value, bodyPartId) => {
46
+ if (value === undefined)
47
+ return undefined;
48
+ if (!isContentDisposition(value)) {
49
+ throw new Error(`Invalid disposition "${value}" on BodyPart ${bodyPartId}; expected one of ${[...CONTENT_DISPOSITION_VALUES].join(", ")}`);
50
+ }
51
+ return value;
52
+ };
53
+ export const FORWARDED_MESSAGE_FILENAME = "Forwarded message.eml";
54
+ const responseFilename = (part) => {
55
+ if (part.dispositionFilename !== undefined)
56
+ return part.dispositionFilename;
57
+ return isEncapsulatedMessage(part) ? FORWARDED_MESSAGE_FILENAME : undefined;
58
+ };
59
+ export const buildBodyPartResponses = (parts, context) => {
60
+ return parts.map((part) => ({
61
+ bodyPartId: part.bodyPartId,
62
+ partPath: part.partPath,
63
+ mediaType: assertMediaType(part.mediaType, part.bodyPartId),
64
+ mediaSubtype: part.mediaSubtype,
65
+ sizeOctets: part.sizeOctets,
66
+ disposition: assertDisposition(part.disposition, part.bodyPartId),
67
+ dispositionFilename: responseFilename(part),
68
+ isMultipart: part.isMultipart,
69
+ contentId: part.contentId,
70
+ contentUrl: buildContentUrl({
71
+ domain: context.contentDeliveryDomain,
72
+ accountConfigId: context.accountConfigId,
73
+ accountId: context.accountId,
74
+ messageId: context.messageId,
75
+ partPath: part.partPath,
76
+ sign: context.sign,
77
+ }),
78
+ }));
79
+ };
80
+ /**
81
+ * The fallback for a failure nobody designed copy for — an AWS SDK message
82
+ * naming a queue URL, an `ECONNREFUSED host:port`, a programmer error.
83
+ * Everything reaching it is flattened, same policy as `error.ts`'s "Internal
84
+ * server error" flattening, because `SpamReportBulkResult.reason` is shown to
85
+ * the user as-is. It says "try again" because retrying is the only thing left
86
+ * to say about a cause we cannot name; an outcome the service DOES understand
87
+ * carries its own text through `DESIGNED_FAILURES` instead of landing here.
88
+ */
89
+ export const GENERIC_FAILURE_REASON = "This message could not be processed. Please try again. If it keeps failing, report it.";
90
+ /**
91
+ * The designed, user-facing outcomes whose own message is returned verbatim:
92
+ * each names what happened and what the user can do about it, which the
93
+ * generic text cannot.
94
+ */
95
+ const DESIGNED_FAILURES = [
96
+ MoveNotSettledError,
97
+ NoJunkMailboxError,
98
+ MessagePlacementUnsettledError,
99
+ ];
100
+ const isDesignedFailure = (reason) => DESIGNED_FAILURES.some((designed) => reason instanceof designed);
101
+ /**
102
+ * The placement refusal is the one designed outcome whose own message is not
103
+ * the text to ship: it names a uuid and no remedy. One remedy answers it, and
104
+ * only one refusal reaches it — a mutation still in flight, which settles on
105
+ * its own (imap-mutations R3). Worded here, the way the delete path words the
106
+ * same refusal for the client.
107
+ */
108
+ const placementRefusalReason = (_error) => "This message is still being moved on the mail server. Try again in a moment.";
109
+ const failureReason = (reason) => {
110
+ if (!isDesignedFailure(reason))
111
+ return GENERIC_FAILURE_REASON;
112
+ if (reason instanceof MessagePlacementUnsettledError)
113
+ return placementRefusalReason(reason);
114
+ return reason.message;
115
+ };
116
+ /**
117
+ * Drive one report-spam/not-spam operation per message, concurrently. Each
118
+ * message's own wait for its move to settle (SpamReportService's R2 wait) is
119
+ * bounded on its own, but running the batch sequentially would let those
120
+ * waits accumulate across the whole request — 50 messages could each wait
121
+ * seconds, serializing into minutes and dying at the proxy. Running them
122
+ * concurrently instead bounds the whole request by a single message's wait,
123
+ * regardless of batch size.
124
+ */
125
+ export const settleSpamReportBulk = async (messageIds, run) => {
126
+ const results = await Promise.allSettled(messageIds.map((messageId) => run(messageId)));
127
+ let successCount = 0;
128
+ const failures = [];
129
+ results.forEach((result, index) => {
130
+ if (result.status === "fulfilled") {
131
+ successCount += 1;
132
+ return;
133
+ }
134
+ const messageId = messageIds[index];
135
+ failures.push({ messageId, reason: failureReason(result.reason) });
136
+ logger.error({ messageId, error: result.reason }, "spam-report bulk operation failed for one message");
137
+ });
138
+ return {
139
+ successCount,
140
+ failureCount: failures.length,
141
+ ...(failures.length > 0 ? { failures } : {}),
142
+ };
143
+ };
144
+ export const MessageOperations = {
145
+ MessageOperations_describeMessage: async (context, ctx) => {
146
+ const accountConfigId = ctx.identity.accountConfigId;
147
+ const { messageId } = context.request.params;
148
+ const client = ctx.client;
149
+ const ownedAccountId = unwrap(await ownedMessages(client, [messageId], accountConfigId, "read"));
150
+ const description = await client.message.describe(messageId);
151
+ const message = description.message[0];
152
+ const envelope = description.envelope[0];
153
+ const threadMessage = await client.threadMessage.findByMessageId(accountConfigId, messageId);
154
+ const autoMoved = deriveAutoMoved(message);
155
+ // Labels applied to this message (issue #26) — filter-, organize-, and
156
+ // manually-applied alike. A message rarely carries more than a handful, so
157
+ // a `get` per label id (not a batch fetch) is the whole cost.
158
+ const messageLabelRows = await client.messageLabel.listByMessageId(message.messageId);
159
+ const labels = await Promise.all(messageLabelRows.map(async (row) => {
160
+ const label = await client.label.get(accountConfigId, row.labelId);
161
+ return { labelId: label.labelId, name: label.name, color: label.color };
162
+ }));
163
+ // Not typed against the generated `MessageSummaryResponse`: `labels` is a
164
+ // new field on it in this same PR, and that package publishes separately
165
+ // from this repo — a local-codegen-only field would fail
166
+ // `check-consumer-typecheck`/`release:dry-run`, which resolve generated
167
+ // `@remit/*` packages off the registry, not the local `build/` output.
168
+ const messageSummary = {
169
+ messageId: message.messageId,
170
+ mailboxId: message.mailboxId,
171
+ uid: message.uid,
172
+ rfc822Size: message.rfc822Size,
173
+ internalDate: message.internalDate,
174
+ messageIdHeader: threadMessage?.messageIdHeader,
175
+ authenticity: message.authenticity,
176
+ status: message.status,
177
+ syncStatus: message.syncStatus,
178
+ abandonedMutation: message.abandonedMutation,
179
+ ...(autoMoved ? { autoMoved } : {}),
180
+ ...(labels.length > 0 ? { labels } : {}),
181
+ ...(message.spamReport ? { spamReport: message.spamReport } : {}),
182
+ };
183
+ // Batch-fetch the resolved Address rows so each EnvelopeAddressResponse can
184
+ // carry the sender-level `flags` map. Without this the From-line UI would
185
+ // need a second round-trip to render trust state on every message open.
186
+ const uniqueAddressIds = Array.from(new Set(description.envelopeAddress.map((a) => a.addressId)));
187
+ const addresses = uniqueAddressIds.length
188
+ ? await client.address.getAddress(accountConfigId, uniqueAddressIds)
189
+ : [];
190
+ const flagsByAddressId = new Map(addresses.map((a) => [a.addressId, a.flags]));
191
+ const groupedAddresses = description.envelopeAddress.reduce((acc, addr) => {
192
+ const role = addr.addressRole;
193
+ if (!acc[role])
194
+ acc[role] = [];
195
+ acc[role].push({
196
+ addressId: addr.addressId,
197
+ displayName: addr.displayName,
198
+ normalizedEmail: addr.normalizedEmail,
199
+ addressRole: addr.addressRole,
200
+ addressOrder: addr.addressOrder,
201
+ flags: flagsByAddressId.get(addr.addressId),
202
+ });
203
+ return acc;
204
+ }, {});
205
+ const fromAddress = groupedAddresses.from?.[0];
206
+ const fromFlags = fromAddress
207
+ ? flagsByAddressId.get(fromAddress.addressId)
208
+ : undefined;
209
+ const senderTrust = fromAddress
210
+ ? deriveSenderTrust(fromFlags)
211
+ : SenderTrust.Unknown;
212
+ const envelopeResponse = {
213
+ messageId: envelope?.messageId ?? messageId,
214
+ date: envelope?.dateValue ?? message.internalDate,
215
+ subject: envelope?.subject,
216
+ messageIdValue: envelope?.messageIdValue,
217
+ from: groupedAddresses.from ?? [],
218
+ to: groupedAddresses.to ?? [],
219
+ cc: groupedAddresses.cc ?? [],
220
+ bcc: groupedAddresses.bcc ?? [],
221
+ replyTo: groupedAddresses.reply_to ?? [],
222
+ category: message.category ?? MessageCategory.uncategorized,
223
+ senderTrust,
224
+ };
225
+ const flags = description.messageFlag.map((f) => f.flagName);
226
+ // `accountId` is needed to build per-part `contentUrl` values for the
227
+ // BodyPartResponse list and on the IMAP-backfill path. Resolve it from
228
+ // the bodyStorageKey when the body has already been synced (free — the
229
+ // URI encodes both ids), or fall back to a single Mailbox.get() lookup
230
+ // to avoid a redundant query on the fast path.
231
+ const idsFromKey = message.bodyStorageKey
232
+ ? extractAccountIdsFromBodyKey(message.bodyStorageKey)
233
+ : null;
234
+ let accountId = idsFromKey?.accountId;
235
+ let mailbox;
236
+ if (!accountId) {
237
+ mailbox = await client.mailbox.get(ownedAccountId, message.mailboxId);
238
+ accountId = mailbox.accountId;
239
+ }
240
+ // Trigger an IMAP backfill when the body has never been stored. The
241
+ // fetcher writes the raw .eml + parsed.json.gz cache + per-part rows
242
+ // as a side-effect; the SPA then renders body content via each
243
+ // BodyPartResponse.contentUrl (CloudFront-fronted, JWT-authorized at
244
+ // the edge). The handler no longer returns body text/html — that
245
+ // payload moved to per-part fetches once content delivery cut over
246
+ // (#224 PR 3/3). When bodyStorageKey is already set we skip IMAP
247
+ // entirely; the worker has already populated parts.
248
+ let bodyPartRows = description.bodyPart;
249
+ let bodyStorageKey = message.bodyStorageKey;
250
+ if (!bodyStorageKey) {
251
+ // A message whose body-sync exhausted every retry against real
252
+ // (broken) content never gets a bodyStorageKey (issue #1270 / epic
253
+ // #1281 invariant 3) — this branch is the ONLY place that condition
254
+ // can occur, so the sentinel check lives here instead of unconditionally
255
+ // at the top of the handler, which would tax every already-synced
256
+ // read with an S3 HEAD it can never need. Checking before the backfill
257
+ // attempt stops a client from looping through repeated 202s
258
+ // (materializeBodyParts) or repeated synchronous IMAP round-trips
259
+ // (fetchAndGetBody) on a message that can never succeed.
260
+ if (await isMessageBodySyncBroken(client.storage, accountConfigId, accountId, messageId)) {
261
+ throw new UnrecoverableBodyError("This message's content could not be retrieved. Please report this issue.");
262
+ }
263
+ if (!mailbox) {
264
+ mailbox = await client.mailbox.get(accountId, message.mailboxId);
265
+ }
266
+ // Cheap frugal skip (epic #1281 invariant 6): a mailbox already known
267
+ // paused never even opens a connection. Optimization only —
268
+ // guardConnectionCursor's openBox wrap below is the structural
269
+ // guarantee (issue #1272's read-path detection point).
270
+ if (isCursorRebuildNeeded(mailbox.cursorState)) {
271
+ logger.debug({
272
+ accountId,
273
+ mailboxId: mailbox.mailboxId,
274
+ messageId,
275
+ cursorState: mailbox.cursorState,
276
+ }, "Mailbox cursor not normal; skipping IMAP body backfill for this request");
277
+ }
278
+ else {
279
+ const folder = {
280
+ mailboxId: mailbox.mailboxId,
281
+ fullPath: mailbox.fullPath,
282
+ placement: await resolveFolderPlacement(client.mailboxSpecialUse, accountId, mailbox.mailboxId),
283
+ };
284
+ const scope = await client.createConnectionScope(accountId);
285
+ // Guard at the one choke point every UID-based IMAP op already
286
+ // passes through: openBox. A mismatch trips the mailbox to
287
+ // cursor_invalid and throws; caught below as a routine skip (epic
288
+ // #1281 invariant 3) — the request just returns without a fresh
289
+ // backfill this time.
290
+ const connection = guardConnectionCursor(await scope.getConnection(), { mailboxService: client.mailbox }, accountId, mailbox);
291
+ await client.bodySync
292
+ .fetchAndGetBody(messageId, accountId, accountConfigId, folder, () => Promise.resolve(connection))
293
+ .catch((error) => {
294
+ if (error instanceof MailboxCursorPausedError) {
295
+ logger.debug({
296
+ accountId,
297
+ mailboxId: mailbox?.mailboxId,
298
+ messageId,
299
+ cursorState: error.state,
300
+ }, "Mailbox cursor not normal; skipping IMAP body backfill for this request");
301
+ return;
302
+ }
303
+ throw error;
304
+ })
305
+ .finally(() => scope.disconnect());
306
+ // Body just landed via IMAP — re-read so the response includes the
307
+ // parts the worker just wrote. Without this the first describe of a
308
+ // never-synced message would return an empty bodyParts array.
309
+ const refreshed = await client.message.describe(messageId);
310
+ bodyPartRows = refreshed.bodyPart;
311
+ bodyStorageKey = refreshed.message[0]?.bodyStorageKey;
312
+ }
313
+ }
314
+ // Per-part storage objects are deferred during bulk sync (DEFER_BODY_PARTS)
315
+ // to cut write amplification. Content delivery serves each part straight
316
+ // from storage with no Lambda in the request path, so a deferred (missing)
317
+ // object would surface to the SPA as a hard failure. Materialize them here —
318
+ // idempotent, skips parts already stored — so every contentUrl below
319
+ // resolves on first open. No-op when parts were written eagerly.
320
+ //
321
+ // A missing body object re-arms the SYNC_MESSAGE_BODY cue (so the worker
322
+ // (re)stores it) and leaves the contentUrls to surface a retryable 202 on
323
+ // the content route; any other error is rethrown and 500s loudly.
324
+ await materializeBodyParts(client.bodySync, {
325
+ accountConfigId,
326
+ accountId,
327
+ mailboxId: message.mailboxId,
328
+ messageId,
329
+ uid: message.uid,
330
+ bodyStorageKey,
331
+ cue: client.bodySyncQueue,
332
+ logger,
333
+ });
334
+ const bodyParts = buildBodyPartResponses(bodyPartRows, {
335
+ contentDeliveryDomain: ctx.config.contentDeliveryDomain,
336
+ accountConfigId,
337
+ accountId,
338
+ messageId,
339
+ sign: getContentSigner(ctx.config),
340
+ });
341
+ const references = description.messageReference.map((ref) => ({
342
+ messageIdValue: ref.messageIdValue,
343
+ referenceType: ref.referenceType,
344
+ referenceOrder: ref.referenceOrder,
345
+ }));
346
+ return {
347
+ message: messageSummary,
348
+ envelope: envelopeResponse,
349
+ flags,
350
+ bodyParts,
351
+ references,
352
+ };
353
+ },
354
+ MessageOperations_getRawMessage: async (context, ctx) => {
355
+ const accountConfigId = ctx.identity.accountConfigId;
356
+ const { messageId } = context.request.params;
357
+ const client = ctx.client;
358
+ // Cross-tenant ownership guard. Returning raw RFC822 bytes is sensitive,
359
+ // so resolve the owning account and prove the caller owns it BEFORE any
360
+ // storage read or backfill. `read` mode throws NotFoundError (→ 404) on a
361
+ // foreign message and never leaks the owner's accountConfigId.
362
+ const accountId = unwrap(await ownedMessages(client, [messageId], accountConfigId, "read"));
363
+ // Resolve the message row for its mailboxId / bodyStorageKey.
364
+ const message = await client.message.get(messageId);
365
+ // Backfill from IMAP when the body has never been stored, reusing the
366
+ // exact path describeMessage uses. fetchAndGetBody writes the raw .eml +
367
+ // parsed cache + per-part rows and sets message.bodyStorageKey as a
368
+ // side-effect; we then re-read the row to pick up the freshly written key.
369
+ if (!message.bodyStorageKey) {
370
+ const mailbox = await client.mailbox.get(accountId, message.mailboxId);
371
+ // Cheap frugal skip (epic #1281 invariant 6): a mailbox already known
372
+ // paused never even opens a connection. The existing "no
373
+ // bodyStorageKey after backfill" check just past this block is
374
+ // already the right signal for that case, so no new response shape
375
+ // is needed here.
376
+ if (isCursorRebuildNeeded(mailbox.cursorState)) {
377
+ logger.debug({
378
+ accountId,
379
+ mailboxId: mailbox.mailboxId,
380
+ messageId,
381
+ cursorState: mailbox.cursorState,
382
+ }, "Mailbox cursor not normal; skipping IMAP body backfill for this request");
383
+ }
384
+ else {
385
+ const folder = {
386
+ mailboxId: mailbox.mailboxId,
387
+ fullPath: mailbox.fullPath,
388
+ placement: await resolveFolderPlacement(client.mailboxSpecialUse, accountId, mailbox.mailboxId),
389
+ };
390
+ const scope = await client.createConnectionScope(accountId);
391
+ // Guard at the openBox choke point — a fresh mismatch trips the
392
+ // mailbox and throws once the SELECT reveals it; caught below.
393
+ const connection = guardConnectionCursor(await scope.getConnection(), { mailboxService: client.mailbox }, accountId, mailbox);
394
+ await client.bodySync
395
+ .fetchAndGetBody(messageId, accountId, accountConfigId, folder, () => Promise.resolve(connection))
396
+ .catch((error) => {
397
+ if (error instanceof MailboxCursorPausedError) {
398
+ logger.debug({
399
+ accountId,
400
+ mailboxId: mailbox.mailboxId,
401
+ messageId,
402
+ cursorState: error.state,
403
+ }, "Mailbox cursor not normal; skipping IMAP body backfill for this request");
404
+ return;
405
+ }
406
+ throw error;
407
+ })
408
+ .finally(() => scope.disconnect());
409
+ }
410
+ }
411
+ // Re-read so we have the bodyStorageKey the backfill just wrote (or the
412
+ // key that was already present), then pull the raw bytes from storage.
413
+ const stored = await client.message.get(messageId);
414
+ if (!stored.bodyStorageKey) {
415
+ throw new Error(`Raw source unavailable for message ${messageId}: no bodyStorageKey after backfill`);
416
+ }
417
+ const body = await client.storage.retrieve(stored.bodyStorageKey);
418
+ return { raw: decodeRawEml(body) };
419
+ },
420
+ MessageOperations_updateMessageFlags: async (context, ctx) => {
421
+ const accountConfigId = ctx.identity.accountConfigId;
422
+ const { messageId } = context.request.params;
423
+ const { isRead, isStarred, starColor } = context.request.requestBody;
424
+ const client = ctx.client;
425
+ const accountId = unwrap(await ownedMessages(client, [messageId], accountConfigId, "act"));
426
+ // FlagQueueService handles: MessageFlag + ThreadMessage updates + SQS event
427
+ const result = await client.flagQueue.updateFlags(accountConfigId, messageId, accountId, {
428
+ isRead,
429
+ isStarred,
430
+ starColor: starColor,
431
+ });
432
+ return {
433
+ messageId: result.messageId,
434
+ isRead: result.isRead,
435
+ isStarred: result.isStarred,
436
+ };
437
+ },
438
+ };
439
+ export const MessageBulkOperations = {
440
+ MessageBulkOperations_updateFlags: async (context, ctx) => {
441
+ const accountConfigId = ctx.identity.accountConfigId;
442
+ const { messageIds, isRead, isStarred, starColor } = context.request
443
+ .requestBody;
444
+ if (messageIds.length === 0) {
445
+ return { successCount: 0, failureCount: 0 };
446
+ }
447
+ const client = ctx.client;
448
+ const accountId = unwrap(await ownedMessages(client, messageIds, accountConfigId, "act"));
449
+ // FlagQueueService handles: MessageFlag + ThreadMessage updates + SQS events
450
+ // Process each message individually (same pattern as moveMessages)
451
+ for (const messageId of messageIds) {
452
+ await client.flagQueue.updateFlags(accountConfigId, messageId, accountId, {
453
+ isRead,
454
+ isStarred,
455
+ starColor: starColor,
456
+ });
457
+ }
458
+ return {
459
+ successCount: messageIds.length,
460
+ failureCount: 0,
461
+ };
462
+ },
463
+ MessageBulkOperations_deleteMessages: async (context, ctx) => {
464
+ const accountConfigId = ctx.identity.accountConfigId;
465
+ const { messageIds, permanent } = context.request.requestBody;
466
+ if (messageIds.length === 0) {
467
+ return { successCount: 0, failureCount: 0 };
468
+ }
469
+ const client = ctx.client;
470
+ const accountId = unwrap(await ownedMessages(client, messageIds, accountConfigId, "act"));
471
+ // MessageMoveService handles: Message + ThreadMessage updates + SQS events
472
+ const { refusedMessageIds } = await client.messageMove.deleteMessages(accountConfigId, messageIds, accountId, {
473
+ permanent,
474
+ });
475
+ // A row whose placement changed under the batch is reported, not dropped.
476
+ // The response already carries per-row counts, which is the honest shape
477
+ // for a batch that partly applied — a 409 over it would deny the deletes
478
+ // that did happen, and silence leaves the client's optimistic removal to
479
+ // reappear with nothing said (imap-mutations R3).
480
+ return {
481
+ successCount: messageIds.length - refusedMessageIds.length,
482
+ failureCount: refusedMessageIds.length,
483
+ };
484
+ },
485
+ MessageBulkOperations_moveMessages: async (context, ctx) => {
486
+ const accountConfigId = ctx.identity.accountConfigId;
487
+ const { messageIds, destinationMailboxId } = context.request
488
+ .requestBody;
489
+ if (messageIds.length === 0) {
490
+ return { successCount: 0, failureCount: 0 };
491
+ }
492
+ const client = ctx.client;
493
+ const accountId = unwrap(await ownedMessages(client, messageIds, accountConfigId, "act"));
494
+ // Destination must belong to the same (caller-owned) account, and must be
495
+ // a folder the mail server has settled: the row records the destination
496
+ // before the move confirms, so binding to a folder that may never exist
497
+ // leaves the message pointing at nothing (D12, first row).
498
+ const destination = await client.mailbox.get(accountId, destinationMailboxId);
499
+ if (destination.accountId !== accountId) {
500
+ throw new ForbiddenError(`Destination mailbox ${destinationMailboxId} not in account`);
501
+ }
502
+ unwrap(settledFolder(destination));
503
+ // MessageMoveService handles: Message + ThreadMessage updates + SQS events
504
+ await client.messageMove.moveMessages(accountConfigId, messageIds, destinationMailboxId, accountId);
505
+ return {
506
+ successCount: messageIds.length,
507
+ failureCount: 0,
508
+ };
509
+ },
510
+ MessageBulkOperations_copyMessages: async (context, ctx) => {
511
+ const accountConfigId = ctx.identity.accountConfigId;
512
+ const { messageIds, destinationMailboxId } = context.request
513
+ .requestBody;
514
+ if (messageIds.length === 0) {
515
+ return { successCount: 0, failureCount: 0 };
516
+ }
517
+ const client = ctx.client;
518
+ const accountId = unwrap(await ownedMessages(client, messageIds, accountConfigId, "act"));
519
+ // Destination must belong to the same (caller-owned) account, and must be
520
+ // a folder the mail server has settled: the row records the destination
521
+ // before the move confirms, so binding to a folder that may never exist
522
+ // leaves the message pointing at nothing (D12, first row).
523
+ const destination = await client.mailbox.get(accountId, destinationMailboxId);
524
+ if (destination.accountId !== accountId) {
525
+ throw new ForbiddenError(`Destination mailbox ${destinationMailboxId} not in account`);
526
+ }
527
+ unwrap(settledFolder(destination));
528
+ // MessageMoveService handles: Message copies + ThreadMessage creation + SQS events
529
+ await client.messageMove.copyMessages(accountConfigId, messageIds, destinationMailboxId, accountId);
530
+ return {
531
+ successCount: messageIds.length,
532
+ failureCount: 0,
533
+ };
534
+ },
535
+ MessageBulkOperations_updateMessageLabels: async (context, ctx) => {
536
+ const accountConfigId = ctx.identity.accountConfigId;
537
+ const { messageIds, labelId, action } = context.request.requestBody;
538
+ if (messageIds.length === 0) {
539
+ return { successCount: 0, failureCount: 0 };
540
+ }
541
+ const client = ctx.client;
542
+ unwrap(await ownedMessages(client, messageIds, accountConfigId, "act"));
543
+ // Scoped by accountConfigId (RFC 030) — a foreign labelId 404s here rather
544
+ // than silently applying to messages under someone else's label.
545
+ await client.label.get(accountConfigId, labelId);
546
+ // `appliedByFilterId` stays absent — this is the manual "just these" scope
547
+ // (RFC 034 Decision 3.3), never filter-attributed.
548
+ if (action === "Apply") {
549
+ for (const messageId of messageIds) {
550
+ await client.messageLabel.apply({
551
+ accountConfigId,
552
+ messageId,
553
+ labelId,
554
+ });
555
+ }
556
+ }
557
+ else {
558
+ for (const messageId of messageIds) {
559
+ await client.messageLabel.remove(messageId, labelId);
560
+ }
561
+ }
562
+ return {
563
+ successCount: messageIds.length,
564
+ failureCount: 0,
565
+ };
566
+ },
567
+ MessageBulkOperations_reportSpam: async (context, ctx) => {
568
+ const accountConfigId = ctx.identity.accountConfigId;
569
+ const { messageIds: requestedIds } = context.request.requestBody;
570
+ const messageIds = [...new Set(requestedIds)];
571
+ if (messageIds.length === 0) {
572
+ return { successCount: 0, failureCount: 0 };
573
+ }
574
+ const client = ctx.client;
575
+ const accountId = unwrap(await ownedMessages(client, messageIds, accountConfigId, "act"));
576
+ const setBy = ctx.identity.sub;
577
+ return settleSpamReportBulk(messageIds, (messageId) => client.spamReport.reportSpam({
578
+ accountConfigId,
579
+ accountId,
580
+ messageId,
581
+ setBy,
582
+ }));
583
+ },
584
+ MessageBulkOperations_notSpam: async (context, ctx) => {
585
+ const accountConfigId = ctx.identity.accountConfigId;
586
+ const { messageIds: requestedIds } = context.request.requestBody;
587
+ const messageIds = [...new Set(requestedIds)];
588
+ if (messageIds.length === 0) {
589
+ return { successCount: 0, failureCount: 0 };
590
+ }
591
+ const client = ctx.client;
592
+ const accountId = unwrap(await ownedMessages(client, messageIds, accountConfigId, "act"));
593
+ return settleSpamReportBulk(messageIds, (messageId) => client.spamReport.notSpam({ accountConfigId, accountId, messageId }));
594
+ },
595
+ };
@@ -0,0 +1,10 @@
1
+ import type { OrganizeInput } from "@remit/api-openapi-types";
2
+ import { type OrganizePredicate } from "../service/organize.js";
3
+ import type { OperationHandler, OrganizeJobDetailOperationIds, OrganizeOperationIds } from "../types.js";
4
+ /**
5
+ * Normalize the request body into the flattened predicate the job row and the
6
+ * matcher share.
7
+ */
8
+ export declare const predicateFromInput: (input: OrganizeInput) => OrganizePredicate;
9
+ export declare const OrganizeOperations: Record<OrganizeOperationIds, OperationHandler<OrganizeOperationIds, "accountFanout">>;
10
+ export declare const OrganizeJobDetailOperations: Record<OrganizeJobDetailOperationIds, OperationHandler<OrganizeJobDetailOperationIds, "accountFanout">>;