mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-07-14 07:19:25 +00:00
Recalculate group message stats if it doesn't contain holes
This commit is contained in:
parent
4c707772ce
commit
6e204a47b8
@ -636,4 +636,36 @@ final class ChatListIndexTable: Table {
|
||||
|
||||
return (rootState, summaries)
|
||||
}
|
||||
|
||||
func reindexPeerGroupUnreadCounts(postbox: Postbox, groupId: PeerGroupId) -> PeerGroupUnreadCountersCombinedSummary {
|
||||
var summary = PeerGroupUnreadCountersCombinedSummary(namespaces: [:])
|
||||
|
||||
postbox.chatListTable.forEachPeer(groupId: groupId, { peerId in
|
||||
if peerId.namespace == Int32.max {
|
||||
return
|
||||
}
|
||||
/*guard let peer = postbox.peerTable.get(peerId) else {
|
||||
return
|
||||
}*/
|
||||
guard let combinedState = postbox.readStateTable.getCombinedState(peerId) else {
|
||||
return
|
||||
}
|
||||
/*let notificationPeerId: PeerId = peer.associatedPeerId ?? peerId
|
||||
let notificationSettings = postbox.peerNotificationSettingsTable.getEffective(notificationPeerId)*/
|
||||
let inclusion = self.get(peerId: peerId)
|
||||
if let (inclusionGroupId, _) = inclusion.includedIndex(peerId: peerId), inclusionGroupId == groupId {
|
||||
for (namespace, state) in combinedState.states {
|
||||
if summary.namespaces[namespace] == nil {
|
||||
summary.namespaces[namespace] = PeerGroupUnreadCountersSummary(all: PeerGroupUnreadCounters(messageCount: 0, chatCount: 0))
|
||||
}
|
||||
if state.count > 0 {
|
||||
summary.namespaces[namespace]!.all.chatCount += 1
|
||||
summary.namespaces[namespace]!.all.messageCount += state.count
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return summary
|
||||
}
|
||||
}
|
||||
|
@ -780,4 +780,27 @@ final class ChatListTable: Table {
|
||||
return lhs.index > rhs.index
|
||||
})
|
||||
}
|
||||
|
||||
func doesGroupContainHoles(groupId: PeerGroupId) -> Bool {
|
||||
var result = false
|
||||
self.valueBox.range(self.table, start: self.lowerBound(groupId: groupId), end: self.upperBound(groupId: groupId), keys: { key in
|
||||
if extractKey(key).type == ChatListEntryType.hole.rawValue {
|
||||
result = true
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}, limit: 0)
|
||||
return result
|
||||
}
|
||||
|
||||
func forEachPeer(groupId: PeerGroupId, _ f: (PeerId) -> Void) {
|
||||
self.valueBox.range(self.table, start: self.lowerBound(groupId: groupId), end: self.upperBound(groupId: groupId), keys: { key in
|
||||
let extracted = extractKey(key)
|
||||
if extracted.type == ChatListEntryType.message.rawValue {
|
||||
f(extracted.index.id.peerId)
|
||||
}
|
||||
return true
|
||||
}, limit: 0)
|
||||
}
|
||||
}
|
||||
|
@ -57,6 +57,16 @@ public final class Transaction {
|
||||
return self.postbox?.messageHistoryHoleIndexTable.containing(id: id) ?? [:]
|
||||
}
|
||||
|
||||
public func doesChatListGroupContainHoles(groupId: PeerGroupId) -> Bool {
|
||||
assert(!self.disposed)
|
||||
return self.postbox?.chatListTable.doesGroupContainHoles(groupId: groupId) ?? false
|
||||
}
|
||||
|
||||
public func recalculateChatListGroupStats(groupId: PeerGroupId) {
|
||||
assert(!self.disposed)
|
||||
self.postbox?.recalculateChatListGroupStats(groupId: groupId)
|
||||
}
|
||||
|
||||
public func replaceChatListHole(groupId: PeerGroupId, index: MessageIndex, hole: ChatListHole?) {
|
||||
assert(!self.disposed)
|
||||
self.postbox?.replaceChatListHole(groupId: groupId, index: index, hole: hole)
|
||||
@ -1474,67 +1484,11 @@ public final class Postbox {
|
||||
self.messageHistoryHoleIndexTable.remove(peerId: peerId, namespace: namespace, space: space, range: range, operations: &self.currentPeerHoleOperations)
|
||||
}
|
||||
|
||||
/*fileprivate func fillMultipleGroupFeedHoles(groupId: PeerGroupId, mainHoleMaxIndex: MessageIndex, fillType: HoleFill, messages: [StoreMessage]) {
|
||||
let initialGroupFeedOperationsCount = self.currentGroupFeedOperations[groupId]?.count ?? 0
|
||||
self.groupFeedIndexTable.fillMultipleHoles(insertMessage: { message in
|
||||
self.insertMessageInternal(message: message)
|
||||
}, groupId: groupId, mainHoleMaxIndex: mainHoleMaxIndex, fillType: fillType, messages: self.messageHistoryTable.internalStoreMessages(messages), addOperation: { groupId, operation in
|
||||
if self.currentGroupFeedOperations[groupId] == nil {
|
||||
self.currentGroupFeedOperations[groupId] = []
|
||||
fileprivate func recalculateChatListGroupStats(groupId: PeerGroupId) {
|
||||
let summary = self.chatListIndexTable.reindexPeerGroupUnreadCounts(postbox: self, groupId: groupId)
|
||||
self.groupMessageStatsTable.set(groupId: groupId, summary: summary)
|
||||
self.currentUpdatedGroupTotalUnreadSummaries[groupId] = summary
|
||||
}
|
||||
self.currentGroupFeedOperations[groupId]!.append(operation)
|
||||
})
|
||||
|
||||
var filledMessageIndices: [MessageIndex: HoleFillDirection] = [:]
|
||||
if let operations = self.currentGroupFeedOperations[groupId] {
|
||||
for i in initialGroupFeedOperationsCount ..< operations.count {
|
||||
switch operations[i] {
|
||||
case let .insertHole(hole, _):
|
||||
filledMessageIndices[hole.maxIndex] = fillType.direction
|
||||
case let .insertMessage(message):
|
||||
filledMessageIndices[MessageIndex(message)] = fillType.direction
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !filledMessageIndices.isEmpty {
|
||||
if self.currentGroupFeedIdsWithFilledHoles[groupId] == nil {
|
||||
self.currentGroupFeedIdsWithFilledHoles[groupId] = [:]
|
||||
}
|
||||
for (messageIndex, direction) in filledMessageIndices {
|
||||
self.currentGroupFeedIdsWithFilledHoles[groupId]![messageIndex] = direction
|
||||
}
|
||||
}
|
||||
|
||||
if self.currentRemovedHolesByPeerGroupId[groupId] == nil {
|
||||
self.currentRemovedHolesByPeerGroupId[groupId] = [:]
|
||||
}
|
||||
self.currentRemovedHolesByPeerGroupId[groupId]![mainHoleMaxIndex] = fillType.direction
|
||||
}
|
||||
|
||||
fileprivate func addFeedHoleFromLatestEntries(groupId: PeerGroupId) {
|
||||
self.groupFeedIndexTable.addHoleFromLatestEntries(groupId: groupId, messageHistoryTable: self.messageHistoryTable, operations: &self.currentGroupFeedOperations)
|
||||
}
|
||||
|
||||
fileprivate func addMessagesToGroupFeedIndex(groupId: PeerGroupId, ids: [MessageId]) {
|
||||
for id in ids {
|
||||
if let entry = self.messageHistoryIndexTable.getMaybeUninitialized(id), case let .Message(index) = entry {
|
||||
if let message = self.messageHistoryTable.getMessage(index) {
|
||||
self.groupFeedIndexTable.add(groupId: groupId, message: message, operations: &self.currentGroupFeedOperations)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func removeMessagesFromGroupFeedIndex(groupId: PeerGroupId, ids: [MessageId]) {
|
||||
for id in ids {
|
||||
if let entry = self.messageHistoryIndexTable.getMaybeUninitialized(id), case let .Message(index) = entry {
|
||||
self.groupFeedIndexTable.remove(groupId: groupId, messageIndex: index, operations: &self.currentGroupFeedOperations)
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
fileprivate func replaceChatListHole(groupId: PeerGroupId, index: MessageIndex, hole: ChatListHole?) {
|
||||
self.chatListTable.replaceHole(groupId: groupId, index: index, hole: hole, operations: &self.currentChatListOperations)
|
||||
|
@ -72,10 +72,15 @@ func managedSynchronizeGroupMessageStats(network: Network, postbox: Postbox, sta
|
||||
}
|
||||
|
||||
private func synchronizeGroupMessageStats(postbox: Postbox, network: Network, groupId: PeerGroupId, namespace: MessageId.Namespace) -> Signal<Void, NoError> {
|
||||
return postbox.transaction { transaction -> Signal<Void, NoError> in
|
||||
if namespace != Namespaces.Message.Cloud || groupId == .root {
|
||||
return postbox.transaction { transaction in
|
||||
transaction.confirmSynchronizedPeerGroupMessageStats(groupId: groupId, namespace: namespace)
|
||||
return .complete()
|
||||
}
|
||||
|
||||
if !transaction.doesChatListGroupContainHoles(groupId: groupId) {
|
||||
transaction.recalculateChatListGroupStats(groupId: groupId)
|
||||
return .complete()
|
||||
}
|
||||
|
||||
return network.request(Api.functions.messages.getPeerDialogs(peers: [.inputDialogPeerFolder(folderId: groupId.rawValue)]))
|
||||
@ -103,3 +108,5 @@ private func synchronizeGroupMessageStats(postbox: Postbox, network: Network, gr
|
||||
}
|
||||
}
|
||||
}
|
||||
|> switchToLatest
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user