Repository navigation
feat(drafts): updateDraft edits only the given fields of a draft - #292
Open
6801318d8d wants to merge 2 commits into
Open
6801318d8d wants to merge 2 commits into
6801318d8d wants to merge 2 commits into
Conversation
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>
Changing a draft meant calling saveDraft with replaceMessageId and passing every field again (recipients, subject, the whole body, every attachment), because nothing is merged from the old draft. updateDraft takes the draft's messageId and folderPath and any of to, cc, bcc, subject, body, isHtml, from, addAttachments and removeAttachments. Everything not given is read from the stored MIME with the raw MIME helpers getMessage already uses: recipients, subject, the identity (X-Identity-Key, else the From address), In-Reply-To/References, the body in its own format, and every attachment, carried over as inline base64 with its bytes, name and content type. An added attachment replaces one of the same name. The merged draft goes through saveDraft's replacement path, so the old draft is deleted only once the new one is stored; the result lists the new messageId, the changed fields and the final attachment names. A stored HTML body is saved as is (saveDraft's keepHtmlBody option): formatBodyHtml strips newlines for compose windows, which would join the lines of <pre> blocks such as quoted replies. No signature is appended. It refuses a missing draft, unknown removeAttachments names, calls that change nothing, encrypted or unverifiable drafts, drafts scheduled by Send Later and drafts whose HTML shows cid: images, which saveDraft cannot embed. The Drafts-folder check is shared with saveDraft (findStoredDraft). Tool access makes updateDraft depend on saveDraft, like dryRunCompose on sendMail; the bridge inlines addAttachments paths and gives updateDraft the 150 s draft-save timeout. The README also no longer says saveDraft omits the saved draft's Message-ID, which it returns since the previous commit. This builds on the previous commit (TKasperczyk#290): updateDraft relies on its replacement path (save anew, then delete the old draft) and on saveDraft returning the new messageId. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Depends on #290
This PR builds on #290 and should be merged after it. Until #290 is merged, the diff here also shows #290's commit (
fix(drafts): replace a draft by saving anew and deleting the old one). The new work is the second commit only.Change
To change one field of a draft, a caller has to use
saveDraftwithreplaceMessageIdand pass every field again: recipients, subject, the whole body and every attachment. Nothing is merged from the old draft, so a caller that only wants a new subject must first read the draft and send all of it back.updateDraftchanges only the fields it is given and keeps the rest:messageIdandfolderPathof the draft (checked as forsaveDraft'sreplaceMessageIdandreplaceFolderPath: accessible, Drafts flag, the identity's drafts folder), plus any ofto,cc,bcc,subject,body,isHtml,from,addAttachmentsandremoveAttachments.getMessagealready uses:X-Identity-Key, else the From address);In-Reply-ToandReferences;formatBodyHtmlis skipped for it because it strips newlines, which would join the lines of<pre>blocks such as quoted replies. A newbodyis formatted as insaveDraft.isHtmlwithoutbodyconverts the stored body;addAttachmentstakes the item shapes ofattachments. An added attachment whose name matches an existing one replaces it.removeAttachmentstakes file names.saveDraft's replacement path from fix(drafts): replace a draft by saving anew and deleting the old one #290, so the old draft is deleted only after the new one is stored.{ success, messageId, folderPath, changed, attachments }: the new Message-ID, the fields that changed and the final attachment names.removeAttachmentsname the draft does not have;cid:) images, whichsaveDraftcannot embed.Other parts:
saveDraft(findStoredDraft).updateDraftdepend onsaveDraft, asdryRunComposedepends onsendMail.addAttachmentspaths, with the same limits asattachments, and givesupdateDraftthe 150 s draft-save timeout.saveDraftdoes not return the saved draft's Message-ID is removed (it does since fix(drafts): replace a draft by saving anew and deleting the old one #290).Validation
Unit tests.
test/update-draft.test.cjs(new) passes 22/22. It covers:isHtmlconversion and a change of identity;saveDraft.addAttachmentspath inlining, the 150 s timeout and the tool list.Full suite (
npm testwith an isolatedTEMP/TMP): 5035 tests, 5015 pass, 4 fail, 16 skipped. On fix(drafts): replace a draft by saving anew and deleting the old one #290's head alone: 5006 tests, 4986 pass, the same 4 fail, 16 skipped. The 4 failures are the Windows-only ones listed in fix(drafts): replace a draft by saving anew and deleting the old one #290: one case ofbridge-requests,message-headers,protected-subjects,search-messages-scan.ESLint on the changed files: 0 errors. There are 5 warnings, the same 5 as on fix(drafts): replace a draft by saving anew and deleting the old one #290's head, all on lines this PR does not touch.
Manual check on Thunderbird 161.0, Windows 11, with a draft in a Gmail IMAP Drafts folder:
This also exercises fix(drafts): replace a draft by saving anew and deleting the old one #290's delete of the old draft on an IMAP Drafts folder, which fix(drafts): replace a draft by saving anew and deleting the old one #290 had not checked.
🤖 Generated with Claude Code