Change K9BackendFolder.getOldestMessageDate() to use MessageStore

This commit is contained in:
cketti 2021-04-14 01:10:16 +02:00
parent 4e4babeea6
commit cf51883637
5 changed files with 47 additions and 7 deletions

View file

@ -107,13 +107,7 @@ class K9BackendFolder(
}
override fun getOldestMessageDate(): Date? {
return database.rawQuery("SELECT MIN(date) FROM messages WHERE folder_id = ?", databaseId) { cursor ->
if (cursor.moveToFirst()) {
Date(cursor.getLong(0))
} else {
null
}
}
return messageStore.getOldestMessageDate(folderId)
}
override fun getFolderExtraString(name: String): String? {

View file

@ -5,6 +5,7 @@ import com.fsck.k9.mail.Flag
import com.fsck.k9.mail.FolderClass
import com.fsck.k9.mail.FolderType
import com.fsck.k9.mail.Header
import java.util.Date
/**
* Functions for accessing and modifying locally stored messages.
@ -81,6 +82,11 @@ interface MessageStore {
*/
fun getAllMessagesAndEffectiveDates(folderId: Long): Map<String, Long?>
/**
* Retrieve the date of the oldest message in the given folder.
*/
fun getOldestMessageDate(folderId: Long): Date?
/**
* Retrieve the header fields of a message.
*/

View file

@ -15,6 +15,7 @@ import com.fsck.k9.mailstore.MoreMessages
import com.fsck.k9.mailstore.SaveMessageData
import com.fsck.k9.mailstore.StorageManager
import com.fsck.k9.message.extractors.BasicPartInfoExtractor
import java.util.Date
// TODO: Remove dependency on LocalStore
class K9MessageStore(
@ -86,6 +87,10 @@ class K9MessageStore(
return retrieveMessageOperations.getAllMessagesAndEffectiveDates(folderId)
}
override fun getOldestMessageDate(folderId: Long): Date? {
return retrieveMessageOperations.getOldestMessageDate(folderId)
}
override fun getHeaders(folderId: Long, messageServerId: String): List<Header> {
return retrieveMessageOperations.getHeaders(folderId, messageServerId)
}

View file

@ -9,6 +9,7 @@ import com.fsck.k9.mail.internet.MimeHeader
import com.fsck.k9.mail.message.MessageHeaderParser
import com.fsck.k9.mailstore.LockableDatabase
import com.fsck.k9.mailstore.MessageNotFoundException
import java.util.Date
internal class RetrieveMessageOperations(private val lockableDatabase: LockableDatabase) {
@ -151,6 +152,22 @@ internal class RetrieveMessageOperations(private val lockableDatabase: LockableD
}
}
fun getOldestMessageDate(folderId: Long): Date? {
return lockableDatabase.execute(false) { database ->
database.rawQuery(
"SELECT MIN(date) FROM messages WHERE folder_id = ?",
arrayOf(folderId.toString())
).use { cursor ->
if (cursor.moveToFirst()) {
val timestamp = cursor.getLong(0)
if (timestamp != 0L) Date(timestamp) else null
} else {
null
}
}
}
}
fun getHeaders(folderId: Long, messageServerId: String): List<Header> {
return lockableDatabase.execute(false) { database ->
database.rawQuery(

View file

@ -6,6 +6,7 @@ import com.fsck.k9.mail.MessagingException
import com.fsck.k9.mail.crlf
import com.fsck.k9.storage.RobolectricTest
import com.google.common.truth.Truth.assertThat
import java.util.Date
import org.junit.Test
class RetrieveMessageOperationsTest : RobolectricTest() {
@ -188,4 +189,21 @@ class RetrieveMessageOperationsTest : RobolectricTest() {
assertThat(highestUid).isNull()
}
@Test
fun `get oldest message date`() {
sqliteDatabase.createMessage(folderId = 1, date = 42)
sqliteDatabase.createMessage(folderId = 1, date = 23)
val oldestMessageDate = retrieveMessageOperations.getOldestMessageDate(folderId = 1)
assertThat(oldestMessageDate).isEqualTo(Date(23))
}
@Test
fun `get oldest message date without any messages in the store`() {
val oldestMessageDate = retrieveMessageOperations.getOldestMessageDate(folderId = 1)
assertThat(oldestMessageDate).isNull()
}
}