fix(webmail): Discard really discards the draft

The button was a link to the inbox, so the autosaved draft stayed in Drafts.
It now removes the stored draft for good (not into Trash), stops the autosave,
and cleans up a save that was already in flight.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas RayanandClaude Sonnet 5.5 committed 2026-10-22 16:02:27 -03:00
1 parent d283ff6f8d
commit 657a43d612
8 files changed
+132 -8

No files matched your search

@@ -402,6 +402,28 @@ func TestDeleteFromTrashExpunges(t *testing.T) {
}
}
// "Discard" in the composer promises the draft is gone. Delete would leave it
// in Trash, so discarding removes it outright - and only ever from Drafts.
func TestDiscardingADraftRemovesItForGood(t *testing.T) {
mailboxID := uuid.New()
repo := &stubWebmailRepo{mailboxID: mailboxID.String()}
uc := &WebmailUseCase{
repo: repo,
auth: &stubAuthRepo{account: &port.MailboxAuth{ID: mailboxID, EmailAddress: "me@example.test"}},
localHost: "mail.example.test",
}
if err := uc.DiscardDraft(context.Background(), "me@example.test", 9); err != nil {
t.Fatalf("discard: %v", err)
}
if len(repo.expunged) != 1 || repo.expunged[0] != 9 {
t.Errorf("the draft was not expunged: %v", repo.expunged)
}
if len(repo.moved) != 0 {
t.Errorf("a discarded draft was moved instead of removed: %v", repo.moved)
}
}
// --- moving between folders ---------------------------------------------
// Filing a message somewhere else is the one ordinary mail action the webmail
@@ -283,6 +283,20 @@ func (uc *WebmailUseCase) Delete(ctx context.Context, address, folder string, ui
}
}
// DiscardDraft removes a draft for good.
//
// Delete would move it to Trash, which is right for a message someone might
// want back and wrong for "Discard": the composer's button promises the draft
// is gone, and one waiting in Trash is a draft that is not. Only Drafts is
// reachable from here, so this cannot be used to expunge anything else.
func (uc *WebmailUseCase) DiscardDraft(ctx context.Context, address string, uid uint32) error {
mailboxID, err := uc.mailboxID(ctx, address)
if err != nil {
return err
}
return uc.repo.Expunge(ctx, mailboxID, "Drafts", uid)
}
// SaveDraft files the message being composed into the Drafts folder.
//
// Nothing did this before: the composer displayed "draft saved automatically"