mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-11-08 18:47:30 +00:00
The count is seeded by getUsersInfo, which counts RcvNew items where ntfsEnabled holds - every item for an unmuted chat, only mentions for a mentions-only one, none for a muted one. The deltas that maintain it afterwards did not follow that rule for the active profile: addChatItem incremented for every item regardless of the chat's notification setting; on Android and desktop the mark-read helpers subtracted the full unread count, while on iOS Mark read and Clear followed the mentions-only setting but subtracted the full count for muted chats, and per-item reads forwarded the raw delta; removeChat left a deleted chat's unread behind; and changing the setting moved a chat between rules without moving the count. Chat.userUnreadCount names what a chat contributes, and it is not a new rule: totalUnreadCountForAllUsers already computed exactly this inline for the app badge, and now calls it. The sites that change a chat's stats, its settings or its presence report the contribution before and after, and the count moves by the difference - apart from the exceptions noted at the end. For that to hold the stats it reads have to be maintained, so on Android and desktop decreaseCounterInPrimaryContext now decrements unreadMentions alongside unreadCount when the item was a mention. Without it a mentions-only chat's contribution never moved on any of that function's three paths - a deleted item, a moderated member item, or an item going from New to Read. iOS already passed the mention delta on its deletion paths. This is visible beyond the profile picker, which badges only other profiles: the desktop tray dot sums the count across profiles including the active one, and on iOS these same deltas drive the app icon badge. So on iOS, muting a chat with unread now drops the badge by its contribution, deleting a chat drops it, and a message in a muted chat no longer raises it - each of which now agrees with totalUnreadCountForAllUsers, which the badge is also set from. addChat is deliberately not the mirror of removeChat: adding a chat means it is being loaded into view, and its items were already counted by the seed, so counting them again on ChatItemsLoader's addChat would double them. The one place that removes and re-adds a chat while keeping its items - deleting a contact in "keep conversation" mode on iOS - now carries its stats across and restores the contribution explicitly, since the core keeps both the contact row and its items there and the seed still counts them. Wherever the chat list is re-read wholesale the counts are re-read with it: both resume paths, and setting a chat item TTL, which deletes the active profile's items without per-item events and so leaves its count stale. They are assigned before the chats, which matters on iOS because updateChats recomputes the app badge from users there. Reading users is best effort on both platforms - losing them must not cost us the chats already fetched. This does not make the count exact. getUsersInfo has no group_scope_tag filter where the query behind chatStats.unreadCount does, so the seed counts support-scope items that no client-side rule can, and the count steps up by the outstanding support unread on each reseed. replaceChat also installs a whole chatStats without moving the count: on Android and desktop from a snapshot taken before the API call, in ChatListNavLinkView's markChatRead and markChatUnread and ChatView's markUnreadChatAsRead, and on iOS from the server in the receivedContactRequest handler in SimpleXAPI. Settings changes that arrive in events through updateChatInfo do not move it either.