langmail-mcp 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +89 -169
  3. package/dist/chunk-ad2666h8.js +12696 -0
  4. package/dist/langmail-mcp.js +7 -0
  5. package/package.json +94 -98
  6. package/THIRD_PARTY_NOTICES.md +0 -388
  7. package/dist/attachments/download.js +0 -69
  8. package/dist/attachments/routes.js +0 -42
  9. package/dist/attachments/tickets.js +0 -45
  10. package/dist/attachments/upload-tickets.js +0 -43
  11. package/dist/attachments/upload.js +0 -136
  12. package/dist/caldav/account-settings.js +0 -92
  13. package/dist/caldav/agenda.js +0 -277
  14. package/dist/caldav/alarm.js +0 -97
  15. package/dist/caldav/client.js +0 -408
  16. package/dist/caldav/ical.js +0 -248
  17. package/dist/caldav/occurrence.js +0 -534
  18. package/dist/caldav/recurrence.js +0 -313
  19. package/dist/caldav/rsvp.js +0 -107
  20. package/dist/caldav/scheduling-handover.js +0 -22
  21. package/dist/caldav/timezone.js +0 -103
  22. package/dist/caldav/types.js +0 -5
  23. package/dist/config.js +0 -219
  24. package/dist/contacts/address-class.js +0 -79
  25. package/dist/contacts/birthdays.js +0 -81
  26. package/dist/contacts/card.js +0 -58
  27. package/dist/contacts/constants.js +0 -46
  28. package/dist/contacts/fold.js +0 -309
  29. package/dist/contacts/index.js +0 -10
  30. package/dist/contacts/merge.js +0 -388
  31. package/dist/contacts/observations.js +0 -115
  32. package/dist/contacts/profile.js +0 -402
  33. package/dist/contacts/scoring.js +0 -146
  34. package/dist/contacts/types.js +0 -7
  35. package/dist/contacts/upsert.js +0 -131
  36. package/dist/drafts/attachments.js +0 -254
  37. package/dist/drafts/idempotency.js +0 -60
  38. package/dist/drafts/send-guard.js +0 -184
  39. package/dist/email-render/email-viewer.html +0 -143
  40. package/dist/email-render/image-proxy.js +0 -389
  41. package/dist/email-render/meta.js +0 -48
  42. package/dist/email-render/sanitize.js +0 -93
  43. package/dist/email-render/viewer-resource.js +0 -104
  44. package/dist/http/bearer.js +0 -30
  45. package/dist/http/guard.js +0 -57
  46. package/dist/http/hosts.js +0 -10
  47. package/dist/http/listener.js +0 -170
  48. package/dist/http/routes.js +0 -72
  49. package/dist/ical/ical-parse.js +0 -357
  50. package/dist/ical/index.js +0 -4
  51. package/dist/ical/timezone.js +0 -22
  52. package/dist/ical/windows-zones.js +0 -155
  53. package/dist/index.js +0 -79
  54. package/dist/jmap/blob.js +0 -49
  55. package/dist/jmap/contacts.js +0 -226
  56. package/dist/jmap/drafts.js +0 -480
  57. package/dist/jmap/email.js +0 -668
  58. package/dist/jmap/index.js +0 -12
  59. package/dist/jmap/keywords.js +0 -22
  60. package/dist/jmap/live.js +0 -21
  61. package/dist/jmap/origin.js +0 -113
  62. package/dist/jmap/patch.js +0 -29
  63. package/dist/jmap/request.js +0 -37
  64. package/dist/jmap/session.js +0 -89
  65. package/dist/jmap/sieve.js +0 -71
  66. package/dist/jmap/submission.js +0 -107
  67. package/dist/jmap/timeout.js +0 -30
  68. package/dist/jmap/types.js +0 -6
  69. package/dist/logger.js +0 -48
  70. package/dist/mcp/handler.js +0 -255
  71. package/dist/mcp/http.js +0 -29
  72. package/dist/mcp/instructions.js +0 -44
  73. package/dist/mcp/links.js +0 -64
  74. package/dist/mcp/tool-logging.js +0 -67
  75. package/dist/observability/errors.js +0 -41
  76. package/dist/rules/apply.js +0 -236
  77. package/dist/rules/rule.js +0 -280
  78. package/dist/rules/store.js +0 -92
  79. package/dist/tools/annotations.js +0 -71
  80. package/dist/tools/archive-emails.js +0 -40
  81. package/dist/tools/calendar-lookup.js +0 -64
  82. package/dist/tools/contact-common.js +0 -146
  83. package/dist/tools/contact-format.js +0 -72
  84. package/dist/tools/contact-write-through.js +0 -37
  85. package/dist/tools/create-calendar-event.js +0 -231
  86. package/dist/tools/create-calendar.js +0 -54
  87. package/dist/tools/create-contact.js +0 -172
  88. package/dist/tools/create-draft.js +0 -369
  89. package/dist/tools/create-mail-rule.js +0 -101
  90. package/dist/tools/delete-calendar-event.js +0 -165
  91. package/dist/tools/delete-contact.js +0 -60
  92. package/dist/tools/delete-draft.js +0 -129
  93. package/dist/tools/delete-mail-rule.js +0 -38
  94. package/dist/tools/download-attachment.js +0 -90
  95. package/dist/tools/download-email.js +0 -112
  96. package/dist/tools/draft-common.js +0 -600
  97. package/dist/tools/email-types.js +0 -58
  98. package/dist/tools/error-detail.js +0 -10
  99. package/dist/tools/file-emails-common.js +0 -75
  100. package/dist/tools/format.js +0 -131
  101. package/dist/tools/forward-message.js +0 -237
  102. package/dist/tools/forward-quote.js +0 -179
  103. package/dist/tools/get-draft.js +0 -61
  104. package/dist/tools/get-email.js +0 -164
  105. package/dist/tools/get-thread.js +0 -67
  106. package/dist/tools/keywords.js +0 -43
  107. package/dist/tools/list-calendar-events.js +0 -180
  108. package/dist/tools/list-calendars.js +0 -44
  109. package/dist/tools/list-drafts.js +0 -134
  110. package/dist/tools/list-mail-rules.js +0 -28
  111. package/dist/tools/list-unsubscribe.js +0 -152
  112. package/dist/tools/list-upcoming-birthdays.js +0 -165
  113. package/dist/tools/mail-rule-common.js +0 -43
  114. package/dist/tools/mail-rule-format.js +0 -79
  115. package/dist/tools/mailbox-lookup.js +0 -13
  116. package/dist/tools/mark-emails.js +0 -78
  117. package/dist/tools/merge-contacts.js +0 -145
  118. package/dist/tools/move-calendar-events.js +0 -340
  119. package/dist/tools/move-emails.js +0 -64
  120. package/dist/tools/one-click-post.js +0 -275
  121. package/dist/tools/preview-email.js +0 -204
  122. package/dist/tools/preview-mail-rule.js +0 -79
  123. package/dist/tools/reply-quote.js +0 -426
  124. package/dist/tools/reply-to-message.js +0 -292
  125. package/dist/tools/respond-to-event.js +0 -110
  126. package/dist/tools/result.js +0 -9
  127. package/dist/tools/search-contacts.js +0 -431
  128. package/dist/tools/search-emails.js +0 -570
  129. package/dist/tools/send-draft.js +0 -308
  130. package/dist/tools/trash-emails.js +0 -36
  131. package/dist/tools/unsubscribe-from-list.js +0 -249
  132. package/dist/tools/update-calendar-event.js +0 -973
  133. package/dist/tools/update-calendar.js +0 -100
  134. package/dist/tools/update-contact.js +0 -382
  135. package/dist/tools/update-draft.js +0 -714
  136. package/dist/tools/update-mail-rule.js +0 -103
  137. package/dist/tools/upload-attachments.js +0 -126
