diff --git a/backend/chat-cache-test b/backend/chat-cache-test new file mode 100644 index 0000000..ccbf1ed Binary files /dev/null and b/backend/chat-cache-test differ diff --git a/backend/chat-cache-test.lock b/backend/chat-cache-test.lock new file mode 100644 index 0000000..4b8f81e Binary files /dev/null and b/backend/chat-cache-test.lock differ diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt b/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt index 3fa9405..bff0c62 100644 --- a/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt +++ b/backend/src/main/java/net/buzzert/kordophone/backend/db/CachedChatDatabase.kt @@ -62,12 +62,10 @@ class CachedChatDatabase (private val realmConfig: RealmConfiguration) { // Flow for watching for message changes for a given conversation fun messagesChanged(conversation: ModelConversation): Flow> { - return realm.query(Conversation::class, "guid == $0", conversation.guid) + return realm.query(Message::class, "conversationGUID == $0", conversation.guid) .find() - .first() - .messages .asFlow() - .map { it.set.map { it.toMessage(conversation) } } + .map { it.list.map { it.toMessage(conversation) } } } fun updateConversations(incomingConversations: List) = realm.writeBlocking { @@ -98,6 +96,7 @@ class CachedChatDatabase (private val realmConfig: RealmConfiguration) { participants = conversation.participants date = conversation.date unreadCount = conversation.unreadCount + lastMessagePreview = conversation.lastMessagePreview } } catch (e: NoSuchElementException) { // Conversation does not exist. Copy it to the realm. @@ -124,17 +123,21 @@ class CachedChatDatabase (private val realmConfig: RealmConfiguration) { fun writeMessages(messages: List, conversation: ModelConversation, outgoing: Boolean = false) { val dbConversation = getManagedConversationByGuid(conversation.guid) realm.writeBlocking { - val dbMessages = messages + messages .map { it.toDatabaseMessage(outgoing = outgoing) } .map { copyToRealm(it, updatePolicy = UpdatePolicy.ALL) } - findLatest(dbConversation)?.messages?.addAll(dbMessages) + findLatest(dbConversation)?.let { + it.lastMessagePreview = messages.last().displayText + it.date = messages.last().date.toInstant().toRealmInstant() + } } } fun fetchMessages(conversation: ModelConversation): List { - val dbConversation = getConversationByGuid(conversation.guid) - return dbConversation.messages.map { it.toMessage(dbConversation.toConversation()) } + return realm.query(Message::class, "conversationGUID == $0", conversation.guid) + .find() + .map { it.toMessage(conversation) } } fun close() { diff --git a/backend/src/main/java/net/buzzert/kordophone/backend/db/model/Conversation.kt b/backend/src/main/java/net/buzzert/kordophone/backend/db/model/Conversation.kt index 927d60a..0c455db 100644 --- a/backend/src/main/java/net/buzzert/kordophone/backend/db/model/Conversation.kt +++ b/backend/src/main/java/net/buzzert/kordophone/backend/db/model/Conversation.kt @@ -25,7 +25,6 @@ open class Conversation( var unreadCount: Int, var lastMessagePreview: String?, - var messages: RealmSet, ): RealmObject { constructor() : this( @@ -36,8 +35,6 @@ open class Conversation( date = RealmInstant.now(), unreadCount = 0, lastMessagePreview = null, - - messages = realmSetOf() ) fun toConversation(): ModelConversation { @@ -51,19 +48,9 @@ open class Conversation( lastMessage = null, ) - if (messages.isNotEmpty()) { - val lastMessage = sortedMessages().last() - conversation.lastMessage = lastMessage.toMessage(conversation) - conversation.lastMessagePreview = lastMessage.text - } - return conversation } - private fun sortedMessages(): List { - return messages.sortedBy { it.date } - } - override fun equals(other: Any?): Boolean { if (other == null || javaClass != other.javaClass) return false