Skip to content

fix(drafts): replace a draft by saving anew and deleting the old one - #290

Open
6801318d8d wants to merge 1 commit into
TKasperczyk:mainfrom
6801318d8d:fix/draft-edit-in-place
Open

6801318d8d wants to merge 1 commit into
TKasperczyk:mainfrom
6801318d8d:fix/draft-edit-in-place

Conversation

@6801318d8d

Copy link
Copy Markdown
Contributor

Change

saveDraft with replaceMessageId on a local (mailbox://) Drafts folder does not replace the draft:

  • after one replacement, the Drafts index holds two rows, both carrying the old Message-ID and subject. The new text is in the mbox, but getMessage on the new Message-ID answers "not found";
  • the old draft is never removed, so each correction adds one more copy. Deleting them later leaves stale index rows that only Properties → Repair Folder clears.

Cause. saveDraft passes the old header to nsIMsgSend.createAndSendMessage as msgToReplace. For local folders:

  • nsMsgLocalMailFolder::CopyFileMessage keeps that header in the copy state as the source of a move;
  • EndCopy copies its properties (Message-ID, subject, date, recipients) onto the new header with CopyPropertiesToMsgHdr;
  • nothing deletes the source.

Thunderbird's compose window never takes this path. nsMsgCompose::SendMsgToServer passes null, and RemoveCurrentDraftMessage deletes the previous draft once the new one is saved.

Fix. saveDraft now does what the compose window does:

  • msgToReplace is always null. The parameter of sendMessageDirectly that carried it is removed.
  • Only after the new draft is stored, the old header is deleted with the call RemoveCurrentDraftMessage makes: deleteMessages([hdr], null, true, false, null, false), which deletes storage, with no Trash copy and no undo. If the save fails, the old draft is left untouched.
  • The result returns the new draft's id as messageId. Each save gets a new Message-ID, so a caller needs it to replace the draft again.
  • If the delete fails, the result reports success with a warning, since the new draft is already stored.

The tool description now says that the old draft is deleted and that the new id is returned.

Validation

  • Unit tests. test/save-draft-replace.test.cjs passes 48/48. It now checks that:

    • msgToReplace is null;
    • the delete happens after the save, with the arguments above;
    • the old draft is kept when the save fails;
    • a failing delete gives success with a warning;
    • messageId is returned.
  • Full suite (npm test with an isolated TMPDIR): no new failures. 4 tests fail here, and the same 4 fail on main in this environment (Windows): one case of bridge-requests, message-headers, protected-subjects, search-messages-scan.

  • ESLint on the changed files: 0 errors. There are 5 warnings, all on lines this PR does not touch.

  • Manual check on Thunderbird 160.0, Windows 11, Local Folders/Drafts. One draft was replaced three times in a row. After each replacement:

    • searchMessages with dedupByMessageId: false listed exactly one row, with the new id, subject and recipient;
    • getMessage on the new id returned the new text and headers;
    • the old id answered "not found";
    • the mbox held one live copy.

    Before the fix, the same steps left two index rows carrying the old Message-ID.

  • Not checked: IMAP Drafts folders. There, the delete is the same call the compose window makes on every draft save.

🤖 Generated with Claude Code

saveDraft with replaceMessageId passed the old header to
nsIMsgSend.createAndSendMessage as msgToReplace. For a local (mailbox://)
Drafts folder, nsMsgLocalMailFolder::CopyFileMessage treats that header as
the source of a move: EndCopy copies its properties (Message-ID, subject,
date, recipients) onto the new copy's index row, and the old message is
never removed. Each replacement therefore left the old draft in place and
added a second row that looked like the old one while pointing at the new
text, so the new Message-ID was not findable and later deletes left stale
rows behind. Thunderbird's own compose window never uses this path: it
passes null and deletes the previous draft itself.

Do the same: save the replacement as a new draft (msgToReplace is always
null), then, only after the copy has succeeded, delete the old header with
the call nsMsgCompose's RemoveCurrentDraftMessage uses (delete storage, no
Trash copy, no undo). The result reports the new draft's Message-ID as
messageId so the next replacement can name it; a failed delete is reported
as a warning instead of an error, since the new draft is already stored.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant