Repository: thundernest/k-9
Issue: Unable to delete messages from inbox

**Describe the bug**
When trying to delete messages, messages disappear, but are not moved into Trash directory.
When re-opening k-9, deleted messages still appear in inbox.

Please note that it could also happen with messages from other directory ( I didn't test it)

**To Reproduce**
Steps to reproduce the behavior:
1. Open k-9 (unified inbox in my case)
2. Select one or many messages
3. Delete them
4. Quit k-9
5. Re-open k-9
6. Messages are still in inbox

**Expected behavior**
Deleted messages should be visible in Trash directory and not in inbox anymore

**Environment (please complete the following information):**
 - K-9 Mail version: 5.722 (beta)
 - Android version: 11
 - Device: Google Pixel 4a
 - Account type: IMAP

**Additional context**
Add any other context about the problem here.

**Logs**
[k9-mail.log.txt](https://github.com/k9mail/k-9/files/5434913/k9-mail.log.txt)

Changed files:
app/storage/src/main/java/com/fsck/k9/storage/messages/ThreadMessageOperations.kt
app/storage/src/test/java/com/fsck/k9/storage/messages/MoveMessageOperationsTest.kt
app/storage/src/test/java/com/fsck/k9/storage/messages/MessageDatabaseHelpers.kt

diff --git a/app/storage/src/main/java/com/fsck/k9/storage/messages/ThreadMessageOperations.kt b/app/storage/src/main/java/com/fsck/k9/storage/messages/ThreadMessageOperations.kt
index 6afa1c10f..26f59a571 100644
--- a/app/storage/src/main/java/com/fsck/k9/storage/messages/ThreadMessageOperations.kt
+++ b/app/storage/src/main/java/com/fsck/k9/storage/messages/ThreadMessageOperations.kt
@@ -85,7 +85,7 @@ internal class ThreadMessageOperations(private val localStore: LocalStore) {
 internal data class ThreadInfo(
     val threadId: Long?,
     val messageId: Long?,
-    val messageIdHeader: String,
+    val messageIdHeader: String?,
     val rootId: Long?,
     val parentId: Long?
 )
diff --git a/app/storage/src/test/java/com/fsck/k9/storage/messages/MessageDatabaseHelpers.kt b/app/storage/src/test/java/com/fsck/k9/storage/messages/MessageDatabaseHelpers.kt
index 4c2773f96..62877bd81 100644
--- a/app/storage/src/test/java/com/fsck/k9/storage/messages/MessageDatabaseHelpers.kt
+++ b/app/storage/src/test/java/com/fsck/k9/storage/messages/MessageDatabaseHelpers.kt
@@ -50,7 +50,7 @@ fun SQLiteDatabase.createMessage(
     replyToList: String = "",
     attachmentCount: Int = 0,
     internalDate: Long = 0L,
-    messageId: String = "",
+    messageIdHeader: String? = null,
     previewType: DatabasePreviewType = DatabasePreviewType.NONE,
     preview: String = "",
     mimeType: String = "text/plain",
@@ -77,7 +77,7 @@ fun SQLiteDatabase.createMessage(
         put("reply_to_list", replyToList)
         put("attachment_count", attachmentCount)
         put("internal_date", internalDate)
-        put("message_id", messageId)
+        put("message_id", messageIdHeader)
         put("preview_type", previewType.databaseValue)
         put("preview", preview)
         put("mime_type", mimeType)
diff --git a/app/storage/src/test/java/com/fsck/k9/storage/messages/MoveMessageOperationsTest.kt b/app/storage/src/test/java/com/fsck/k9/storage/messages/MoveMessageOperationsTest.kt
index 1af8cf68e..57f9015ce 100644
--- a/app/storage/src/test/java/com/fsck/k9/storage/messages/MoveMessageOperationsTest.kt
+++ b/app/storage/src/test/java/com/fsck/k9/storage/messages/MoveMessageOperationsTest.kt
@@ -24,7 +24,7 @@ class MoveMessageOperationsTest : RobolectricTest() {
             folderId = SOURCE_FOLDER_ID,
             uid = "uid1",
             subject = "Move me",
-            messageId = MESSAGE_ID_HEADER
+            messageIdHeader = MESSAGE_ID_HEADER
         )
         val originalMessage = sqliteDatabase.readMessages().first()
         val messageThreader = createMessageThreader(
@@ -73,14 +73,14 @@ class MoveMessageOperationsTest : RobolectricTest() {
             folderId = SOURCE_FOLDER_ID,
             uid = "uid1",
             subject = "Move me",
-            messageId = MESSAGE_ID_HEADER,
+            messageIdHeader = MESSAGE_ID_HEADER,
             read = false
         )
         val originalMessage = sqliteDatabase.readMessages().first()
         val placeholderMessageId = sqliteDatabase.createMessage(
             empty = true,
             folderId = DESTINATION_FOLDER_ID,
-            messageId = MESSAGE_ID_HEADER,
+            messageIdHeader = MESSAGE_ID_HEADER,
             uid = ""
         )
         val messageThreader = createMessageThreader(
@@ -123,6 +123,54 @@ class MoveMessageOperationsTest : RobolectricTest() {
         )
     }
 
+    @Test
+    fun `move message not containing a message-id header`() {
+        val originalMessageId = sqliteDatabase.createMessage(
+            folderId = SOURCE_FOLDER_ID,
+            uid = "uid1",
+            subject = "Move me",
+            messageIdHeader = null
+        )
+        val originalMessage = sqliteDatabase.readMessages().first()
+        val messageThreader = createMessageThreader(
+            ThreadInfo(
+                threadId = null,
+                messageId = null,
+                messageIdHeader = null,
+                rootId = null,
+                parentId = null
+            )
+        )
+        val moveMessageOperations = MoveMessageOperations(lockableDatabase, messageThreader)
+
+        val destinationMessageId = moveMessageOperations.moveMessage(
+            messageId = originalMessageId,
+            destinationFolderId = DESTINATION_FOLDER_ID
+        )
+
+        val messages = sqliteDatabase.readMessages()
+        assertThat(messages).hasSize(2)
+
+        val sourceMessage = messages.find { it.id == originalMessageId }
+            ?: fail("Original message not found")
+        assertThat(sourceMessage.folderId).isEqualTo(SOURCE_FOLDER_ID)
+        assertThat(sourceMessage.uid).isEqualTo("uid1")
+        assertPlaceholderEntry(sourceMessage)
+
+        val destinationMessage = messages.find { it.id == destinationMessageId }
+            ?: fail("Destination message not found")
+        assertThat(destinationMessage.uid).startsWith(K9.LOCAL_UID_PREFIX)
+        assertThat(destinationMessage).isEqualTo(
+            originalMessage.copy(
+                id = destinationMessageId,
+                folderId = DESTINATION_FOLDER_ID,
+                uid = destinationMessage.uid,
+                deleted = 0,
+                empty = 0
+            )
+        )
+    }
+
     private fun createMessageThreader(threadInfo: ThreadInfo): ThreadMessageOperations {
         return mock {
             on { createOrUpdateParentThreadEntries(any(), anyLong(), anyLong()) } doReturn threadInfo