From 51b8fd5708d23108d532be1cf38e4bb619eeb777 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Sun, 6 Sep 2026 15:26:31 +0200 Subject: fix: keep one Message-ID across a draft's revisions Every autosave called MessageBuilder::build(), which generated a fresh Message-ID unconditionally, so each revision of a draft was a different message rather than a new version of one. The entry called this invisible while the file is replaced correctly, and that turned out to be wrong: mbsync uploads each revision to the drafts folder before the next save removes the local file, so the server keeps one message per revision and syncs them all back down. Measured on real mail as four independent messages for a single reply, all four carrying a ,U= infix, threading into the conversation and putting a draft tag on a Sent row. Deleting a local file does not retract an uploaded one, which is why the local cleanup, which is correct, could never fix it. The user chose a stable id while drafting, discarded at send: the sent copy is a different item from the draft, and the draft is deleted once the message goes out, which the code already did. Three links, none of which existed. OutgoingMessage::messageId is the field, where empty means generate, so the send path is unchanged by construction rather than by remembering to clear it. MessageBuilder::build() uses a supplied id when there is one. ComposeWindow::m_draftMessageId holds the identity between revisions, assigned from built.messageId so the first save adopts the id GMime just generated, and ComposeContext::draftMessageId carries it across a reopen, read in forDraft() from ParsedMessage::messageId, which the parser already provided and nothing had ever used. Five tests, because the property spans three objects and a test at any one of them passes while another link is broken. Two are the safety constraint rather than the feature: a field defaulting to a fixed value would satisfy the reuse test and make two sent messages share an id, which is far worse than the defect this fixes. One comment is corrected rather than left: the autosave's dirty check justified comparing the message rather than the built bytes with "GMime is given a fresh Date and Message-ID on every build". Half of that is no longer true. The Date still is, so the conclusion stands. Revisions already on the server are not touched by this; the four found on real mail were deleted by hand. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01Jq9gXquUo9W4KXDagJXMmn --- tests/test_composewindow.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) (limited to 'tests/test_composewindow.cpp') diff --git a/tests/test_composewindow.cpp b/tests/test_composewindow.cpp index d95d55d..49bb390 100644 --- a/tests/test_composewindow.cpp +++ b/tests/test_composewindow.cpp @@ -66,6 +66,8 @@ private slots: void theMenuBarReachesEveryComposerAction(); void saveDraftWritesAndReports(); void aSavedDraftIsFlaggedSeen(); + void everyRevisionOfADraftKeepsOneMessageId(); + void aSentMessageDoesNotInheritTheDraftsMessageId(); void aForwardCarriesTheOriginalHtmlAndStripsRemoteContent(); void anHtmlForwardPreviewsTheOriginalInsteadOfQuotingIt(); void theMenusReuseTheToolbarActions(); @@ -752,6 +754,97 @@ void TestComposeWindow::saveDraftWritesAndReports() QVERIFY2(!window.isWindowModified(), "a manual save must clear the marker"); } +/// Item 165. Two saves of one draft must produce ONE message, not two. +/// +/// Every save used to mint a fresh Message-ID, and mbsync uploads each +/// revision to the drafts folder before the next save removes the local file, +/// so the server kept one message per revision: measured on the user's own +/// mail as four independent messages for a single reply, which then threaded +/// into the conversation and put a `draft` tag on a Sent row. Deleting a local +/// file does not retract an uploaded one, so the local cleanup, which is +/// correct, could never fix this. +/// +/// Asserted on the FILE's header rather than on a notmuch tag, for the reason +/// the test below gives: the bytes are what the code here controls. +void TestComposeWindow::everyRevisionOfADraftKeepsOneMessageId() +{ + const Config config = configWithDrafts(); + + ComposeContext context; + context.kind = ComposeContext::Kind::New; + context.accountKey = QStringLiteral("work"); + + ComposeWindow window(context, config, m_dir->path()); + auto *body = window.findChild(QStringLiteral("body")); + QVERIFY(body); + auto *save = window.findChild(QStringLiteral("compose_save")); + QVERIFY(save); + + const auto messageIdIn = [](const QString &path) { + QFile file(path); + if (!file.open(QIODevice::ReadOnly)) + return QString(); + const QString text = QString::fromUtf8(file.readAll()); + for (const QString &line : text.split(QLatin1Char('\n'))) { + if (line.startsWith(QStringLiteral("Message-Id:"), Qt::CaseInsensitive)) + return line.section(QLatin1Char(':'), 1).trimmed(); + if (line.trimmed().isEmpty()) + break; // end of headers + } + return QString(); + }; + + QSignalSpy saved(&window, &ComposeWindow::draftSaved); + + body->setPlainText(QStringLiteral("First revision.")); + save->trigger(); + QCOMPARE(saved.size(), 1); + const QString firstPath = saved.at(0).at(0).toString(); + const QString firstId = messageIdIn(firstPath); + QVERIFY2(!firstId.isEmpty(), "the first revision carries no Message-ID"); + + // A REAL change, or the dirty check short-circuits and no second file is + // written at all, which would pass this test while proving nothing. + body->setPlainText(QStringLiteral("Second revision, genuinely different.")); + save->trigger(); + QCOMPARE(saved.size(), 2); + const QString secondPath = saved.at(1).at(0).toString(); + QVERIFY2(secondPath != firstPath, + "the second save wrote no new file, so the ids cannot be compared"); + + QCOMPARE(messageIdIn(secondPath), firstId); +} + +/// The constraint that makes item 165 safe, and the one a future edit is most +/// likely to break: the SEND path must mint its own id. +/// +/// The user's decision was a stable id while drafting, DISCARDED at send, so +/// the sent copy is a different item from the draft. Two sent messages sharing +/// a Message-ID would be far worse than the defect this fixed, and a +/// Message-ID reaches the server and every recipient, so it is not a local +/// matter. currentMessage() leaves the field empty and the send path passes it +/// straight to build(); this pins that. +void TestComposeWindow::aSentMessageDoesNotInheritTheDraftsMessageId() +{ + const Config config = configWithDrafts(); + + ComposeContext context; + context.kind = ComposeContext::Kind::Draft; + context.accountKey = QStringLiteral("work"); + context.draftMessageId = QStringLiteral("the-draft-id@example.org"); + + ComposeWindow window(context, config, m_dir->path()); + auto *body = window.findChild(QStringLiteral("body")); + QVERIFY(body); + body->setPlainText(QStringLiteral("About to send.")); + + // The message the SEND path builds from, which is what decides the id. + const OutgoingMessage outgoing = window.currentMessage(); + QVERIFY2(outgoing.messageId.isEmpty(), + "the send path carried the draft's Message-ID, so the sent copy " + "would share an id with a message already on the server"); +} + /// A draft is authored by the user, so it is SEEN by definition and must never /// be tagged `unread`. /// -- cgit v1.2.3