Private
Public Access
1
0

backend: don't fetch messages with conversations.

This commit is contained in:
2024-03-29 01:20:08 -07:00
parent 46c2fd6bf9
commit b160baae3e
4 changed files with 11 additions and 21 deletions

BIN
backend/chat-cache-test Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -62,12 +62,10 @@ class CachedChatDatabase (private val realmConfig: RealmConfiguration) {
// Flow for watching for message changes for a given conversation // Flow for watching for message changes for a given conversation
fun messagesChanged(conversation: ModelConversation): Flow<List<ModelMessage>> { fun messagesChanged(conversation: ModelConversation): Flow<List<ModelMessage>> {
return realm.query(Conversation::class, "guid == $0", conversation.guid) return realm.query(Message::class, "conversationGUID == $0", conversation.guid)
.find() .find()
.first()
.messages
.asFlow() .asFlow()
.map { it.set.map { it.toMessage(conversation) } } .map { it.list.map { it.toMessage(conversation) } }
} }
fun updateConversations(incomingConversations: List<ModelConversation>) = realm.writeBlocking { fun updateConversations(incomingConversations: List<ModelConversation>) = realm.writeBlocking {
@@ -98,6 +96,7 @@ class CachedChatDatabase (private val realmConfig: RealmConfiguration) {
participants = conversation.participants participants = conversation.participants
date = conversation.date date = conversation.date
unreadCount = conversation.unreadCount unreadCount = conversation.unreadCount
lastMessagePreview = conversation.lastMessagePreview
} }
} catch (e: NoSuchElementException) { } catch (e: NoSuchElementException) {
// Conversation does not exist. Copy it to the realm. // Conversation does not exist. Copy it to the realm.
@@ -124,17 +123,21 @@ class CachedChatDatabase (private val realmConfig: RealmConfiguration) {
fun writeMessages(messages: List<ModelMessage>, conversation: ModelConversation, outgoing: Boolean = false) { fun writeMessages(messages: List<ModelMessage>, conversation: ModelConversation, outgoing: Boolean = false) {
val dbConversation = getManagedConversationByGuid(conversation.guid) val dbConversation = getManagedConversationByGuid(conversation.guid)
realm.writeBlocking { realm.writeBlocking {
val dbMessages = messages messages
.map { it.toDatabaseMessage(outgoing = outgoing) } .map { it.toDatabaseMessage(outgoing = outgoing) }
.map { copyToRealm(it, updatePolicy = UpdatePolicy.ALL) } .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<ModelMessage> { fun fetchMessages(conversation: ModelConversation): List<ModelMessage> {
val dbConversation = getConversationByGuid(conversation.guid) return realm.query(Message::class, "conversationGUID == $0", conversation.guid)
return dbConversation.messages.map { it.toMessage(dbConversation.toConversation()) } .find()
.map { it.toMessage(conversation) }
} }
fun close() { fun close() {

View File

@@ -25,7 +25,6 @@ open class Conversation(
var unreadCount: Int, var unreadCount: Int,
var lastMessagePreview: String?, var lastMessagePreview: String?,
var messages: RealmSet<Message>,
): RealmObject ): RealmObject
{ {
constructor() : this( constructor() : this(
@@ -36,8 +35,6 @@ open class Conversation(
date = RealmInstant.now(), date = RealmInstant.now(),
unreadCount = 0, unreadCount = 0,
lastMessagePreview = null, lastMessagePreview = null,
messages = realmSetOf<Message>()
) )
fun toConversation(): ModelConversation { fun toConversation(): ModelConversation {
@@ -51,19 +48,9 @@ open class Conversation(
lastMessage = null, lastMessage = null,
) )
if (messages.isNotEmpty()) {
val lastMessage = sortedMessages().last()
conversation.lastMessage = lastMessage.toMessage(conversation)
conversation.lastMessagePreview = lastMessage.text
}
return conversation return conversation
} }
private fun sortedMessages(): List<Message> {
return messages.sortedBy { it.date }
}
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (other == null || javaClass != other.javaClass) return false if (other == null || javaClass != other.javaClass) return false