@remit/mailbox-service 0.0.110 → 0.0.112
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/package.json +21 -6
- package/src/body-part-mapper.ts +51 -1
- package/src/body-sync-queue.test.ts +39 -99
- package/src/body-sync-terminal.test.ts +115 -277
- package/src/body-sync-terminal.ts +10 -12
- package/src/body-sync.test.ts +925 -0
- package/src/body-sync.ts +226 -200
- package/src/classification-backfill.test.ts +197 -527
- package/src/classify-message.test.ts +11 -74
- package/src/external-move.test.ts +102 -209
- package/src/external-move.ts +13 -0
- package/src/filter-operations.test.ts +319 -0
- package/src/filter-operations.ts +377 -0
- package/src/filter-update.test.ts +313 -0
- package/src/filters/filter-config.test.ts +55 -197
- package/src/filters/filter-config.ts +4 -39
- package/src/filters/match.test.ts +1 -38
- package/src/filters/match.ts +3 -52
- package/src/filters/pipeline.test.ts +158 -289
- package/src/filters/pipeline.ts +56 -170
- package/src/flag-push-terminal.ts +9 -11
- package/src/flag-push.test.ts +76 -372
- package/src/flag-queue.test.ts +197 -0
- package/src/folder-operations.test.ts +273 -0
- package/src/folder-operations.ts +217 -0
- package/src/forwarded-message.test.ts +122 -0
- package/src/imapflow-connect-retry.test.ts +76 -113
- package/src/imapflow-connection.test.ts +34 -0
- package/src/imapflow-connection.ts +19 -15
- package/src/index.ts +59 -12
- package/src/list-id-backfill.test.ts +133 -499
- package/src/mailbox-cursor.test.ts +81 -189
- package/src/mailbox-delete-intent.test.ts +78 -205
- package/src/mailbox-intent.ts +5 -3
- package/src/mailbox-management.test.ts +107 -220
- package/src/mailbox-management.ts +1 -2
- package/src/mailbox-presence.test.ts +36 -34
- package/src/mailbox-queue.ts +2 -20
- package/src/mailbox-rename-intent.test.ts +156 -325
- package/src/mailbox-rename-subtree.test.ts +205 -582
- package/src/mailbox-sync.test.ts +182 -744
- package/src/message-move.test.ts +726 -0
- package/src/message-move.ts +227 -57
- package/src/message-read.ts +87 -0
- package/src/message-save-retry.test.ts +90 -0
- package/src/message-sync-flags.test.ts +10 -1
- package/src/message-sync.test.ts +762 -0
- package/src/message-sync.ts +409 -239
- package/src/mime-walker.ts +16 -15
- package/src/organize-job.ts +93 -0
- package/src/outbox-attachment.test.ts +97 -209
- package/src/outbox-failed-recovery.test.ts +198 -269
- package/src/outbox-queue.test.ts +24 -0
- package/src/outbox-queue.ts +25 -8
- package/src/ownership.ts +140 -0
- package/src/placement-move-terminal.ts +9 -12
- package/src/placement-move.ts +33 -6
- package/src/placement-settled.ts +28 -4
- package/src/placement.test.ts +404 -0
- package/src/quarantine.test.ts +90 -148
- package/src/quarantine.ts +3 -2
- package/src/refusal.ts +29 -0
- package/src/restore-source-placement.ts +0 -4
- package/src/spam-report.test.ts +238 -710
- package/src/sync-warning.test.ts +213 -0
- package/src/sync-warning.ts +56 -0
- package/src/test-helpers/fake-imap.ts +690 -0
- package/src/test-helpers/test-queue.ts +56 -0
- package/src/test-helpers/test-store.ts +299 -0
- package/src/thread-root.test.ts +29 -0
- package/src/thread-root.ts +51 -0
- package/src/types/mailparser-augment.d.ts +8 -0
- package/src/types.ts +1 -1
- package/src/adversarial-mime.e2e.test.ts +0 -307
- package/src/body-sync-auto-read-writeonce.test.ts +0 -219
- package/src/body-sync-calendar-suggestion.test.ts +0 -433
- package/src/body-sync-category-denormalize.test.ts +0 -352
- package/src/body-sync-category-override.test.ts +0 -501
- package/src/body-sync-category-writeonce.test.ts +0 -234
- package/src/body-sync-filter.test.ts +0 -237
- package/src/body-sync-placement-flags.test.ts +0 -347
- package/src/body-sync-placement-writeonce.test.ts +0 -337
- package/src/body-sync-quarantine.test.ts +0 -276
- package/src/body-sync-resync.test.ts +0 -605
- package/src/body-sync-unclassifiable-once.test.ts +0 -260
- package/src/body-sync-unsubscribed-auto-read.test.ts +0 -176
- package/src/filters/anchor-drift.ts +0 -55
- package/src/filters/message-embedder.test.ts +0 -39
- package/src/filters/message-embedder.ts +0 -33
- package/src/flag-push-terminal.test.ts +0 -209
- package/src/flag-queue-stars.test.ts +0 -171
- package/src/imapflow-connection.e2e.test.ts +0 -335
- package/src/imapflow-connection.integ.test.ts +0 -316
- package/src/message-delete-junk-reconcile.test.ts +0 -117
- package/src/message-delete-no-trash.test.ts +0 -245
- package/src/message-delete-unsettled-placement.test.ts +0 -396
- package/src/message-move-copy.test.ts +0 -562
- package/src/message-move-empty-trash.test.ts +0 -326
- package/src/message-move-same-mailbox.test.ts +0 -139
- package/src/message-move-unsettled-placement.test.ts +0 -378
- package/src/message-sync-address-spoof.test.ts +0 -293
- package/src/message-sync-category.test.ts +0 -142
- package/src/message-sync-changedsince.test.ts +0 -840
- package/src/message-sync-junk-harvest.test.ts +0 -244
- package/src/message-sync-quarantine.test.ts +0 -416
- package/src/message-sync-repoint.test.ts +0 -419
- package/src/outbox-queue-settled-row.test.ts +0 -140
- package/src/pass-through-unit-of-work.ts +0 -17
- package/src/placement-move-terminal.test.ts +0 -300
- package/src/placement-move-unsettled.test.ts +0 -275
- package/src/placement-transition-race.test.ts +0 -482
- package/src/stale-message-reconcile.test.ts +0 -82
- package/src/stale-message-reconcile.ts +0 -55
- package/src/test-helpers/isolated-mailbox.ts +0 -44
- package/src/test-helpers/mailfuzz-connection.ts +0 -27
- package/test/fixtures/mime-adversarial-e2e/01-octet-stream-pdf.eml +0 -21
- package/test/fixtures/mime-adversarial-e2e/02-nested-related-cid-image.eml +0 -31
- package/test/fixtures/mime-adversarial-e2e/03-calendar-invite.eml +0 -33
- package/test/fixtures/mime-adversarial-e2e/04-forwarded-rfc822-with-attachments.eml +0 -42
- package/test/fixtures/mime-adversarial-e2e/05-tnef-winmail.eml +0 -25
- package/test/fixtures/mime-adversarial-e2e/06-8bit-non-ascii.eml +0 -15
- package/test/fixtures/mime-adversarial-e2e/07-quoted-printable-soft-breaks.eml +0 -17
- package/test/fixtures/mime-adversarial-e2e/08-alternative-empty-text.eml +0 -18
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { afterEach, beforeEach, describe, it } from "node:test";
|
|
3
|
+
import type { AccountItem, FilterItem, MailboxItem } from "@remit/data-ports";
|
|
4
|
+
import {
|
|
5
|
+
FilterDisabledReason,
|
|
6
|
+
FilterMatchOperator,
|
|
7
|
+
FilterScope,
|
|
8
|
+
FilterState,
|
|
9
|
+
MailboxSyncStatus,
|
|
10
|
+
} from "@remit/domain-enums";
|
|
11
|
+
import {
|
|
12
|
+
createFilter,
|
|
13
|
+
type FilterPatch,
|
|
14
|
+
listFilters,
|
|
15
|
+
type NewFilter,
|
|
16
|
+
updateFilter,
|
|
17
|
+
} from "./filter-operations.js";
|
|
18
|
+
import type { Refusable } from "./refusal.js";
|
|
19
|
+
import {
|
|
20
|
+
captureQueue,
|
|
21
|
+
openStore,
|
|
22
|
+
type QueueCapture,
|
|
23
|
+
type Store,
|
|
24
|
+
seedAccount,
|
|
25
|
+
seedMailbox,
|
|
26
|
+
} from "./test-helpers/test-store.js";
|
|
27
|
+
|
|
28
|
+
let store: Store;
|
|
29
|
+
let account: AccountItem;
|
|
30
|
+
let capture: QueueCapture;
|
|
31
|
+
|
|
32
|
+
const FANOUT_QUEUE = "http://localhost:4566/000000000000/account-fanout";
|
|
33
|
+
|
|
34
|
+
beforeEach(async () => {
|
|
35
|
+
capture = captureQueue();
|
|
36
|
+
store = await openStore();
|
|
37
|
+
account = await seedAccount(store);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterEach(async () => {
|
|
41
|
+
capture.restore();
|
|
42
|
+
await store.close();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const deps = () => ({
|
|
46
|
+
account: store.account,
|
|
47
|
+
mailbox: store.mailbox,
|
|
48
|
+
filter: store.filter,
|
|
49
|
+
organizeJobRequest: store.organizeJobRequest,
|
|
50
|
+
sqsQueueUrl: FANOUT_QUEUE,
|
|
51
|
+
logger: { info: () => {} },
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const rule = (actionMailboxId: string): NewFilter => ({
|
|
55
|
+
name: "Receipts",
|
|
56
|
+
scope: FilterScope.Standing,
|
|
57
|
+
expiresAt: undefined,
|
|
58
|
+
matchOperator: FilterMatchOperator.And,
|
|
59
|
+
literalClauses: [{ field: "From", value: "billing@example.com" }],
|
|
60
|
+
actionLabelId: "None",
|
|
61
|
+
actionMailboxId,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const refusalOf = <T>(outcome: Refusable<T>) => {
|
|
65
|
+
assert.equal(outcome.ok, false, "expected a refusal");
|
|
66
|
+
return outcome.ok ? undefined : outcome.refusal;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const pendingFolder = async (fullPath: string): Promise<MailboxItem> => {
|
|
70
|
+
const created = await store.mailbox.create({
|
|
71
|
+
accountId: account.accountId,
|
|
72
|
+
namespacePrefix: "",
|
|
73
|
+
hierarchyDelimiter: "/",
|
|
74
|
+
fullPath,
|
|
75
|
+
uidValidity: 0,
|
|
76
|
+
uidNext: 1,
|
|
77
|
+
highestModseq: "0",
|
|
78
|
+
messageCount: 0,
|
|
79
|
+
unseenCount: 0,
|
|
80
|
+
deletedCount: 0,
|
|
81
|
+
totalSize: 0,
|
|
82
|
+
lastSyncUid: 0,
|
|
83
|
+
highWaterMarkUid: 0,
|
|
84
|
+
lastMessageSyncAt: 0,
|
|
85
|
+
syncStatus: MailboxSyncStatus.pending,
|
|
86
|
+
});
|
|
87
|
+
if (created.outcome !== "Created") throw new Error(`${fullPath} exists`);
|
|
88
|
+
return created.mailbox;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const storedFilter = (over: Partial<FilterItem>): Promise<FilterItem> =>
|
|
92
|
+
store.filter.create({
|
|
93
|
+
accountConfigId: account.accountConfigId,
|
|
94
|
+
...rule("None"),
|
|
95
|
+
...over,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe("createFilter", () => {
|
|
99
|
+
it("stores the filter and queues its pass over existing mail", async () => {
|
|
100
|
+
const folder = await seedMailbox(store, account, "Receipts");
|
|
101
|
+
|
|
102
|
+
const outcome = await createFilter(
|
|
103
|
+
deps(),
|
|
104
|
+
{ accountConfigId: account.accountConfigId, userId: "user-1" },
|
|
105
|
+
{ accountId: account.accountId, filter: rule(folder.mailboxId) },
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
assert.equal(outcome.ok, true);
|
|
109
|
+
if (!outcome.ok) return;
|
|
110
|
+
const job = await store.organizeJobRequest.get(
|
|
111
|
+
outcome.value.organizeJob.organizeJobId,
|
|
112
|
+
);
|
|
113
|
+
assert.equal(job.filterId, outcome.value.filter.filterId);
|
|
114
|
+
assert.equal(job.userId, "user-1");
|
|
115
|
+
assert.deepEqual(job.literalClauses, []);
|
|
116
|
+
assert.equal(job.actionMailboxId, "None");
|
|
117
|
+
assert.deepEqual(capture.sent, [
|
|
118
|
+
{
|
|
119
|
+
queueUrl: FANOUT_QUEUE,
|
|
120
|
+
body: {
|
|
121
|
+
type: "OrganizeJob",
|
|
122
|
+
accountConfigId: account.accountConfigId,
|
|
123
|
+
organizeJobId: job.organizeJobId,
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
]);
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("waits for the folder it moves mail into, storing nothing", async () => {
|
|
130
|
+
const folder = await pendingFolder("Receipts");
|
|
131
|
+
|
|
132
|
+
const outcome = await createFilter(
|
|
133
|
+
deps(),
|
|
134
|
+
{ accountConfigId: account.accountConfigId, userId: "user-1" },
|
|
135
|
+
{ accountId: account.accountId, filter: rule(folder.mailboxId) },
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
assert.deepEqual(refusalOf(outcome), {
|
|
139
|
+
kind: "NotSettled",
|
|
140
|
+
message:
|
|
141
|
+
"“Receipts” isn’t ready yet — the mail server hasn’t confirmed it.",
|
|
142
|
+
mailboxId: folder.mailboxId,
|
|
143
|
+
syncStatus: MailboxSyncStatus.pending,
|
|
144
|
+
});
|
|
145
|
+
assert.deepEqual(
|
|
146
|
+
await store.filter.listByAccountConfig(account.accountConfigId),
|
|
147
|
+
[],
|
|
148
|
+
);
|
|
149
|
+
assert.deepEqual(capture.sent, []);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("refuses a Temporary filter without an expiry, storing nothing", async () => {
|
|
153
|
+
const folder = await seedMailbox(store, account, "Receipts");
|
|
154
|
+
|
|
155
|
+
const outcome = await createFilter(
|
|
156
|
+
deps(),
|
|
157
|
+
{ accountConfigId: account.accountConfigId, userId: "user-1" },
|
|
158
|
+
{
|
|
159
|
+
accountId: account.accountId,
|
|
160
|
+
filter: { ...rule(folder.mailboxId), scope: FilterScope.Temporary },
|
|
161
|
+
},
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
assert.deepEqual(refusalOf(outcome), {
|
|
165
|
+
kind: "Invalid",
|
|
166
|
+
message:
|
|
167
|
+
"A Temporary filter needs expiresAt — pick a date, or switch scope to Standing.",
|
|
168
|
+
});
|
|
169
|
+
assert.deepEqual(
|
|
170
|
+
await store.filter.listByAccountConfig(account.accountConfigId),
|
|
171
|
+
[],
|
|
172
|
+
);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe("updateFilter", () => {
|
|
177
|
+
it("writes only the fields a patch may set, dropping server-derived ones", async () => {
|
|
178
|
+
const filter = await storedFilter({});
|
|
179
|
+
const smuggled: Record<string, unknown> = {
|
|
180
|
+
name: "Bills",
|
|
181
|
+
ttl: 123,
|
|
182
|
+
disabledReason: FilterDisabledReason.FolderMissing,
|
|
183
|
+
ruleChangedAt: 999,
|
|
184
|
+
filterId: "sneaky",
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const outcome = await updateFilter(deps(), account.accountConfigId, {
|
|
188
|
+
accountId: account.accountId,
|
|
189
|
+
filterId: filter.filterId,
|
|
190
|
+
patch: smuggled as FilterPatch,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
assert.equal(outcome.ok, true);
|
|
194
|
+
const stored = await store.filter.get(
|
|
195
|
+
account.accountConfigId,
|
|
196
|
+
filter.filterId,
|
|
197
|
+
);
|
|
198
|
+
assert.equal(stored.name, "Bills");
|
|
199
|
+
assert.equal(stored.ttl, undefined);
|
|
200
|
+
assert.equal(stored.disabledReason, FilterDisabledReason.None);
|
|
201
|
+
assert.equal(stored.ruleChangedAt, filter.ruleChangedAt);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("records UserDisabled when the user turns a filter off", async () => {
|
|
205
|
+
const filter = await storedFilter({});
|
|
206
|
+
|
|
207
|
+
await updateFilter(deps(), account.accountConfigId, {
|
|
208
|
+
accountId: account.accountId,
|
|
209
|
+
filterId: filter.filterId,
|
|
210
|
+
patch: { state: FilterState.Disabled },
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
const stored = await store.filter.get(
|
|
214
|
+
account.accountConfigId,
|
|
215
|
+
filter.filterId,
|
|
216
|
+
);
|
|
217
|
+
assert.equal(stored.state, FilterState.Disabled);
|
|
218
|
+
assert.equal(stored.disabledReason, FilterDisabledReason.UserDisabled);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it("refuses to bind to a folder the mail server has not confirmed", async () => {
|
|
222
|
+
const filter = await storedFilter({});
|
|
223
|
+
const folder = await pendingFolder("Receipts");
|
|
224
|
+
|
|
225
|
+
const outcome = await updateFilter(deps(), account.accountConfigId, {
|
|
226
|
+
accountId: account.accountId,
|
|
227
|
+
filterId: filter.filterId,
|
|
228
|
+
patch: { actionMailboxId: folder.mailboxId },
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
assert.equal(refusalOf(outcome)?.kind, "NotSettled");
|
|
232
|
+
const stored = await store.filter.get(
|
|
233
|
+
account.accountConfigId,
|
|
234
|
+
filter.filterId,
|
|
235
|
+
);
|
|
236
|
+
assert.equal(stored.actionMailboxId, "None");
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("will not turn on a filter whose folder is gone", async () => {
|
|
240
|
+
const filter = await storedFilter({
|
|
241
|
+
actionMailboxId: "mbx-gone",
|
|
242
|
+
state: FilterState.Disabled,
|
|
243
|
+
disabledReason: FilterDisabledReason.UserDisabled,
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
const outcome = await updateFilter(deps(), account.accountConfigId, {
|
|
247
|
+
accountId: account.accountId,
|
|
248
|
+
filterId: filter.filterId,
|
|
249
|
+
patch: { state: FilterState.Active },
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
assert.deepEqual(refusalOf(outcome), {
|
|
253
|
+
kind: "Invalid",
|
|
254
|
+
message:
|
|
255
|
+
"The folder this filter moves mail into no longer exists. Pick another folder, then turn the filter on.",
|
|
256
|
+
});
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("refuses a Temporary filter without an expiry", async () => {
|
|
260
|
+
const filter = await storedFilter({});
|
|
261
|
+
|
|
262
|
+
const outcome = await updateFilter(deps(), account.accountConfigId, {
|
|
263
|
+
accountId: account.accountId,
|
|
264
|
+
filterId: filter.filterId,
|
|
265
|
+
patch: { scope: FilterScope.Temporary },
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
assert.deepEqual(refusalOf(outcome), {
|
|
269
|
+
kind: "Invalid",
|
|
270
|
+
message:
|
|
271
|
+
"A Temporary filter needs expiresAt — pick a date, or switch scope to Standing.",
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("renames a filter", async () => {
|
|
276
|
+
const filter = await storedFilter({});
|
|
277
|
+
|
|
278
|
+
const outcome = await updateFilter(deps(), account.accountConfigId, {
|
|
279
|
+
accountId: account.accountId,
|
|
280
|
+
filterId: filter.filterId,
|
|
281
|
+
patch: { name: "Bills" },
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
assert.equal(outcome.ok && outcome.value.name, "Bills");
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
describe("listFilters", () => {
|
|
289
|
+
it("expires a lapsed Temporary filter as it reads it", async () => {
|
|
290
|
+
await storedFilter({
|
|
291
|
+
scope: FilterScope.Temporary,
|
|
292
|
+
expiresAt: "2020-01-01T00:00:00+00:00",
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
const outcome = await listFilters(deps(), account.accountConfigId, {
|
|
296
|
+
accountId: account.accountId,
|
|
297
|
+
continuationToken: undefined,
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
assert.deepEqual(
|
|
301
|
+
outcome.ok && outcome.value.items.map((item) => item.state),
|
|
302
|
+
[FilterState.Expired],
|
|
303
|
+
);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("answers another tenant's account as not found", async () => {
|
|
307
|
+
const other = await seedAccount(store, { accountConfigId: "cfg-other" });
|
|
308
|
+
|
|
309
|
+
const outcome = await listFilters(deps(), account.accountConfigId, {
|
|
310
|
+
accountId: other.accountId,
|
|
311
|
+
continuationToken: undefined,
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
assert.deepEqual(refusalOf(outcome), {
|
|
315
|
+
kind: "NotFound",
|
|
316
|
+
message: `Account not found: ${other.accountId}`,
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
});
|
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deriveFilterTtl,
|
|
3
|
+
FILTER_NO_ACTION,
|
|
4
|
+
type FilterItem,
|
|
5
|
+
type IAccountRepository,
|
|
6
|
+
type IFilterRepository,
|
|
7
|
+
type IMailboxRepository,
|
|
8
|
+
type MailboxItem,
|
|
9
|
+
type OrganizeJobRequestItem,
|
|
10
|
+
type UpdateFilterInput,
|
|
11
|
+
} from "@remit/data-ports";
|
|
12
|
+
import {
|
|
13
|
+
FilterDisabledReason,
|
|
14
|
+
FilterScope,
|
|
15
|
+
FilterState,
|
|
16
|
+
} from "@remit/domain-enums";
|
|
17
|
+
import {
|
|
18
|
+
type OrganizeJobDeps,
|
|
19
|
+
type OrganizeJobOwner,
|
|
20
|
+
queueOrganizeJob,
|
|
21
|
+
} from "./organize-job.js";
|
|
22
|
+
import { folderInAccount, ownedAccount, settledFolder } from "./ownership.js";
|
|
23
|
+
import { accept, invalid, type Refusable } from "./refusal.js";
|
|
24
|
+
|
|
25
|
+
export type NewFilter = Pick<
|
|
26
|
+
FilterItem,
|
|
27
|
+
| "name"
|
|
28
|
+
| "scope"
|
|
29
|
+
| "expiresAt"
|
|
30
|
+
| "matchOperator"
|
|
31
|
+
| "literalClauses"
|
|
32
|
+
| "actionLabelId"
|
|
33
|
+
| "actionMailboxId"
|
|
34
|
+
>;
|
|
35
|
+
|
|
36
|
+
export type FilterPatch = Pick<
|
|
37
|
+
UpdateFilterInput,
|
|
38
|
+
| "name"
|
|
39
|
+
| "scope"
|
|
40
|
+
| "expiresAt"
|
|
41
|
+
| "matchOperator"
|
|
42
|
+
| "literalClauses"
|
|
43
|
+
| "actionLabelId"
|
|
44
|
+
| "actionMailboxId"
|
|
45
|
+
| "state"
|
|
46
|
+
>;
|
|
47
|
+
|
|
48
|
+
interface ResolvedScopeExpiry {
|
|
49
|
+
scope: FilterItem["scope"];
|
|
50
|
+
expiresAt?: string;
|
|
51
|
+
ttl?: number;
|
|
52
|
+
state: FilterItem["state"];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface FilterPage {
|
|
56
|
+
items: FilterItem[];
|
|
57
|
+
continuationToken?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface CreatedFilter {
|
|
61
|
+
filter: FilterItem;
|
|
62
|
+
organizeJob: OrganizeJobRequestItem;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
interface AccountDeps {
|
|
66
|
+
account: Pick<IAccountRepository, "get">;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface ActionFolderDeps {
|
|
70
|
+
mailbox: Pick<IMailboxRepository, "resolveAccountId"> & {
|
|
71
|
+
get(accountId: string, mailboxId: string): Promise<MailboxItem>;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Only these keys reach the repo, and a key absent from the body stays absent,
|
|
76
|
+
// so a rename never bumps ruleChangedAt and a smuggled ttl is dropped.
|
|
77
|
+
const pickFilterUpdate = (body: FilterPatch): FilterPatch => {
|
|
78
|
+
const patch: FilterPatch = {};
|
|
79
|
+
if (Object.hasOwn(body, "name")) patch.name = body.name;
|
|
80
|
+
if (Object.hasOwn(body, "scope")) patch.scope = body.scope;
|
|
81
|
+
if (Object.hasOwn(body, "expiresAt")) patch.expiresAt = body.expiresAt;
|
|
82
|
+
if (Object.hasOwn(body, "matchOperator")) {
|
|
83
|
+
patch.matchOperator = body.matchOperator;
|
|
84
|
+
}
|
|
85
|
+
if (Object.hasOwn(body, "literalClauses")) {
|
|
86
|
+
patch.literalClauses = body.literalClauses;
|
|
87
|
+
}
|
|
88
|
+
if (Object.hasOwn(body, "actionLabelId")) {
|
|
89
|
+
patch.actionLabelId = body.actionLabelId;
|
|
90
|
+
}
|
|
91
|
+
if (Object.hasOwn(body, "actionMailboxId")) {
|
|
92
|
+
patch.actionMailboxId = body.actionMailboxId;
|
|
93
|
+
}
|
|
94
|
+
if (Object.hasOwn(body, "state")) patch.state = body.state;
|
|
95
|
+
return patch;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// A Temporary filter always carries expiresAt; outside Temporary both
|
|
99
|
+
// expiresAt and the table-wide ttl stay absent, or the row would be swept.
|
|
100
|
+
const resolveFilterScopeExpiry = (
|
|
101
|
+
current: Pick<FilterItem, "scope" | "expiresAt">,
|
|
102
|
+
patch: FilterPatch,
|
|
103
|
+
): Refusable<ResolvedScopeExpiry> => {
|
|
104
|
+
const scope = patch.scope ?? current.scope;
|
|
105
|
+
|
|
106
|
+
if (scope !== FilterScope.Temporary) {
|
|
107
|
+
if (Object.hasOwn(patch, "expiresAt") && patch.expiresAt) {
|
|
108
|
+
return invalid(
|
|
109
|
+
"expiresAt only applies to a Temporary filter — switch scope to Temporary to set one.",
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
return accept({
|
|
113
|
+
scope,
|
|
114
|
+
expiresAt: undefined,
|
|
115
|
+
ttl: undefined,
|
|
116
|
+
state: FilterState.Active,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const expiresAt = Object.hasOwn(patch, "expiresAt")
|
|
121
|
+
? patch.expiresAt
|
|
122
|
+
: current.expiresAt;
|
|
123
|
+
if (!expiresAt) {
|
|
124
|
+
return invalid(
|
|
125
|
+
"A Temporary filter needs expiresAt — pick a date, or switch scope to Standing.",
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const ttl = deriveFilterTtl(scope, expiresAt);
|
|
130
|
+
const state =
|
|
131
|
+
new Date(expiresAt).getTime() > Date.now()
|
|
132
|
+
? FilterState.Active
|
|
133
|
+
: FilterState.Expired;
|
|
134
|
+
|
|
135
|
+
return accept({ scope, expiresAt, ttl, state });
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const hasLapsed = (
|
|
139
|
+
filter: Pick<FilterItem, "scope" | "expiresAt">,
|
|
140
|
+
now: number,
|
|
141
|
+
): boolean =>
|
|
142
|
+
filter.scope === FilterScope.Temporary &&
|
|
143
|
+
filter.expiresAt !== undefined &&
|
|
144
|
+
new Date(filter.expiresAt).getTime() <= now;
|
|
145
|
+
|
|
146
|
+
const FOLDERLESS_REASONS: ReadonlySet<FilterItem["disabledReason"]> = new Set([
|
|
147
|
+
FilterDisabledReason.AwaitingFolder,
|
|
148
|
+
FilterDisabledReason.FolderCreateFailed,
|
|
149
|
+
]);
|
|
150
|
+
|
|
151
|
+
const folderlessMessage = (reason: FilterItem["disabledReason"]): string =>
|
|
152
|
+
reason === FilterDisabledReason.AwaitingFolder
|
|
153
|
+
? "This filter has no folder to move mail into yet. Wait for its folder to be created on the mail server, or pick a folder in the rule, then turn it on."
|
|
154
|
+
: "The mail server refused to create this filter's folder. Pick a folder in the rule, then turn it on.";
|
|
155
|
+
|
|
156
|
+
export const resolveFilterUpdate = (
|
|
157
|
+
current: Pick<
|
|
158
|
+
FilterItem,
|
|
159
|
+
"scope" | "expiresAt" | "state" | "disabledReason" | "actionMailboxId"
|
|
160
|
+
>,
|
|
161
|
+
patch: FilterPatch,
|
|
162
|
+
now: number = Date.now(),
|
|
163
|
+
): Refusable<UpdateFilterInput> => {
|
|
164
|
+
if (patch.state === FilterState.Expired) {
|
|
165
|
+
return invalid(
|
|
166
|
+
"A filter expires through its date, not its state. Set it Active or Disabled, or change its expiry.",
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
const folderless =
|
|
170
|
+
current.state === FilterState.Disabled &&
|
|
171
|
+
FOLDERLESS_REASONS.has(current.disabledReason);
|
|
172
|
+
const folder = patch.actionMailboxId ?? current.actionMailboxId;
|
|
173
|
+
const picked = folderless && folder !== FILTER_NO_ACTION;
|
|
174
|
+
const pickedReason = picked
|
|
175
|
+
? { disabledReason: FilterDisabledReason.UserDisabled }
|
|
176
|
+
: {};
|
|
177
|
+
|
|
178
|
+
const touchesTiming =
|
|
179
|
+
Object.hasOwn(patch, "scope") || Object.hasOwn(patch, "expiresAt");
|
|
180
|
+
if (!touchesTiming && patch.state === undefined) {
|
|
181
|
+
return accept({ ...patch, ...pickedReason });
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let timing: ResolvedScopeExpiry | undefined;
|
|
185
|
+
if (touchesTiming) {
|
|
186
|
+
const resolved = resolveFilterScopeExpiry(current, patch);
|
|
187
|
+
if (!resolved.ok) return resolved;
|
|
188
|
+
timing = resolved.value;
|
|
189
|
+
}
|
|
190
|
+
const lapsed = timing
|
|
191
|
+
? timing.state === FilterState.Expired
|
|
192
|
+
: hasLapsed(current, now);
|
|
193
|
+
|
|
194
|
+
if (patch.state === FilterState.Active && lapsed) {
|
|
195
|
+
return invalid(
|
|
196
|
+
"This filter has expired. Move its date forward or make it Standing to turn it back on.",
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const wanted = patch.state ?? current.state;
|
|
201
|
+
const merged = { ...patch, ...timing };
|
|
202
|
+
if (wanted === FilterState.Disabled) {
|
|
203
|
+
return accept({
|
|
204
|
+
...merged,
|
|
205
|
+
state: FilterState.Disabled,
|
|
206
|
+
...(folderless
|
|
207
|
+
? pickedReason
|
|
208
|
+
: patch.state === FilterState.Disabled
|
|
209
|
+
? { disabledReason: FilterDisabledReason.UserDisabled }
|
|
210
|
+
: {}),
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
if (lapsed) {
|
|
214
|
+
return accept({
|
|
215
|
+
...merged,
|
|
216
|
+
state: FilterState.Expired,
|
|
217
|
+
disabledReason: FilterDisabledReason.None,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
if (folderless && folder === FILTER_NO_ACTION) {
|
|
221
|
+
return invalid(folderlessMessage(current.disabledReason));
|
|
222
|
+
}
|
|
223
|
+
return accept({
|
|
224
|
+
...merged,
|
|
225
|
+
state: FilterState.Active,
|
|
226
|
+
disabledReason: FilterDisabledReason.None,
|
|
227
|
+
});
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// The sentinel means "no move action" and binds to nothing, so it is not
|
|
231
|
+
// gated, and neither is a patch that leaves the field alone.
|
|
232
|
+
const settledActionFolder = async (
|
|
233
|
+
deps: Pick<ActionFolderDeps, "mailbox">,
|
|
234
|
+
accountId: string,
|
|
235
|
+
actionMailboxId: string | undefined,
|
|
236
|
+
): Promise<Refusable<string | undefined>> => {
|
|
237
|
+
if (actionMailboxId === undefined || actionMailboxId === FILTER_NO_ACTION) {
|
|
238
|
+
return accept(actionMailboxId);
|
|
239
|
+
}
|
|
240
|
+
const target = await deps.mailbox.get(accountId, actionMailboxId);
|
|
241
|
+
const inAccount = folderInAccount(target, accountId, "act");
|
|
242
|
+
if (!inAccount.ok) return inAccount;
|
|
243
|
+
const settled = settledFolder(target);
|
|
244
|
+
if (!settled.ok) return settled;
|
|
245
|
+
return accept(actionMailboxId);
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const existingActionFolder = async (
|
|
249
|
+
deps: ActionFolderDeps,
|
|
250
|
+
accountId: string,
|
|
251
|
+
actionMailboxId: string,
|
|
252
|
+
): Promise<Refusable<string | undefined>> => {
|
|
253
|
+
if (actionMailboxId === FILTER_NO_ACTION) return accept(actionMailboxId);
|
|
254
|
+
if ((await deps.mailbox.resolveAccountId(actionMailboxId)) === null) {
|
|
255
|
+
return invalid(
|
|
256
|
+
"The folder this filter moves mail into no longer exists. Pick another folder, then turn the filter on.",
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
return settledActionFolder(deps, accountId, actionMailboxId);
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
export const listFilters = async (
|
|
263
|
+
deps: AccountDeps & {
|
|
264
|
+
filter: Pick<
|
|
265
|
+
IFilterRepository,
|
|
266
|
+
"listPageByAccountConfig" | "refreshExpiry"
|
|
267
|
+
>;
|
|
268
|
+
},
|
|
269
|
+
accountConfigId: string,
|
|
270
|
+
query: { accountId: string; continuationToken: string | undefined },
|
|
271
|
+
): Promise<Refusable<FilterPage>> => {
|
|
272
|
+
const owned = ownedAccount(
|
|
273
|
+
await deps.account.get(query.accountId),
|
|
274
|
+
accountConfigId,
|
|
275
|
+
"read",
|
|
276
|
+
);
|
|
277
|
+
if (!owned.ok) return owned;
|
|
278
|
+
|
|
279
|
+
const page = await deps.filter.listPageByAccountConfig(accountConfigId, {
|
|
280
|
+
continuationToken: query.continuationToken,
|
|
281
|
+
});
|
|
282
|
+
// Reading the list lazily expires a lapsed Temporary filter.
|
|
283
|
+
const items = await Promise.all(
|
|
284
|
+
page.items.map((item) => deps.filter.refreshExpiry(item)),
|
|
285
|
+
);
|
|
286
|
+
return accept({ items, continuationToken: page.continuationToken });
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
export const createFilter = async (
|
|
290
|
+
deps: AccountDeps &
|
|
291
|
+
ActionFolderDeps &
|
|
292
|
+
OrganizeJobDeps & { filter: Pick<IFilterRepository, "create"> },
|
|
293
|
+
owner: OrganizeJobOwner,
|
|
294
|
+
input: { accountId: string; filter: NewFilter },
|
|
295
|
+
): Promise<Refusable<CreatedFilter>> => {
|
|
296
|
+
const { accountConfigId, userId } = owner;
|
|
297
|
+
const { accountId, filter: rule } = input;
|
|
298
|
+
const owned = ownedAccount(
|
|
299
|
+
await deps.account.get(accountId),
|
|
300
|
+
accountConfigId,
|
|
301
|
+
"act",
|
|
302
|
+
);
|
|
303
|
+
if (!owned.ok) return owned;
|
|
304
|
+
const scoped = resolveFilterScopeExpiry(
|
|
305
|
+
{ scope: rule.scope, expiresAt: undefined },
|
|
306
|
+
{ scope: rule.scope, expiresAt: rule.expiresAt },
|
|
307
|
+
);
|
|
308
|
+
if (!scoped.ok) return scoped;
|
|
309
|
+
const target = await settledActionFolder(
|
|
310
|
+
deps,
|
|
311
|
+
accountId,
|
|
312
|
+
rule.actionMailboxId,
|
|
313
|
+
);
|
|
314
|
+
if (!target.ok) return target;
|
|
315
|
+
|
|
316
|
+
const filter = await deps.filter.create({
|
|
317
|
+
accountConfigId,
|
|
318
|
+
name: rule.name,
|
|
319
|
+
scope: scoped.value.scope,
|
|
320
|
+
expiresAt: scoped.value.expiresAt,
|
|
321
|
+
ttl: scoped.value.ttl,
|
|
322
|
+
matchOperator: rule.matchOperator,
|
|
323
|
+
literalClauses: rule.literalClauses,
|
|
324
|
+
actionLabelId: rule.actionLabelId,
|
|
325
|
+
actionMailboxId: rule.actionMailboxId,
|
|
326
|
+
});
|
|
327
|
+
// The new filter's first pass over existing mail.
|
|
328
|
+
const organizeJob = await queueOrganizeJob(deps, {
|
|
329
|
+
accountConfigId,
|
|
330
|
+
userId,
|
|
331
|
+
filterId: filter.filterId,
|
|
332
|
+
});
|
|
333
|
+
return accept({ filter, organizeJob });
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
export const updateFilter = async (
|
|
337
|
+
deps: AccountDeps &
|
|
338
|
+
ActionFolderDeps & {
|
|
339
|
+
filter: Pick<IFilterRepository, "get" | "update">;
|
|
340
|
+
},
|
|
341
|
+
accountConfigId: string,
|
|
342
|
+
input: { accountId: string; filterId: string; patch: FilterPatch },
|
|
343
|
+
): Promise<Refusable<FilterItem>> => {
|
|
344
|
+
const { accountId, filterId } = input;
|
|
345
|
+
const owned = ownedAccount(
|
|
346
|
+
await deps.account.get(accountId),
|
|
347
|
+
accountConfigId,
|
|
348
|
+
"act",
|
|
349
|
+
);
|
|
350
|
+
if (!owned.ok) return owned;
|
|
351
|
+
|
|
352
|
+
const patch = pickFilterUpdate(input.patch);
|
|
353
|
+
const target = await settledActionFolder(
|
|
354
|
+
deps,
|
|
355
|
+
accountId,
|
|
356
|
+
patch.actionMailboxId,
|
|
357
|
+
);
|
|
358
|
+
if (!target.ok) return target;
|
|
359
|
+
const current = await deps.filter.get(accountConfigId, filterId);
|
|
360
|
+
const resolved = resolveFilterUpdate(current, patch);
|
|
361
|
+
if (!resolved.ok) return resolved;
|
|
362
|
+
if (
|
|
363
|
+
patch.state === FilterState.Active &&
|
|
364
|
+
patch.actionMailboxId === undefined
|
|
365
|
+
) {
|
|
366
|
+
const existing = await existingActionFolder(
|
|
367
|
+
deps,
|
|
368
|
+
accountId,
|
|
369
|
+
current.actionMailboxId,
|
|
370
|
+
);
|
|
371
|
+
if (!existing.ok) return existing;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return accept(
|
|
375
|
+
await deps.filter.update(accountConfigId, filterId, resolved.value),
|
|
376
|
+
);
|
|
377
|
+
};
|