@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.
- package/dist/dev-server/content-handler.d.ts +38 -0
- package/dist/dev-server/content-handler.js +46 -0
- package/dist/src/api.d.ts +7 -0
- package/dist/src/api.js +144 -0
- package/dist/src/auth.d.ts +30 -0
- package/dist/src/auth.js +73 -0
- package/dist/src/config/msoauth.d.ts +30 -0
- package/dist/src/config/msoauth.js +30 -0
- package/{src/data-backend.ts → dist/src/data-backend.d.ts} +2 -4
- package/dist/src/data-backend.js +12 -0
- package/{src/derive/autoMoved.ts → dist/src/derive/autoMoved.d.ts} +1 -29
- package/dist/src/derive/autoMoved.js +49 -0
- package/dist/src/derive/contentSignature.d.ts +41 -0
- package/{src/derive/contentSignature.ts → dist/src/derive/contentSignature.js} +27 -72
- package/dist/src/derive/contentUrl.d.ts +46 -0
- package/dist/src/derive/contentUrl.js +46 -0
- package/{src/derive/deriveMuted.ts → dist/src/derive/deriveMuted.d.ts} +1 -3
- package/dist/src/derive/deriveMuted.js +7 -0
- package/dist/src/derive/enrichThreadRows.d.ts +118 -0
- package/dist/src/derive/enrichThreadRows.js +151 -0
- package/dist/src/derive/filterThreadCriteria.d.ts +23 -0
- package/dist/src/derive/filterThreadCriteria.js +24 -0
- package/{src/derive/pendingMoveCounts.ts → dist/src/derive/pendingMoveCounts.d.ts} +5 -53
- package/dist/src/derive/pendingMoveCounts.js +53 -0
- package/{src/derive/senderTrust.ts → dist/src/derive/senderTrust.d.ts} +1 -9
- package/dist/src/derive/senderTrust.js +19 -0
- package/dist/src/error.d.ts +21 -0
- package/dist/src/error.js +85 -0
- package/dist/src/handlers/account-guards.d.ts +36 -0
- package/dist/src/handlers/account-guards.js +107 -0
- package/dist/src/handlers/account-oauth.d.ts +14 -0
- package/dist/src/handlers/account-oauth.js +387 -0
- package/dist/src/handlers/account-overrides.d.ts +53 -0
- package/dist/src/handlers/account-overrides.js +233 -0
- package/dist/src/handlers/account-signature.d.ts +28 -0
- package/dist/src/handlers/account-signature.js +85 -0
- package/dist/src/handlers/account.d.ts +6 -0
- package/dist/src/handlers/account.js +350 -0
- package/dist/src/handlers/address.d.ts +35 -0
- package/dist/src/handlers/address.js +141 -0
- package/dist/src/handlers/calendar-event.d.ts +70 -0
- package/dist/src/handlers/calendar-event.js +425 -0
- package/dist/src/handlers/calendar-feed.d.ts +47 -0
- package/dist/src/handlers/calendar-feed.js +192 -0
- package/dist/src/handlers/calendar-suggestion.d.ts +47 -0
- package/dist/src/handlers/calendar-suggestion.js +212 -0
- package/dist/src/handlers/calendar.d.ts +116 -0
- package/dist/src/handlers/calendar.js +330 -0
- package/dist/src/handlers/config.d.ts +43 -0
- package/dist/src/handlers/config.js +176 -0
- package/dist/src/handlers/ensure-account-config.d.ts +9 -0
- package/dist/src/handlers/ensure-account-config.js +27 -0
- package/dist/src/handlers/filter.d.ts +3 -0
- package/dist/src/handlers/filter.js +103 -0
- package/dist/src/handlers/folder-role-appointments.d.ts +68 -0
- package/dist/src/handlers/folder-role-appointments.js +168 -0
- package/dist/src/handlers/folder-role-labels.d.ts +26 -0
- package/dist/src/handlers/folder-role-labels.js +40 -0
- package/dist/src/handlers/folder-role.d.ts +9 -0
- package/dist/src/handlers/folder-role.js +40 -0
- package/dist/src/handlers/index.d.ts +2 -0
- package/dist/src/handlers/index.js +60 -0
- package/dist/src/handlers/label.d.ts +43 -0
- package/dist/src/handlers/label.js +114 -0
- package/dist/src/handlers/mailbox-bindings.d.ts +41 -0
- package/dist/src/handlers/mailbox-bindings.js +61 -0
- package/dist/src/handlers/mailbox.d.ts +23 -0
- package/dist/src/handlers/mailbox.js +219 -0
- package/dist/src/handlers/me.d.ts +2 -0
- package/dist/src/handlers/me.js +138 -0
- package/dist/src/handlers/message.d.ts +84 -0
- package/dist/src/handlers/message.js +607 -0
- package/dist/src/handlers/organize.d.ts +10 -0
- package/dist/src/handlers/organize.js +112 -0
- package/dist/src/handlers/outbox-attachment.d.ts +23 -0
- package/dist/src/handlers/outbox-attachment.js +81 -0
- package/dist/src/handlers/outbox.d.ts +4 -0
- package/dist/src/handlers/outbox.js +144 -0
- package/dist/src/handlers/sync-progress.d.ts +46 -0
- package/dist/src/handlers/sync-progress.js +63 -0
- package/dist/src/handlers/sync.d.ts +28 -0
- package/dist/src/handlers/sync.js +82 -0
- package/dist/src/handlers/system-update.d.ts +2 -0
- package/dist/src/handlers/system-update.js +172 -0
- package/dist/src/handlers/thread.d.ts +148 -0
- package/dist/src/handlers/thread.js +233 -0
- package/dist/src/handlers/unified-threads.d.ts +247 -0
- package/dist/src/handlers/unified-threads.js +409 -0
- package/{src/handlers/vip-suggestions.ts → dist/src/handlers/vip-suggestions.d.ts} +1 -13
- package/dist/src/handlers/vip-suggestions.js +16 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +37 -0
- package/{src/json.ts → dist/src/json.d.ts} +1 -4
- package/dist/src/json.js +10 -0
- package/dist/src/jwt-auth.d.ts +18 -0
- package/dist/src/jwt-auth.js +97 -0
- package/dist/src/request-context.d.ts +9 -0
- package/dist/src/request-context.js +22 -0
- package/dist/src/request.d.ts +3 -0
- package/dist/src/request.js +7 -0
- package/dist/src/response.d.ts +27 -0
- package/dist/src/response.js +87 -0
- package/dist/src/service/account-deletion.d.ts +22 -0
- package/dist/src/service/account-deletion.js +39 -0
- package/dist/src/service/compose-sqlite.d.ts +2 -0
- package/dist/src/service/compose-sqlite.js +11 -0
- package/dist/src/service/create-remit-client.d.ts +95 -0
- package/dist/src/service/create-remit-client.js +207 -0
- package/dist/src/service/data-client.d.ts +12 -0
- package/dist/src/service/data-client.js +44 -0
- package/dist/src/service/export-identity.d.ts +3 -0
- package/dist/src/service/export-identity.js +35 -0
- package/dist/src/service/fire-and-forget.d.ts +54 -0
- package/dist/src/service/fire-and-forget.js +38 -0
- package/dist/src/service/organize.d.ts +184 -0
- package/dist/src/service/organize.js +384 -0
- package/dist/src/service/refusal.d.ts +4 -0
- package/dist/src/service/refusal.js +20 -0
- package/dist/src/service/sender-category.d.ts +51 -0
- package/{src/service/sender-category.ts → dist/src/service/sender-category.js} +40 -81
- package/dist/src/service/sqs.d.ts +1 -0
- package/{src/service/sqs.ts → dist/src/service/sqs.js} +0 -1
- package/dist/src/service/trigger-sync.d.ts +51 -0
- package/dist/src/service/trigger-sync.js +39 -0
- package/dist/src/types.d.ts +49 -0
- package/dist/src/types.js +1 -0
- package/package.json +113 -98
- package/.env.test +0 -4
- package/dev-server/content-auth.test.ts +0 -113
- package/dev-server/content-auth.ts +0 -54
- package/dev-server/content-handler.test.ts +0 -105
- package/dev-server/content-handler.ts +0 -79
- package/dev-server/content-path.test.ts +0 -37
- package/dev-server/content-path.ts +0 -27
- package/dev-server/cors.test.ts +0 -48
- package/dev-server/cors.ts +0 -25
- package/dev-server/keep-alive.test.ts +0 -24
- package/dev-server/lambda-helpers.ts +0 -69
- package/dev-server/log-output.test.ts +0 -173
- package/dev-server/oauth-callback-route.test.ts +0 -66
- package/dev-server/relational-health.test.ts +0 -83
- package/dev-server/relational-health.ts +0 -39
- package/dev-server/server.ts +0 -384
- package/dev-server/sync-age.test.ts +0 -91
- package/dev-server/sync-age.ts +0 -55
- package/dev-server/upload-handler.ts +0 -132
- package/dev-server/upload-route.test.ts +0 -196
- package/scripts/backfill-classification.ts +0 -91
- package/scripts/backfill-list-id.ts +0 -83
- package/scripts/config-save.ts +0 -128
- package/src/auth.test.ts +0 -12
- package/src/auth.ts +0 -92
- package/src/config/msoauth.test.ts +0 -48
- package/src/config/msoauth.ts +0 -48
- package/src/data-backend.test.ts +0 -23
- package/src/derive/autoMoved.test.ts +0 -104
- package/src/derive/contentSignature.test.ts +0 -161
- package/src/derive/contentUrl.test.ts +0 -172
- package/src/derive/contentUrl.ts +0 -86
- package/src/derive/deriveMuted.test.ts +0 -34
- package/src/derive/enrichThreadRows.test.ts +0 -360
- package/src/derive/enrichThreadRows.ts +0 -234
- package/src/derive/filterThreadCriteria.test.ts +0 -126
- package/src/derive/filterThreadCriteria.ts +0 -51
- package/src/derive/senderTrust.test.ts +0 -67
- package/src/error.test.ts +0 -201
- package/src/error.ts +0 -123
- package/src/handlers/account-guards.test.ts +0 -67
- package/src/handlers/account-guards.ts +0 -169
- package/src/handlers/account-oauth-callback.test.ts +0 -155
- package/src/handlers/account-oauth-consent.test.ts +0 -336
- package/src/handlers/account-oauth-start.test.ts +0 -191
- package/src/handlers/account-oauth.test.ts +0 -439
- package/src/handlers/account-oauth.ts +0 -635
- package/src/handlers/account-overrides.test.ts +0 -88
- package/src/handlers/account-overrides.ts +0 -323
- package/src/handlers/account-ownership.ts +0 -25
- package/src/handlers/account-signature.ts +0 -117
- package/src/handlers/account-synced-services.test.ts +0 -245
- package/src/handlers/account.ts +0 -552
- package/src/handlers/address.test.ts +0 -381
- package/src/handlers/address.ts +0 -200
- package/src/handlers/calendar-event.test.ts +0 -685
- package/src/handlers/calendar-event.ts +0 -738
- package/src/handlers/calendar-feed-validators.test.ts +0 -228
- package/src/handlers/calendar-feed.test.ts +0 -439
- package/src/handlers/calendar-feed.ts +0 -291
- package/src/handlers/calendar-sqlite-fixture.ts +0 -36
- package/src/handlers/calendar-suggestion.test.ts +0 -1269
- package/src/handlers/calendar-suggestion.ts +0 -425
- package/src/handlers/calendar.test.ts +0 -1080
- package/src/handlers/calendar.ts +0 -517
- package/src/handlers/config.test.ts +0 -245
- package/src/handlers/config.ts +0 -332
- package/src/handlers/ensure-account-config.ts +0 -30
- package/src/handlers/filter.test.ts +0 -644
- package/src/handlers/filter.ts +0 -518
- package/src/handlers/folder-role-appointments.test.ts +0 -438
- package/src/handlers/folder-role-appointments.ts +0 -249
- package/src/handlers/folder-role-labels.ts +0 -63
- package/src/handlers/folder-role.test.ts +0 -209
- package/src/handlers/folder-role.ts +0 -80
- package/src/handlers/index.ts +0 -83
- package/src/handlers/label.test.ts +0 -112
- package/src/handlers/label.ts +0 -210
- package/src/handlers/mailbox-bindings.ts +0 -111
- package/src/handlers/mailbox-rename-refusal.test.ts +0 -247
- package/src/handlers/mailbox-settled-target.test.ts +0 -251
- package/src/handlers/mailbox.test.ts +0 -280
- package/src/handlers/mailbox.ts +0 -581
- package/src/handlers/me.test.ts +0 -81
- package/src/handlers/me.ts +0 -203
- package/src/handlers/message-read-backfill.test.ts +0 -103
- package/src/handlers/message.test.ts +0 -131
- package/src/handlers/message.ts +0 -1131
- package/src/handlers/organize.test.ts +0 -277
- package/src/handlers/organize.ts +0 -197
- package/src/handlers/outbox-attachment.test.ts +0 -765
- package/src/handlers/outbox-attachment.ts +0 -126
- package/src/handlers/outbox-list.test.ts +0 -179
- package/src/handlers/outbox.test.ts +0 -772
- package/src/handlers/outbox.ts +0 -258
- package/src/handlers/search.test.ts +0 -161
- package/src/handlers/search.ts +0 -105
- package/src/handlers/sync-progress.test.ts +0 -205
- package/src/handlers/sync-progress.ts +0 -99
- package/src/handlers/sync.test.ts +0 -168
- package/src/handlers/sync.ts +0 -118
- package/src/handlers/system-update.test.ts +0 -497
- package/src/handlers/system-update.ts +0 -230
- package/src/handlers/thread.test.ts +0 -396
- package/src/handlers/thread.ts +0 -427
- package/src/handlers/unified-threads-mail-sources.sqlite.test.ts +0 -197
- package/src/handlers/unified-threads.test.ts +0 -785
- package/src/handlers/unified-threads.ts +0 -669
- package/src/index.test.ts +0 -83
- package/src/index.ts +0 -228
- package/src/jwt-auth.test.ts +0 -152
- package/src/jwt-auth.ts +0 -132
- package/src/log-redaction.test.ts +0 -144
- package/src/openapi-spec.test.ts +0 -118
- package/src/request-context.test.ts +0 -61
- package/src/request-context.ts +0 -33
- package/src/request-pipeline.test.ts +0 -88
- package/src/request.ts +0 -10
- package/src/response.test.ts +0 -130
- package/src/response.ts +0 -144
- package/src/service/compose-sqlite.ts +0 -98
- package/src/service/config-import.ts +0 -39
- package/src/service/create-remit-client.ts +0 -521
- package/src/service/data-client.test.ts +0 -97
- package/src/service/data-client.ts +0 -58
- package/src/service/export-identity.ts +0 -39
- package/src/service/filter.ts +0 -40
- package/src/service/fire-and-forget.test.ts +0 -126
- package/src/service/fire-and-forget.ts +0 -79
- package/src/service/localhost-env-config.test.ts +0 -36
- package/src/service/organize-queue.ts +0 -98
- package/src/service/organize.test.ts +0 -1451
- package/src/service/organize.ts +0 -1025
- package/src/service/read-path-filter-config.test.ts +0 -243
- package/src/service/semantic-capability.test.ts +0 -78
- package/src/service/semantic-capability.ts +0 -95
- package/src/service/sender-category.test.ts +0 -235
- package/src/service/trigger-sync.test.ts +0 -205
- package/src/service/trigger-sync.ts +0 -100
- package/src/stdout-capture-fixture.ts +0 -40
- package/src/types.ts +0 -271
- 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>>;
|