@open-workspace/email 0.3.0 → 0.4.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 (221) hide show
  1. package/README.md +22 -22
  2. package/alchemy/index.ts +273 -0
  3. package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
  4. package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
  5. package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
  6. package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
  7. package/drizzle/mailbox/meta/_journal.json +20 -0
  8. package/drizzle/mailbox/migrations.js +12 -0
  9. package/index.ts +3 -1
  10. package/migrations/{0000_email.sql → 0000_dry_moon_knight.sql} +26 -20
  11. package/migrations/meta/0000_snapshot.json +125 -109
  12. package/migrations/meta/_journal.json +2 -2
  13. package/package.json +74 -68
  14. package/shared/attachment-preview.ts +37 -27
  15. package/shared/folder-order.ts +106 -0
  16. package/shared/mail-authentication.ts +8 -3
  17. package/shared/mail-feedback.ts +18 -25
  18. package/shared/mail-headers.ts +114 -39
  19. package/shared/mail-import.ts +38 -0
  20. package/shared/mail-path.ts +42 -0
  21. package/shared/mail-received.ts +174 -31
  22. package/shared/mail-remote.ts +59 -2
  23. package/shared/mail-sender-intel.ts +22 -4
  24. package/shared/mail.ts +77 -28
  25. package/shared/module.ts +33 -0
  26. package/shared/office-preview.ts +14 -2
  27. package/shared/pdfjs-assets.ts +35 -0
  28. package/shared/permissions.ts +73 -0
  29. package/shared/webhooks.ts +41 -0
  30. package/src/admin/blocked-senders.tsx +189 -0
  31. package/src/client.tsx +97 -0
  32. package/src/lib/mail-api.ts +9 -0
  33. package/src/mail/attachment-preview-dialog.tsx +161 -0
  34. package/src/mail/blocked-senders.tsx +138 -0
  35. package/src/mail/compose-window-chrome.tsx +194 -0
  36. package/src/mail/compose-window.tsx +486 -0
  37. package/src/mail/composer-asset-bookmarks.ts +409 -0
  38. package/src/mail/composer-asset.tsx +229 -0
  39. package/src/mail/composer-attachment-list.tsx +138 -0
  40. package/src/mail/composer-editor-document.ts +65 -0
  41. package/src/mail/composer-editor.tsx +284 -0
  42. package/src/mail/composer-linked-attachment-text.ts +12 -0
  43. package/src/mail/composer-recipient-fields.tsx +165 -0
  44. package/src/mail/composer-recipients.ts +208 -0
  45. package/src/mail/composer-session.ts +562 -0
  46. package/src/mail/composer-toolbar.tsx +303 -0
  47. package/src/mail/composer-upload-client.ts +105 -0
  48. package/src/mail/composer-upload-progress.ts +53 -0
  49. package/src/mail/conversation-list.tsx +550 -0
  50. package/src/mail/conversation-loading-skeleton.tsx +119 -0
  51. package/src/mail/conversation-message.tsx +346 -0
  52. package/src/mail/conversation-reply.tsx +470 -0
  53. package/src/mail/conversation-selection-toolbar.tsx +322 -0
  54. package/src/mail/conversation-view.tsx +437 -0
  55. package/src/mail/delivery-indicator.tsx +272 -0
  56. package/src/mail/email-html-body.tsx +203 -0
  57. package/src/mail/folder-glyph.tsx +46 -0
  58. package/src/mail/folder-manager.tsx +532 -0
  59. package/src/mail/format-bytes.ts +7 -0
  60. package/src/mail/mail-app-bar.tsx +253 -0
  61. package/src/mail/mail-background.tsx +12 -0
  62. package/src/mail/mail-dock.tsx +137 -0
  63. package/src/mail/mail-folders.ts +44 -0
  64. package/src/mail/mail-layout.css +236 -0
  65. package/src/mail/mail-location.ts +73 -0
  66. package/src/mail/mail-mailbox-sheet.tsx +275 -0
  67. package/src/mail/mail-nav-rail.tsx +129 -0
  68. package/src/mail/mail-nav.tsx +324 -0
  69. package/src/mail/mail-prefetch.ts +59 -0
  70. package/src/mail/mail-primitives.tsx +66 -0
  71. package/src/mail/mail-queries.ts +209 -0
  72. package/src/mail/mail-query-cache.ts +486 -0
  73. package/src/mail/mail-seed.ts +47 -0
  74. package/src/mail/mail-shell.tsx +126 -0
  75. package/src/mail/mailbox-ai-settings.tsx +185 -0
  76. package/src/mail/mailbox-realtime.tsx +251 -0
  77. package/src/mail/mailbox-route.tsx +15 -0
  78. package/src/mail/message-actions.tsx +353 -0
  79. package/src/mail/message-attachments.tsx +200 -0
  80. package/src/mail/message-format.ts +75 -0
  81. package/src/mail/message-header.tsx +260 -0
  82. package/src/mail/message-translation.tsx +225 -0
  83. package/src/mail/original-message-sheet.tsx +619 -0
  84. package/src/mail/original-message.ts +191 -0
  85. package/src/mail/outbound-notification.ts +53 -0
  86. package/src/mail/pane-splitter.tsx +117 -0
  87. package/src/mail/pdf-preview-toolbar.tsx +152 -0
  88. package/src/mail/pdf-preview.css +44 -0
  89. package/src/mail/pdf-preview.tsx +156 -0
  90. package/src/mail/pdf-translation-layout.ts +95 -0
  91. package/src/mail/print-message.ts +92 -0
  92. package/src/mail/reader-pane.tsx +535 -0
  93. package/src/mail/recipient-combobox.tsx +335 -0
  94. package/src/mail/recipient-field-row.tsx +34 -0
  95. package/src/mail/reply-to-warning.tsx +50 -0
  96. package/src/mail/sanitize-email-html.ts +430 -0
  97. package/src/mail/sender-actions.tsx +149 -0
  98. package/src/mail/sender-authentication.tsx +485 -0
  99. package/src/mail/sender-dialogs.tsx +384 -0
  100. package/src/mail/sender-security.tsx +545 -0
  101. package/src/mail/toolbar-tooltip.tsx +29 -0
  102. package/src/mail/translation-html.ts +49 -0
  103. package/src/mail/translation-language.ts +21 -0
  104. package/src/mail/types.ts +53 -0
  105. package/src/mail/unread-document-indicator.tsx +60 -0
  106. package/src/mail/use-compose-submission.ts +208 -0
  107. package/src/mail/use-composer-attachment-preflight.ts +184 -0
  108. package/src/mail/use-composer-submission.ts +117 -0
  109. package/src/mail/use-conversation-selection.ts +186 -0
  110. package/src/mail/use-mail-controller.ts +427 -0
  111. package/src/mail/use-mail-data.ts +279 -0
  112. package/src/mail/use-mail-safety.ts +155 -0
  113. package/src/mail/use-mail-send.ts +142 -0
  114. package/src/mail/use-pdf-translation-overlay.ts +156 -0
  115. package/src/mail/use-pdf-translation.ts +100 -0
  116. package/src/mail/use-pdf-viewer.ts +215 -0
  117. package/src/mail/use-reply-submission.ts +114 -0
  118. package/src/mail/use-sanitized-email-html.ts +20 -0
  119. package/src/mail/use-visible-message-read.ts +138 -0
  120. package/src/pwa/push-subscription.ts +151 -0
  121. package/src/settings/mail-apps-page.tsx +235 -0
  122. package/src/settings/notifications-page.tsx +291 -0
  123. package/worker/admin/blocked-sender-routes.ts +166 -0
  124. package/worker/admin/schemas.ts +11 -0
  125. package/worker/bindings.d.ts +14 -10
  126. package/worker/db/schema.ts +63 -40
  127. package/worker/jmap/eventsource.ts +84 -16
  128. package/worker/jmap/methods.ts +1630 -524
  129. package/worker/jmap/push-types.ts +27 -0
  130. package/worker/jmap/push.ts +140 -56
  131. package/worker/jmap/routes.ts +366 -105
  132. package/worker/mail/access.ts +258 -21
  133. package/worker/mail/ai-classification.ts +36 -20
  134. package/worker/mail/ai-translation.ts +13 -6
  135. package/worker/mail/auth-headers.ts +395 -137
  136. package/worker/mail/bimi.ts +200 -73
  137. package/worker/mail/bootstrap.ts +79 -0
  138. package/worker/mail/capped-body.ts +36 -0
  139. package/worker/mail/classification-signals.ts +35 -25
  140. package/worker/mail/conversation-cursor.ts +30 -19
  141. package/worker/mail/delivery-events.ts +88 -62
  142. package/worker/mail/delivery-facts.ts +46 -10
  143. package/worker/mail/demo-routes.ts +34 -10
  144. package/worker/mail/doh.ts +62 -23
  145. package/worker/mail/downloads.ts +75 -40
  146. package/worker/mail/edge-cache.ts +155 -0
  147. package/worker/mail/header-metadata.ts +88 -22
  148. package/worker/mail/impersonation.ts +160 -38
  149. package/worker/mail/import-routes.ts +313 -0
  150. package/worker/mail/inbound-delivery.ts +19 -9
  151. package/worker/mail/inbound-threading.ts +18 -8
  152. package/worker/mail/inbound.ts +82 -40
  153. package/worker/mail/ip-intel.ts +110 -27
  154. package/worker/mail/isolate-cache.ts +35 -0
  155. package/worker/mail/link-domains.ts +29 -7
  156. package/worker/mail/mailbox-deletion.ts +10 -4
  157. package/worker/mail/mailbox-directory.ts +16 -6
  158. package/worker/mail/mailing-list.ts +94 -39
  159. package/worker/mail/message-security.ts +90 -15
  160. package/worker/mail/message-view.ts +18 -6
  161. package/worker/mail/office-conversion.ts +31 -9
  162. package/worker/mail/office-preview-container.ts +40 -13
  163. package/worker/mail/office-preview-queue.ts +76 -32
  164. package/worker/mail/org-domain.ts +8 -1
  165. package/worker/mail/outbound-content.ts +71 -26
  166. package/worker/mail/outbound-delivery.ts +31 -17
  167. package/worker/mail/outbound-service.ts +48 -30
  168. package/worker/mail/outbound-threading.ts +30 -17
  169. package/worker/mail/outbound.ts +97 -70
  170. package/worker/mail/outgoing-context.ts +25 -15
  171. package/worker/mail/participants.ts +44 -29
  172. package/worker/mail/push-notifications.ts +271 -173
  173. package/worker/mail/quote-parser.ts +21 -10
  174. package/worker/mail/r2-presigned-urls.ts +31 -25
  175. package/worker/mail/receive.ts +23 -11
  176. package/worker/mail/recipients.ts +6 -1
  177. package/worker/mail/remote-proxy.ts +187 -31
  178. package/worker/mail/remote-routes.ts +87 -0
  179. package/worker/mail/remote-token.ts +104 -0
  180. package/worker/mail/repeat.ts +19 -0
  181. package/worker/mail/reply-plan.ts +62 -33
  182. package/worker/mail/rfc.ts +32 -7
  183. package/worker/mail/routes.ts +870 -541
  184. package/worker/mail/safety-routes.ts +328 -200
  185. package/worker/mail/schemas.ts +84 -105
  186. package/worker/mail/search.ts +1 -0
  187. package/worker/mail/security-settings.ts +41 -9
  188. package/worker/mail/sender-intel.ts +206 -51
  189. package/worker/mail/spamhaus.ts +92 -45
  190. package/worker/mail/unsubscribe.ts +76 -18
  191. package/worker/mail/uploads.ts +126 -93
  192. package/worker/mail/webhook-source.ts +35 -12
  193. package/worker/mail/workspace-safety.ts +97 -38
  194. package/worker/mailbox/demo-html.ts +7 -7
  195. package/worker/mailbox/demo-seed.ts +10 -44
  196. package/worker/mailbox/demo.ts +595 -225
  197. package/worker/mailbox/email-index.ts +53 -0
  198. package/worker/mailbox/folder-model.ts +53 -17
  199. package/worker/mailbox/import-placement.ts +88 -0
  200. package/worker/mailbox/inbox-counts.ts +106 -0
  201. package/worker/mailbox/index.ts +1734 -721
  202. package/worker/mailbox/jmap-store.ts +139 -35
  203. package/worker/mailbox/model.ts +8 -6
  204. package/worker/mailbox/office-preview-jobs.ts +56 -25
  205. package/worker/mailbox/schema.ts +87 -66
  206. package/worker/mailbox/senders.ts +176 -70
  207. package/worker/mcp/mail-action-tools.ts +128 -87
  208. package/worker/mcp/mail-compose-tools.ts +133 -82
  209. package/worker/mcp/mail-query-tools.ts +120 -71
  210. package/worker/mcp/mail-safety-tools.ts +321 -248
  211. package/worker/mcp/schemas.ts +43 -0
  212. package/worker/mcp/tools.ts +13 -10
  213. package/worker/module.ts +43 -18
  214. package/worker/sql.d.ts +8 -0
  215. package/alchemy.ts +0 -142
  216. package/containers/office-preview/Dockerfile +0 -10
  217. package/shared/mail-rules.ts +0 -156
  218. package/worker/admin/report-routes.ts +0 -164
  219. package/worker/mail/reported-messages.ts +0 -68
  220. package/worker/mailbox/migrations.ts +0 -10
  221. package/worker/mailbox/rules.ts +0 -110
