android, desktop: fix message preview and unread count applied to the wrong chat

When a member support chat or group reports view is open (a secondary chats
context is active), incoming messages could overwrite the last-message preview
of an unrelated chat in the main chat list, increment the wrong chat's unread
badge, and pop the wrong chat to the top.

ChatsContext.addChatItem computed the chat index against the receiver context's
own list via getChatIndex, but then read/wrote chatsContext.chats[i], which
always refers to the primary (main) chat list. On the secondary context the two
lists are ordered differently, so the index pointed at a different chat.

Use the receiver context's own list (chats[i]) consistently, matching every
other method in ChatsContext and the iOS implementation. No change for the
primary context, where chats and chatsContext.chats are the same list.
This commit is contained in:
Narasimha-sc
2026-07-20 13:31:04 +00:00
parent 966c9f7947
commit 0812748df5
@@ -533,7 +533,7 @@ object ChatModel {
val i = getChatIndex(rhId, cInfo.id)
val chat: Chat
if (i >= 0) {
chat = chatsContext.chats[i]
chat = chats[i]
// update preview (for chat from main scope to show new items for invitee in pending status)
if (cInfo.groupChatScope() == null || cInfo.groupInfo_?.membership?.memberPending == true) {
val newPreviewItem = when (cInfo) {
@@ -553,7 +553,7 @@ object ChatModel {
else -> cItem
}
val wasUnread = chat.unreadTag
chatsContext.chats[i] = chat.copy(
chats[i] = chat.copy(
chatItems = arrayListOf(newPreviewItem),
chatStats =
if (cItem.meta.itemStatus is CIStatus.RcvNew) {
@@ -562,11 +562,11 @@ object ChatModel {
} else
chat.chatStats
)
updateChatTagReadInPrimaryContext(chatsContext.chats[i], wasUnread)
updateChatTagReadInPrimaryContext(chats[i], wasUnread)
}
// pop chat
if (appPlatform.isDesktop && cItem.chatDir.sent) {
reorderChat(chatsContext.chats[i], 0)
reorderChat(chats[i], 0)
} else {
popChatCollector.throttlePopChat(chat.remoteHostId, chat.id, currentPosition = i)
}