fix: reply to own thread message addresses original recipients #703

This commit is contained in:
Linus Rath
2026-07-30 19:02:39 +02:00
parent aa7a814b86
commit 59bc7fd64c
5 changed files with 501 additions and 31 deletions
+114
View File
@@ -0,0 +1,114 @@
import { describe, it, expect } from 'vitest';
import { buildReplyRecipients, isSelfSent } from '@/lib/reply-recipients';
const OWN = ['me@example.com', 'info@example.com'];
const emails = (list: { email?: string }[]) => list.map((r) => r.email);
describe('buildReplyRecipients', () => {
describe('received message', () => {
const received = {
from: [{ email: 'bob@other.com', name: 'Bob' }],
to: [{ email: 'me@example.com' }, { email: 'carol@other.com' }],
cc: [{ email: 'dave@other.com' }],
};
it('replies to the sender', () => {
const { to, cc } = buildReplyRecipients(received, 'reply', OWN);
expect(emails(to)).toEqual(['bob@other.com']);
expect(cc).toEqual([]);
});
it('prefers the Reply-To header over From', () => {
const { to } = buildReplyRecipients(
{ ...received, replyToAddresses: [{ email: 'list@other.com' }] },
'reply',
OWN,
);
expect(emails(to)).toEqual(['list@other.com']);
});
it('reply-all keeps the other recipients and drops our own address', () => {
const { to, cc } = buildReplyRecipients(received, 'replyAll', OWN);
expect(emails(to)).toEqual(['bob@other.com', 'carol@other.com']);
expect(emails(cc)).toEqual(['dave@other.com']);
});
it('reply-all drops our own address even with +tag sub-addressing', () => {
const { to } = buildReplyRecipients(
{ ...received, to: [{ email: 'me+newsletter@example.com' }, { email: 'carol@other.com' }] },
'replyAll',
OWN,
);
expect(emails(to)).toEqual(['bob@other.com', 'carol@other.com']);
});
});
describe('self-sent message (#703)', () => {
const sent = {
from: [{ email: 'me@example.com', name: 'Me' }],
to: [{ email: 'bob@other.com', name: 'Bob' }],
cc: [{ email: 'carol@other.com' }],
};
it('replies to the original recipient, not to ourselves', () => {
const { to, cc } = buildReplyRecipients(sent, 'reply', OWN);
expect(emails(to)).toEqual(['bob@other.com']);
expect(cc).toEqual([]);
});
it('reply-all restores the original To and Cc', () => {
const { to, cc } = buildReplyRecipients(sent, 'replyAll', OWN);
expect(emails(to)).toEqual(['bob@other.com']);
expect(emails(cc)).toEqual(['carol@other.com']);
});
it('recognises the sending identity through +tag sub-addressing', () => {
const { to } = buildReplyRecipients(
{ ...sent, from: [{ email: 'me+project@example.com' }] },
'reply',
OWN,
);
expect(emails(to)).toEqual(['bob@other.com']);
});
it('ignores our own Reply-To header so the reply leaves our mailbox', () => {
const { to } = buildReplyRecipients(
{ ...sent, replyToAddresses: [{ email: 'info@example.com' }] },
'reply',
OWN,
);
expect(emails(to)).toEqual(['bob@other.com']);
});
it('keeps a self-addressed recipient we chose ourselves', () => {
const { to } = buildReplyRecipients(
{ ...sent, to: [{ email: 'info@example.com' }] },
'reply',
OWN,
);
expect(emails(to)).toEqual(['info@example.com']);
});
it('falls back to the sender when there is no visible recipient (Bcc-only)', () => {
const { to } = buildReplyRecipients({ ...sent, to: [], cc: [] }, 'reply', OWN);
expect(emails(to)).toEqual(['me@example.com']);
});
it('keeps the display names of the original recipients', () => {
const { to } = buildReplyRecipients(sent, 'reply', OWN);
expect(to[0]).toEqual({ email: 'bob@other.com', name: 'Bob' });
});
});
it('returns nothing without a source message', () => {
expect(buildReplyRecipients(undefined, 'replyAll', OWN)).toEqual({ to: [], cc: [] });
});
it('treats a message as foreign when no identity matches', () => {
expect(isSelfSent({ from: [{ email: 'bob@other.com' }] }, OWN)).toBe(false);
expect(isSelfSent({ from: [{ email: 'ME@Example.com ' }] }, OWN)).toBe(true);
expect(isSelfSent({ from: [] }, OWN)).toBe(false);
expect(isSelfSent(undefined, OWN)).toBe(false);
});
});
+108
View File
@@ -0,0 +1,108 @@
export interface ReplyAddress {
email?: string;
name?: string;
}
export interface ReplySource {
from?: ReplyAddress[];
/** Addresses from the original message's Reply-To header. */
replyToAddresses?: ReplyAddress[];
to?: ReplyAddress[];
cc?: ReplyAddress[];
}
export interface ReplyRecipientsResult {
to: ReplyAddress[];
cc: ReplyAddress[];
}
function normalize(email: string): string {
return email.trim().toLowerCase();
}
function normalizeBase(email: string): string {
const normalized = normalize(email);
const at = normalized.indexOf('@');
if (at <= 0) return normalized;
const local = normalized.slice(0, at);
const domain = normalized.slice(at + 1);
const plus = local.indexOf('+');
return `${plus >= 0 ? local.slice(0, plus) : local}@${domain}`;
}
/**
* Does `email` belong to the user? Matches exactly first, then with `+tag`
* sub-addressing stripped (info+news@ is still info@).
*/
function isOwnAddress(email: string | undefined, ownEmails: string[]): boolean {
if (!email?.trim()) return false;
const exact = normalize(email);
if (ownEmails.some((own) => normalize(own) === exact)) return true;
const base = normalizeBase(email);
return ownEmails.some((own) => normalizeBase(own) === base);
}
/**
* Is this a message the user themself sent? True when the From address is one
* of their own identities - the case that shows up when browsing a thread and
* replying to your own last message.
*/
export function isSelfSent(source: ReplySource | undefined, ownEmails: string[]): boolean {
return isOwnAddress(source?.from?.[0]?.email, ownEmails);
}
/**
* Work out the To/Cc a reply should open with.
*
* Normal case: reply goes to the Reply-To header if the original carried one,
* else to From (RFC 5322). Reply-all adds the other original recipients,
* minus the user's own addresses.
*
* Self-sent case (#703): replying to your own message inside a thread must
* continue the conversation, not mail yourself. Gmail and Thunderbird address
* the reply to the message's original recipients instead, so that's what we do
* - the original To for reply, plus the original Cc for reply-all. Those
* addresses were the user's own choice, so they're kept verbatim (no self-
* filtering) and the Reply-To header is ignored, since answering your own
* Reply-To would land the mail back in your inbox again.
*
* A self-sent message with no visible recipients (Bcc-only) has nothing to
* continue to, so it falls back to the normal behaviour.
*/
export function buildReplyRecipients(
source: ReplySource | undefined,
mode: 'reply' | 'replyAll',
ownEmails: string[],
): ReplyRecipientsResult {
if (!source) return { to: [], cc: [] };
const withEmail = (list: ReplyAddress[] | undefined) => (list ?? []).filter((r) => Boolean(r.email));
if (isSelfSent(source, ownEmails)) {
const originalTo = withEmail(source.to);
if (originalTo.length > 0) {
return {
to: originalTo,
cc: mode === 'replyAll' ? withEmail(source.cc) : [],
};
}
}
const replyTarget = withEmail(source.replyToAddresses).length
? withEmail(source.replyToAddresses)
: (source.from?.[0]?.email ? [source.from[0]] : []);
if (mode === 'reply') {
return { to: replyTarget, cc: [] };
}
const others = (list: ReplyAddress[] | undefined) =>
withEmail(list).filter((r) => !isOwnAddress(r.email, ownEmails));
return {
to: [...replyTarget, ...others(source.to)],
cc: others(source.cc),
};
}