@@ -0,0 +1,27 @@
1
+ /**
2
+ * The data types JMAP pushes for a mailbox account, apart from push.ts so
3
+ * the event stream does not load the Web Push sender.
4
+ */
5
+
6
+ /** Data types whose state a mailbox account reports; one state covers them all. */
7
+ export const JMAP_PUSH_TYPES = [
8
+ "Email",
9
+ "Mailbox",
10
+ "Thread",
11
+ "EmailDelivery",
12
+ ] as const;
13
+
14
+ type JmapPushType = (typeof JMAP_PUSH_TYPES)[number];
15
+
16
+ /** RFC 8620 §7.1: what changed, per account and data type. */
17
+ export type StateChange = {
18
+ "@type": "StateChange";
19
+ changed: Record<string, Record<JmapPushType, string>>;
20
+ };
21
+
22
+ const pushTypeNames: ReadonlySet<string> = new Set(JMAP_PUSH_TYPES);
23
+
24
+ /** Whether a client that asked for `types` (null: all) wants mail changes. */
25
+ export function wantsMailChanges(types: readonly string[] | null) {
26
+ return !types || types.some((type) => pushTypeNames.has(type));
27
+ }
@@ -1,36 +1,71 @@
1
- import { rawPayload, sendPushNotification, topicFromString } from "@mmmike/web-push/send";
1
+ import {
2
+ rawPayload,
3
+ sendPushNotification,
4
+ topicFromString,
5
+ } from "@mmmike/web-push/send";
6
+ import {
7
+ createDb,
8
+ mailboxAccess,
9
+ mailboxes,
10
+ createId,
11
+ } from "@open-workspace/core/module";
2
12
  import { and, eq, gt, inArray, isNotNull, lte } from "drizzle-orm";
