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
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()
.first()
.messages
.asFlow()
.map { it.set.map { it.toMessage(conversation) } }
.map { it.list.map { it.toMessage(conversation) } }
}
fun updateConversations(incomingConversations: List<ModelConversation>) = 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<ModelMessage>, 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<ModelMessage> {
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() {

View File

@@ -25,7 +25,6 @@ open class Conversation(
var unreadCount: Int,
var lastMessagePreview: String?,
var messages: RealmSet<Message>,
): RealmObject
{
constructor() : this(
@@ -36,8 +35,6 @@ open class Conversation(
date = RealmInstant.now(),
unreadCount = 0,
lastMessagePreview = null,
messages = realmSetOf<Message>()
)
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<Message> {
return messages.sortedBy { it.date }
}
override fun equals(other: Any?): Boolean {
if (other == null || javaClass != other.javaClass) return false