@open-workspace/email 0.4.1 → 0.4.2
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/drizzle/mailbox/0002_good_komodo.sql +2 -0
- package/drizzle/mailbox/meta/0002_snapshot.json +1017 -0
- package/drizzle/mailbox/meta/_journal.json +7 -0
- package/drizzle/mailbox/migrations.js +3 -1
- package/migrations/0001_overrated_ricochet.sql +2 -0
- package/migrations/meta/0001_snapshot.json +564 -0
- package/migrations/meta/_journal.json +7 -0
- package/package.json +4 -4
- package/src/mail/conversation-view.tsx +6 -1
- package/src/mail/reader-pane.tsx +1 -0
- package/src/mail/sanitize-email-html.ts +4 -1
- package/src/mail/sender-actions.tsx +11 -3
- package/worker/db/schema.ts +13 -1
- package/worker/mail/push-notifications.ts +63 -10
- package/worker/mail/quote-parser.ts +92 -22
- package/worker/mail/r2-presigned-urls.ts +7 -0
- package/worker/mail/safety-routes.ts +7 -1
- package/worker/mail/uploads.ts +1 -0
- package/worker/mailbox/email-index.ts +49 -0
- package/worker/mailbox/index.ts +445 -45
- package/worker/mailbox/schema.ts +11 -0
- package/worker/mcp/mail-compose-tools.ts +1 -1
- package/worker/mcp/mail-safety-tools.ts +1 -1
- package/worker/module.ts +2 -0
|
@@ -23,10 +23,13 @@ type SenderDialogKind = "unsubscribe" | "block";
|
|
|
23
23
|
export function useSenderActions({
|
|
24
24
|
mailbox,
|
|
25
25
|
onLeaveConversation,
|
|
26
|
+
onOpenConversation,
|
|
26
27
|
}: {
|
|
27
28
|
mailbox?: Mailbox;
|
|
28
29
|
/** Called after an action moved the open conversation out of the current folder. */
|
|
29
30
|
onLeaveConversation: () => void;
|
|
31
|
+
/** Called with the conversation a message rejoined (Not spam) and its folder, to open it instead. */
|
|
32
|
+
onOpenConversation: (conversationId: string, folder: string) => void;
|
|
30
33
|
}) {
|
|
31
34
|
const [dialog, setDialog] = useState<{
|
|
32
35
|
kind: SenderDialogKind;
|
|
@@ -88,11 +91,16 @@ export function useSenderActions({
|
|
|
88
91
|
notSpam.mutate(
|
|
89
92
|
{ mailboxId: mailbox.id, messageId: message.id },
|
|
90
93
|
{
|
|
91
|
-
onSuccess: () => {
|
|
94
|
+
onSuccess: (result) => {
|
|
92
95
|
toast.success(
|
|
93
|
-
|
|
96
|
+
`${result.rejoined ? "Moved back to its conversation" : "Moved to Inbox"}. Mail from ${message.fromAddress} won't be treated as spam.`
|
|
94
97
|
);
|
|
95
|
-
|
|
98
|
+
|
|
99
|
+
if (result.rejoined) {
|
|
100
|
+
onOpenConversation(result.conversationId, result.folder);
|
|
101
|
+
} else {
|
|
102
|
+
onLeaveConversation();
|
|
103
|
+
}
|
|
96
104
|
},
|
|
97
105
|
onError: (error) => toast.error(error.message),
|
|
98
106
|
}
|
package/worker/db/schema.ts
CHANGED
|
@@ -32,7 +32,11 @@ export const mailboxNotificationPreferences = sqliteTable(
|
|
|
32
32
|
]
|
|
33
33
|
);
|
|
34
34
|
|
|
35
|
-
/**
|
|
35
|
+
/**
|
|
36
|
+
* Browsers that get new-mail notifications, one row per device, bound to
|
|
37
|
+
* the session that last registered it. Rows from before sessions were
|
|
38
|
+
* recorded have none and are removed only with all of a person's sessions.
|
|
39
|
+
*/
|
|
36
40
|
export const pushSubscriptions = sqliteTable(
|
|
37
41
|
"push_subscriptions",
|
|
38
42
|
{
|
|
@@ -40,6 +44,13 @@ export const pushSubscriptions = sqliteTable(
|
|
|
40
44
|
userId: text("user_id")
|
|
41
45
|
.notNull()
|
|
42
46
|
.references(() => users.id, { onDelete: "cascade" }),
|
|
47
|
+
/**
|
|
48
|
+
* Not a foreign key: expiry and core's pruning of old sessions must not
|
|
49
|
+
* take the device's notifications with them. Re-registering moves the
|
|
50
|
+
* row to the new session; only revoking or signing out of this one
|
|
51
|
+
* removes it (the `sessionRevoked` hook).
|
|
52
|
+
*/
|
|
53
|
+
sessionId: text("session_id"),
|
|
43
54
|
endpoint: text("endpoint").notNull(),
|
|
44
55
|
p256dh: text("p256dh").notNull(),
|
|
45
56
|
auth: text("auth").notNull(),
|
|
@@ -53,6 +64,7 @@ export const pushSubscriptions = sqliteTable(
|
|
|
53
64
|
(table) => [
|
|
54
65
|
uniqueIndex("push_subscriptions_endpoint_unique").on(table.endpoint),
|
|
55
66
|
index("push_subscriptions_user_idx").on(table.userId),
|
|
67
|
+
index("push_subscriptions_session_idx").on(table.sessionId),
|
|
56
68
|
]
|
|
57
69
|
);
|
|
58
70
|
|
|
@@ -16,7 +16,11 @@ import {
|
|
|
16
16
|
apiError,
|
|
17
17
|
createId,
|
|
18
18
|
} from "@open-workspace/core/module";
|
|
19
|
-
import type {
|
|
19
|
+
import type {
|
|
20
|
+
BatchStatement,
|
|
21
|
+
Database,
|
|
22
|
+
AppEnv,
|
|
23
|
+
} from "@open-workspace/core/module";
|
|
20
24
|
import { and, desc, eq, isNull, or } from "drizzle-orm";
|
|
21
25
|
import { Hono } from "hono";
|
|
22
26
|
import { z } from "zod";
|
|
@@ -30,15 +34,29 @@ import {
|
|
|
30
34
|
import { deliverJmapStateChange } from "../jmap/push";
|
|
31
35
|
import { mailboxStub } from "../mailbox";
|
|
32
36
|
import { getMailboxAccess } from "./access";
|
|
37
|
+
import { assertProxyableRemoteUrl } from "./remote-proxy";
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Push goes to whatever push service the subscriber's browser uses, so any
|
|
41
|
+
* public HTTPS host is accepted (a self-hosted Mozilla autopush included).
|
|
42
|
+
* What is refused is what the image proxy refuses: credentials in the URL,
|
|
43
|
+
* localhost, private and internal addresses, and internal-only names.
|
|
44
|
+
*/
|
|
45
|
+
function isPublicPushEndpoint(endpoint: string) {
|
|
46
|
+
try {
|
|
47
|
+
return assertProxyableRemoteUrl(endpoint).protocol === "https:";
|
|
48
|
+
} catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
33
52
|
|
|
34
53
|
const pushSubscriptionSchema = z.object({
|
|
54
|
+
// The Worker POSTs to the endpoint for every new message, so it must be a
|
|
55
|
+
// public HTTPS URL (isPublicPushEndpoint), never a relay into the network.
|
|
35
56
|
endpoint: z
|
|
36
57
|
.url()
|
|
37
58
|
.max(4096)
|
|
38
|
-
.refine(
|
|
39
|
-
(value) => new URL(value).protocol === "https:",
|
|
40
|
-
"Push endpoint must use HTTPS"
|
|
41
|
-
),
|
|
59
|
+
.refine(isPublicPushEndpoint, "Push endpoint must be a public HTTPS URL"),
|
|
42
60
|
keys: z.object({
|
|
43
61
|
p256dh: z
|
|
44
62
|
.string()
|
|
@@ -306,11 +324,16 @@ export const pushNotificationRoutes = new Hono<AppEnv>()
|
|
|
306
324
|
async (c) => {
|
|
307
325
|
const input = c.req.valid("json");
|
|
308
326
|
const now = new Date();
|
|
327
|
+
// Bound to this session, so revoking or signing out of it ends the
|
|
328
|
+
// device's notifications. The app registers again on every start, so
|
|
329
|
+
// after a new sign-in on the device the row follows the new session.
|
|
330
|
+
const sessionId = c.get("sessionId");
|
|
309
331
|
await createDb(c.env.DB)
|
|
310
332
|
.insert(pushSubscriptions)
|
|
311
333
|
.values({
|
|
312
334
|
id: createId("push"),
|
|
313
335
|
userId: c.get("user").id,
|
|
336
|
+
sessionId,
|
|
314
337
|
endpoint: input.endpoint,
|
|
315
338
|
p256dh: input.keys.p256dh,
|
|
316
339
|
auth: input.keys.auth,
|
|
@@ -321,6 +344,7 @@ export const pushNotificationRoutes = new Hono<AppEnv>()
|
|
|
321
344
|
target: pushSubscriptions.endpoint,
|
|
322
345
|
set: {
|
|
323
346
|
userId: c.get("user").id,
|
|
347
|
+
sessionId,
|
|
324
348
|
p256dh: input.keys.p256dh,
|
|
325
349
|
auth: input.keys.auth,
|
|
326
350
|
updatedAt: now,
|
|
@@ -347,14 +371,36 @@ export const pushNotificationRoutes = new Hono<AppEnv>()
|
|
|
347
371
|
}
|
|
348
372
|
);
|
|
349
373
|
|
|
374
|
+
/**
|
|
375
|
+
* One session was revoked or signed out: its device stops getting
|
|
376
|
+
* notifications, in the batch that deletes the session. Rows without a
|
|
377
|
+
* session (registered before they were recorded) stay until the device
|
|
378
|
+
* registers again or every session goes.
|
|
379
|
+
*/
|
|
380
|
+
export function deleteSessionPushSubscriptions(
|
|
381
|
+
db: Database,
|
|
382
|
+
session: { userId: string; sessionId: string }
|
|
383
|
+
): BatchStatement[] {
|
|
384
|
+
return [
|
|
385
|
+
db
|
|
386
|
+
.delete(pushSubscriptions)
|
|
387
|
+
.where(
|
|
388
|
+
and(
|
|
389
|
+
eq(pushSubscriptions.userId, session.userId),
|
|
390
|
+
eq(pushSubscriptions.sessionId, session.sessionId)
|
|
391
|
+
)
|
|
392
|
+
),
|
|
393
|
+
];
|
|
394
|
+
}
|
|
395
|
+
|
|
350
396
|
/** A person's sessions were all revoked: no device of theirs gets notifications. */
|
|
351
|
-
export
|
|
397
|
+
export function deleteUserPushSubscriptions(
|
|
352
398
|
db: Database,
|
|
353
399
|
userId: string
|
|
354
|
-
) {
|
|
355
|
-
|
|
356
|
-
.delete(pushSubscriptions)
|
|
357
|
-
|
|
400
|
+
): BatchStatement[] {
|
|
401
|
+
return [
|
|
402
|
+
db.delete(pushSubscriptions).where(eq(pushSubscriptions.userId, userId)),
|
|
403
|
+
];
|
|
358
404
|
}
|
|
359
405
|
|
|
360
406
|
function subscriptionsForMailbox(env: Env, mailboxId: string) {
|
|
@@ -498,6 +544,13 @@ async function deliverPushJob(
|
|
|
498
544
|
|
|
499
545
|
if (!subscription) return;
|
|
500
546
|
|
|
547
|
+
// Registered before endpoints had to be public HTTPS URLs.
|
|
548
|
+
if (!isPublicPushEndpoint(subscription.endpoint)) {
|
|
549
|
+
await removeSubscription(env, subscription.id);
|
|
550
|
+
|
|
551
|
+
return;
|
|
552
|
+
}
|
|
553
|
+
|
|
501
554
|
const suppressed = await mailboxStub(env, job.mailboxId).shouldSuppressPush(
|
|
502
555
|
job.messageId,
|
|
503
556
|
subscription.userId
|
|
@@ -3,45 +3,115 @@ export type ParsedReplyText = {
|
|
|
3
3
|
quotedText: string | null;
|
|
4
4
|
};
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Attribution lines are short; longer lines are never tested, which keeps a
|
|
8
|
+
* crafted single-line body from costing more than a scan.
|
|
9
|
+
*/
|
|
10
|
+
const MAX_ATTRIBUTION_LENGTH = 300;
|
|
11
|
+
|
|
12
|
+
/** How many lines after an Outlook `From:` line may hold its other headers. */
|
|
13
|
+
const OUTLOOK_FOLLOWUP_LINES = 4;
|
|
14
|
+
|
|
15
|
+
const OUTLOOK_HEADER = /^from:\s+\S/iu;
|
|
16
|
+
const OUTLOOK_FOLLOWUP = /^(?:sent|date|to|subject):\s+\S/iu;
|
|
17
|
+
const QUOTED_LINE = /^\s*>/u;
|
|
18
|
+
|
|
19
|
+
/** Characters `.` does not match; an attribution line never contains one. */
|
|
20
|
+
const LINE_TERMINATOR = /[\r\u2028\u2029]/u;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Whether `line` is `<prefix><at least one character><suffix>`, the shape of
|
|
24
|
+
* `^prefix.+suffix$`, tested without a regular expression.
|
|
25
|
+
*/
|
|
26
|
+
function wraps(line: string, prefix: string, suffix: string) {
|
|
27
|
+
return (
|
|
28
|
+
line.length > prefix.length + suffix.length &&
|
|
29
|
+
line.startsWith(prefix) &&
|
|
30
|
+
line.endsWith(suffix)
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** `Am <date> schrieb <name>:` (German). */
|
|
35
|
+
function isGermanAttribution(line: string) {
|
|
36
|
+
if (!wraps(line, "am ", ":")) return false;
|
|
37
|
+
const marker = line.indexOf("schrieb ", "am ".length + 1);
|
|
38
|
+
|
|
39
|
+
// At least one character between the marker and the closing colon.
|
|
40
|
+
return marker !== -1 && marker + "schrieb ".length < line.length - 1;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** `Le <date>, <name> a écrit :` (French). */
|
|
44
|
+
function isFrenchAttribution(line: string) {
|
|
45
|
+
if (!wraps(line, "le ", ":")) return false;
|
|
46
|
+
|
|
47
|
+
return wraps(line.slice(0, -1).trimEnd(), "le ", "a écrit");
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const SEPARATORS = [
|
|
11
51
|
/^-----\s*original message\s*-----$/iu,
|
|
12
52
|
/^-----\s*исходное сообщение\s*-----$/iu,
|
|
13
53
|
];
|
|
14
54
|
|
|
15
|
-
|
|
16
|
-
|
|
55
|
+
/** Whether a trimmed line is a quote's attribution line in one of the known languages. */
|
|
56
|
+
function isAttributionLine(line: string) {
|
|
57
|
+
if (line.length > MAX_ATTRIBUTION_LENGTH || LINE_TERMINATOR.test(line)) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const lower = line.toLowerCase();
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
wraps(lower, "on ", "wrote:") ||
|
|
65
|
+
isGermanAttribution(lower) ||
|
|
66
|
+
isFrenchAttribution(lower) ||
|
|
67
|
+
wraps(lower, "в ", "написал:") ||
|
|
68
|
+
wraps(lower, "в ", "написала:") ||
|
|
69
|
+
SEPARATORS.some((pattern) => pattern.test(line))
|
|
70
|
+
);
|
|
71
|
+
}
|
|
17
72
|
|
|
18
|
-
/** Whether `
|
|
19
|
-
function isAttribution(
|
|
20
|
-
const line =
|
|
73
|
+
/** Whether line `index` starts a quote: an attribution line, or an Outlook header block (with the lines after it). */
|
|
74
|
+
function isAttribution(lines: string[], index: number) {
|
|
75
|
+
const line = (lines[index] ?? "").trim();
|
|
21
76
|
|
|
22
|
-
if (
|
|
77
|
+
if (isAttributionLine(line)) return true;
|
|
23
78
|
|
|
24
79
|
if (!OUTLOOK_HEADER.test(line)) return false;
|
|
25
80
|
|
|
26
|
-
return
|
|
27
|
-
.slice(
|
|
81
|
+
return lines
|
|
82
|
+
.slice(index + 1, index + 1 + OUTLOOK_FOLLOWUP_LINES)
|
|
28
83
|
.some((candidate) => OUTLOOK_FOLLOWUP.test(candidate.trim()));
|
|
29
84
|
}
|
|
30
85
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
86
|
+
/**
|
|
87
|
+
* The first `>` line from which at least 60% of the non-blank lines are
|
|
88
|
+
* quoted. One backward pass counts both for every suffix of the body.
|
|
89
|
+
*/
|
|
90
|
+
function trailingQuoteStart(lines: string[]) {
|
|
91
|
+
let nonBlank = 0;
|
|
92
|
+
let quoted = 0;
|
|
93
|
+
let start = -1;
|
|
94
|
+
|
|
95
|
+
for (let index = lines.length - 1; index >= 0; index -= 1) {
|
|
96
|
+
const line = lines[index] ?? "";
|
|
97
|
+
|
|
98
|
+
if (line.trim()) nonBlank += 1;
|
|
99
|
+
|
|
100
|
+
if (!QUOTED_LINE.test(line)) continue;
|
|
101
|
+
quoted += 1;
|
|
102
|
+
|
|
103
|
+
if (quoted / nonBlank >= 0.6) start = index;
|
|
34
104
|
}
|
|
35
105
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const remaining = lines.slice(index).filter((line) => line.trim());
|
|
39
|
-
const quoted = remaining.filter((line) => /^\s*>/u.test(line)).length;
|
|
106
|
+
return start;
|
|
107
|
+
}
|
|
40
108
|
|
|
41
|
-
|
|
109
|
+
function quotedBlockStart(lines: string[]) {
|
|
110
|
+
for (const index of lines.keys()) {
|
|
111
|
+
if (isAttribution(lines, index)) return index;
|
|
42
112
|
}
|
|
43
113
|
|
|
44
|
-
return
|
|
114
|
+
return trailingQuoteStart(lines);
|
|
45
115
|
}
|
|
46
116
|
|
|
47
117
|
export function parseReplyText(
|
|
@@ -39,10 +39,16 @@ function r2Signer(env: Bindings, r2Key: string) {
|
|
|
39
39
|
};
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* A PUT URL for exactly `size` bytes of `contentType`: both headers are
|
|
44
|
+
* signed, so R2 refuses any other length (SignatureDoesNotMatch). Clients
|
|
45
|
+
* send Content-Length on their own; browsers don't let a page set it.
|
|
46
|
+
*/
|
|
42
47
|
export async function signR2PutUrl(input: {
|
|
43
48
|
env: Bindings;
|
|
44
49
|
r2Key: string;
|
|
45
50
|
contentType: string;
|
|
51
|
+
size: number;
|
|
46
52
|
}) {
|
|
47
53
|
const signer = r2Signer(input.env, input.r2Key);
|
|
48
54
|
|
|
@@ -50,6 +56,7 @@ export async function signR2PutUrl(input: {
|
|
|
50
56
|
method: "PUT",
|
|
51
57
|
headers: {
|
|
52
58
|
"content-type": input.contentType,
|
|
59
|
+
"content-length": String(input.size),
|
|
53
60
|
},
|
|
54
61
|
aws: {
|
|
55
62
|
allHeaders: true,
|
|
@@ -372,7 +372,13 @@ export const mailSafetyRoutes = new Hono<AppEnv>()
|
|
|
372
372
|
);
|
|
373
373
|
}
|
|
374
374
|
|
|
375
|
-
return c.json({
|
|
375
|
+
return c.json({
|
|
376
|
+
ok: true as const,
|
|
377
|
+
restored: result.restored,
|
|
378
|
+
conversationId: result.conversationId,
|
|
379
|
+
rejoined: result.rejoined,
|
|
380
|
+
folder: result.folder,
|
|
381
|
+
});
|
|
376
382
|
}
|
|
377
383
|
)
|
|
378
384
|
.get(
|
package/worker/mail/uploads.ts
CHANGED
|
@@ -51,3 +51,52 @@ export function indexInsertedEmail(sql: SqlStorage, emailId: string) {
|
|
|
51
51
|
emailId
|
|
52
52
|
);
|
|
53
53
|
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Moves every message of conversation `fromId` into `intoId`, inside the
|
|
57
|
+
* caller's transaction: the messages, their search rows, and `intoId`'s
|
|
58
|
+
* latest message, timeline and directions, which are recomputed from its
|
|
59
|
+
* messages. `intoId` keeps its place (state and folder); `fromId` is removed.
|
|
60
|
+
* Read states belong to messages and move with them.
|
|
61
|
+
*/
|
|
62
|
+
export function mergeConversation(
|
|
63
|
+
sql: SqlStorage,
|
|
64
|
+
fromId: string,
|
|
65
|
+
intoId: string
|
|
66
|
+
) {
|
|
67
|
+
sql.exec(
|
|
68
|
+
"update emails set conversation_id = ? where conversation_id = ?",
|
|
69
|
+
intoId,
|
|
70
|
+
fromId
|
|
71
|
+
);
|
|
72
|
+
sql.exec(
|
|
73
|
+
"update email_search set conversation_id = ? where conversation_id = ?",
|
|
74
|
+
intoId,
|
|
75
|
+
fromId
|
|
76
|
+
);
|
|
77
|
+
sql.exec(
|
|
78
|
+
`update conversations set
|
|
79
|
+
latest_email_id = (
|
|
80
|
+
select message.id from emails message
|
|
81
|
+
where message.conversation_id = conversations.id
|
|
82
|
+
order by message.timeline_at desc, message.id desc limit 1
|
|
83
|
+
),
|
|
84
|
+
timeline_at = (
|
|
85
|
+
select max(message.timeline_at) from emails message
|
|
86
|
+
where message.conversation_id = conversations.id
|
|
87
|
+
),
|
|
88
|
+
has_incoming = exists (
|
|
89
|
+
select 1 from emails message
|
|
90
|
+
where message.conversation_id = conversations.id
|
|
91
|
+
and message.direction = 'incoming'
|
|
92
|
+
),
|
|
93
|
+
has_outgoing = exists (
|
|
94
|
+
select 1 from emails message
|
|
95
|
+
where message.conversation_id = conversations.id
|
|
96
|
+
and message.direction = 'outgoing'
|
|
97
|
+
)
|
|
98
|
+
where id = ?`,
|
|
99
|
+
intoId
|
|
100
|
+
);
|
|
101
|
+
sql.exec("delete from conversations where id = ?", fromId);
|
|
102
|
+
}
|