Skip to content

feat: remove gmail prefix in folder structure #9213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,14 +70,30 @@ 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 {
val messageStore = messageStoreManager.getMessageStore(account)
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) }
}