From f15f2d68fc6b0844fe372775d55c4080e4a210c5 Mon Sep 17 00:00:00 2001
From: Mechiel Lukkien <mechiel@ueber.net>
Date: Wed, 22 Jan 2025 20:09:19 +0100
Subject: [PATCH] webmail: more helpful error message when emptying a mailbox
 that is already empty

and mention in a tooltip too that "empty mailbox" only affects messages in the
mailbox, not submailboxes or their messages.

prompted by a question on matrix/irc
---
 webmail/api.go     | 6 +++++-
 webmail/webmail.js | 2 +-
 webmail/webmail.ts | 2 +-
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/webmail/api.go b/webmail/api.go
index 760ef89..7500d62 100644
--- a/webmail/api.go
+++ b/webmail/api.go
@@ -1276,9 +1276,13 @@ func (Webmail) MailboxEmpty(ctx context.Context, mailboxID int64) {
 			qm.FilterEqual("Expunged", false)
 			qm.SortAsc("UID")
 			qm.Gather(&expunged)
-			_, err = qm.UpdateNonzero(store.Message{ModSeq: modseq, Expunged: true})
+			n, err := qm.UpdateNonzero(store.Message{ModSeq: modseq, Expunged: true})
 			xcheckf(ctx, err, "deleting messages")
 
+			if n == 0 {
+				xcheckf(ctx, errors.New("no messages in mailbox"), "emptying mailbox")
+			}
+
 			// Remove Recipients.
 			anyIDs := make([]any, len(expunged))
 			for i, m := range expunged {
diff --git a/webmail/webmail.js b/webmail/webmail.js
index b7b0c08..2a96841 100644
--- a/webmail/webmail.js
+++ b/webmail/webmail.js
@@ -5486,7 +5486,7 @@ const newMailboxView = (xmb, mailboxlistView, otherMailbox) => {
 			}
 			remove();
 			await withStatus('Deleting mailbox', client.MailboxDelete(mbv.mailbox.ID));
-		})), dom.div(dom.clickbutton('Empty mailbox', async function click() {
+		})), dom.div(dom.clickbutton('Empty mailbox', attr.title('Remove all messages from the mailbox, but not mailboxes inside this mailbox or their messages.'), async function click() {
 			if (!window.confirm('Are you sure you want to empty this mailbox, permanently removing its messages? Mailboxes inside this mailbox are not affected.')) {
 				return;
 			}
diff --git a/webmail/webmail.ts b/webmail/webmail.ts
index f6bfccc..5a029e5 100644
--- a/webmail/webmail.ts
+++ b/webmail/webmail.ts
@@ -5157,7 +5157,7 @@ const newMailboxView = (xmb: api.Mailbox, mailboxlistView: MailboxlistView, othe
 					}),
 				),
 				dom.div(
-					dom.clickbutton('Empty mailbox', async function click() {
+					dom.clickbutton('Empty mailbox', attr.title('Remove all messages from the mailbox, but not mailboxes inside this mailbox or their messages.'), async function click() {
 						if (!window.confirm('Are you sure you want to empty this mailbox, permanently removing its messages? Mailboxes inside this mailbox are not affected.')) {
 							return
 						}