From 4914ac589fb1d295c06fce0572241022919d88d6 Mon Sep 17 00:00:00 2001 From: cketti Date: Tue, 12 May 2020 11:37:16 +0200 Subject: [PATCH] Fix IMAP folder refresh We were using the old format server ID (UTF-7 decoded, with prefix removed) to create and update folders. But we used the proper server ID to figure out which folders to remove locally. This could lead to too many folders being removed. They'd be added back the next time the folder list was refreshed and then be removed again the next refresh after that. --- .../backend/imap/CommandRefreshFolderList.kt | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandRefreshFolderList.kt b/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandRefreshFolderList.kt index 2aadab12b..7ebc02cee 100644 --- a/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandRefreshFolderList.kt +++ b/backend/imap/src/main/java/com/fsck/k9/backend/imap/CommandRefreshFolderList.kt @@ -3,6 +3,8 @@ package com.fsck.k9.backend.imap import com.fsck.k9.backend.api.BackendStorage import com.fsck.k9.backend.api.FolderInfo import com.fsck.k9.backend.api.updateFolders +import com.fsck.k9.mail.FolderType +import com.fsck.k9.mail.store.imap.FolderListItem import com.fsck.k9.mail.store.imap.ImapStore internal class CommandRefreshFolderList( @@ -10,19 +12,18 @@ internal class CommandRefreshFolderList( private val imapStore: ImapStore ) { fun refreshFolderList() { - val foldersOnServer = imapStore.folders + // TODO: Start using the proper server ID. + // For now we still use the old server ID format (decoded, with prefix removed). + val foldersOnServer = imapStore.folders.toLegacyFolderList() val oldFolderServerIds = backendStorage.getFolderServerIds() backendStorage.updateFolders { val foldersToCreate = mutableListOf() for (folder in foldersOnServer) { - // TODO: Start using the proper server ID. For now we still use the old server ID. - val serverId = folder.oldServerId ?: continue - - if (serverId !in oldFolderServerIds) { - foldersToCreate.add(FolderInfo(serverId, folder.name, folder.type)) + if (folder.serverId !in oldFolderServerIds) { + foldersToCreate.add(FolderInfo(folder.serverId, folder.name, folder.type)) } else { - changeFolder(serverId, folder.name, folder.type) + changeFolder(folder.serverId, folder.name, folder.type) } } createFolders(foldersToCreate) @@ -33,3 +34,14 @@ internal class CommandRefreshFolderList( } } } + +private fun List.toLegacyFolderList(): List { + return this.filterNot { it.oldServerId == null } + .map { LegacyFolderListItem(it.oldServerId!!, it.name, it.type) } +} + +private data class LegacyFolderListItem( + val serverId: String, + val name: String, + val type: FolderType +)