Skip to content

Email for professionals and teams

Improve read state

Hieu Nguyen (Jack)2e24bfd3b231
diff --git a/src/components/message-actions/utils.ts b/src/components/message-actions/utils.ts
index c318370..19e3325 100644
--- a/src/components/message-actions/utils.ts
+++ b/src/components/message-actions/utils.ts
@@ -130,7 +130,7 @@ export function getMessageActionRedirect(
export function buildReplySubject(subject: string | null | undefined) {
const trimmed = (subject ?? "").trim();
if (!trimmed) return "Re:";
- return /^re:/i.test(trimmed) ? trimmed : `Re: ${trimmed}`;
+ return /^re\s*(\[\d+\])?\s*:/i.test(trimmed) ? trimmed : `Re: ${trimmed}`;
}
export function buildReplyQuote(
diff --git a/src/lib/agent/tools.ts b/src/lib/agent/tools.ts
index 427a52f..f785608 100644
--- a/src/lib/agent/tools.ts
+++ b/src/lib/agent/tools.ts
@@ -156,7 +156,7 @@ export async function runEmailTool(context: AgentToolContext, name: EmailToolNam
const source = await ownMessage(context, input.emailId as string);
if (source.direction !== "inbound" || source.status === "spam" || source.status === "trash") throw new Error("Cannot reply to this message");
const recipients = await getReplyRecipients(context, source, input.replyAll as boolean);
- const subject = /^Re:/i.test(source.subject ?? "") ? source.subject! : `Re: ${source.subject ?? ""}`;
+ const subject = /^re\s*(\[\d+\])?\s*:/i.test(source.subject ?? "") ? source.subject! : `Re: ${source.subject ?? ""}`;
return createDraft(context, { ...recipients, subject, body: input.body as string, source });
}
if (name === "edit_draft") return editAgentDraft(context, input as unknown as AgentDraftEditInput);
diff --git a/src/lib/email/inbound.ts b/src/lib/email/inbound.ts
index 93c1f85..b5384f8 100644
--- a/src/lib/email/inbound.ts
+++ b/src/lib/email/inbound.ts
@@ -1,6 +1,6 @@
import { and, eq, isNotNull } from "drizzle-orm";
import { getDb } from "@/db";
-import { messages, outboundJobs, users } from "@/db/schema";
+import { domains, mailboxAliases, mailboxes, messages, outboundJobs, users } from "@/db/schema";
import { newId } from "@/lib/ids";
import { buildSnippet, parseRawMime } from "@/lib/email/parse";
import { resolveInboundAddress, resolveInboxRuleDestination } from "@/lib/email/routing";
@@ -32,6 +32,25 @@ export type InboundQueueMessage = {
headers?: Record<string, string>;
};
+/** True when the address is a mailbox or alias owned by the user, so their own mail never counts as unread. */
+async function isOwnAddress(db: ReturnType<typeof getDb>, userId: string, address: string) {
+ const at = address.lastIndexOf("@");
+ if (at < 1) return false;
+ const localPart = address.slice(0, at).toLowerCase();
+ const hostname = address.slice(at + 1).toLowerCase();
+ const [mailbox] = await db.select({ id: mailboxes.id }).from(mailboxes)
+ .innerJoin(domains, eq(mailboxes.domainId, domains.id))
+ .where(and(eq(mailboxes.userId, userId), eq(mailboxes.localPart, localPart), eq(domains.hostname, hostname)))
+ .limit(1);
+ if (mailbox) return true;
+ const [alias] = await db.select({ id: mailboxAliases.id }).from(mailboxAliases)
+ .innerJoin(mailboxes, eq(mailboxAliases.mailboxId, mailboxes.id))
+ .innerJoin(domains, eq(mailboxAliases.domainId, domains.id))
+ .where(and(eq(mailboxes.userId, userId), eq(mailboxAliases.localPart, localPart), eq(domains.hostname, hostname)))
+ .limit(1);
+ return Boolean(alias);
+}
+
export async function processInboundMessage(
env: CloudflareEnv,
payload: InboundQueueMessage,
@@ -133,6 +152,7 @@ export async function processInboundMessage(
address: fromAddr,
source: "inbound",
});
+ const sentByOwner = await isOwnAddress(db, decision.mailbox.userId, getEmailAddress(fromAddr) || fromAddr).catch(() => false);
let threadId: string;
try {
threadId = await resolveThreadId(db, {
@@ -168,6 +188,7 @@ export async function processInboundMessage(
htmlBody: parsed.html,
rawR2Key: payload.rawR2Key,
status,
+ read: sentByOwner,
threadId,
inReplyTo: parsed.inReplyTo,
references: parsed.references.length ? parsed.references.join(" ") : null,
diff --git a/src/lib/email/send.ts b/src/lib/email/send.ts
index 066334c..f6cbd97 100644
--- a/src/lib/email/send.ts
+++ b/src/lib/email/send.ts
@@ -128,6 +128,7 @@ export async function sendEmail(
textBody: input.text ?? null,
htmlBody: input.html ?? null,
status: "queued",
+ read: true,
threadId: input.threadId ?? null,
inReplyTo,
references: references.length ? references.join(" ") : null,