3
- import { createDb } from "@open-workspace/core/worker/db/client.ts";
4
- import { mailboxAccess, mailboxes } from "@open-workspace/core/worker/db/schema.ts";
5
- import { createId } from "@open-workspace/core/worker/lib/ids.ts";
13
+
6
14
  import { jmapPushSubscriptions } from "../db/schema";
15
+ import { wantsMailChanges } from "./push-types";
16
+ import type { StateChange } from "./push-types";
7
17
 
8
- /** Data types whose state a mailbox account reports; one state covers them all. */
9
- export const JMAP_PUSH_TYPES = ["Email", "Mailbox", "Thread", "EmailDelivery"] as const;
18
+ export { JMAP_PUSH_TYPES } from "./push-types";
10
19
 
11
20
  /** Subscriptions live a week unless the client asks for less, then renew. */
12
- const MAX_SUBSCRIPTION_MS = 7 * 24 * 60 * 60 * 1_000;
21
+ const MAX_SUBSCRIPTION_MS = 7 * 24 * 60 * 60 * 1000;
22
+
13
23
  /** State changes are worth little once a client resyncs anyway. */
14
24
  const STATE_CHANGE_TTL_SECONDS = 24 * 60 * 60;
15
25
 
16
- export type PushVapid = { publicKey: string; privateKey: string; subject: string };
26
+ export type PushVapid = {
27
+ publicKey: string;
28
+ privateKey: string;
29
+ subject: string;
30
+ };
17
31
 
