@open-workspace/email 0.5.2 → 0.6.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.
- package/README.md +1 -2
- package/alchemy/index.ts +0 -16
- package/migrations/0003_after_0_5_2.sql +1 -0
- package/migrations/meta/0003_snapshot.json +703 -0
- package/migrations/meta/_journal.json +7 -0
- package/package.json +6 -5
- package/shared/mail.ts +3 -2
- package/shared/module.ts +1 -1
- package/src/client.tsx +20 -30
- package/src/extensions.ts +176 -0
- package/src/mail/compose-window-chrome.tsx +5 -10
- package/src/mail/compose-window.tsx +13 -2
- package/src/mail/composer-attach-button.tsx +106 -0
- package/src/mail/composer-editor.tsx +17 -0
- package/src/mail/composer-inserts.tsx +186 -0
- package/src/mail/composer-toolbar.tsx +3 -0
- package/src/mail/conversation-message.tsx +37 -1
- package/src/mail/conversation-reply.tsx +5 -10
- package/src/mail/message-actions.tsx +30 -0
- package/src/mail/message-attachments.tsx +62 -0
- package/src/mail/message-panels.tsx +206 -0
- package/src/mail/module-extensions.ts +87 -0
- package/src/mail/reader-pane.tsx +75 -47
- package/src/mail/recipient-combobox.tsx +32 -12
- package/src/mail/shared-compose.ts +45 -0
- package/src/settings/mail-notifications.tsx +119 -0
- package/src/settings/settings-queries.ts +0 -7
- package/worker/backups/file-parts.ts +9 -6
- package/worker/backups/mail-backup.ts +21 -4
- package/worker/backups/settings-backup.ts +56 -24
- package/worker/db/schema.ts +0 -36
- package/worker/jmap/method-core.ts +2 -3
- package/worker/jmap/push-subscription-methods.ts +5 -2
- package/worker/jmap/push.ts +10 -11
- package/worker/jmap/routes.ts +10 -49
- package/worker/mail/bimi.ts +2 -3
- package/worker/mail/person-erasure.ts +58 -0
- package/worker/mail/push-notifications.ts +59 -419
- package/worker/mail/remote-proxy.ts +3 -206
- package/worker/mail/remote-routes.ts +2 -2
- package/worker/mail/uploads.ts +14 -6
- package/worker/mail/webhook-source.ts +13 -1
- package/worker/mailbox/index.ts +27 -0
- package/worker/mailbox/mailbox-drafts.ts +5 -0
- package/worker/module-api.ts +239 -0
- package/worker/module.ts +11 -7
- package/src/pwa/push-key.ts +0 -18
- package/src/pwa/push-subscription.ts +0 -213
- package/src/settings/notifications-page.tsx +0 -306
- package/worker/mail/push-endpoint.ts +0 -17
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
sendPushNotification,
|
|
3
|
-
topicFromString,
|
|
4
|
-
WebPushError,
|
|
5
|
-
} from "@mmmike/web-push/send";
|
|
6
|
-
import { createVapidJwt, urlBase64ToUint8Array } from "@mmmike/web-push/vapid";
|
|
7
1
|
import {
|
|
8
2
|
zValidator,
|
|
9
3
|
requireSessionAuth,
|
|
@@ -11,8 +5,9 @@ import {
|
|
|
11
5
|
mailboxAddressSql,
|
|
12
6
|
mailboxKindOrderSql,
|
|
13
7
|
apiError,
|
|
14
|
-
createId,
|
|
15
8
|
sendInBatches,
|
|
9
|
+
sendPush,
|
|
10
|
+
vapidConfiguration,
|
|
16
11
|
} from "@open-workspace/core/module";
|
|
17
12
|
import type {
|
|
18
13
|
BatchStatement,
|
|
@@ -23,9 +18,9 @@ import {
|
|
|
23
18
|
mailboxAccess,
|
|
24
19
|
mailboxes,
|
|
25
20
|
users,
|
|
26
|
-
|
|
21
|
+
webPushSubscriptions,
|
|
27
22
|
} from "@open-workspace/core/schema";
|
|
28
|
-
import { and, desc, eq,
|
|
23
|
+
import { and, desc, eq, exists, isNull, or } from "drizzle-orm";
|
|
29
24
|
import { Hono } from "hono";
|
|
30
25
|
import { z } from "zod";
|
|
31
26
|
|
|
@@ -34,51 +29,10 @@ import { mailPath } from "../../shared/mail-path";
|
|
|
34
29
|
import {
|
|
35
30
|
mailboxNotificationPreferences,
|
|
36
31
|
jmapPushSubscriptions,
|
|
37
|
-
pushSubscriptions,
|
|
38
32
|
} from "../db/schema";
|
|
39
33
|
import { deliverJmapStateChange } from "../jmap/push";
|
|
40
34
|
import { mailboxStub } from "../mailbox";
|
|
41
35
|
import { getMailboxAccess } from "./access";
|
|
42
|
-
import { isPublicPushEndpoint } from "./push-endpoint";
|
|
43
|
-
|
|
44
|
-
const pushSubscriptionSchema = z.object({
|
|
45
|
-
// The Worker POSTs to the endpoint for every new message, so it must be a
|
|
46
|
-
// public HTTPS URL (isPublicPushEndpoint), never a relay into the network.
|
|
47
|
-
endpoint: z
|
|
48
|
-
.url()
|
|
49
|
-
.max(4096)
|
|
50
|
-
.refine(isPublicPushEndpoint, "Push endpoint must be a public HTTPS URL"),
|
|
51
|
-
keys: z.object({
|
|
52
|
-
p256dh: z
|
|
53
|
-
.string()
|
|
54
|
-
.min(1)
|
|
55
|
-
.max(512)
|
|
56
|
-
.refine((value) => {
|
|
57
|
-
try {
|
|
58
|
-
const key = urlBase64ToUint8Array(value);
|
|
59
|
-
|
|
60
|
-
return key.length === 65 && key[0] === 4;
|
|
61
|
-
} catch {
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
}, "p256dh must be an uncompressed P-256 public key"),
|
|
65
|
-
auth: z
|
|
66
|
-
.string()
|
|
67
|
-
.min(1)
|
|
68
|
-
.max(512)
|
|
69
|
-
.refine((value) => {
|
|
70
|
-
try {
|
|
71
|
-
return urlBase64ToUint8Array(value).length >= 16;
|
|
72
|
-
} catch {
|
|
73
|
-
return false;
|
|
74
|
-
}
|
|
75
|
-
}, "auth must contain at least 16 bytes"),
|
|
76
|
-
}),
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
const subscriptionEndpointSchema = z.object({
|
|
80
|
-
endpoint: z.string().url().max(4096),
|
|
81
|
-
});
|
|
82
36
|
|
|
83
37
|
const notificationPreferenceSchema = z.object({ enabled: z.boolean() });
|
|
84
38
|
|
|
@@ -100,149 +54,17 @@ const pushJobSchema = z.discriminatedUnion("type", [
|
|
|
100
54
|
}),
|
|
101
55
|
pushJobBaseSchema.extend({ type: z.literal("dispatch") }),
|
|
102
56
|
pushJobBaseSchema.extend({
|
|
103
|
-
type: z.literal("
|
|
104
|
-
|
|
57
|
+
type: z.literal("notify"),
|
|
58
|
+
userId: z.string().min(1),
|
|
105
59
|
mailboxDisplayName: z.string().min(1).max(500),
|
|
106
60
|
}),
|
|
107
61
|
]) satisfies z.ZodType<MailboxPushJob>;
|
|
108
62
|
|
|
109
63
|
const MAX_PUSH_JOB_AGE_MS = 15 * 60 * 1000;
|
|
110
64
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
privateKey: string;
|
|
114
|
-
subject: string;
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
type PushConfigurationStatus = {
|
|
118
|
-
configuration: PushConfiguration | null;
|
|
119
|
-
error: string | null;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
let cachedPushConfiguration: {
|
|
123
|
-
source: string;
|
|
124
|
-
status: Promise<PushConfigurationStatus>;
|
|
125
|
-
} | null = null;
|
|
126
|
-
|
|
127
|
-
class RetryablePushError extends Error {
|
|
128
|
-
constructor(
|
|
129
|
-
readonly delaySeconds: number,
|
|
130
|
-
cause: unknown
|
|
131
|
-
) {
|
|
132
|
-
super("Push delivery should be retried", { cause });
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function retryDelaySeconds(cause: unknown) {
|
|
137
|
-
if (
|
|
138
|
-
cause instanceof WebPushError &&
|
|
139
|
-
cause.statusCode !== 408 &&
|
|
140
|
-
cause.statusCode !== 429 &&
|
|
141
|
-
cause.statusCode < 500
|
|
142
|
-
) {
|
|
143
|
-
return null;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const timedOut =
|
|
147
|
-
cause instanceof DOMException &&
|
|
148
|
-
(cause.name === "AbortError" || cause.name === "TimeoutError");
|
|
149
|
-
|
|
150
|
-
if (
|
|
151
|
-
!(cause instanceof WebPushError) &&
|
|
152
|
-
!(cause instanceof TypeError) &&
|
|
153
|
-
!timedOut
|
|
154
|
-
) {
|
|
155
|
-
return null;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const delayMs =
|
|
159
|
-
cause instanceof WebPushError ? (cause.retryAfterMs ?? 30_000) : 30_000;
|
|
160
|
-
|
|
161
|
-
return Math.max(1, Math.min(12 * 60 * 60, Math.ceil(delayMs / 1000)));
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function pushFailureDetails(cause: unknown) {
|
|
165
|
-
if (cause instanceof WebPushError) {
|
|
166
|
-
return {
|
|
167
|
-
name: cause.name,
|
|
168
|
-
message: cause.message,
|
|
169
|
-
statusCode: cause.statusCode,
|
|
170
|
-
retryAfterMs: cause.retryAfterMs,
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
return cause instanceof Error
|
|
175
|
-
? { name: cause.name, message: cause.message }
|
|
176
|
-
: { message: String(cause) };
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/** The Web Push settings, as push code reads them. */
|
|
180
|
-
export type PushBindings = Pick<
|
|
181
|
-
Env,
|
|
182
|
-
"VAPID_PUBLIC_KEY" | "VAPID_PRIVATE_KEY" | "VAPID_SUBJECT"
|
|
183
|
-
>;
|
|
184
|
-
|
|
185
|
-
function pushConfiguration(
|
|
186
|
-
env: PushBindings & { IDENTITY_PROVIDER_ORIGIN: string }
|
|
187
|
-
) {
|
|
188
|
-
const publicKey = env.VAPID_PUBLIC_KEY;
|
|
189
|
-
const privateKey = env.VAPID_PRIVATE_KEY;
|
|
190
|
-
// Push services want a contact; the app's own URL is one.
|
|
191
|
-
const subject = env.VAPID_SUBJECT ?? env.IDENTITY_PROVIDER_ORIGIN;
|
|
192
|
-
|
|
193
|
-
if (!publicKey || !privateKey || !subject) {
|
|
194
|
-
return Promise.resolve({
|
|
195
|
-
configuration: null,
|
|
196
|
-
error: null,
|
|
197
|
-
} satisfies PushConfigurationStatus);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const source = `${publicKey}\0${privateKey}\0${subject}`;
|
|
201
|
-
|
|
202
|
-
if (cachedPushConfiguration?.source === source) {
|
|
203
|
-
return cachedPushConfiguration.status;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
const configuration = { publicKey, privateKey, subject };
|
|
207
|
-
|
|
208
|
-
const status = createVapidJwt({
|
|
209
|
-
audience: "https://push.invalid",
|
|
210
|
-
expiration: 60,
|
|
211
|
-
...configuration,
|
|
212
|
-
})
|
|
213
|
-
.then(() => ({
|
|
214
|
-
configuration,
|
|
215
|
-
error: null,
|
|
216
|
-
}))
|
|
217
|
-
.catch((cause: unknown) => {
|
|
218
|
-
console.error(
|
|
219
|
-
"Invalid Web Push configuration",
|
|
220
|
-
pushFailureDetails(cause)
|
|
221
|
-
);
|
|
222
|
-
|
|
223
|
-
return {
|
|
224
|
-
configuration: null,
|
|
225
|
-
error: "Web Push server configuration is invalid.",
|
|
226
|
-
};
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
cachedPushConfiguration = { source, status };
|
|
230
|
-
|
|
231
|
-
return status;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
export const pushNotificationRoutes = new Hono<AppEnv>()
|
|
65
|
+
/** Which of their mailboxes may notify the signed-in person of new mail. */
|
|
66
|
+
export const notificationPreferenceRoutes = new Hono<AppEnv>()
|
|
235
67
|
.use("*", requireSessionAuth)
|
|
236
|
-
.get("/config", async (c) => {
|
|
237
|
-
const status = await pushConfiguration(c.env);
|
|
238
|
-
|
|
239
|
-
return c.json({
|
|
240
|
-
ok: true as const,
|
|
241
|
-
enabled: Boolean(status.configuration),
|
|
242
|
-
publicKey: status.configuration?.publicKey ?? null,
|
|
243
|
-
error: status.error,
|
|
244
|
-
});
|
|
245
|
-
})
|
|
246
68
|
.get("/preferences", async (c) => {
|
|
247
69
|
const userId = c.get("user").id;
|
|
248
70
|
|
|
@@ -305,132 +127,52 @@ export const pushNotificationRoutes = new Hono<AppEnv>()
|
|
|
305
127
|
set: { enabled: c.req.valid("json").enabled, updatedAt: now },
|
|
306
128
|
});
|
|
307
129
|
|
|
308
|
-
return c.json({ ok: true as const });
|
|
309
|
-
}
|
|
310
|
-
)
|
|
311
|
-
.put(
|
|
312
|
-
"/subscriptions",
|
|
313
|
-
zValidator("json", pushSubscriptionSchema),
|
|
314
|
-
async (c) => {
|
|
315
|
-
const input = c.req.valid("json");
|
|
316
|
-
const now = new Date();
|
|
317
|
-
// Bound to this session, so revoking or signing out of it ends the
|
|
318
|
-
// device's notifications. The app registers again on every start, so
|
|
319
|
-
// after a new sign-in on the device the row follows the new session.
|
|
320
|
-
const sessionId = c.get("sessionId");
|
|
321
|
-
await createDb(c.env.DB)
|
|
322
|
-
.insert(pushSubscriptions)
|
|
323
|
-
.values({
|
|
324
|
-
id: createId("push"),
|
|
325
|
-
userId: c.get("user").id,
|
|
326
|
-
sessionId,
|
|
327
|
-
endpoint: input.endpoint,
|
|
328
|
-
p256dh: input.keys.p256dh,
|
|
329
|
-
auth: input.keys.auth,
|
|
330
|
-
createdAt: now,
|
|
331
|
-
updatedAt: now,
|
|
332
|
-
})
|
|
333
|
-
.onConflictDoUpdate({
|
|
334
|
-
target: pushSubscriptions.endpoint,
|
|
335
|
-
set: {
|
|
336
|
-
userId: c.get("user").id,
|
|
337
|
-
sessionId,
|
|
338
|
-
p256dh: input.keys.p256dh,
|
|
339
|
-
auth: input.keys.auth,
|
|
340
|
-
updatedAt: now,
|
|
341
|
-
},
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
return c.json({ ok: true as const });
|
|
345
|
-
}
|
|
346
|
-
)
|
|
347
|
-
.delete(
|
|
348
|
-
"/subscriptions",
|
|
349
|
-
zValidator("json", subscriptionEndpointSchema),
|
|
350
|
-
async (c) => {
|
|
351
|
-
await createDb(c.env.DB)
|
|
352
|
-
.delete(pushSubscriptions)
|
|
353
|
-
.where(
|
|
354
|
-
and(
|
|
355
|
-
eq(pushSubscriptions.userId, c.get("user").id),
|
|
356
|
-
eq(pushSubscriptions.endpoint, c.req.valid("json").endpoint)
|
|
357
|
-
)
|
|
358
|
-
);
|
|
359
|
-
|
|
360
130
|
return c.json({ ok: true as const });
|
|
361
131
|
}
|
|
362
132
|
);
|
|
363
133
|
|
|
364
|
-
/**
|
|
365
|
-
|
|
366
|
-
* getting notifications, in the batch that deletes them. Rows without a
|
|
367
|
-
* session (registered before they were recorded) stay until the device
|
|
368
|
-
* registers again or every session goes; so do those of expired sessions,
|
|
369
|
-
* whose device moves them when it signs in again.
|
|
370
|
-
*/
|
|
371
|
-
export function deleteEndedSessionPushSubscriptions(
|
|
372
|
-
db: Database,
|
|
373
|
-
userId: string
|
|
374
|
-
) {
|
|
134
|
+
/** A person's sessions were all revoked: their mail apps' JMAP tokens went with them. */
|
|
135
|
+
export function deleteUserJmapPushSubscriptions(db: Database, userId: string) {
|
|
375
136
|
return [
|
|
376
|
-
db
|
|
377
|
-
.delete(pushSubscriptions)
|
|
378
|
-
.where(
|
|
379
|
-
and(
|
|
380
|
-
eq(pushSubscriptions.userId, userId),
|
|
381
|
-
isNotNull(pushSubscriptions.sessionId),
|
|
382
|
-
notInArray(
|
|
383
|
-
pushSubscriptions.sessionId,
|
|
384
|
-
db
|
|
385
|
-
.select({ id: sessions.id })
|
|
386
|
-
.from(sessions)
|
|
387
|
-
.where(eq(sessions.userId, userId))
|
|
388
|
-
)
|
|
389
|
-
)
|
|
390
|
-
),
|
|
391
|
-
] satisfies BatchStatement[];
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/** A person's sessions were all revoked: no device of theirs gets notifications. */
|
|
395
|
-
export function deleteUserPushSubscriptions(db: Database, userId: string) {
|
|
396
|
-
return [
|
|
397
|
-
db.delete(pushSubscriptions).where(eq(pushSubscriptions.userId, userId)),
|
|
398
|
-
// Mail apps' JMAP subscriptions too: their tokens went with the sessions.
|
|
399
137
|
db
|
|
400
138
|
.delete(jmapPushSubscriptions)
|
|
401
139
|
.where(eq(jmapPushSubscriptions.userId, userId)),
|
|
402
140
|
] satisfies BatchStatement[];
|
|
403
141
|
}
|
|
404
142
|
|
|
405
|
-
|
|
143
|
+
/**
|
|
144
|
+
* The active people who may open the mailbox and haven't turned its
|
|
145
|
+
* notifications off; with `userId`, only them. Only people with a device
|
|
146
|
+
* that takes push notifications: the rest have nothing to be told.
|
|
147
|
+
*/
|
|
148
|
+
function recipients(env: Env, mailboxId: string, userId?: string) {
|
|
406
149
|
const db = createDb(env.DB);
|
|
407
150
|
|
|
408
151
|
return db
|
|
409
|
-
.select({
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
.from(pushSubscriptions)
|
|
413
|
-
.innerJoin(users, eq(pushSubscriptions.userId, users.id))
|
|
414
|
-
.innerJoin(
|
|
415
|
-
mailboxAccess,
|
|
416
|
-
and(
|
|
417
|
-
eq(mailboxAccess.userId, pushSubscriptions.userId),
|
|
418
|
-
eq(mailboxAccess.mailboxId, mailboxId)
|
|
419
|
-
)
|
|
420
|
-
)
|
|
152
|
+
.select({ userId: mailboxAccess.userId })
|
|
153
|
+
.from(mailboxAccess)
|
|
154
|
+
.innerJoin(users, eq(mailboxAccess.userId, users.id))
|
|
421
155
|
.leftJoin(
|
|
422
156
|
mailboxNotificationPreferences,
|
|
423
157
|
and(
|
|
424
|
-
eq(mailboxNotificationPreferences.userId,
|
|
158
|
+
eq(mailboxNotificationPreferences.userId, mailboxAccess.userId),
|
|
425
159
|
eq(mailboxNotificationPreferences.mailboxId, mailboxId)
|
|
426
160
|
)
|
|
427
161
|
)
|
|
428
162
|
.where(
|
|
429
163
|
and(
|
|
164
|
+
eq(mailboxAccess.mailboxId, mailboxId),
|
|
165
|
+
userId ? eq(mailboxAccess.userId, userId) : undefined,
|
|
430
166
|
eq(users.status, "active"),
|
|
431
167
|
or(
|
|
432
168
|
isNull(mailboxNotificationPreferences.enabled),
|
|
433
169
|
eq(mailboxNotificationPreferences.enabled, true)
|
|
170
|
+
),
|
|
171
|
+
exists(
|
|
172
|
+
db
|
|
173
|
+
.select({ id: webPushSubscriptions.id })
|
|
174
|
+
.from(webPushSubscriptions)
|
|
175
|
+
.where(eq(webPushSubscriptions.userId, mailboxAccess.userId))
|
|
434
176
|
)
|
|
435
177
|
)
|
|
436
178
|
);
|
|
@@ -442,167 +184,73 @@ async function dispatchPushJob(
|
|
|
442
184
|
) {
|
|
443
185
|
const db = createDb(env.DB);
|
|
444
186
|
|
|
445
|
-
const [mailbox,
|
|
187
|
+
const [mailbox, people] = await Promise.all([
|
|
446
188
|
db
|
|
447
189
|
.select({ displayName: mailboxes.displayName })
|
|
448
190
|
.from(mailboxes)
|
|
449
191
|
.where(eq(mailboxes.id, job.mailboxId))
|
|
450
192
|
.limit(1)
|
|
451
193
|
.then((rows) => rows[0]),
|
|
452
|
-
|
|
194
|
+
recipients(env, job.mailboxId),
|
|
453
195
|
]);
|
|
454
196
|
|
|
455
|
-
if (!mailbox || !
|
|
197
|
+
if (!mailbox || !people.length) return;
|
|
456
198
|
|
|
457
|
-
// Each
|
|
199
|
+
// Each person's notification is its own job, retried on its own.
|
|
458
200
|
await sendInBatches(
|
|
459
201
|
env.PUSH_NOTIFICATIONS,
|
|
460
|
-
|
|
461
|
-
(
|
|
202
|
+
people.map(
|
|
203
|
+
({ userId }) =>
|
|
462
204
|
({
|
|
463
205
|
...job,
|
|
464
|
-
type: "
|
|
465
|
-
|
|
206
|
+
type: "notify",
|
|
207
|
+
userId,
|
|
466
208
|
mailboxDisplayName: mailbox.displayName,
|
|
467
209
|
}) satisfies MailboxPushJob
|
|
468
210
|
)
|
|
469
211
|
);
|
|
470
212
|
}
|
|
471
213
|
|
|
472
|
-
function
|
|
214
|
+
async function notifyPerson(
|
|
473
215
|
env: Env,
|
|
474
|
-
job: Extract<MailboxPushJob, { type: "
|
|
216
|
+
job: Extract<MailboxPushJob, { type: "notify" }>
|
|
475
217
|
) {
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
id: pushSubscriptions.id,
|
|
479
|
-
userId: pushSubscriptions.userId,
|
|
480
|
-
endpoint: pushSubscriptions.endpoint,
|
|
481
|
-
p256dh: pushSubscriptions.p256dh,
|
|
482
|
-
auth: pushSubscriptions.auth,
|
|
483
|
-
})
|
|
484
|
-
.from(pushSubscriptions)
|
|
485
|
-
.innerJoin(users, eq(pushSubscriptions.userId, users.id))
|
|
486
|
-
.innerJoin(
|
|
487
|
-
mailboxAccess,
|
|
488
|
-
and(
|
|
489
|
-
eq(mailboxAccess.userId, pushSubscriptions.userId),
|
|
490
|
-
eq(mailboxAccess.mailboxId, job.mailboxId)
|
|
491
|
-
)
|
|
492
|
-
)
|
|
493
|
-
.leftJoin(
|
|
494
|
-
mailboxNotificationPreferences,
|
|
495
|
-
and(
|
|
496
|
-
eq(mailboxNotificationPreferences.userId, pushSubscriptions.userId),
|
|
497
|
-
eq(mailboxNotificationPreferences.mailboxId, job.mailboxId)
|
|
498
|
-
)
|
|
499
|
-
)
|
|
500
|
-
.where(
|
|
501
|
-
and(
|
|
502
|
-
eq(users.status, "active"),
|
|
503
|
-
or(
|
|
504
|
-
isNull(mailboxNotificationPreferences.enabled),
|
|
505
|
-
eq(mailboxNotificationPreferences.enabled, true)
|
|
506
|
-
),
|
|
507
|
-
eq(pushSubscriptions.id, job.targetSubscriptionId)
|
|
508
|
-
)
|
|
509
|
-
)
|
|
510
|
-
.limit(1)
|
|
511
|
-
.then((rows) => rows[0]);
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
async function removeSubscription(env: Env, subscriptionId: string) {
|
|
515
|
-
try {
|
|
516
|
-
await createDb(env.DB)
|
|
517
|
-
.delete(pushSubscriptions)
|
|
518
|
-
.where(eq(pushSubscriptions.id, subscriptionId));
|
|
519
|
-
} catch (error) {
|
|
520
|
-
console.error("Could not remove invalid push subscription", error);
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
async function deliverPushJob(
|
|
525
|
-
env: Env,
|
|
526
|
-
job: Extract<MailboxPushJob, { type: "deliver" }>,
|
|
527
|
-
configuration: PushConfiguration,
|
|
528
|
-
ageMs: number
|
|
529
|
-
) {
|
|
530
|
-
const subscription = await targetSubscription(env, job);
|
|
531
|
-
|
|
532
|
-
if (!subscription) return;
|
|
218
|
+
// Asked again: they may have lost the mailbox or turned it off since.
|
|
219
|
+
const [recipient] = await recipients(env, job.mailboxId, job.userId);
|
|
533
220
|
|
|
534
|
-
|
|
535
|
-
if (!isPublicPushEndpoint(subscription.endpoint)) {
|
|
536
|
-
await removeSubscription(env, subscription.id);
|
|
537
|
-
|
|
538
|
-
return;
|
|
539
|
-
}
|
|
221
|
+
if (!recipient) return;
|
|
540
222
|
|
|
223
|
+
// Nothing when they read it already or have the mailbox open.
|
|
541
224
|
const destination = await mailboxStub(env, job.mailboxId).pushDestination(
|
|
542
225
|
job.messageId,
|
|
543
|
-
|
|
226
|
+
job.userId
|
|
544
227
|
);
|
|
545
228
|
|
|
546
229
|
if (!destination) return;
|
|
547
230
|
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
job.mailboxId,
|
|
559
|
-
destination.folder,
|
|
560
|
-
destination.conversationId
|
|
561
|
-
),
|
|
562
|
-
tag: `message-${job.messageId}`,
|
|
563
|
-
},
|
|
564
|
-
configuration,
|
|
565
|
-
{
|
|
566
|
-
ttl: Math.max(1, Math.ceil((MAX_PUSH_JOB_AGE_MS - ageMs) / 1000)),
|
|
567
|
-
urgency: "high",
|
|
568
|
-
topic: await topicFromString(`message:${job.messageId}`),
|
|
569
|
-
}
|
|
570
|
-
);
|
|
571
|
-
|
|
572
|
-
if (!delivered) await removeSubscription(env, subscription.id);
|
|
573
|
-
} catch (error) {
|
|
574
|
-
const delaySeconds = retryDelaySeconds(error);
|
|
575
|
-
|
|
576
|
-
if (delaySeconds === null) {
|
|
577
|
-
console.error(
|
|
578
|
-
"Push notification delivery was rejected",
|
|
579
|
-
pushFailureDetails(error)
|
|
580
|
-
);
|
|
581
|
-
|
|
582
|
-
return;
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
throw new RetryablePushError(delaySeconds, error);
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
|
|
589
|
-
/** The server's VAPID identity, or null when push is not configured. */
|
|
590
|
-
export async function vapidConfiguration(env: Env) {
|
|
591
|
-
return (await pushConfiguration(env)).configuration;
|
|
231
|
+
await sendPush(env, job.userId, {
|
|
232
|
+
title: `New email in ${job.mailboxDisplayName}`,
|
|
233
|
+
body: `${job.sender} · ${job.subject || "(no subject)"}`,
|
|
234
|
+
url: mailPath(
|
|
235
|
+
job.mailboxId,
|
|
236
|
+
destination.folder,
|
|
237
|
+
destination.conversationId
|
|
238
|
+
),
|
|
239
|
+
tag: `message-${job.messageId}`,
|
|
240
|
+
});
|
|
592
241
|
}
|
|
593
242
|
|
|
594
243
|
async function processPushJob(env: Env, job: MailboxPushJob) {
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
const { configuration } = await pushConfiguration(env);
|
|
244
|
+
if (Date.now() - job.occurredAt >= MAX_PUSH_JOB_AGE_MS) return;
|
|
245
|
+
// Without Web Push configured there is nothing to send.
|
|
246
|
+
const configuration = await vapidConfiguration(env);
|
|
599
247
|
|
|
600
248
|
if (!configuration) return;
|
|
601
249
|
|
|
602
250
|
if (job.type === "jmap-state") {
|
|
603
251
|
await deliverJmapStateChange(env, job, configuration);
|
|
604
252
|
} else if (job.type === "dispatch") await dispatchPushJob(env, job);
|
|
605
|
-
else await
|
|
253
|
+
else await notifyPerson(env, job);
|
|
606
254
|
}
|
|
607
255
|
|
|
608
256
|
export async function consumePushNotifications(
|
|
@@ -624,16 +272,8 @@ export async function consumePushNotifications(
|
|
|
624
272
|
await processPushJob(env, parsed.data);
|
|
625
273
|
message.ack();
|
|
626
274
|
} catch (error) {
|
|
627
|
-
console.error(
|
|
628
|
-
|
|
629
|
-
pushFailureDetails(
|
|
630
|
-
error instanceof RetryablePushError ? error.cause : error
|
|
631
|
-
)
|
|
632
|
-
);
|
|
633
|
-
message.retry({
|
|
634
|
-
delaySeconds:
|
|
635
|
-
error instanceof RetryablePushError ? error.delaySeconds : 30,
|
|
636
|
-
});
|
|
275
|
+
console.error("Could not process push notification job", error);
|
|
276
|
+
message.retry({ delaySeconds: 30 });
|
|
637
277
|
}
|
|
638
278
|
})
|
|
639
279
|
);
|