Skip to content

Commit 3acd908

Browse files
committed
chore: synchronize chat message event stream with chats in history controller for top level update
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 4fe30d0 commit 3acd908

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

api/src/main/java/com/getcode/network/ConversationController.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class ConversationStreamController @Inject constructor(
131131
val updates = result.getOrNull() ?: return@result
132132
val (messages, pointers) = updates
133133

134+
historyController.updateChatWithMessages(chat, messages)
134135
val messagesWithContent = messages.map {
135136
messageWithContentMapper.map(chat.id to it)
136137
}

api/src/main/java/com/getcode/network/HistoryController.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,26 @@ class HistoryController @Inject constructor(
8383
chat = _chats.value?.find { it.id == chatId },
8484
onMessagesFetched = { messages ->
8585
val chat = _chats.value?.find { it.id == chatId } ?: return@ChatMessagePagingSource
86-
val updatedMessages = (chat.messages + messages).distinctBy { it.id }
87-
val updatedChat = chat.copy(messages = updatedMessages)
88-
val chats = _chats.value?.map {
89-
if (it.id == updatedChat.id) {
90-
updatedChat
91-
} else {
92-
it
93-
}
94-
}
95-
_chats.update { chats }
86+
updateChatWithMessages(chat, messages)
9687
}
9788
).also {
9889
pagerMap[chatId] = it
9990
}
10091
}
10192

93+
fun updateChatWithMessages(chat: Chat, messages: List<ChatMessage>) {
94+
val updatedMessages = (chat.messages + messages).distinctBy { it.id }
95+
val updatedChat = chat.copy(messages = updatedMessages)
96+
val chats = _chats.value?.map {
97+
if (it.id == updatedChat.id) {
98+
updatedChat
99+
} else {
100+
it
101+
}
102+
}
103+
_chats.update { chats }
104+
}
105+
102106
fun chatFlow(chatId: ID) =
103107
chatFlows[chatId] ?: chatMessagePager(chatId).flow.cachedIn(GlobalScope).also {
104108
chatFlows[chatId] = it

0 commit comments

Comments
 (0)