18
32
  type Subscription = typeof jmapPushSubscriptions.$inferSelect;
19
33
 
20
- export function stateChange(changed: Record<string, string>) {
34
+ /** RFC 8620 §7.2.2: proves the client can read what is pushed to the URL. */
35
+ type PushVerification = {
36
+ "@type": "PushVerification";
37
+ pushSubscriptionId: string;
38
+ verificationCode: string;
39
+ };
40
+
41
+ export function stateChange(changed: Record<string, string>): StateChange {
21
42
  return {
22
43
  "@type": "StateChange",
23
- changed: Object.fromEntries(Object.entries(changed).map(([accountId, state]) => [
24
- accountId,
25
- Object.fromEntries(JMAP_PUSH_TYPES.map((type) => [type, state])),
26
- ])),
44
+ changed: Object.fromEntries(
45
+ Object.entries(changed).map(([accountId, state]) => [
46
+ accountId,
47
+ {
48
+ Email: state,
49
+ Mailbox: state,
50
+ Thread: state,
51
+ EmailDelivery: state,
52
+ },
53
+ ])
54
+ ),
27
55
  };
28
56
  }
29
57
 
30
- export function subscriptionExpiry(requested: unknown, now = Date.now()) {
58
+ /** When a subscription expires: as asked (a UTCDate), but within a week. */
59
+ export function subscriptionExpiry(
60
+ requested: string | null | undefined,
61
+ now = Date.now()
62
+ ) {
31
63
  const latest = now + MAX_SUBSCRIPTION_MS;
32
- const asked = typeof requested === "string" ? Date.parse(requested) : Number.NaN;
33
- return new Date(Number.isFinite(asked) ? Math.max(now, Math.min(asked, latest)) : latest);
64
+ const asked = requested ? Date.parse(requested) : Number.NaN;
65
+
66
+ return new Date(
67
+ Number.isFinite(asked) ? Math.max(now, Math.min(asked, latest)) : latest
68
+ );
34
69
  }
35
70
 
36
71
  /**
@@ -40,31 +75,45 @@ export function subscriptionExpiry(requested: unknown, now = Date.now()) {
40
75
  */
41
76
  export async function postToSubscription(
42
77
  subscription: Pick<Subscription, "url" | "p256dh" | "auth">,
43
- body: unknown,
78
+ body: StateChange | PushVerification,
44
79
  vapid: PushVapid | null,
45
- options: { ttl: number; topic?: string },
80
+ options: { ttl: number; topic?: string }
46
81
  ) {
47
82
  if (subscription.p256dh && subscription.auth) {
48
83
  if (!vapid) throw new Error("Web Push is not configured");
84
+
49
85
  return sendPushNotification(
50
- { endpoint: subscription.url, keys: { p256dh: subscription.p256dh, auth: subscription.auth } },
86
+ {
87
+ endpoint: subscription.url,
88
+ keys: { p256dh: subscription.p256dh, auth: subscription.auth },
89
+ },
51
90
  rawPayload(JSON.stringify(body)),
52
91
  vapid,
53
- { ttl: options.ttl, urgency: "normal", ...(options.topic ? { topic: options.topic } : {}) },
92
+ // An undefined topic sends no Topic header.
93
+ { ttl: options.ttl, urgency: "normal", topic: options.topic }
54
94
  );
55
95
  }
96
+
97
+ const headers = new Headers({
98
+ "content-type": "application/json; charset=utf-8",
99
+ ttl: String(options.ttl),
100
+ });
101
+
102
+ if (options.topic) headers.set("topic", options.topic);
103
+
56
104
  const response = await fetch(subscription.url, {
57
105
  method: "POST",
58
- headers: {
59
- "content-type": "application/json; charset=utf-8",
60
- ttl: String(options.ttl),
61
- ...(options.topic ? { topic: options.topic } : {}),
62
- },
106
+ headers,
63
107
  body: JSON.stringify(body),
64
108
  signal: AbortSignal.timeout(30_000),
65
109
  });
110
+
66
111
  if (response.status === 404 || response.status === 410) return false;
67
- if (!response.ok) throw new Error(`Push endpoint answered ${response.status}`);
112
+
113
+ if (!response.ok) {
114
+ throw new Error(`Push endpoint answered ${response.status}`);
115
+ }
116
+
68
117
  return true;
69
118
  }
70
119
 
@@ -73,12 +122,20 @@ export function newVerificationCode() {
73
122
  }
74
123
 
75
124
  /** Sends the PushVerification object a new subscription is confirmed with. */
76
- export async function sendVerification(subscription: Subscription, vapid: PushVapid | null) {
77
- await postToSubscription(subscription, {
78
- "@type": "PushVerification",
79
- pushSubscriptionId: subscription.id,
80
- verificationCode: subscription.verificationCode,
81
- }, vapid, { ttl: 60 * 60 });
125
+ export async function sendVerification(
126
+ subscription: Subscription,
127
+ vapid: PushVapid | null
128
+ ) {
129
+ await postToSubscription(
130
+ subscription,
131
+ {
132
+ "@type": "PushVerification",
133
+ pushSubscriptionId: subscription.id,
134
+ verificationCode: subscription.verificationCode,
135
+ },
136
+ vapid,
137
+ { ttl: 60 * 60 }
138
+ );
82
139
  }
83
140
 
84
141
  /**
@@ -88,36 +145,63 @@ export async function sendVerification(subscription: Subscription, vapid: PushVa
88
145
  export async function deliverJmapStateChange(
89
146
  env: Env,
90
147
  input: { mailboxId: string; state: string },
91
- vapid: PushVapid,
148
+ vapid: PushVapid
92
149
  ) {
93
150
  const db = createDb(env.DB);
94
151
  const now = new Date();
95
- await db.delete(jmapPushSubscriptions).where(lte(jmapPushSubscriptions.expiresAt, now));
152
+ await db
153
+ .delete(jmapPushSubscriptions)
154
+ .where(lte(jmapPushSubscriptions.expiresAt, now));
155
+
96
156
  const targets = await db
97
157
  .select({ subscription: jmapPushSubscriptions })
98
158
  .from(jmapPushSubscriptions)
99
- .innerJoin(mailboxAccess, eq(mailboxAccess.userId, jmapPushSubscriptions.userId))
159
+ .innerJoin(
160
+ mailboxAccess,
161
+ eq(mailboxAccess.userId, jmapPushSubscriptions.userId)
162
+ )
100
163
  .innerJoin(mailboxes, eq(mailboxes.id, mailboxAccess.mailboxId))
101
- .where(and(
102
- eq(mailboxAccess.mailboxId, input.mailboxId),
103
- eq(mailboxes.delivery, "inbox"),
104
- isNotNull(jmapPushSubscriptions.verifiedAt),
105
- gt(jmapPushSubscriptions.expiresAt, now),
106
- ));
164
+ .where(
165
+ and(
166
+ eq(mailboxAccess.mailboxId, input.mailboxId),
167
+ eq(mailboxes.delivery, "inbox"),
168
+ isNotNull(jmapPushSubscriptions.verifiedAt),
169
+ gt(jmapPushSubscriptions.expiresAt, now)
170
+ )
171
+ );
172
+
107
173
  const gone: string[] = [];
108
- await Promise.all(targets.map(async ({ subscription }) => {
109
- const types = subscription.typesJson;
110
- if (types && !types.some((type) => (JMAP_PUSH_TYPES as readonly string[]).includes(type))) return;
111
- try {
112
- const delivered = await postToSubscription(subscription, stateChange({ [input.mailboxId]: input.state }), vapid, {
113
- ttl: STATE_CHANGE_TTL_SECONDS,
114
- // A newer state replaces an undelivered older one.
115
- topic: await topicFromString(`jmap:${subscription.id}:${input.mailboxId}`),
116
- });
117
- if (!delivered) gone.push(subscription.id);
118
- } catch (error) {
119
- console.error("JMAP push delivery failed", error instanceof Error ? error.message : String(error));
120
- }
121
- }));
122
- if (gone.length) await db.delete(jmapPushSubscriptions).where(inArray(jmapPushSubscriptions.id, gone));
174
+ await Promise.all(
175
+ targets.map(async ({ subscription }) => {
176
+ if (!wantsMailChanges(subscription.typesJson)) return;
177
+
178
+ try {
179
+ const delivered = await postToSubscription(
180
+ subscription,
181
+ stateChange({ [input.mailboxId]: input.state }),
182
+ vapid,
183
+ {
184
+ ttl: STATE_CHANGE_TTL_SECONDS,
185
+ // A newer state replaces an undelivered older one.
186
+ topic: await topicFromString(
187
+ `jmap:${subscription.id}:${input.mailboxId}`
188
+ ),
189
+ }
190
+ );
191
+
192
+ if (!delivered) gone.push(subscription.id);
193
+ } catch (error) {
194
+ console.error(
195
+ "JMAP push delivery failed",
196
+ error instanceof Error ? error.message : String(error)
197
+ );
198
+ }
199
+ })
200
+ );
201
+
202
+ if (gone.length) {
203
+ await db
204
+ .delete(jmapPushSubscriptions)
205
+ .where(inArray(jmapPushSubscriptions.id, gone));
206
+ }
123
207
  }