Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 6610cbdc authored by Ashley Soucar's avatar Ashley Soucar Committed by GitHub
Browse files

Merge pull request #9213 from shamim-emon/fix-issue-9134

feat: remove gmail prefix in folder structure
parents 15a7aea5 fee87972
Loading
Loading
Loading
Loading
+20 −1
Original line number Diff line number Diff line
@@ -7,6 +7,9 @@ import assertk.assertions.isEqualTo
import com.fsck.k9.K9RobolectricTest
import com.fsck.k9.Preferences
import com.fsck.k9.backend.api.BackendStorage
import com.fsck.k9.mail.AuthType
import com.fsck.k9.mail.ConnectionSecurity
import com.fsck.k9.mail.ServerSettings
import net.thunderbird.core.android.account.LegacyAccount
import org.junit.After
import org.junit.Test
@@ -67,7 +70,10 @@ class K9BackendDefaultStorageTest : K9RobolectricTest() {
        // FIXME: This is a hack to get Preferences into a state where it's safe to call newAccount()
        preferences.clearAccounts()

        return preferences.newAccount()
        return preferences.newAccount().apply {
            incomingServerSettings = SERVER_SETTINGS
            outgoingServerSettings = SERVER_SETTINGS
        }
    }

    private fun createBackendStorage(): BackendStorage {
@@ -75,6 +81,19 @@ class K9BackendDefaultStorageTest : K9RobolectricTest() {
        val folderSettingsProvider = createFolderSettingsProvider()
        return K9BackendStorage(messageStore, folderSettingsProvider, saveMessageDataCreator, emptyList())
    }

    companion object {
        private val SERVER_SETTINGS = ServerSettings(
            type = "irrelevant",
            host = "irrelevant",
            port = 993,
            connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
            authenticationType = AuthType.PLAIN,
            username = "username",
            password = null,
            clientCertificateAlias = null,
        )
    }
}

internal fun createFolderSettingsProvider(): FolderSettingsProvider {
+18 −2
Original line number Diff line number Diff line
@@ -16,10 +16,13 @@ import com.fsck.k9.backend.api.BackendFolder
import com.fsck.k9.backend.api.FolderInfo
import com.fsck.k9.backend.api.updateFolders
import com.fsck.k9.mail.Address
import com.fsck.k9.mail.AuthType
import com.fsck.k9.mail.ConnectionSecurity
import com.fsck.k9.mail.Flag
import com.fsck.k9.mail.FolderType
import com.fsck.k9.mail.Message
import com.fsck.k9.mail.MessageDownloadState
import com.fsck.k9.mail.ServerSettings
import com.fsck.k9.mail.internet.MimeMessage
import com.fsck.k9.mail.internet.MimeMessageHelper
import com.fsck.k9.mail.internet.TextBody
@@ -97,8 +100,10 @@ class K9BackendFolderTest : K9RobolectricTest() {
    fun createAccount(): LegacyAccount {
        // FIXME: This is a hack to get Preferences into a state where it's safe to call newAccount()
        preferences.clearAccounts()

        return preferences.newAccount()
        return preferences.newAccount().apply {
            incomingServerSettings = SERVER_SETTINGS
            outgoingServerSettings = SERVER_SETTINGS
        }
    }

    fun createBackendFolder(): BackendFolder {
@@ -158,5 +163,16 @@ class K9BackendFolderTest : K9RobolectricTest() {
        const val FOLDER_NAME = "Test Folder"
        val FOLDER_TYPE = FolderType.INBOX
        const val MESSAGE_SERVER_ID = "msg001"

        private val SERVER_SETTINGS = ServerSettings(
            type = "irrelevant",
            host = "irrelevant",
            port = 993,
            connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
            authenticationType = AuthType.PLAIN,
            username = "username",
            password = null,
            clientCertificateAlias = null,
        )
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ internal class CreateFolderOperations(private val lockableDatabase: LockableData
            for (folder in folders) {
                val folderSettings = folder.settings
                val values = ContentValues().apply {
                    put("name", folder.name)
                    put("name", folder.name.replace("\\[(Gmail|Google Mail)]/".toRegex(), ""))
                    put("visible_limit", folderSettings.visibleLimit)
                    put("integrate", folderSettings.integrate)
                    put("top_group", folderSettings.inTopGroup)
+36 −0
Original line number Diff line number Diff line
package com.fsck.k9.storage.messages

import android.content.ContentValues
import com.fsck.k9.mailstore.LockableDatabase

internal class FolderNameSanitizer(private val lockableDatabase: LockableDatabase) {
    fun removeGmailPrefixFromFolders() {
        lockableDatabase.execute(false) { db ->
            val cursor = db.query(
                "folders",
                arrayOf("id", "name"),
                "name LIKE ? OR name LIKE ?",
                arrayOf("%[Gmail]/%", "%[Google Mail]/%"),
                null,
                null,
                null,
            )

            while (cursor.moveToNext()) {
                val id = cursor.getLong(cursor.getColumnIndexOrThrow("id"))
                val name = cursor.getString(cursor.getColumnIndexOrThrow("name"))
                val updatedName = name
                    .replace("[Gmail]/", "")
                    .replace("[Google Mail]/", "")

                val values = ContentValues().apply {
                    put("name", updatedName)
                }

                db.update("folders", values, "id = ?", arrayOf(id.toString()))
            }

            cursor.close()
        }
    }
}
+15 −0
Original line number Diff line number Diff line
@@ -13,8 +13,18 @@ class K9MessageStoreFactory(
    private val storageFilesProviderFactory: StorageFilesProviderFactory,
    private val basicPartInfoExtractor: BasicPartInfoExtractor,
) : MessageStoreFactory {
    private lateinit var folderNameSanitizer: FolderNameSanitizer

    override fun create(account: LegacyAccount): ListenableMessageStore {
        val localStore = localStoreProvider.getInstance(account)
        if (account.incomingServerSettings.host.isGoogle() ||
            account.outgoingServerSettings.host.isGoogle()
        ) {
            if (!this::folderNameSanitizer.isInitialized) {
                folderNameSanitizer = FolderNameSanitizer(lockableDatabase = localStore.database)
            }
            folderNameSanitizer.removeGmailPrefixFromFolders()
        }
        val storageFilesProvider = storageFilesProviderFactory.createStorageFilesProvider(account.uuid)
        val messageStore = K9MessageStore(
            localStore.database,
@@ -25,3 +35,8 @@ class K9MessageStoreFactory(
        return ListenableMessageStore(notifierMessageStore)
    }
}

private fun String.isGoogle(): Boolean {
    val domains = listOf(".gmail.com", ".googlemail.com")
    return domains.any { this.endsWith(it, ignoreCase = true) }
}