Adds Message retrieval
This commit is contained in:
@@ -3,12 +3,16 @@ package net.buzzert.kordophone.backend.db
|
|||||||
import io.realm.kotlin.Realm
|
import io.realm.kotlin.Realm
|
||||||
import io.realm.kotlin.RealmConfiguration
|
import io.realm.kotlin.RealmConfiguration
|
||||||
import net.buzzert.kordophone.backend.db.model.Conversation
|
import net.buzzert.kordophone.backend.db.model.Conversation
|
||||||
|
import net.buzzert.kordophone.backend.db.model.Message
|
||||||
import net.buzzert.kordophone.backend.db.model.toDatabaseConversation
|
import net.buzzert.kordophone.backend.db.model.toDatabaseConversation
|
||||||
|
import net.buzzert.kordophone.backend.db.model.toDatabaseMessage
|
||||||
|
import net.buzzert.kordophone.backend.model.GUID
|
||||||
import net.buzzert.kordophone.backend.model.Conversation as ModelConversation
|
import net.buzzert.kordophone.backend.model.Conversation as ModelConversation
|
||||||
|
import net.buzzert.kordophone.backend.model.Message as ModelMessage
|
||||||
|
|
||||||
internal class CachedChatDatabase (private val realmConfig: RealmConfiguration) {
|
internal class CachedChatDatabase (private val realmConfig: RealmConfiguration) {
|
||||||
companion object {
|
companion object {
|
||||||
private val schema = setOf(Conversation::class)
|
private val schema = setOf(Conversation::class, Message::class)
|
||||||
|
|
||||||
fun liveDatabase(): CachedChatDatabase {
|
fun liveDatabase(): CachedChatDatabase {
|
||||||
return CachedChatDatabase(
|
return CachedChatDatabase(
|
||||||
@@ -43,4 +47,22 @@ internal class CachedChatDatabase (private val realmConfig: RealmConfiguration)
|
|||||||
val items = realm.query(Conversation::class).find()
|
val items = realm.query(Conversation::class).find()
|
||||||
return items.map { it.toConversation() }
|
return items.map { it.toConversation() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun writeMessages(messages: List<ModelMessage>, conversation: ModelConversation) {
|
||||||
|
val dbConversation = getConversationByGuid(conversation.guid)
|
||||||
|
realm.writeBlocking {
|
||||||
|
findLatest(dbConversation)?.messages?.addAll(messages.map { it.toDatabaseMessage() })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun fetchMessages(conversation: ModelConversation): List<ModelMessage> {
|
||||||
|
val dbConversation = getConversationByGuid(conversation.guid)
|
||||||
|
return dbConversation.messages.map { it.toMessage() }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getConversationByGuid(guid: GUID): Conversation {
|
||||||
|
return realm.query(Conversation::class, "guid == '$guid'")
|
||||||
|
.find()
|
||||||
|
.first()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -24,18 +24,19 @@ open class Conversation(
|
|||||||
var lastMessagePreview: String,
|
var lastMessagePreview: String,
|
||||||
var guid: GUID,
|
var guid: GUID,
|
||||||
|
|
||||||
// TODO: Not sure how to do this yet...
|
var messages: RealmList<Message>,
|
||||||
// var messages: RealmList<Message>,
|
|
||||||
): RealmObject
|
): RealmObject
|
||||||
{
|
{
|
||||||
constructor(): this(
|
constructor(): this(
|
||||||
_id = ObjectId().toString(),
|
_id = ObjectId().toString(),
|
||||||
displayName = null,
|
displayName = null,
|
||||||
participants = realmListOf(),
|
participants = realmListOf<String>(),
|
||||||
date = RealmInstant.now(),
|
date = RealmInstant.now(),
|
||||||
unreadCount = 0,
|
unreadCount = 0,
|
||||||
lastMessagePreview = "",
|
lastMessagePreview = "",
|
||||||
guid = "",
|
guid = "",
|
||||||
|
|
||||||
|
messages = realmListOf<Message>()
|
||||||
)
|
)
|
||||||
|
|
||||||
fun toConversation(): ModelConversation {
|
fun toConversation(): ModelConversation {
|
||||||
|
|||||||
@@ -2,13 +2,14 @@ package net.buzzert.kordophone.backend
|
|||||||
|
|
||||||
import net.buzzert.kordophone.backend.db.CachedChatDatabase
|
import net.buzzert.kordophone.backend.db.CachedChatDatabase
|
||||||
import net.buzzert.kordophone.backend.model.Conversation
|
import net.buzzert.kordophone.backend.model.Conversation
|
||||||
|
import net.buzzert.kordophone.backend.model.Message
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Assert.assertEquals
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
|
||||||
class DatabaseTests {
|
class DatabaseTests {
|
||||||
@Test
|
@Test
|
||||||
fun testCreateRetrieve() {
|
fun testConversationRetrieval() {
|
||||||
val db = CachedChatDatabase.testDatabase()
|
val db = CachedChatDatabase.testDatabase()
|
||||||
|
|
||||||
val conversation = Conversation(
|
val conversation = Conversation(
|
||||||
@@ -28,4 +29,35 @@ class DatabaseTests {
|
|||||||
val readConversation = readBackConversations[0]
|
val readConversation = readBackConversations[0]
|
||||||
assertEquals(readConversation, conversation)
|
assertEquals(readConversation, conversation)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testMessageRetrieval() {
|
||||||
|
val db = CachedChatDatabase.testDatabase()
|
||||||
|
|
||||||
|
val conversation = Conversation(
|
||||||
|
date = Date(),
|
||||||
|
participants = listOf("james@magahern.com"),
|
||||||
|
displayName = "Test",
|
||||||
|
unreadCount = 1,
|
||||||
|
lastMessagePreview = "Hello!",
|
||||||
|
guid = "1234",
|
||||||
|
)
|
||||||
|
|
||||||
|
db.writeConversations(listOf(conversation))
|
||||||
|
|
||||||
|
val message = Message(
|
||||||
|
text = "Hello!",
|
||||||
|
guid = "4321",
|
||||||
|
date = Date(),
|
||||||
|
sender = "james@magahern.com"
|
||||||
|
)
|
||||||
|
|
||||||
|
db.writeMessages(listOf(message), conversation)
|
||||||
|
|
||||||
|
val readMessages = db.fetchMessages(conversation)
|
||||||
|
assertEquals(readMessages.count(), 1)
|
||||||
|
|
||||||
|
val readMessage = readMessages[0]
|
||||||
|
assertEquals(readMessage, message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user