Send a message again as a new one

A mail the far end rejected, or one that went to an address with a typo in
it, is a mail you want to send again -- not forward, and not reply to.
Doing it by hand meant a new message and copying five fields across.

"Compose as new" sits with Reply and Forward, in the message menu and in
the list's right-click menu. It opens a composer holding the recipients
the message had, its Reply-To, its subject with nothing prefixed to it,
its body with nothing wrapped around it, and its attachments.

What makes it a new mail is what it leaves behind. `draftId` stays null,
or sending would destroy the message it was made from. `inReplyTo`,
`references`, `relatedEmailId` and `relatedKeyword` stay null, so it hangs
off no thread and sending it marks the original neither answered nor
forwarded. The Message-ID is the server's and the date is stamped at
build time, so both are new without anything asking for them -- the send
path needed no changes at all.

A message you sent is composed as the identity you sent it as. One
somebody else sent has no identity of yours to match, and guessing from
whom it was addressed to would put the resend behind an alias that was
only ever the receiving end, so that case takes the account's default.

No signature is added. The body is the one that was sent, which already
ends in whatever signature went with it, and appending the identity's
would give it two.

Closes #176
This commit is contained in:
2026-09-01 08:58:15 -07:00
parent 53513e6744
commit 02c0332d8f
5 changed files with 214 additions and 2 deletions
@@ -0,0 +1,134 @@
import { beforeEach, afterEach, describe, expect, it } from "vitest";
import { useCompose } from "@/store/compose";
import { useMail } from "@/store/mail";
import type { Email } from "@/jmap/types";
/**
* "Compose as new" is a mail sent again, not a mail passed on. What it keeps is
* easy to see on screen; what it must leave behind is not, and that is what
* these are for -- a draft that kept `draftId` would destroy the message it was
* made from on send, and one that kept `relatedEmailId` would mark it answered
* or forwarded by a mail that is neither.
*/
const SENT: Email = {
id: "m1",
messageId: ["<[email protected]>"],
from: [{ name: "John", email: "[email protected]" }],
to: [{ name: "Ann", email: "[email protected]" }],
cc: [{ name: null, email: "[email protected]" }],
bcc: [{ name: null, email: "[email protected]" }],
replyTo: [{ name: null, email: "[email protected]" }],
subject: "Quarterly numbers",
references: ["<[email protected]>"],
inReplyTo: ["<[email protected]>"],
keywords: {},
htmlBody: [{ partId: "1", type: "text/html" }],
textBody: [{ partId: "1", type: "text/html" }],
bodyValues: { "1": { value: "<p>Here they are.</p>", isEncodingProblem: false, isTruncated: false } },
attachments: [
{ blobId: "b1", name: "numbers.pdf", type: "application/pdf", size: 1024, cid: null, disposition: "attachment" },
{ blobId: "b2", name: "logo.png", type: "image/png", size: 64, cid: "logo@x", disposition: "inline" },
],
} as unknown as Email;
/** The same mail, but from somebody else. */
const RECEIVED: Email = { ...SENT, id: "m2", from: [{ name: "Ann", email: "[email protected]" }] } as Email;
const IDENTITIES = [
{ id: "i1", name: "John", email: "[email protected]", replyTo: null },
{ id: "i2", name: "John (other)", email: "[email protected]", replyTo: null },
];
function mailState(email: Email) {
useMail.setState({
accountId: "a1",
identities: IDENTITIES as never,
getEmails: (async () => [email]) as never,
defaultIdentity: (() => IDENTITIES[1]) as never,
});
}
const draftFor = async (email: Email) => {
mailState(email);
const key = await useCompose.getState().composeAsNew(email);
return useCompose.getState().drafts.find((d) => d.key === key)!;
};
beforeEach(() => useCompose.setState({ drafts: [], activeKey: null }));
afterEach(() => useCompose.setState({ drafts: [], activeKey: null }));
describe("compose as new", () => {
it("keeps every recipient the message had, bcc included", async () => {
const d = await draftFor(SENT);
expect(d.to).toEqual([{ name: "Ann", email: "[email protected]" }]);
expect(d.cc).toEqual([{ name: null, email: "[email protected]" }]);
expect(d.bcc).toEqual([{ name: null, email: "[email protected]" }]);
// Fields with something in them are shown, or the copy is invisible.
expect([d.showCc, d.showBcc]).toEqual([true, true]);
});
it("keeps the subject as it stands, with no Re: or Fwd: on it", async () => {
const d = await draftFor(SENT);
expect(d.subject).toBe("Quarterly numbers");
});
it("keeps the reply-to the message carried", async () => {
const d = await draftFor(SENT);
expect(d.replyTo).toEqual([{ name: null, email: "[email protected]" }]);
expect(d.showReplyTo).toBe(true);
});
it("keeps the body, unquoted and unwrapped", async () => {
const d = await draftFor(SENT);
expect(d.html).toContain("Here they are.");
expect(d.html).not.toContain("ihm-quote");
expect(d.html).not.toContain("blockquote");
expect(d.html).not.toContain("Forwarded message");
});
it("keeps the attachments, by the blobs they already have", async () => {
const d = await draftFor(SENT);
expect(d.attachments.map((a) => a.name)).toEqual(["numbers.pdf", "logo.png"]);
// A blobId and no error is what the send path needs to accept one.
expect(d.attachments.every((a) => a.blobId && !a.error && a.progress === 100)).toBe(true);
expect(d.attachments[1]!.inline).toBe(true);
expect(d.attachments[0]!.inline).toBe(false);
});
it("sends as the identity the message was sent from", async () => {
const d = await draftFor(SENT);
expect(d.identityId).toBe("i1");
});
it("falls back to the default identity for a message somebody else sent", async () => {
const d = await draftFor(RECEIVED);
expect(d.identityId).toBe("i2");
});
it("is not the message it came from, so sending cannot destroy it", async () => {
const d = await draftFor(SENT);
expect(d.draftId).toBeNull();
});
it("threads onto nothing and marks nothing", async () => {
const d = await draftFor(SENT);
expect(d.inReplyTo).toBeNull();
expect(d.references).toBeNull();
expect(d.relatedEmailId).toBeNull();
expect(d.relatedKeyword).toBeNull();
});
it("adds no second signature to a body that already has one", async () => {
const d = await draftFor(SENT);
expect(d.signatureHtml).toBe("");
expect(d.html).not.toContain("ihm-signature");
});
it("opens a separate draft each time, rather than reusing the last", async () => {
const first = await draftFor(SENT);
const second = await draftFor(SENT);
expect(second.key).not.toBe(first.key);
expect(useCompose.getState().drafts).toHaveLength(2);
});
});