diff --git a/submodules/Postbox/Postbox/Postbox.swift b/submodules/Postbox/Postbox/Postbox.swift index 71d85df1e0..08a58f189f 100644 --- a/submodules/Postbox/Postbox/Postbox.swift +++ b/submodules/Postbox/Postbox/Postbox.swift @@ -282,6 +282,11 @@ public final class Transaction { return self.postbox?.getTopPeerMessageIndex(peerId: peerId, namespace: namespace) } + public func getTopPeerMessageIndex(peerId: PeerId) -> MessageIndex? { + assert(!self.disposed) + return self.postbox?.getTopPeerMessageIndex(peerId: peerId) + } + public func getPeerChatListIndex(_ peerId: PeerId) -> (PeerGroupId, ChatListIndex)? { assert(!self.disposed) return self.postbox?.chatListTable.getPeerChatListIndex(peerId: peerId) @@ -1780,7 +1785,16 @@ public final class Postbox { return nil } } - + + fileprivate func getTopPeerMessageIndex(peerId: PeerId) -> MessageIndex? { + var indices: [MessageIndex] = [] + for namespace in self.messageHistoryIndexTable.existingNamespaces(peerId: peerId) { + if let index = self.messageHistoryTable.topIndexEntry(peerId: peerId, namespace: namespace) { + indices.append(index) + } + } + return indices.max() + } fileprivate func getPeerChatListInclusion(_ id: PeerId) -> PeerChatListInclusion { if let inclusion = self.currentUpdatedChatListInclusions[id] { return inclusion diff --git a/submodules/TelegramCore/TelegramCore/ApplyMaxReadIndexInteractively.swift b/submodules/TelegramCore/TelegramCore/ApplyMaxReadIndexInteractively.swift index 3a13aace67..308874f095 100644 --- a/submodules/TelegramCore/TelegramCore/ApplyMaxReadIndexInteractively.swift +++ b/submodules/TelegramCore/TelegramCore/ApplyMaxReadIndexInteractively.swift @@ -123,32 +123,36 @@ public func togglePeerUnreadMarkInteractively(postbox: Postbox, viewTracker: Acc } public func togglePeerUnreadMarkInteractively(transaction: Transaction, viewTracker: AccountViewTracker, peerId: PeerId, setToValue: Bool? = nil) { - let namespace: MessageId.Namespace + let principalNamespace: MessageId.Namespace if peerId.namespace == Namespaces.Peer.SecretChat { - namespace = Namespaces.Message.SecretIncoming + principalNamespace = Namespaces.Message.SecretIncoming } else { - namespace = Namespaces.Message.Cloud + principalNamespace = Namespaces.Message.Cloud } + var hasUnread = false if let states = transaction.getPeerReadStates(peerId) { - for i in 0 ..< states.count { - if states[i].0 == namespace { - if states[i].1.isUnread { - if setToValue == nil || !(setToValue!) { - if let index = transaction.getTopPeerMessageIndex(peerId: peerId, namespace: namespace) { - let _ = transaction.applyInteractiveReadMaxIndex(index) - } else { - transaction.applyMarkUnread(peerId: peerId, namespace: namespace, value: false, interactive: true) - } - viewTracker.updateMarkAllMentionsSeen(peerId: peerId) - } - } else if namespace == Namespaces.Message.Cloud || namespace == Namespaces.Message.SecretIncoming { - if setToValue == nil || setToValue! { - transaction.applyMarkUnread(peerId: peerId, namespace: namespace, value: true, interactive: true) - } - } + for state in states { + if state.1.isUnread { + hasUnread = true + break } } } + + if hasUnread { + if setToValue == nil || !(setToValue!) { + if let index = transaction.getTopPeerMessageIndex(peerId: peerId) { + let _ = transaction.applyInteractiveReadMaxIndex(index) + } else { + transaction.applyMarkUnread(peerId: peerId, namespace: principalNamespace, value: false, interactive: true) + } + viewTracker.updateMarkAllMentionsSeen(peerId: peerId) + } + } else { + if setToValue == nil || setToValue! { + transaction.applyMarkUnread(peerId: peerId, namespace: principalNamespace, value: true, interactive: true) + } + } } public func clearPeerUnseenPersonalMessagesInteractively(account: Account, peerId: PeerId) -> Signal {