-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7349 from thunderbird/folder_fetcher
Add `FolderFetcher` interface
- Loading branch information
Showing
9 changed files
with
392 additions
and
2 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
mail/common/src/main/java/com/fsck/k9/mail/folders/FolderFetcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.fsck.k9.mail.folders | ||
|
||
import com.fsck.k9.mail.ServerSettings | ||
import com.fsck.k9.mail.oauth.AuthStateStorage | ||
|
||
/** | ||
* Fetches the list of folders from a server. | ||
* | ||
* @throws FolderFetcherException in case of an error | ||
*/ | ||
fun interface FolderFetcher { | ||
fun getFolders( | ||
serverSettings: ServerSettings, | ||
authStateStorage: AuthStateStorage?, | ||
): List<RemoteFolder> | ||
} |
9 changes: 9 additions & 0 deletions
9
mail/common/src/main/java/com/fsck/k9/mail/folders/FolderFetcherException.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.fsck.k9.mail.folders | ||
|
||
/** | ||
* Thrown by [FolderFetcher] in case of an error. | ||
*/ | ||
class FolderFetcherException( | ||
cause: Throwable, | ||
val messageFromServer: String? = null, | ||
) : RuntimeException(cause.message, cause) |
4 changes: 4 additions & 0 deletions
4
mail/common/src/main/java/com/fsck/k9/mail/folders/FolderServerId.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.fsck.k9.mail.folders | ||
|
||
@JvmInline | ||
value class FolderServerId(val serverId: String) |
9 changes: 9 additions & 0 deletions
9
mail/common/src/main/java/com/fsck/k9/mail/folders/RemoteFolder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.fsck.k9.mail.folders | ||
|
||
import com.fsck.k9.mail.FolderType | ||
|
||
data class RemoteFolder( | ||
val serverId: FolderServerId, | ||
val displayName: String, | ||
val type: FolderType, | ||
) |
77 changes: 77 additions & 0 deletions
77
mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapFolderFetcher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.fsck.k9.mail.store.imap | ||
|
||
import com.fsck.k9.mail.AuthenticationFailedException | ||
import com.fsck.k9.mail.ServerSettings | ||
import com.fsck.k9.mail.folders.FolderFetcher | ||
import com.fsck.k9.mail.folders.FolderFetcherException | ||
import com.fsck.k9.mail.folders.FolderServerId | ||
import com.fsck.k9.mail.folders.RemoteFolder | ||
import com.fsck.k9.mail.oauth.AuthStateStorage | ||
import com.fsck.k9.mail.oauth.OAuth2TokenProvider | ||
import com.fsck.k9.mail.oauth.OAuth2TokenProviderFactory | ||
import com.fsck.k9.mail.ssl.TrustedSocketFactory | ||
|
||
/** | ||
* Fetches the list of folders from an IMAP server. | ||
*/ | ||
class ImapFolderFetcher internal constructor( | ||
private val trustedSocketFactory: TrustedSocketFactory, | ||
private val oAuth2TokenProviderFactory: OAuth2TokenProviderFactory?, | ||
private val clientIdAppName: String, | ||
private val clientIdAppVersion: String, | ||
private val imapStoreFactory: ImapStoreFactory, | ||
) : FolderFetcher { | ||
constructor( | ||
trustedSocketFactory: TrustedSocketFactory, | ||
oAuth2TokenProviderFactory: OAuth2TokenProviderFactory?, | ||
clientIdAppName: String, | ||
clientIdAppVersion: String, | ||
) : this( | ||
trustedSocketFactory, | ||
oAuth2TokenProviderFactory, | ||
clientIdAppName, | ||
clientIdAppVersion, | ||
imapStoreFactory = ImapStore.Companion, | ||
) | ||
|
||
@Suppress("TooGenericExceptionCaught") | ||
override fun getFolders(serverSettings: ServerSettings, authStateStorage: AuthStateStorage?): List<RemoteFolder> { | ||
require(serverSettings.type == "imap") | ||
|
||
val config = object : ImapStoreConfig { | ||
override val logLabel = "folder-fetcher" | ||
override fun isSubscribedFoldersOnly() = false | ||
override fun clientId() = ImapClientId(appName = clientIdAppName, appVersion = clientIdAppVersion) | ||
} | ||
val oAuth2TokenProvider = createOAuth2TokenProviderOrNull(authStateStorage) | ||
val store = imapStoreFactory.create(serverSettings, config, trustedSocketFactory, oAuth2TokenProvider) | ||
|
||
return try { | ||
store.getFolders() | ||
.asSequence() | ||
.filterNot { it.oldServerId == null } | ||
.map { folder -> | ||
RemoteFolder( | ||
serverId = FolderServerId(folder.oldServerId!!), | ||
displayName = folder.name, | ||
type = folder.type, | ||
) | ||
} | ||
.toList() | ||
} catch (e: AuthenticationFailedException) { | ||
throw FolderFetcherException(messageFromServer = e.messageFromServer, cause = e) | ||
} catch (e: NegativeImapResponseException) { | ||
throw FolderFetcherException(messageFromServer = e.responseText, cause = e) | ||
} catch (e: Exception) { | ||
throw FolderFetcherException(cause = e) | ||
} finally { | ||
store.closeAllConnections() | ||
} | ||
} | ||
|
||
private fun createOAuth2TokenProviderOrNull(authStateStorage: AuthStateStorage?): OAuth2TokenProvider? { | ||
return authStateStorage?.let { | ||
oAuth2TokenProviderFactory?.create(it) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
mail/protocols/imap/src/main/java/com/fsck/k9/mail/store/imap/ImapStoreFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.fsck.k9.mail.store.imap | ||
|
||
import com.fsck.k9.mail.ServerSettings | ||
import com.fsck.k9.mail.oauth.OAuth2TokenProvider | ||
import com.fsck.k9.mail.ssl.TrustedSocketFactory | ||
|
||
internal fun interface ImapStoreFactory { | ||
fun create( | ||
serverSettings: ServerSettings, | ||
config: ImapStoreConfig, | ||
trustedSocketFactory: TrustedSocketFactory, | ||
oauthTokenProvider: OAuth2TokenProvider?, | ||
): ImapStore | ||
} |
28 changes: 28 additions & 0 deletions
28
mail/protocols/imap/src/test/java/com/fsck/k9/mail/store/imap/FakeImapStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.fsck.k9.mail.store.imap | ||
|
||
import kotlin.test.fail | ||
|
||
class FakeImapStore : ImapStore { | ||
private var openConnectionCount = 0 | ||
|
||
var getFoldersAction: () -> List<FolderListItem> = { fail("getFoldersAction not set") } | ||
val hasOpenConnections: Boolean | ||
get() = openConnectionCount != 0 | ||
|
||
override fun checkSettings() { | ||
throw UnsupportedOperationException("not implemented") | ||
} | ||
|
||
override fun getFolder(name: String): ImapFolder { | ||
throw UnsupportedOperationException("not implemented") | ||
} | ||
|
||
override fun getFolders(): List<FolderListItem> { | ||
openConnectionCount++ | ||
return getFoldersAction() | ||
} | ||
|
||
override fun closeAllConnections() { | ||
openConnectionCount = 0 | ||
} | ||
} |
Oops, something went wrong.