@@ -1,480 +0,0 @@
1
- // Typed JMAP wrappers for composition: Email/set `create` to stage a draft, and
2
- // Email/get with the composition-relevant properties — every recipient field
3
- // plus the threading headers and both body parts.
4
- //
5
- // That read serves two callers, which is why it isn't named for drafts: reading
6
- // a staged draft back so it can be edited, and reading the message being replied
7
- // to so the reply's recipients and threading can be derived from it.
8
- //
9
- // Editing a draft is create-then-destroy, not an update. RFC 8621 §4.6 makes
10
- // every Email property except `keywords` and `mailboxIds` immutable — a
11
- // message's content is its blob — so "change the subject" means writing a new
12
- // Email and destroying the old one.
13
- //
14
- // The two halves are two round trips, deliberately, and not one Email/set with
15
- // both a `create` and a `destroy`. `Foo/set` is explicitly not a transaction
16
- // (RFC 8620 §5.3: each create/update/destroy is an independent atomic unit, a
17
- // rejected one is reported under `notCreated`/`notDestroyed` and the server
18
- // carries on) — so batching them means a create Stalwart refuses, for quota or
19
- // any other reason, destroys the draft anyway and the user's staged message is
20
- // gone from everywhere. Sequencing costs a round trip and a transient window in
21
- // which both drafts exist; losing the content costs the content. `Email/set` has
22
- // no `onSuccessDestroy`, so one call cannot express the dependency at all.
23
- import { buildEmailSetPatch } from "./patch.js";
24
- import { jmapCall as call, methodResponse } from "./request.js";
25
- import { JmapError } from "./session.js";
26
- const USING = ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"];
27
- // RFC 8621 §4.1.5 system keyword marking a message as an unsent draft. Clients
28
- // (and Langmail's own UI) key "this is editable, not delivered" off it.
29
- export const DRAFT_KEYWORD = "$draft";
30
- // The prefix of the keyword that keeps a draft's id stable across edits. An
31
- // edit writes a new Email (see the header), so the JMAP id changes every time;
32
- // every revision carries `$draftid:<token>`, and a lookup by that keyword finds
33
- // the current revision from the token the caller was first handed. A keyword
34
- // rather than a header because Stalwart answers an Email/query `header`
35
- // condition with nothing, while `hasKeyword` is indexed — and a keyword never
36
- // leaves the mailbox, where a header would ride along on the sent mail.
37
- export const DRAFT_ID_KEYWORD_PREFIX = "$draftid:";
38
- // The prefix of the keyword a reply draft carries naming the message it
39
- // answers, by JMAP id. The draft's In-Reply-To header names it too, but only by
40
- // Message-ID, which Stalwart cannot query — and once an edit changes the
41
- // subject, Stalwart files the replacement in a thread of its own, so the
42
- // original is not even a thread sibling any more. update_draft reads this to
43
- // re-quote the original below edited text.
44
- export const DRAFT_REPLY_TO_KEYWORD_PREFIX = "$draftreplyto:";
45
- // The prefix of the keyword a forward draft carries naming the message it
46
- // forwards, by JMAP id — the counterpart of `$draftreplyto:`. A forward has no
47
- // In-Reply-To to find the original by, so this is the only link an edit of the
48
- // note has back to the message it re-attaches below it, and the one send_draft
49
- // reads to mark that message `$forwarded` once the forward has gone.
50
- export const DRAFT_FORWARD_OF_KEYWORD_PREFIX = "$draftforwardof:";
51
- // A draft the user composed is not unread mail — Stalwart would otherwise
52
- // count it towards the Drafts folder's unread badge.
53
- const SEEN_KEYWORD = "$seen";
54
- // Client-chosen creation ids. Only ever one create and one destroy per call,
55
- // so fixed ids are enough and keep the response lookup readable.
56
- const CREATE_ID = "draft";
57
- // partIds are client-supplied on create (RFC 8621 §4.6) and only have to be
58
- // unique within the message — they name the bodyValues entry each body part
59
- // pulls its content from.
60
- const TEXT_PART_ID = "text";
61
- const HTML_PART_ID = "html";
62
- // Everything composition needs off a message: recipients (bcc included, which
63
- // only a draft has), the threading headers, and both body part lists.
64
- const COMPOSE_PROPERTIES = [
65
- "id",
66
- "blobId",
67
- "size",
68
- "threadId",
69
- "mailboxIds",
70
- "keywords",
71
- "subject",
72
- "from",
73
- "to",
74
- "cc",
75
- "bcc",
76
- "replyTo",
77
- "messageId",
78
- "inReplyTo",
79
- "references",
80
- "receivedAt",
81
- // The Date header — what a reply's "On …, X wrote:" line cites.
82
- "sentAt",
83
- "textBody",
84
- "htmlBody",
85
- "bodyValues",
86
- "attachments",
87
- "hasAttachment",
88
- ];
89
- const COMPOSE_BODY_PROPERTIES = [
90
- "partId",
91
- "blobId",
92
- "name",
93
- "type",
94
- "size",
95
- "disposition",
96
- "cid",
97
- // Read so a rewrite can put it back — an HTML body may resolve an inline
98
- // resource through Content-Location rather than `cid:`.
99
- "location",
100
- ];
101
- // Translate the caller's draft into the Email/set `create` object. Only the
102
- // properties the caller actually set are emitted: JMAP distinguishes an absent
103
- // property from an explicitly null one, and sending `cc: null` on a create is
104
- // noise at best.
105
- const draftProperties = (p) => {
106
- const draft = p.draft;
107
- const bodyValues = {};
108
- const props = {
109
- from: draft.from,
110
- keywords: {
111
- [DRAFT_KEYWORD]: true,
112
- [SEEN_KEYWORD]: true,
113
- ...Object.fromEntries((p.keywords ?? []).map((keyword) => [keyword, true])),
114
- },
115
- mailboxIds: { [p.mailboxId]: true },
116
- };
117
- // Empty recipient lists are omitted, not sent as `[]`: every draft is written
118
- // by a create (an edit included — see the header), so "no cc" and "cc
119
- // cleared" are the same message, and the absent property is the canonical
120
- // spelling of it.
121
- for (const [field, addresses] of [
122
- ["to", draft.to],
123
- ["cc", draft.cc],
124
- ["bcc", draft.bcc],
125
- ["replyTo", draft.replyTo],
126
- ]) {
127
- if (addresses && addresses.length > 0) {
128
- props[field] = addresses;
129
- }
130
- }
131
- if (draft.subject !== undefined) {
132
- props.subject = draft.subject;
133
- }
134
- if (draft.inReplyTo && draft.inReplyTo.length > 0) {
135
- props.inReplyTo = draft.inReplyTo;
136
- }
137
- if (draft.references && draft.references.length > 0) {
138
- props.references = draft.references;
139
- }
140
- if (draft.text !== undefined) {
141
- bodyValues[TEXT_PART_ID] = { value: draft.text };
142
- props.textBody = [{ partId: TEXT_PART_ID, type: "text/plain" }];
143
- }
144
- if (draft.html !== undefined) {
145
- bodyValues[HTML_PART_ID] = { value: draft.html };
146
- props.htmlBody = [{ partId: HTML_PART_ID, type: "text/html" }];
147
- }
148
- // Omitted rather than sent as `{}` when the draft has no body at all —
149
- // reachable by editing a bodiless draft.
150
- if (Object.keys(bodyValues).length > 0) {
151
- props.bodyValues = bodyValues;
152
- }
153
- if (draft.attachments && draft.attachments.length > 0) {
154
- props.attachments = draft.attachments.map(attachmentPart);
155
- }
156
- return props;
157
- };
158
- // One EmailBodyPart reference, as Email/set wants it.
159
- //
160
- // `disposition` is set explicitly rather than left to the server: it is what
161
- // decides whether a receiving client shows the part as a file to download or
162
- // renders it in place, and RFC 8621 gives no default. A disposition read off a
163
- // stored message wins, so an edit presents the file the way it was already
164
- // presented; only a newly supplied attachment falls back to inferring it from
165
- // the `cid`, which is then the only evidence there is. `size` is dropped — the
166
- // server computes it from the blob, and it only exists on this type so a stored
167
- // draft can be read back.
168
- const attachmentPart = (attachment) => ({
169
- blobId: attachment.blobId,
170
- disposition: attachment.disposition ?? (attachment.cid ? "inline" : "attachment"),
171
- type: attachment.type ?? "application/octet-stream",
172
- ...(attachment.cid ? { cid: attachment.cid } : {}),
173
- ...(attachment.location ? { location: attachment.location } : {}),
174
- ...(attachment.name === undefined ? {} : { name: attachment.name }),
175
- });
176
- // Permanently destroy one draft — no Trash copy. The one Email/set destroy path
177
- // on this surface: an edit retires the revision it replaces through it, and
178
- // delete_draft retires a whole draft.
179
- export const draftDestroy = async (p) => {
180
- const resp = await call(p, {
181
- methodCalls: [
182
- ["Email/set", { accountId: p.accountId, destroy: [p.id] }, "0"],
183
- ],
184
- using: USING,
185
- });
186
- const out = methodResponse(resp, "Email/set", "0");
187
- if ((out.destroyed ?? []).includes(p.id)) {
188
- return { destroyed: true };
189
- }
190
- return { notDestroyed: out.notDestroyed?.[p.id]?.type ?? "unknown" };
191
- };
192
- // The destroy half of an edit, with every failure folded into `false`. Once the
193
- // replacement exists, throwing would discard its id — and the caller, told only
194
- // that the edit failed, would retry against the old id and pile up more drafts.
195
- // A leftover draft the caller is told about beats a lost id.
196
- const destroyedQuietly = async (p) => {
197
- try {
198
- return "destroyed" in (await draftDestroy(p));
199
- }
200
- catch {
201
- return false;
202
- }
203
- };
204
- // Take the `$draftid:` keyword off a superseded draft the destroy left behind,
205
- // so the draft id names exactly one message: the replacement. Left on, the two
206
- // copies tie on `receivedAt` (one-second resolution) when an edit follows
207
- // quickly, and `draftIdResolve` could pick the pre-edit one — even after the
208
- // replacement was sent, delivering the old wording a second time. Keywords are
209
- // the one thing RFC 8621 lets a client patch, so this can land where the
210
- // destroy did not; if it fails as well, the caller's leftover note still names
211
- // the stale copy.
212
- const supersededQuietly = async (p) => {
213
- const patch = buildEmailSetPatch({
214
- keywords: Object.fromEntries((p.keywords ?? [])
215
- .filter((keyword) => keyword.startsWith(DRAFT_ID_KEYWORD_PREFIX))
216
- .map((keyword) => [keyword, false])),
217
- });
218
- if (Object.keys(patch).length === 0) {
219
- return;
220
- }
221
- try {
222
- await call(p, {
223
- methodCalls: [
224
- [
225
- "Email/set",
226
- { accountId: p.accountId, update: { [p.id]: patch } },
227
- "0",
228
- ],
229
- ],
230
- using: USING,
231
- });
232
- }
233
- catch {
234
- // Reported through `replacedDestroyed: false` either way.
235
- }
236
- };
237
- // Stage a draft and return its id. When `replaces` is given, the superseded
238
- // draft is destroyed only after the new one exists — see the header for why that
239
- // is a second round trip and not one batched Email/set.
240
- //
241
- // Throws JmapError when Stalwart reports the create under `notCreated`; the
242
- // draft being replaced is then untouched, so nothing has been lost.
243
- export const draftCreate = async (p) => {
244
- const resp = await call(p, {
245
- methodCalls: [
246
- [
247
- "Email/set",
248
- { accountId: p.accountId, create: { [CREATE_ID]: draftProperties(p) } },
249
- "0",
250
- ],
251
- ],
252
- using: USING,
253
- });
254
- const out = methodResponse(resp, "Email/set", "0");
255
- const created = out.created?.[CREATE_ID];
256
- if (!created?.id) {
257
- // `type` is a JMAP error code (`invalidProperties`, `overQuota`, …), not
258
- // free text, so it is safe to surface; the description can quote the
259
- // message the caller staged and stays out, as it does on submission.
260
- //
261
- // It is passed through as the error type rather than only interpolated so
262
- // callers can tell "the server refused this draft" from a transport
263
- // failure — an unresolvable attachment blobId lands here as
264
- // `invalidProperties`, and that is a mistake the caller can correct.
265
- const refusal = out.notCreated?.[CREATE_ID];
266
- throw new JmapError(200, `Email/set did not create the draft: ${refusal?.type ?? "unknown"}`, refusal?.type);
267
- }
268
- if (!p.replaces) {
269
- return { id: created.id, threadId: created.threadId ?? null };
270
- }
271
- const replacedDestroyed = await destroyedQuietly({ ...p, id: p.replaces });
272
- if (!replacedDestroyed) {
273
- await supersededQuietly({ ...p, id: p.replaces });
274
- }
275
- return {
276
- id: created.id,
277
- replacedDestroyed,
278
- threadId: created.threadId ?? null,
279
- };
280
- };
281
- // Read one message with the composition-relevant properties: a staged draft to
282
- // edit, or the message a reply is being derived from. Returns null when the id
283
- // doesn't exist — a destroyed or unknown id is a caller-visible condition, not
284
- // an exception.
285
- export const emailComposeGet = async (p) => {
286
- const resp = await call(p, {
287
- methodCalls: [
288
- [
289
- "Email/get",
290
- {
291
- accountId: p.accountId,
292
- bodyProperties: [...COMPOSE_BODY_PROPERTIES],
293
- // Both parts, not just text: an html-only draft would otherwise read
294
- // back with an empty body.
295
- ...(p.bodyValues === "body"
296
- ? { fetchHTMLBodyValues: true, fetchTextBodyValues: true }
297
- : { fetchAllBodyValues: true }),
298
- ids: [p.id],
299
- maxBodyValueBytes: p.maxBodyValueBytes,
300
- properties: [...COMPOSE_PROPERTIES],
301
- },
302
- "0",
303
- ],
304
- ],
305
- using: USING,
306
- });
307
- const out = methodResponse(resp, "Email/get", "0");
308
- return out.list?.[0] ?? null;
309
- };
310
- // The mailbox/keyword patch that turns a sent draft into sent mail: `$draft`
311
- // cleared, and out of Drafts into Sent. Exported for EmailSubmission's
312
- // `onSuccessUpdateEmail`, which applies it server-side iff the send succeeds.
313
- //
314
- // Clearing `$draft` is unconditional, and that is the load-bearing half. It is
315
- // what makes "this message is no longer a draft" the server-side record that a
316
- // send already happened — the guard a second send call trips over. Only the
317
- // mailbox move depends on the account having a Sent mailbox; skipping the whole
318
- // patch when it doesn't would leave the message looking unsent forever, so a
319
- // retried call would deliver it again.
320
- //
321
- // The `$draftid:` and `$draftreplyto:` keywords are deliberately left on. The
322
- // draft id still naming the sent message is what turns a later send of the
323
- // same draft id into "already sent" rather than "not found" — clearing it
324
- // would leave that retry nothing to resolve and nothing to explain.
325
- export const sentEmailPatch = (draftsMailboxId, sentMailboxId) => buildEmailSetPatch({
326
- keywords: { [DRAFT_KEYWORD]: false },
327
- ...(sentMailboxId
328
- ? { mailboxIds: { [draftsMailboxId]: false, [sentMailboxId]: true } }
329
- : {}),
330
- });
331
- // The current revision of the draft known as `draftId`, or null when no
332
- // message carries its `$draftid:` keyword (it is a JMAP id, not a draft id).
333
- // Newest first as a backstop: `draftCreate` takes the token off a superseded
334
- // revision its destroy left behind, so only if that patch failed too can two
335
- // messages answer to the token — and then the replacement wins.
336
- export const draftIdResolve = async (p) => {
337
- const resp = await call(p, {
338
- methodCalls: [
339
- [
340
- "Email/query",
341
- {
342
- accountId: p.accountId,
343
- calculateTotal: false,
344
- filter: { hasKeyword: `${DRAFT_ID_KEYWORD_PREFIX}${p.draftId}` },
345
- limit: 1,
346
- sort: [{ isAscending: false, property: "receivedAt" }],
347
- },
348
- "0",
349
- ],
350
- ],
351
- using: USING,
352
- });
353
- const out = methodResponse(resp, "Email/query", "0");
354
- return out.ids?.[0] ?? null;
355
- };
356
- // The drafts staged in the Drafts mailbox, newest first, read with the same
357
- // properties as `emailComposeGet` so a listed draft formats exactly like one
358
- // read on its own. One round trip: the query, and an Email/get that
359
- // back-references its ids. `$draft` is required as well as the mailbox,
360
- // because a message filed into Drafts by hand is not something send_draft
361
- // would accept. Only the body parts are fetched — a listing previews the
362
- // body and never re-stages it.
363
- export const draftList = async (p) => {
364
- const conditions = [
365
- { inMailbox: p.mailboxId },
366
- { hasKeyword: DRAFT_KEYWORD },
367
- ];
368
- // NOT, and not `inMailboxOtherThan`: that one means "in at least one other
369
- // mailbox" (RFC 8621 §4.4.1), which every message in Drafts already is.
370
- if (p.excludeMailboxId) {
371
- conditions.push({
372
- conditions: [{ inMailbox: p.excludeMailboxId }],
373
- operator: "NOT",
374
- });
375
- }
376
- if (p.after) {
377
- conditions.push({ after: p.after });
378
- }
379
- if (p.before) {
380
- conditions.push({ before: p.before });
381
- }
382
- if (p.text) {
383
- const text = p.text;
384
- conditions.push({
385
- conditions: ["subject", "to", "cc", "bcc"].map((field) => ({
386
- [field]: text,
387
- })),
388
- operator: "OR",
389
- });
390
- }
391
- const resp = await call(p, {
392
- methodCalls: [
393
- [
394
- "Email/query",
395
- {
396
- accountId: p.accountId,
397
- // Unconditional, unlike search_emails' deadlined count: Drafts is a
398
- // small folder, and a count riding this batch cannot be raced or
399
- // dropped, so it is paid on every call — cheaply.
400
- calculateTotal: true,
401
- filter: { conditions, operator: "AND" },
402
- limit: p.limit,
403
- sort: [{ isAscending: false, property: "receivedAt" }],
404
- },
405
- "0",
406
- ],
407
- [
408
- "Email/get",
409
- {
410
- "#ids": { name: "Email/query", path: "/ids", resultOf: "0" },
411
- accountId: p.accountId,
412
- bodyProperties: [...COMPOSE_BODY_PROPERTIES],
413
- fetchHTMLBodyValues: true,
414
- fetchTextBodyValues: true,
415
- maxBodyValueBytes: p.maxBodyValueBytes,
416
- properties: [...COMPOSE_PROPERTIES],
417
- },
418
- "1",
419
- ],
420
- ],
421
- using: USING,
422
- });
423
- const query = methodResponse(resp, "Email/query", "0");
424
- const got = methodResponse(resp, "Email/get", "1");
425
- // Email/get does not promise the order of `ids`, so the query's order is
426
- // restored here.
427
- const byId = new Map((got.list ?? []).map((record) => [record.id, record]));
428
- const ids = query.ids ?? [];
429
- const records = ids.flatMap((id) => {
430
- const record = byId.get(id);
431
- return record ? [record] : [];
432
- });
433
- return { records, total: query.total ?? ids.length };
434
- };
435
- // The id of the message in `emailId`'s thread whose Message-ID is `messageId`,
436
- // or null. This is how an edited reply finds the message it quotes: the draft
437
- // keeps only the In-Reply-To header, and Stalwart cannot query by header, but
438
- // it threads a reply onto its original — so the original is one of the
439
- // thread's messages. One round trip: the draft's threadId, the thread, and the
440
- // Message-ID of every message in it.
441
- export const threadMessageFind = async (p) => {
442
- const resp = await call(p, {
443
- methodCalls: [
444
- [
445
- "Email/get",
446
- { accountId: p.accountId, ids: [p.emailId], properties: ["threadId"] },
447
- "0",
448
- ],
449
- [
450
- "Thread/get",
451
- {
452
- "#ids": {
453
- name: "Email/get",
454
- path: "/list/*/threadId",
455
- resultOf: "0",
456
- },
457
- accountId: p.accountId,
458
- },
459
- "1",
460
- ],
461
- [
462
- "Email/get",
463
- {
464
- "#ids": {
465
- name: "Thread/get",
466
- path: "/list/*/emailIds",
467
- resultOf: "1",
468
- },
469
- accountId: p.accountId,
470
- properties: ["id", "messageId"],
471
- },
472
- "2",
473
- ],
474
- ],
475
- using: USING,
476
- });
477
- const out = methodResponse(resp, "Email/get", "2");
478
- return (out.list?.find((email) => email.messageId?.includes(p.messageId))?.id ??
479
- null);
480
- };