@remit/mailbox-service 0.0.78 → 0.0.80

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remit/mailbox-service",
3
- "version": "0.0.78",
3
+ "version": "0.0.80",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
@@ -43,7 +43,8 @@
43
43
  "stopword": "*",
44
44
  "@types/mailparser": "*",
45
45
  "@types/nodemailer": "*",
46
- "@types/stopword": "^2.0.3"
46
+ "@types/stopword": "^2.0.3",
47
+ "@remit/calendar-service": "*"
47
48
  },
48
49
  "license": "MIT",
49
50
  "publishConfig": {
@@ -0,0 +1,516 @@
1
+ /**
2
+ * Issue #1033 stage 0.3: an invitation arriving in the mail becomes a card
3
+ * beside the message, never a calendar entry. These drive `BodySyncService`
4
+ * end to end — read-path body materialization into `applyPostStoreSteps` — so
5
+ * the first-seen gate is asserted against the real pass, not a helper.
6
+ */
7
+
8
+ import assert from "node:assert/strict";
9
+ import { describe, it } from "node:test";
10
+ import type {
11
+ CalendarSuggestionItem,
12
+ FilterItem,
13
+ IAddressRepository,
14
+ ICalendarSuggestionRepository,
15
+ ICalendarUnitOfWork,
16
+ IEnvelopeRepository,
17
+ IFilterRepository,
18
+ IMessageRepository,
19
+ IThreadMessageRepository,
20
+ PutCalendarSuggestionInput,
21
+ ResultList,
22
+ } from "@remit/data-ports";
23
+ import { deriveCalendarSuggestionId } from "@remit/data-ports/id";
24
+ import {
25
+ CalendarInviteMethod,
26
+ CalendarSuggestionSource,
27
+ CalendarSuggestionState,
28
+ } from "@remit/domain-enums";
29
+ import type { StorageService } from "@remit/storage-service";
30
+ import { BodySyncService } from "./body-sync.js";
31
+ import type { IImapConnection } from "./types.js";
32
+
33
+ const UID = "invite-4711@example.test";
34
+
35
+ const VCALENDAR = (sequence: number, method: string): string =>
36
+ [
37
+ "BEGIN:VCALENDAR",
38
+ "VERSION:2.0",
39
+ "PRODID:-//Example Corp//Scheduler//EN",
40
+ `METHOD:${method}`,
41
+ "BEGIN:VEVENT",
42
+ `UID:${UID}`,
43
+ "DTSTAMP:20260801T090000Z",
44
+ `SEQUENCE:${sequence}`,
45
+ "DTSTART:20260901T080000Z",
46
+ "DTEND:20260901T090000Z",
47
+ "SUMMARY:Quarterly review",
48
+ "ORGANIZER:mailto:organizer@example.test",
49
+ "ATTENDEE;PARTSTAT=NEEDS-ACTION:mailto:user@example.test",
50
+ "END:VEVENT",
51
+ "END:VCALENDAR",
52
+ ].join("\r\n");
53
+
54
+ const INVITATION_EML = (sequence = 0, method = "REQUEST"): Buffer =>
55
+ Buffer.from(
56
+ [
57
+ "From: Organizer <organizer@example.test>",
58
+ "To: user@example.test",
59
+ "Subject: Quarterly review",
60
+ 'Content-Type: multipart/alternative; boundary="bnd"',
61
+ "",
62
+ "--bnd",
63
+ "Content-Type: text/plain",
64
+ "",
65
+ "You are invited.",
66
+ "--bnd",
67
+ 'Content-Type: text/calendar; method=REQUEST; charset="UTF-8"',
68
+ "",
69
+ VCALENDAR(sequence, method),
70
+ "--bnd--",
71
+ "",
72
+ ].join("\r\n"),
73
+ );
74
+
75
+ /** DTSTART with neither a Z nor a TZID: RFC 5545 floating time. */
76
+ const FLOATING_INVITATION_EML = Buffer.from(
77
+ [
78
+ "From: Organizer <organizer@example.test>",
79
+ "To: user@example.test",
80
+ "Subject: Quarterly review",
81
+ 'Content-Type: multipart/alternative; boundary="bnd"',
82
+ "",
83
+ "--bnd",
84
+ "Content-Type: text/plain",
85
+ "",
86
+ "You are invited.",
87
+ "--bnd",
88
+ "Content-Type: text/calendar; method=REQUEST",
89
+ "",
90
+ [
91
+ "BEGIN:VCALENDAR",
92
+ "VERSION:2.0",
93
+ "PRODID:-//Example Corp//Scheduler//EN",
94
+ "METHOD:REQUEST",
95
+ "BEGIN:VEVENT",
96
+ `UID:${UID}`,
97
+ "DTSTAMP:20260801T090000Z",
98
+ "SEQUENCE:0",
99
+ "DTSTART:20260901T100000",
100
+ "DTEND:20260901T110000",
101
+ "SUMMARY:Quarterly review",
102
+ "ORGANIZER:mailto:organizer@example.test",
103
+ "END:VEVENT",
104
+ "END:VCALENDAR",
105
+ ].join("\r\n"),
106
+ "--bnd--",
107
+ "",
108
+ ].join("\r\n"),
109
+ );
110
+
111
+ /** The shape `dismiss{muteSender:true}` writes: a standing rule, no action. */
112
+ const MUTE_RULE = (sender: string): FilterItem =>
113
+ ({
114
+ filterId: `mute-${sender}`,
115
+ accountConfigId: "cfg-1",
116
+ name: `Muted invitations from ${sender}`,
117
+ scope: "Standing",
118
+ state: "Active",
119
+ matchOperator: "And",
120
+ literalClauses: [{ field: "From", value: sender }],
121
+ actionLabelId: "None",
122
+ actionMailboxId: "None",
123
+ }) as unknown as FilterItem;
124
+
125
+ const PLAIN_EML = Buffer.from(
126
+ [
127
+ "From: Someone <someone@example.test>",
128
+ "To: user@example.test",
129
+ "Subject: Hello",
130
+ "Content-Type: text/plain",
131
+ "",
132
+ "body",
133
+ ].join("\r\n"),
134
+ );
135
+
136
+ class MemorySuggestions implements ICalendarSuggestionRepository {
137
+ readonly rows = new Map<string, CalendarSuggestionItem>();
138
+
139
+ async put(
140
+ input: PutCalendarSuggestionInput,
141
+ ): Promise<CalendarSuggestionItem> {
142
+ const suggestionId = deriveCalendarSuggestionId(
143
+ input.messageId,
144
+ input.bodyPartId,
145
+ input.icalUid,
146
+ );
147
+ const existing = this.rows.get(suggestionId);
148
+ const row: CalendarSuggestionItem = {
149
+ ...input,
150
+ suggestionId,
151
+ state: existing?.state ?? CalendarSuggestionState.Pending,
152
+ acceptedCalendarObjectId: existing?.acceptedCalendarObjectId ?? "",
153
+ createdAt: existing?.createdAt ?? 1,
154
+ updatedAt: 1,
155
+ };
156
+ this.rows.set(suggestionId, row);
157
+ return row;
158
+ }
159
+
160
+ async get(
161
+ _accountConfigId: string,
162
+ suggestionId: string,
163
+ ): Promise<CalendarSuggestionItem> {
164
+ const row = this.rows.get(suggestionId);
165
+ if (!row) throw new Error(`missing ${suggestionId}`);
166
+ return row;
167
+ }
168
+
169
+ async listByMessage(
170
+ _accountConfigId: string,
171
+ messageId: string,
172
+ ): Promise<CalendarSuggestionItem[]> {
173
+ return [...this.rows.values()].filter((row) => row.messageId === messageId);
174
+ }
175
+
176
+ async listByState(
177
+ _accountConfigId: string,
178
+ state: CalendarSuggestionItem["state"],
179
+ ): Promise<ResultList<CalendarSuggestionItem>> {
180
+ return {
181
+ items: [...this.rows.values()].filter((row) => row.state === state),
182
+ continuationToken: undefined,
183
+ };
184
+ }
185
+
186
+ async settle(
187
+ _accountConfigId: string,
188
+ suggestionId: string,
189
+ input: {
190
+ state: CalendarSuggestionItem["state"];
191
+ acceptedCalendarObjectId: string;
192
+ },
193
+ ): Promise<CalendarSuggestionItem> {
194
+ const row = this.rows.get(suggestionId);
195
+ if (!row) throw new Error(`missing ${suggestionId}`);
196
+ const settled = { ...row, ...input };
197
+ this.rows.set(suggestionId, settled);
198
+ return settled;
199
+ }
200
+
201
+ async supersedeIfPending(
202
+ _accountConfigId: string,
203
+ suggestionId: string,
204
+ ): Promise<CalendarSuggestionItem | null> {
205
+ const row = this.rows.get(suggestionId);
206
+ if (!row || row.state !== CalendarSuggestionState.Pending) return null;
207
+ const retired = {
208
+ ...row,
209
+ state: CalendarSuggestionState.Superseded,
210
+ acceptedCalendarObjectId: "",
211
+ };
212
+ this.rows.set(suggestionId, retired);
213
+ return retired;
214
+ }
215
+ }
216
+
217
+ interface Harness {
218
+ service: BodySyncService;
219
+ suggestions: MemorySuggestions;
220
+ }
221
+
222
+ const buildHarness = ({
223
+ bodyStorageKey,
224
+ timezone = "",
225
+ withCalendarConfig = true,
226
+ mutedSenderFilters = [] as FilterItem[],
227
+ }: {
228
+ bodyStorageKey?: string;
229
+ timezone?: string;
230
+ withCalendarConfig?: boolean;
231
+ mutedSenderFilters?: FilterItem[];
232
+ } = {}): Harness => {
233
+ const suggestions = new MemorySuggestions();
234
+
235
+ const messageService = {
236
+ get: async (messageId: string) => ({
237
+ messageId,
238
+ mailboxId: "mb-inbox",
239
+ uid: 1,
240
+ bodyStorageKey,
241
+ }),
242
+ update: async () => {},
243
+ } as unknown as IMessageRepository;
244
+
245
+ const threadMessageService = {
246
+ findAllByMessageId: async () => [
247
+ {
248
+ threadMessageId: "tm-1",
249
+ sentDate: 1,
250
+ mailboxId: "mb-inbox",
251
+ isRead: false,
252
+ isDeleted: false,
253
+ hasStars: false,
254
+ hasAttachment: false,
255
+ },
256
+ ],
257
+ update: async () => {},
258
+ } as unknown as IThreadMessageRepository;
259
+
260
+ const storageService = {
261
+ // The read-miss the forced re-sync rides in on: the row says the body is
262
+ // stored, the object is gone, so the pass re-fetches and re-enters
263
+ // applyPostStoreSteps with `bodyStorageKey` already set.
264
+ retrieve: async () => {
265
+ throw Object.assign(new Error("gone"), { name: "NoSuchKey" });
266
+ },
267
+ storeMessageBody: async () => ({ uri: "s3://bodies/stored" }),
268
+ storeParsedBody: async () => {},
269
+ listBodyParts: async () => [],
270
+ } as unknown as StorageService;
271
+
272
+ const addressService = {
273
+ getAddress: async () => ({ flags: {} }),
274
+ incrementInboundCount: async () => {},
275
+ } as unknown as IAddressRepository;
276
+
277
+ const envelopeService = {
278
+ listBodyParts: async () => [],
279
+ } as unknown as IEnvelopeRepository;
280
+
281
+ // The producer provisions the default collection through the unit of work,
282
+ // so this stands in for it — `create` is idempotent on a derived id, which
283
+ // is why the producer may call it on every calendar-carrying message.
284
+ const collections = new Map<string, { timezone: string }>();
285
+ const calendarUnitOfWork = {
286
+ transaction: <T>(
287
+ run: (repos: {
288
+ calendarCollection: {
289
+ create: (input: {
290
+ accountConfigId: string;
291
+ }) => Promise<{ calendarId: string; timezone: string }>;
292
+ };
293
+ }) => Promise<T>,
294
+ ) =>
295
+ run({
296
+ calendarCollection: {
297
+ create: async (input: { accountConfigId: string }) => {
298
+ const existing = collections.get(input.accountConfigId);
299
+ if (existing) {
300
+ return { calendarId: "cal-1", timezone: existing.timezone };
301
+ }
302
+ collections.set(input.accountConfigId, { timezone });
303
+ return { calendarId: "cal-1", timezone };
304
+ },
305
+ },
306
+ }),
307
+ } as unknown as ICalendarUnitOfWork;
308
+
309
+ const filterService = {
310
+ listByAccountAndState: async () => mutedSenderFilters,
311
+ } as unknown as Pick<IFilterRepository, "listByAccountAndState">;
312
+
313
+ const service = new BodySyncService(
314
+ messageService,
315
+ storageService,
316
+ threadMessageService,
317
+ addressService,
318
+ envelopeService,
319
+ { info: () => {}, error: () => {} },
320
+ undefined,
321
+ undefined,
322
+ undefined,
323
+ undefined,
324
+ withCalendarConfig
325
+ ? {
326
+ calendarSuggestionService: suggestions,
327
+ calendarUnitOfWork,
328
+ filterService,
329
+ }
330
+ : undefined,
331
+ );
332
+
333
+ return { service, suggestions };
334
+ };
335
+
336
+ const readBody = async (
337
+ service: BodySyncService,
338
+ eml: Buffer,
339
+ messageId = "m-1",
340
+ ) => {
341
+ const connection = {
342
+ openBox: async () => {},
343
+ fetchMessageBody: async () => eml,
344
+ } as unknown as IImapConnection;
345
+ return service.fetchAndGetBody(
346
+ messageId,
347
+ "acc-1",
348
+ "cfg-1",
349
+ "INBOX",
350
+ async () => connection,
351
+ );
352
+ };
353
+
354
+ describe("mail-derived calendar suggestions (issue #1033)", () => {
355
+ it("offers an invitation as one Pending suggestion", async () => {
356
+ const harness = buildHarness();
357
+
358
+ await readBody(harness.service, INVITATION_EML());
359
+
360
+ const rows = [...harness.suggestions.rows.values()];
361
+ assert.equal(rows.length, 1);
362
+ assert.equal(rows[0]?.state, CalendarSuggestionState.Pending);
363
+ assert.equal(rows[0]?.icalUid, UID);
364
+ assert.equal(rows[0]?.method, CalendarInviteMethod.Request);
365
+ assert.equal(rows[0]?.summary, "Quarterly review");
366
+ assert.equal(rows[0]?.organizer, "organizer@example.test");
367
+ assert.equal(rows[0]?.source, CalendarSuggestionSource.IcalendarPart);
368
+ });
369
+
370
+ it("stores the invitation's bytes so accepting can write them unchanged", async () => {
371
+ const harness = buildHarness();
372
+
373
+ await readBody(harness.service, INVITATION_EML());
374
+
375
+ const row = [...harness.suggestions.rows.values()][0];
376
+ assert.match(row?.icalData ?? "", /BEGIN:VCALENDAR/);
377
+ assert.match(row?.icalData ?? "", /UID:invite-4711@example\.test/);
378
+ });
379
+
380
+ it("reads a floating start in the collection's own zone", async () => {
381
+ // An invitation whose DTSTART names no zone is RFC 5545 floating time.
382
+ // Read in UTC it lands two hours out for an Amsterdam calendar, and the
383
+ // user is shown the wrong hour with nothing saying so.
384
+ const harness = buildHarness({ timezone: "Europe/Amsterdam" });
385
+
386
+ await readBody(harness.service, FLOATING_INVITATION_EML);
387
+
388
+ const row = [...harness.suggestions.rows.values()][0];
389
+ assert.equal(row?.dtStart, "2026-09-01T10:00:00+02:00");
390
+ assert.equal(row?.zoneCertainty, "Local");
391
+ });
392
+
393
+ it("offers no card for a sender the user muted", async () => {
394
+ // The standing rule `dismiss{muteSender:true}` writes has exactly one
395
+ // reader, and this is it: the index-time filter pipeline skips a rule
396
+ // with no label and no move, so without this the button does nothing.
397
+ const harness = buildHarness({
398
+ mutedSenderFilters: [MUTE_RULE("organizer@example.test")],
399
+ });
400
+
401
+ await readBody(harness.service, INVITATION_EML());
402
+
403
+ assert.equal(harness.suggestions.rows.size, 0);
404
+ });
405
+
406
+ it("still offers a card when the rule names a different sender", async () => {
407
+ const harness = buildHarness({
408
+ mutedSenderFilters: [MUTE_RULE("someone-else@example.test")],
409
+ });
410
+
411
+ await readBody(harness.service, INVITATION_EML());
412
+
413
+ assert.equal(harness.suggestions.rows.size, 1);
414
+ });
415
+
416
+ it("still offers a card when the rule about this sender does something", async () => {
417
+ // A rule that labels or moves this sender's mail is an ordinary filter
418
+ // the pipeline acts on, not the user refusing their invitations.
419
+ const harness = buildHarness({
420
+ mutedSenderFilters: [
421
+ {
422
+ ...MUTE_RULE("organizer@example.test"),
423
+ actionLabelId: "label-1",
424
+ } as FilterItem,
425
+ ],
426
+ });
427
+
428
+ await readBody(harness.service, INVITATION_EML());
429
+
430
+ assert.equal(harness.suggestions.rows.size, 1);
431
+ });
432
+
433
+ it("offers nothing for a message carrying no calendar part", async () => {
434
+ const harness = buildHarness();
435
+
436
+ await readBody(harness.service, PLAIN_EML);
437
+
438
+ assert.equal(harness.suggestions.rows.size, 0);
439
+ });
440
+
441
+ it("offers nothing on a forced body re-sync", async () => {
442
+ // The owner's ruling on #497/#1011: a card is offered at first sight,
443
+ // the same moment the account's filters run. A forced re-sync re-enters
444
+ // this pass with the body already stored, and re-offering there would
445
+ // resurrect a card the user dismissed.
446
+ const harness = buildHarness({ bodyStorageKey: "s3://bodies/m-1" });
447
+
448
+ const result = await readBody(harness.service, INVITATION_EML());
449
+
450
+ // The pass genuinely ran — the body was re-fetched and re-stored — and
451
+ // still offered nothing.
452
+ assert.equal(result.storedAt, "s3://bodies/m-1");
453
+ assert.equal(harness.suggestions.rows.size, 0);
454
+ });
455
+
456
+ it("supersedes the earlier revision when a higher SEQUENCE arrives", async () => {
457
+ const harness = buildHarness();
458
+
459
+ await readBody(harness.service, INVITATION_EML(0), "m-1");
460
+ await readBody(harness.service, INVITATION_EML(1), "m-2");
461
+
462
+ const rows = [...harness.suggestions.rows.values()];
463
+ assert.equal(rows.length, 2);
464
+ assert.deepEqual(
465
+ rows.map((row) => `${row.messageId}:${row.state}`).sort(),
466
+ ["m-1:Superseded", "m-2:Pending"],
467
+ );
468
+ });
469
+
470
+ it("offers a cancellation as its own card, writing no calendar object", async () => {
471
+ const harness = buildHarness();
472
+
473
+ await readBody(harness.service, INVITATION_EML(1, "CANCEL"));
474
+
475
+ const row = [...harness.suggestions.rows.values()][0];
476
+ assert.equal(row?.method, CalendarInviteMethod.Cancel);
477
+ assert.equal(row?.state, CalendarSuggestionState.Pending);
478
+ assert.equal(row?.acceptedCalendarObjectId, "");
479
+ });
480
+
481
+ it("stores the body even when the message carries unreadable iCalendar", async () => {
482
+ const harness = buildHarness();
483
+ const broken = Buffer.from(
484
+ [
485
+ "From: Organizer <organizer@example.test>",
486
+ "To: user@example.test",
487
+ "Subject: Broken",
488
+ 'Content-Type: multipart/alternative; boundary="bnd"',
489
+ "",
490
+ "--bnd",
491
+ "Content-Type: text/plain",
492
+ "",
493
+ "hi",
494
+ "--bnd",
495
+ "Content-Type: text/calendar",
496
+ "",
497
+ "BEGIN:VCALENDAR",
498
+ "--bnd--",
499
+ "",
500
+ ].join("\r\n"),
501
+ );
502
+
503
+ const result = await readBody(harness.service, broken);
504
+
505
+ assert.equal(result.storedAt, "newly-stored");
506
+ assert.equal(harness.suggestions.rows.size, 0);
507
+ });
508
+
509
+ it("is a no-op when body sync was built without a CalendarSuggestionConfig", async () => {
510
+ const harness = buildHarness({ withCalendarConfig: false });
511
+
512
+ await readBody(harness.service, INVITATION_EML());
513
+
514
+ assert.equal(harness.suggestions.rows.size, 0);
515
+ });
516
+ });
package/src/body-sync.ts CHANGED
@@ -1,19 +1,28 @@
1
1
  import { PassThrough, type Readable } from "node:stream";
2
2
  import { inspect } from "node:util";
3
- import type {
4
- IAddressRepository,
5
- IEnvelopeRepository,
6
- IMailboxSpecialUseRepository,
7
- IMessageRepository,
8
- IThreadMessageRepository,
9
- MessageItem,
10
- ThreadMessageItem,
11
- UpdateMessageInput,
3
+ import {
4
+ provisionDefaultCalendar,
5
+ recordCalendarSuggestion,
6
+ } from "@remit/calendar-service";
7
+ import {
8
+ type IAddressRepository,
9
+ type ICalendarSuggestionRepository,
10
+ type ICalendarUnitOfWork,
11
+ type IEnvelopeRepository,
12
+ type IFilterRepository,
13
+ type IMailboxSpecialUseRepository,
14
+ type IMessageRepository,
15
+ type IThreadMessageRepository,
16
+ isSenderMuted,
17
+ type MessageItem,
18
+ type ThreadMessageItem,
19
+ type UpdateMessageInput,
12
20
  } from "@remit/data-ports";
13
21
  import { NotFoundError } from "@remit/data-ports/errors";
14
- import { deriveAddressId } from "@remit/data-ports/id";
22
+ import { deriveAddressId, deriveBodyPartId } from "@remit/data-ports/id";
15
23
  import { isBulkSender } from "@remit/data-ports/wellknown";
16
24
  import {
25
+ FilterState,
17
26
  MessageCategory,
18
27
  PlacementAction,
19
28
  PlacementConfidence,
@@ -29,6 +38,7 @@ import { type ParsedMail, simpleParser } from "mailparser";
29
38
  import pMap from "p-map";
30
39
  import { BodyParseError, parseMessageBody } from "./body-parse.js";
31
40
  import { mapBodyPartsToContent } from "./body-part-mapper.js";
41
+ import { calendarParts } from "./calendar-parts.js";
32
42
  import { extractListId } from "./filters/list-id.js";
33
43
  import type { FilterMessage } from "./filters/match.js";
34
44
  import {
@@ -316,6 +326,25 @@ export interface UnsubscribeConfig {
316
326
  flagQueueService: FlagQueueService;
317
327
  }
318
328
 
329
+ /**
330
+ * What body sync needs to offer a mail's invitation as a card (issue #1033).
331
+ *
332
+ * The unit of work is here to provision the account's default collection on
333
+ * first use, through the one function every other caller provisions with. That
334
+ * is not bookkeeping: an invitation whose DTSTART names no zone is RFC 5545
335
+ * floating time, and reading it anywhere but where the user's calendar lives
336
+ * shows the meeting at the wrong hour. Without a collection there is no zone
337
+ * to read it in, and every such event silently lands in UTC.
338
+ *
339
+ * `filterService` is read to honour `dismiss{muteSender:true}`: a sender the
340
+ * user muted gets no card at all.
341
+ */
342
+ export interface CalendarSuggestionConfig {
343
+ calendarSuggestionService: ICalendarSuggestionRepository;
344
+ calendarUnitOfWork: ICalendarUnitOfWork;
345
+ filterService: Pick<IFilterRepository, "listByAccountAndState">;
346
+ }
347
+
319
348
  export class BodySyncService {
320
349
  private log: BodySyncLogger;
321
350
  private readonly filterPipeline?: FilterPipeline;
@@ -331,6 +360,7 @@ export class BodySyncService {
331
360
  private readonly filterConfig?: FilterConfig,
332
361
  private readonly quarantineConfig?: QuarantineConfig,
333
362
  private readonly unsubscribeConfig?: UnsubscribeConfig,
363
+ private readonly calendarConfig?: CalendarSuggestionConfig,
334
364
  ) {
335
365
  this.log = logger ?? noopLogger;
336
366
  this.filterPipeline = filterConfig
@@ -900,6 +930,13 @@ export class BodySyncService {
900
930
  existingMessage.bodyStorageKey,
901
931
  );
902
932
 
933
+ await this.deriveCalendarSuggestions(
934
+ messageId,
935
+ accountConfigId,
936
+ parsed,
937
+ existingMessage.bodyStorageKey,
938
+ );
939
+
903
940
  const moved = Boolean(resolved.move || filterMoved);
904
941
 
905
942
  // ONE Message UpdateItem per synced message: bodyStorageKey + every
@@ -1351,6 +1388,103 @@ export class BodySyncService {
1351
1388
  );
1352
1389
  }
1353
1390
 
1391
+ /**
1392
+ * Offer what a message's `text/calendar` parts propose, as cards beside the
1393
+ * message (issue #1033). Nothing here reaches a calendar: a suggestion is
1394
+ * written `Pending` and waits for a person, and a `METHOD:CANCEL` is a card
1395
+ * of its own rather than a withdrawal of an event the user added.
1396
+ *
1397
+ * Write-once per message, like the `category`, placement and read-state
1398
+ * derivations alongside it (issues #499, #1011): a card is offered on the
1399
+ * pass that first classifies a message — the same moment the account's
1400
+ * filters run — and never again. The two re-entrant paths a forced body
1401
+ * re-sync goes through (`fetchAndGetBody`'s `NoSuchKey` fallback,
1402
+ * `syncBodies(..., force: true)`) reach this with `bodyStorageKey` already
1403
+ * set, and re-offering there would resurrect a card the user dismissed.
1404
+ *
1405
+ * Isolated the same way placement and filters are: a card is auxiliary to
1406
+ * storing the mail, which is already durable by the time this runs. A
1407
+ * repository failure here is logged loudly with the messageId rather than
1408
+ * failing a body store that otherwise succeeded.
1409
+ */
1410
+ private async deriveCalendarSuggestions(
1411
+ messageId: string,
1412
+ accountConfigId: string,
1413
+ parsed: ParsedMail,
1414
+ storedBodyKey: string | undefined,
1415
+ ): Promise<void> {
1416
+ if (!this.calendarConfig) return;
1417
+ if (hasClassifiedBody(storedBodyKey)) return;
1418
+
1419
+ const parts = calendarParts(parsed);
1420
+ if (parts.length === 0) return;
1421
+
1422
+ const { calendarUnitOfWork, calendarSuggestionService, filterService } =
1423
+ this.calendarConfig;
1424
+
1425
+ await Promise.resolve()
1426
+ .then(async () => {
1427
+ // `dismiss{muteSender:true}` wrote a standing rule naming this
1428
+ // sender. Honouring it here is what makes that button do anything:
1429
+ // the index-time filter pipeline skips a rule with no label and no
1430
+ // move, so this is the only reader such a rule has.
1431
+ const sender = extractPrimaryFromEmail(parsed);
1432
+ if (sender) {
1433
+ const active = await filterService.listByAccountAndState(
1434
+ accountConfigId,
1435
+ FilterState.Active,
1436
+ );
1437
+ if (isSenderMuted(active, sender)) {
1438
+ this.log.debug?.(
1439
+ { messageId, sender },
1440
+ "Sender is muted; offering no calendar suggestion",
1441
+ );
1442
+ return;
1443
+ }
1444
+ }
1445
+
1446
+ // Provisioned rather than looked up, through the one function every
1447
+ // caller provisions with. A missing collection has no timezone, and
1448
+ // a floating DTSTART read in UTC puts the meeting hours from where
1449
+ // the organizer meant it. Idempotent: the id is derived, so a second
1450
+ // first use returns the collection the first one made.
1451
+ const collection = await provisionDefaultCalendar(
1452
+ calendarUnitOfWork,
1453
+ accountConfigId,
1454
+ );
1455
+ for (const part of parts) {
1456
+ const recorded = await recordCalendarSuggestion(
1457
+ calendarSuggestionService,
1458
+ {
1459
+ accountConfigId,
1460
+ messageId,
1461
+ bodyPartId: deriveBodyPartId(messageId, part.partPath),
1462
+ source: part.source,
1463
+ icalData: part.icalData,
1464
+ timezone: collection.timezone,
1465
+ },
1466
+ );
1467
+ if (!recorded.ok) {
1468
+ this.log.info(
1469
+ {
1470
+ messageId,
1471
+ partPath: part.partPath,
1472
+ code: recorded.error.code,
1473
+ reason: recorded.error.message,
1474
+ },
1475
+ "Message carries iCalendar this cannot read; no suggestion offered",
1476
+ );
1477
+ }
1478
+ }
1479
+ })
1480
+ .catch((error: unknown) => {
1481
+ this.log.error?.(
1482
+ { messageId, accountConfigId, error: inspect(error) },
1483
+ "Calendar suggestion derivation failed; body is stored without a card",
1484
+ );
1485
+ });
1486
+ }
1487
+
1354
1488
  private async deriveSenderUnsubscribed(
1355
1489
  accountConfigId: string,
1356
1490
  fromEmail: string,
@@ -0,0 +1,41 @@
1
+ /// <reference path="./types/mailparser-augment.d.ts" />
2
+
3
+ import { ROOT_PART_PATH } from "@remit/data-ports/id";
4
+ import { CalendarSuggestionSource } from "@remit/domain-enums";
5
+ import type { Attachment, ParsedMail } from "mailparser";
6
+
7
+ /**
8
+ * One `text/calendar` part of a message, and where it sat.
9
+ *
10
+ * The distinction between an inline part and an attached one is not cosmetic:
11
+ * a scheduling client sends the invitation inline as part of a
12
+ * `multipart/alternative`, while a client that cannot speak iTIP attaches an
13
+ * `.ics` file. Both carry the same bytes, and a card says which it read.
14
+ */
15
+ export interface CalendarPart {
16
+ partPath: string;
17
+ source:
18
+ | typeof CalendarSuggestionSource.IcalendarPart
19
+ | typeof CalendarSuggestionSource.IcalendarAttachment;
20
+ icalData: string;
21
+ }
22
+
23
+ const isCalendar = (attachment: Attachment): boolean =>
24
+ (attachment.contentType ?? "").toLowerCase().startsWith("text/calendar");
25
+
26
+ /**
27
+ * Every `text/calendar` part of a parsed message, in declaration order.
28
+ *
29
+ * Reads mailparser's `attachments[]`, which is where every non-`text/plain`,
30
+ * non-`text/html` leaf lands — the same list the header classifier already
31
+ * looks at to route an invitation to `transactional`.
32
+ */
33
+ export const calendarParts = (parsed: ParsedMail): CalendarPart[] =>
34
+ (parsed.attachments ?? []).filter(isCalendar).map((attachment) => ({
35
+ partPath: attachment.partId ?? ROOT_PART_PATH,
36
+ source:
37
+ attachment.contentDisposition === "attachment"
38
+ ? CalendarSuggestionSource.IcalendarAttachment
39
+ : CalendarSuggestionSource.IcalendarPart,
40
+ icalData: attachment.content.toString("utf8"),
41
+ }));
@@ -1,4 +1,4 @@
1
- import type { FilterItem } from "@remit/data-ports";
1
+ import { FILTER_NO_ACTION, type FilterItem } from "@remit/data-ports";
2
2
  import { FilterClauseField, FilterMatchOperator } from "@remit/domain-enums";
3
3
  import { getDomain } from "tldts";
4
4
  import { normalizeListId } from "./list-id.js";
@@ -10,7 +10,7 @@ type FilterClause = FilterItem["literalClauses"][number];
10
10
  * absent (RFC 034 Decision 3.1) — `actionLabelId`/`actionMailboxId` are never
11
11
  * empty/optional strings, so a missing action is this exact value, never `""`.
12
12
  */
13
- export const NO_ACTION = "None";
13
+ export const NO_ACTION = FILTER_NO_ACTION;
14
14
 
15
15
  /**
16
16
  * Default cosine cut-off for a semantic-anchor match (RFC 031 "the semantic
@@ -19,7 +19,6 @@ import type { ImapEnvelope } from "./types.js";
19
19
  // in another client arrived unstarred and never appeared in Flagged. The
20
20
  // server's \Flagged keyword is the star; these cover the mapping on create.
21
21
 
22
- const ACCOUNT_ID = "acct-1";
23
22
  const ACCOUNT_CONFIG_ID = "cfg-1";
24
23
  const MAILBOX_ID = "mbx-1";
25
24
  const MESSAGE_ID = "msg-1";
@@ -66,7 +65,6 @@ type CreateThreadForMessage = (
66
65
  threadMessageService: IThreadMessageRepository,
67
66
  messageId: string,
68
67
  mailboxId: string,
69
- accountId: string,
70
68
  accountConfigId: string,
71
69
  uid: number,
72
70
  internalDate: number,
@@ -99,7 +97,6 @@ const createThreadWithFlags = async (
99
97
  repo,
100
98
  MESSAGE_ID,
101
99
  MAILBOX_ID,
102
- ACCOUNT_ID,
103
100
  ACCOUNT_CONFIG_ID,
104
101
  42,
105
102
  now,
@@ -1456,7 +1456,6 @@ export class MessageSyncService {
1456
1456
  repos.threadMessage,
1457
1457
  messageId,
1458
1458
  mailboxId,
1459
- accountId,
1460
1459
  accountConfigId,
1461
1460
  msg.uid,
1462
1461
  msg.internalDate.getTime(),
@@ -1569,7 +1568,6 @@ export class MessageSyncService {
1569
1568
  threadMessageService: IThreadMessageRepository,
1570
1569
  messageId: string,
1571
1570
  mailboxId: string,
1572
- accountId: string,
1573
1571
  accountConfigId: string,
1574
1572
  uid: number,
1575
1573
  internalDate: number,
@@ -1603,7 +1601,7 @@ export class MessageSyncService {
1603
1601
  }
1604
1602
 
1605
1603
  // Derive threadId from the root Message-ID (deterministic)
1606
- const threadId = deriveThreadId(accountId, rootMessageIdHeader);
1604
+ const threadId = deriveThreadId(accountConfigId, rootMessageIdHeader);
1607
1605
 
1608
1606
  const isRead = flags.includes(MessageSystemFlag.Seen);
1609
1607