Email for professionals and teams
Keep own-alias senders as manual reply recipients (#145)
才倾梦华V2a21b8cdca9c
diff --git a/src/components/message-actions/utils.ts b/src/components/message-actions/utils.tsindex c18854b..f9d388c 100644--- a/src/components/message-actions/utils.ts+++ b/src/components/message-actions/utils.ts@@ -171,9 +171,9 @@ export function buildReplyQuoteHtml(/*** Who a reply goes to. A plain reply answers the sender, or the Reply-To* addresses when the message names any (RFC 5322 §3.6.2); reply-all also keeps- * everyone else on the To and Cc lines, minus the mailbox's own addresses so- * the author is not mailing themselves. Replying to a sent message re-addresses- * its original recipients.+ * everyone else on the To and Cc lines, minus the mailbox's own addresses.+ * The original sender stays even when the message came from another own alias.+ * Replying to a sent message re-addresses its original recipients.*/export function getReplyRecipients(message: ReplyableMessage,@@ -182,10 +182,10 @@ export function getReplyRecipients(): ReplyRecipients {const own = new Set(ownAddresses.map((address) => normalizeEmailAddress(address)));const seen = new Set<string>();- const unique = (entries: string[]) =>+ const unique = (entries: string[], excludeOwn = true) =>entries.filter((entry) => {const key = normalizeEmailAddress(entry);- if (!key || own.has(key) || seen.has(key)) return false;+ if (!key || (excludeOwn && own.has(key)) || seen.has(key)) return false;seen.add(key);return true;});@@ -197,7 +197,7 @@ export function getReplyRecipients(}const replyTo = unique(splitEmailAddressList(message.replyTo));- const to = replyTo.length ? replyTo : unique([message.fromAddr]);+ const to = replyTo.length ? replyTo : unique([message.fromAddr], false);if (mode !== "replyAll") return { to, cc: [] };const cc = unique([...splitEmailAddressList(message.toAddr), ...splitEmailAddressList(message.ccAddr)]);return { to, cc };diff --git a/tests/reply-recipients.test.mjs b/tests/reply-recipients.test.mjsindex c8fb48b..99e75e1 100644--- a/tests/reply-recipients.test.mjs+++ b/tests/reply-recipients.test.mjs@@ -11,7 +11,8 @@ after(() => rmSync(directory, { recursive: true, force: true }));await build({stdin: {contents: `- export { getReplyRecipients } from "./src/components/message-actions/utils.ts";+ export { createReplyDraft, getReplyRecipients, getReplyThreading, hasAdditionalRecipients } from "./src/components/message-actions/utils.ts";+ export { getOwnAddressForMessage } from "./src/app/(dashboard)/inbox/[messageId]/utils.ts";export { parseRawMime } from "./src/lib/email/parse.ts";`,resolveDir: root,@@ -26,7 +27,7 @@ await build({packages: "external",logLevel: "silent",});-const { getReplyRecipients, parseRawMime } = await import(pathToFileURL(join(directory, "entry.mjs")).href);+const { createReplyDraft, getReplyRecipients, getReplyThreading, hasAdditionalRecipients, getOwnAddressForMessage, parseRawMime } = await import(pathToFileURL(join(directory, "entry.mjs")).href);const own = ["[email protected]"];const inbound = (fields) => ({@@ -71,3 +72,72 @@ test("the parser keeps the full Reply-To header", async () => {].join("\r\n")).buffer;assert.equal((await parseRawMime(raw)).replyTo, '"Visitor" <[email protected]>, [email protected]');});++const ownAliases = ["[email protected]", "[email protected]"];+const aliasMessage = inbound({+ fromAddr: "Owner <[email protected]>",+ toAddr: "[email protected]",+ providerMessageId: "<[email protected]>",+ references: "<[email protected]>",+ threadId: "alias-thread",+});++test("a reply retains an incoming sender at the same own address or another own alias", () => {+ for (const toAddr of ownAliases) {+ const message = { ...aliasMessage, toAddr };+ assert.deepEqual(getReplyRecipients(message, ownAliases, "reply"), { to: [aliasMessage.fromAddr], cc: [] });+ assert.equal(hasAdditionalRecipients(message, ownAliases), false);+ }+});++test("reply-all retains the own-alias sender while excluding other own addresses and duplicates", () => {+ const message = { ...aliasMessage, toAddr: "[email protected], Friend <[email protected]>", ccAddr: "[email protected], [email protected], [email protected]" };+ assert.deepEqual(getReplyRecipients(message, ownAliases, "replyAll"), { to: [aliasMessage.fromAddr], cc: ["Friend <[email protected]>", "[email protected]"] });+ assert.equal(hasAdditionalRecipients(message, ownAliases), true);+ const outgoing = { ...message, direction: "outbound", toAddr: "[email protected], [email protected]" };+ assert.deepEqual(getReplyRecipients(outgoing, ownAliases, "replyAll"), { to: ["[email protected]"], cc: ["[email protected]"] });+});++test("Reply-To precedence and own-address fallback work with an own-alias sender", () => {+ const message = { ...aliasMessage, replyTo: "Visitor <[email protected]>, [email protected]" };+ assert.deepEqual(getReplyRecipients(message, ownAliases, "replyAll"), { to: ["Visitor <[email protected]>", "[email protected]"], cc: [] });+ assert.deepEqual(getReplyRecipients({ ...aliasMessage, replyTo: "[email protected]" }, ownAliases, "reply"), { to: [aliasMessage.fromAddr], cc: [] });+});++test("a cross-alias reply creates a draft from the receiving alias with the original threading", async (t) => {+ const requests = [];+ t.mock.method(globalThis, "fetch", async (url, options) => {+ assert.equal(url, "/api/drafts", "the test must not contact a real service");+ assert.equal(options.method, "POST");+ requests.push(JSON.parse(options.body));+ return Response.json({ draft: { id: "reply-draft" } });+ });+ const draftId = await createReplyDraft({+ mailboxId: "mailbox-1",+ senderAddress: aliasMessage.fromAddr,+ ownAddress: getOwnAddressForMessage(aliasMessage, ownAliases),+ subject: "Alias test",+ bodyText: "Original message",+ sentAt: "2026-10-14T12:00:00.000Z",+ recipients: getReplyRecipients(aliasMessage, ownAliases, "reply"),+ threading: getReplyThreading(aliasMessage),+ });+ assert.equal(draftId, "reply-draft");+ assert.equal(requests.length, 1);+ assert.equal(requests[0].mailboxId, "mailbox-1");+ assert.equal(requests[0].from, "[email protected]");+ assert.equal(requests[0].to, aliasMessage.fromAddr);+ assert.equal(requests[0].cc, "");+ assert.equal(requests[0].subject, "Re: Alias test");+ assert.equal(requests[0].inReplyTo, "[email protected]");+ assert.equal(requests[0].references, "[email protected] [email protected]");+ assert.equal(requests[0].threadId, aliasMessage.threadId);+ assert.match(requests[0].html, /Original message/);+});++test("a missing incoming sender still cannot create a reply draft", async () => {+ const message = { ...aliasMessage, fromAddr: "" };+ const recipients = getReplyRecipients(message, ownAliases, "reply");+ assert.deepEqual(recipients, { to: [], cc: [] });+ await assert.rejects(createReplyDraft({ mailboxId: "mailbox-1", senderAddress: "", recipients }), /Sender address is required/);+});