Skip to content

Commit 69bae84

Browse files
mahibiclaude
andcommitted
Simplify capability handling in ChatViewModel.messagesFlow
spreedCapabilities was only needed inside the terminal map step of messagesFlow (to filter system messages for channels), but it was being combined into the outer flatMapLatest trigger. Any capabilities emission unrelated to the conversation forced the whole message source subscription to cancel and restart. Move the capabilities dependency to a dedicated isChannelFlow (conversationAndUserFlow + spreedCapabilities -> Boolean, with distinctUntilChanged), and combine only that into the innermost step. handleSystemMessages() now takes a plain Boolean instead of ConversationModel/SpreedCapability, making it simpler to reason about and test. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
1 parent a50d4bc commit 69bae84

1 file changed

Lines changed: 11 additions & 12 deletions

File tree

app/src/main/java/com/nextcloud/talk/chat/viewmodels/ChatViewModel.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,11 @@ class ChatViewModel @AssistedInject constructor(
496496
replay = 1
497497
)
498498

499+
private val isChannelFlow: Flow<Boolean> =
500+
combine(conversationAndUserFlow, spreedCapabilities) { (conversation, _), capabilities ->
501+
ConversationUtils.isChannel(conversation, capabilities)
502+
}.distinctUntilChanged()
503+
499504
// ------------------------------
500505
// Messages
501506
// ------------------------------
@@ -511,10 +516,8 @@ class ChatViewModel @AssistedInject constructor(
511516

512517
@OptIn(ExperimentalCoroutinesApi::class)
513518
private val messagesFlow: Flow<List<ChatMessage>> =
514-
combine(conversationAndUserFlow, spreedCapabilities) { (conversation, user), capabilities ->
515-
Triple(conversation, user, capabilities)
516-
}
517-
.flatMapLatest { (conversation, user, capabilities) ->
519+
conversationAndUserFlow
520+
.flatMapLatest { (conversation, user) ->
518521
combine(chatMode, contextAnchorMessageId) { mode, anchorMessageId ->
519522
mode to anchorMessageId
520523
}
@@ -533,8 +536,8 @@ class ChatViewModel @AssistedInject constructor(
533536
.distinctUntilChanged()
534537
.mapToChatMessages(user.userId!!)
535538
}
536-
.map { messages ->
537-
handleSystemMessages(messages, conversation, capabilities)
539+
.combine(isChannelFlow) { messages, isChannel ->
540+
handleSystemMessages(messages, isChannel)
538541
.let(::handleThreadMessages)
539542
}
540543
}
@@ -1369,12 +1372,8 @@ class ChatViewModel @AssistedInject constructor(
13691372
}
13701373
Log.d(TAG, "fetchNewMessagesWithRetry: no new messages after $POST_UPLOAD_FETCH_MAX_ATTEMPTS attempts")
13711374
}
1372-
private fun handleSystemMessages(
1373-
chatMessageList: List<ChatMessage>,
1374-
conversation: ConversationModel?,
1375-
capabilities: SpreedCapability?
1376-
): List<ChatMessage> {
1377-
if (ConversationUtils.isChannel(conversation, capabilities)) {
1375+
private fun handleSystemMessages(chatMessageList: List<ChatMessage>, isChannel: Boolean): List<ChatMessage> {
1376+
if (isChannel) {
13781377
return chatMessageList.filter { !it.isSystemMessage }
13791378
}
13801379

0 commit comments

Comments
 (0)