@remit/backend 0.0.142 → 0.0.144

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