Email for professionals and teams
Fix #49
Hieu Nguyen (Jack)4c5d093d83f4
diff --git a/src/lib/email/inbound.ts b/src/lib/email/inbound.tsindex d367737..43cabd5 100644--- a/src/lib/email/inbound.ts+++ b/src/lib/email/inbound.ts@@ -14,6 +14,7 @@ import { inboundAttachmentLimitReason } from "@/lib/email/inbound-attachments";import { inboundMessageId } from "@/lib/email/inbound-id";import { getUnsubscribeUrlFromRawR2Key } from "@/lib/email/unsubscribe";import { resolveThreadId } from "@/lib/email/threading";+import { normalizeMessageId } from "@/lib/email/thread-lookup";import type { SessionUser } from "@/lib/auth/types";import { analyzeSpam } from "@/lib/spam/engine";import { getReputationKeys } from "@/lib/spam/analyzers/reputation";@@ -132,12 +133,23 @@ export async function processInboundMessage(address: fromAddr,source: "inbound",});- const threadId = await resolveThreadId(db, {- mailboxId: decision.mailbox.mailboxId,- messageId: parsed.messageId,- inReplyTo: parsed.inReplyTo,- references: parsed.references,- });+ let threadId: string;+ try {+ threadId = await resolveThreadId(db, {+ mailboxId: decision.mailbox.mailboxId,+ messageId: parsed.messageId,+ inReplyTo: parsed.inReplyTo,+ references: parsed.references,+ });+ } catch (error) {+ console.error("Inbound thread lookup failed", {+ rawR2Key: payload.rawR2Key,+ recipient: payload.to,+ referenceCount: parsed.references.length,+ error,+ });+ threadId = normalizeMessageId(parsed.messageId) ?? newId("thr");+ }try {const inserted = await db.insert(messages).values({diff --git a/src/lib/email/thread-lookup.ts b/src/lib/email/thread-lookup.tsnew file mode 100644index 0000000..e8e4d77--- /dev/null+++ b/src/lib/email/thread-lookup.ts@@ -0,0 +1,22 @@+/** RFC 5322 Message-IDs are compared without their angle brackets or surrounding space. */+export function normalizeMessageId(value: string | null | undefined): string | null {+ const trimmed = (value ?? "").trim().replace(/^<|>$/g, "").trim();+ return trimmed || null;+}++/** Keep the parent, thread root, and newest ancestors within D1's 100 parameter limit. */+export function selectThreadLookupIds(inReplyTo: string | null | undefined, references: string[]): string[] {+ const MAX_THREAD_LOOKUP_IDS = 40; // One mailbox parameter plus two variants per ID: at most 81.+ const candidates = new Set<string>();+ const add = (value: string | null | undefined) => {+ const id = normalizeMessageId(value);+ if (id) candidates.add(id);+ };++ add(inReplyTo);+ add(references[0]);+ for (let index = references.length - 1; index >= 1 && candidates.size < MAX_THREAD_LOOKUP_IDS; index--) {+ add(references[index]);+ }+ return Array.from(candidates);+}diff --git a/src/lib/email/threading.ts b/src/lib/email/threading.tsindex 0cdebc6..1c42aaa 100644--- a/src/lib/email/threading.ts+++ b/src/lib/email/threading.ts@@ -3,14 +3,11 @@ import type { getDb } from "@/db";import { messages } from "@/db/schema";import { newId } from "@/lib/ids";import type { ResolveThreadInput } from "@/lib/email/threading-types";+import { normalizeMessageId, selectThreadLookupIds } from "@/lib/email/thread-lookup";-type Db = ReturnType<typeof getDb>;+export { normalizeMessageId } from "@/lib/email/thread-lookup";-/** RFC 5322 Message-IDs are compared without their angle brackets or surrounding space. */-export function normalizeMessageId(value: string | null | undefined): string | null {- const trimmed = (value ?? "").trim().replace(/^<|>$/g, "").trim();- return trimmed || null;-}+type Db = ReturnType<typeof getDb>;/** A References header is a whitespace (occasionally comma) separated list of Message-IDs. */export function parseMessageIdList(value: string | null | undefined): string[] {@@ -50,14 +47,11 @@ export function buildReplyReferences(parentReferences: string[], parentMessageId* its own Message-ID so later replies can find it.*/export async function resolveThreadId(db: Db, input: ResolveThreadInput): Promise<string> {- const candidates = new Set<string>();- for (const id of [normalizeMessageId(input.inReplyTo), ...(input.references ?? [])]) {- if (id) candidates.add(id);- }+ const candidates = selectThreadLookupIds(input.inReplyTo, input.references ?? []);- if (input.mailboxId && candidates.size > 0) {+ if (input.mailboxId && candidates.length > 0) {// Stored Message-IDs may or may not include their angle brackets.- const variants = Array.from(candidates).flatMap((id) => [id, `<${id}>`]);+ const variants = candidates.flatMap((id) => [id, `<${id}>`]);const [parent] = await db.select({ threadId: messages.threadId, providerMessageId: messages.providerMessageId }).from(messages)diff --git a/tests/thread-lookup.test.mjs b/tests/thread-lookup.test.mjsnew file mode 100644index 0000000..b1288be--- /dev/null+++ b/tests/thread-lookup.test.mjs@@ -0,0 +1,44 @@+import assert from "node:assert/strict";+import { mkdtempSync, rmSync } from "node:fs";+import { tmpdir } from "node:os";+import { dirname, join } from "node:path";+import test, { after } from "node:test";+import { fileURLToPath, pathToFileURL } from "node:url";+import { build } from "esbuild";++const root = join(dirname(fileURLToPath(import.meta.url)), "..");+const outDir = mkdtempSync(join(tmpdir(), "mailflare-thread-lookup-test-"));+after(() => rmSync(outDir, { recursive: true, force: true }));++await build({+ entryPoints: [join(root, "src/lib/email/thread-lookup.ts")],+ outfile: join(outDir, "entry.mjs"),+ bundle: true,+ platform: "node",+ format: "esm",+ target: "node22",+ logLevel: "silent",+});++const { selectThreadLookupIds } = await import(pathToFileURL(join(outDir, "entry.mjs")).href);++test("144 References stay within D1's bound parameter limit while preserving parent, root, and recent ancestors", () => {+ const references = Array.from({ length: 144 }, (_, index) => `ref-${index}@example.com`);+ const selected = selectThreadLookupIds("<[email protected]>", references);++ assert.equal(selected.length, 40);+ assert.equal(1 + selected.length * 2, 81);+ assert.equal(selected[0], "[email protected]");+ assert.equal(selected[1], references[0]);+ assert.deepEqual(selected.slice(2), references.slice(-38).reverse());+});++test("an In-Reply-To also present in References is only looked up once", () => {+ const references = Array.from({ length: 144 }, (_, index) => `ref-${index}@example.com`);+ const selected = selectThreadLookupIds(`<${references.at(-1)}>`, references);++ assert.equal(selected.length, 40);+ assert.equal(new Set(selected).size, selected.length);+ assert.equal(selected[0], references.at(-1));+ assert.equal(selected[1], references[0]);+});diff --git a/worker.ts b/worker.tsindex 423e1b4..85d5630 100644--- a/worker.ts+++ b/worker.ts@@ -110,7 +110,12 @@ export default {}msg.ack();} catch (err) {- console.error("Queue processing failed", err);+ console.error("Queue processing failed", {+ rawR2Key: isInboundQueueMessage(msg.body) ? msg.body.rawR2Key : undefined,+ recipient: isInboundQueueMessage(msg.body) ? msg.body.to : undefined,+ attempts: msg.attempts,+ error: err,+ });msg.retry({ delaySeconds: 10 });}}