From 95c8002430bf777450a6fa54407a50e774568d20 Mon Sep 17 00:00:00 2001 From: cketti Date: Mon, 6 May 2024 18:33:29 +0200 Subject: [PATCH] Move expunge logic into `RealImapFolder.deleteAllMessages()` --- .../k9/controller/MessagingController.java | 4 -- .../fsck/k9/mail/store/imap/RealImapFolder.kt | 6 +++ .../k9/mail/store/imap/RealImapFolderTest.kt | 50 +++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java index 7659698f8..f1a6de3f7 100644 --- a/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/app/core/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -2094,10 +2094,6 @@ public class MessagingController { Backend backend = getBackend(account); backend.deleteAllMessages(trashFolderServerId); - if (account.getExpungePolicy() == Expunge.EXPUNGE_IMMEDIATELY && backend.getSupportsExpunge()) { - backend.expunge(trashFolderServerId); - } - // Remove all messages marked as deleted folder.destroyDeletedMessages(); diff --git a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapFolder.kt b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapFolder.kt index f7f6f2747..62cffcabe 100644 --- a/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapFolder.kt +++ b/mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/RealImapFolder.kt @@ -1089,7 +1089,13 @@ internal class RealImapFolder( @Throws(MessagingException::class) override fun deleteAllMessages() { + checkOpenWithWriteAccess() + setFlagsForAllMessages(setOf(Flag.DELETED), true) + + if (internalImapStore.config.isExpungeImmediately()) { + expunge() + } } @Throws(MessagingException::class) diff --git a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapFolderTest.kt b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapFolderTest.kt index 56eab41a5..6c911d135 100644 --- a/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapFolderTest.kt +++ b/mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/RealImapFolderTest.kt @@ -1150,6 +1150,56 @@ class RealImapFolderTest { assertThat(uid).isEqualTo("23") } + @Test + fun `deleteAllMessages() on closed folder should throw`() { + val folder = createFolder("Folder") + + assertFailure { + folder.deleteAllMessages() + }.isInstanceOf() + .hasMessage("Folder 'Folder' is not open.") + + verifyNoMoreInteractions(imapConnection) + } + + @Test + fun `deleteAllMessages() on folder opened as read-only should throw`() { + val folder = createFolder("Folder") + prepareImapFolderForOpen(OpenMode.READ_ONLY) + folder.open(OpenMode.READ_ONLY) + + assertFailure { + folder.deleteAllMessages() + }.isInstanceOf() + .hasMessage("Folder 'Folder' needs to be opened for read-write access.") + } + + @Test + fun `deleteAllMessages() with expungeImmediately = true should set deleted flag and expunge`() { + imapStoreConfig.expungeImmediately = true + val folder = createFolder("Folder") + prepareImapFolderForOpen(OpenMode.READ_WRITE) + folder.open(OpenMode.READ_WRITE) + + folder.deleteAllMessages() + + assertCommandIssued("UID STORE 1:* +FLAGS.SILENT (\\Deleted)") + assertCommandIssued("EXPUNGE") + } + + @Test + fun `deleteAllMessages() with expungeImmediately = false should set deleted flag but not expunge`() { + imapStoreConfig.expungeImmediately = false + val folder = createFolder("Folder") + prepareImapFolderForOpen(OpenMode.READ_WRITE) + folder.open(OpenMode.READ_WRITE) + + folder.deleteAllMessages() + + assertCommandIssued("UID STORE 1:* +FLAGS.SILENT (\\Deleted)") + verify(imapConnection, never()).executeSimpleCommand("EXPUNGE") + } + @Test fun `expunge() on closed folder should throw`() { val folder = createFolder("Folder")