Bot updates

This commit is contained in:
Isaac 2025-08-15 09:58:39 +02:00
parent 4c42c4d5a4
commit 92156ffec0
9 changed files with 60 additions and 49 deletions

View File

@ -20,7 +20,7 @@ final class MutableCachedPeerDataView: MutablePostboxView {
} }
func replay(postbox: PostboxImpl, transaction: PostboxTransaction) -> Bool { func replay(postbox: PostboxImpl, transaction: PostboxTransaction) -> Bool {
if let cachedPeerData = transaction.currentUpdatedCachedPeerData[self.peerId] { if let cachedPeerData = transaction.currentUpdatedCachedPeerData[self.peerId]?.updated {
self.cachedPeerData = cachedPeerData self.cachedPeerData = cachedPeerData
if self.trackAssociatedMessages { if self.trackAssociatedMessages {
self.associatedMessages.removeAll() self.associatedMessages.removeAll()

View File

@ -373,7 +373,7 @@ final class ChatListTable: Table {
return result return result
} }
func replay(postbox: PostboxImpl, historyOperationsByPeerId: [PeerId: [MessageHistoryOperation]], updatedPeerChatListEmbeddedStates: Set<PeerId>, updatedPeers: [PeerId: Peer], updatedChatListInclusions: [PeerId: PeerChatListInclusion], messageHistoryTable: MessageHistoryTable, peerChatInterfaceStateTable: PeerChatInterfaceStateTable, operations: inout [PeerGroupId: [ChatListOperation]]) { func replay(postbox: PostboxImpl, historyOperationsByPeerId: [PeerId: [MessageHistoryOperation]], updatedPeerChatListEmbeddedStates: Set<PeerId>, updatedPeerCachedData: [PeerId: (previous: CachedPeerData?, updated: CachedPeerData)], updatedChatListInclusions: [PeerId: PeerChatListInclusion], messageHistoryTable: MessageHistoryTable, peerChatInterfaceStateTable: PeerChatInterfaceStateTable, operations: inout [PeerGroupId: [ChatListOperation]]) {
var changedPeerIds = Set<PeerId>() var changedPeerIds = Set<PeerId>()
for peerId in historyOperationsByPeerId.keys { for peerId in historyOperationsByPeerId.keys {
changedPeerIds.insert(peerId) changedPeerIds.insert(peerId)
@ -384,12 +384,23 @@ final class ChatListTable: Table {
for peerId in updatedChatListInclusions.keys { for peerId in updatedChatListInclusions.keys {
changedPeerIds.insert(peerId) changedPeerIds.insert(peerId)
} }
/*for peerId in updatedPeers.keys { for (peerId, update) in updatedPeerCachedData {
if let previous = update.previous {
if self.seedConfiguration.decodeAssociatedChatListPeerId(previous) != self.seedConfiguration.decodeAssociatedChatListPeerId(update.updated) {
changedPeerIds.insert(peerId) changedPeerIds.insert(peerId)
if let peer = postbox.peerTable.get(peerId), let targetPeerId = self.seedConfiguration.chatListPeerMergeIntoTargetId(peer) {
changedPeerIds.insert(targetPeerId)
} }
}*/ } else {
changedPeerIds.insert(peerId)
}
}
var additionalChangedPeerIds = Set<PeerId>()
for peerId in changedPeerIds {
//TODO:release move this to seed configuration
if let peer = postbox.peerTable.get(peerId), peer.id.namespace._internalGetInt32Value() == 2, let associatedPeerId = peer.associatedPeerId, associatedPeerId.namespace._internalGetInt32Value() == 0 {
additionalChangedPeerIds.insert(associatedPeerId)
}
}
changedPeerIds.formUnion(additionalChangedPeerIds)
self.ensureInitialized(groupId: .root) self.ensureInitialized(groupId: .root)
@ -419,21 +430,17 @@ final class ChatListTable: Table {
rawTopMessageIndex = nil rawTopMessageIndex = nil
} }
/*for associatedId in postbox.reverseAssociatedPeerTable.get(peerId: peerId) { if let cachedData = postbox.cachedPeerDataTable.get(peerId), let associatedChatListPeerId = self.seedConfiguration.decodeAssociatedChatListPeerId(cachedData) {
if let peer = postbox.peerTable.get(associatedId), let targetPeerId = self.seedConfiguration.chatListPeerMergeIntoTargetId(peer), targetPeerId == peerId { if let associatedTopMessage = messageHistoryTable.topIndex(peerId: associatedChatListPeerId) {
if let associatedTopMessage = messageHistoryTable.topIndex(peerId: associatedId) { if let currentTopMessageIndex = topMessageIndex {
if let currentTopMessageIndex = topMessage { if currentTopMessageIndex.timestamp < associatedTopMessage.timestamp {
if associatedTopMessage > currentTopMessageIndex { topMessageIndex = MessageIndex(id: currentTopMessageIndex.id, timestamp: associatedTopMessage.timestamp)
topMessageIndex = associatedTopMessage
rawTopMessageIndex = associatedTopMessage
} }
} else { } else {
topMessageIndex = associatedTopMessage topMessageIndex = MessageIndex(id: MessageId(peerId: peerId, namespace: 0, id: 1), timestamp: associatedTopMessage.timestamp)
rawTopMessageIndex = associatedTopMessage
} }
} }
} }
}*/
var updatedIndex = self.indexTable.setTopMessageIndex(peerId: peerId, index: topMessageIndex) var updatedIndex = self.indexTable.setTopMessageIndex(peerId: peerId, index: topMessageIndex)
if let updatedInclusion = updatedChatListInclusions[peerId] { if let updatedInclusion = updatedChatListInclusions[peerId] {

View File

@ -1043,6 +1043,7 @@ public final class ChatListView {
//TODO:release //TODO:release
var linkedEntries: [PeerId: [MutableChatListEntry.MessageEntryData]] = [:] var linkedEntries: [PeerId: [MutableChatListEntry.MessageEntryData]] = [:]
if "".isEmpty {
for entry in mutableView.sampledState.entries { for entry in mutableView.sampledState.entries {
guard case let .MessageEntry(entryData) = entry else { guard case let .MessageEntry(entryData) = entry else {
continue continue
@ -1054,6 +1055,7 @@ public final class ChatListView {
linkedEntries[associatedPeerId]?.append(entryData) linkedEntries[associatedPeerId]?.append(entryData)
} }
} }
}
for entry in mutableView.sampledState.entries { for entry in mutableView.sampledState.entries {
switch entry { switch entry {
@ -1069,7 +1071,7 @@ public final class ChatListView {
var forumTopicData = entryData.displayAsRegularChat ? nil : entryData.forumTopicData var forumTopicData = entryData.displayAsRegularChat ? nil : entryData.forumTopicData
if let linkedEntries = linkedEntries[entryData.index.messageIndex.id.peerId] { if let linkedEntries = linkedEntries[entryData.index.messageIndex.id.peerId] {
for entry in linkedEntries { for entry in linkedEntries {
if entry.index > index { if entry.index.messageIndex.timestamp >= index.messageIndex.timestamp {
index = ChatListIndex(pinningIndex: index.pinningIndex, messageIndex: MessageIndex(id: index.messageIndex.id, timestamp: entry.index.messageIndex.timestamp)) index = ChatListIndex(pinningIndex: index.pinningIndex, messageIndex: MessageIndex(id: index.messageIndex.id, timestamp: entry.index.messageIndex.timestamp))
messages = entry.messages messages = entry.messages
forumTopicData = entry.forumTopicData forumTopicData = entry.forumTopicData

View File

@ -452,14 +452,14 @@ final class MutableMessageHistoryView: MutablePostboxView {
switch self.peerIds { switch self.peerIds {
case let .single(peerId, threadId): case let .single(peerId, threadId):
if threadId == nil { if threadId == nil {
if let updatedData = transaction.currentUpdatedCachedPeerData[peerId] { if let updatedData = transaction.currentUpdatedCachedPeerData[peerId]?.updated {
if updatedData.associatedHistoryMessageId != nil { if updatedData.associatedHistoryMessageId != nil {
self.peerIds = .associated(peerId, updatedData.associatedHistoryMessageId) self.peerIds = .associated(peerId, updatedData.associatedHistoryMessageId)
} }
} }
} }
case let .associated(peerId, associatedId): case let .associated(peerId, associatedId):
if let updatedData = transaction.currentUpdatedCachedPeerData[peerId] { if let updatedData = transaction.currentUpdatedCachedPeerData[peerId]?.updated {
if updatedData.associatedHistoryMessageId != associatedId { if updatedData.associatedHistoryMessageId != associatedId {
self.peerIds = .associated(peerId, updatedData.associatedHistoryMessageId) self.peerIds = .associated(peerId, updatedData.associatedHistoryMessageId)
} }
@ -844,7 +844,7 @@ final class MutableMessageHistoryView: MutablePostboxView {
switch additionalDatas[i] { switch additionalDatas[i] {
case let .cachedPeerData(peerId, currentData): case let .cachedPeerData(peerId, currentData):
currentCachedPeerData = currentData currentCachedPeerData = currentData
if let updatedData = transaction.currentUpdatedCachedPeerData[peerId] { if let updatedData = transaction.currentUpdatedCachedPeerData[peerId]?.updated {
if currentData?.messageIds != updatedData.messageIds { if currentData?.messageIds != updatedData.messageIds {
updatedCachedPeerDataMessages = true updatedCachedPeerDataMessages = true
} }

View File

@ -149,7 +149,7 @@ final class MutablePeerView: MutablePostboxView {
var peersUpdated = false var peersUpdated = false
var updateMessages = false var updateMessages = false
if let cachedData = updatedCachedPeerData[self.contactPeerId], self.cachedData == nil || !self.cachedData!.isEqual(to: cachedData) { if let cachedData = updatedCachedPeerData[self.contactPeerId]?.updated, self.cachedData == nil || !self.cachedData!.isEqual(to: cachedData) {
if self.cachedData?.messageIds != cachedData.messageIds { if self.cachedData?.messageIds != cachedData.messageIds {
updateMessages = true updateMessages = true
} }
@ -286,7 +286,7 @@ final class MutablePeerView: MutablePostboxView {
} }
if let associatedPeerId = peer.associatedPeerId { if let associatedPeerId = peer.associatedPeerId {
if let value = updatedCachedPeerData[associatedPeerId] { if let value = updatedCachedPeerData[associatedPeerId]?.updated {
if let current = self.associatedCachedData[associatedPeerId] { if let current = self.associatedCachedData[associatedPeerId] {
if !current.isEqual(to: value) { if !current.isEqual(to: value) {
self.associatedCachedData[associatedPeerId] = value self.associatedCachedData[associatedPeerId] = value

View File

@ -1615,7 +1615,7 @@ final class PostboxImpl {
private var currentUpdatedPeers: [PeerId: Peer] = [:] private var currentUpdatedPeers: [PeerId: Peer] = [:]
private var currentUpdatedPeerNotificationSettings: [PeerId: (PeerNotificationSettings?, PeerNotificationSettings)] = [:] private var currentUpdatedPeerNotificationSettings: [PeerId: (PeerNotificationSettings?, PeerNotificationSettings)] = [:]
private var currentUpdatedPeerNotificationBehaviorTimestamps: [PeerId: PeerNotificationSettingsBehaviorTimestamp] = [:] private var currentUpdatedPeerNotificationBehaviorTimestamps: [PeerId: PeerNotificationSettingsBehaviorTimestamp] = [:]
private var currentUpdatedCachedPeerData: [PeerId: CachedPeerData] = [:] private var currentUpdatedCachedPeerData: [PeerId: (previous: CachedPeerData?, updated: CachedPeerData)] = [:]
private var currentUpdatedPeerPresences: [PeerId: PeerPresence] = [:] private var currentUpdatedPeerPresences: [PeerId: PeerPresence] = [:]
private var currentUpdatedPeerChatListEmbeddedStates = Set<PeerId>() private var currentUpdatedPeerChatListEmbeddedStates = Set<PeerId>()
private var currentUpdatedTotalUnreadStates: [PeerGroupId: ChatListTotalUnreadState] = [:] private var currentUpdatedTotalUnreadStates: [PeerGroupId: ChatListTotalUnreadState] = [:]
@ -2597,7 +2597,7 @@ final class PostboxImpl {
} }
private func beforeCommit(currentTransaction: Transaction) -> (updatedTransactionStateVersion: Int64?, updatedMasterClientId: Int64?) { private func beforeCommit(currentTransaction: Transaction) -> (updatedTransactionStateVersion: Int64?, updatedMasterClientId: Int64?) {
self.chatListTable.replay(postbox: self, historyOperationsByPeerId: self.currentOperationsByPeerId, updatedPeerChatListEmbeddedStates: self.currentUpdatedPeerChatListEmbeddedStates, updatedPeers: self.currentUpdatedPeers, updatedChatListInclusions: self.currentUpdatedChatListInclusions, messageHistoryTable: self.messageHistoryTable, peerChatInterfaceStateTable: self.peerChatInterfaceStateTable, operations: &self.currentChatListOperations) self.chatListTable.replay(postbox: self, historyOperationsByPeerId: self.currentOperationsByPeerId, updatedPeerChatListEmbeddedStates: self.currentUpdatedPeerChatListEmbeddedStates, updatedPeerCachedData: self.currentUpdatedCachedPeerData, updatedChatListInclusions: self.currentUpdatedChatListInclusions, messageHistoryTable: self.messageHistoryTable, peerChatInterfaceStateTable: self.peerChatInterfaceStateTable, operations: &self.currentChatListOperations)
self.peerChatTopTaggedMessageIdsTable.replay(historyOperationsByPeerId: self.currentOperationsByPeerId) self.peerChatTopTaggedMessageIdsTable.replay(historyOperationsByPeerId: self.currentOperationsByPeerId)
@ -2936,7 +2936,7 @@ final class PostboxImpl {
let currentData = self.cachedPeerDataTable.get(peerId) let currentData = self.cachedPeerDataTable.get(peerId)
if let updatedData = update(peerId, currentData) { if let updatedData = update(peerId, currentData) {
self.cachedPeerDataTable.set(id: peerId, data: updatedData) self.cachedPeerDataTable.set(id: peerId, data: updatedData)
self.currentUpdatedCachedPeerData[peerId] = updatedData self.currentUpdatedCachedPeerData[peerId] = (currentData, updatedData)
} }
} }
} }

View File

@ -9,7 +9,7 @@ final class PostboxTransaction {
let currentUpdatedPeers: [PeerId: Peer] let currentUpdatedPeers: [PeerId: Peer]
let currentUpdatedPeerNotificationSettings: [PeerId: (PeerNotificationSettings?, PeerNotificationSettings)] let currentUpdatedPeerNotificationSettings: [PeerId: (PeerNotificationSettings?, PeerNotificationSettings)]
let currentUpdatedPeerNotificationBehaviorTimestamps: [PeerId: PeerNotificationSettingsBehaviorTimestamp] let currentUpdatedPeerNotificationBehaviorTimestamps: [PeerId: PeerNotificationSettingsBehaviorTimestamp]
let currentUpdatedCachedPeerData: [PeerId: CachedPeerData] let currentUpdatedCachedPeerData: [PeerId: (previous: CachedPeerData?, updated: CachedPeerData)]
let currentUpdatedPeerPresences: [PeerId: PeerPresence] let currentUpdatedPeerPresences: [PeerId: PeerPresence]
let currentUpdatedPeerChatListEmbeddedStates: Set<PeerId> let currentUpdatedPeerChatListEmbeddedStates: Set<PeerId>
let currentUpdatedTotalUnreadStates: [PeerGroupId: ChatListTotalUnreadState] let currentUpdatedTotalUnreadStates: [PeerGroupId: ChatListTotalUnreadState]
@ -240,7 +240,7 @@ final class PostboxTransaction {
currentUpdatedPeerNotificationSettings: [PeerId: (PeerNotificationSettings?, currentUpdatedPeerNotificationSettings: [PeerId: (PeerNotificationSettings?,
PeerNotificationSettings)], PeerNotificationSettings)],
currentUpdatedPeerNotificationBehaviorTimestamps: [PeerId: PeerNotificationSettingsBehaviorTimestamp], currentUpdatedPeerNotificationBehaviorTimestamps: [PeerId: PeerNotificationSettingsBehaviorTimestamp],
currentUpdatedCachedPeerData: [PeerId: CachedPeerData], currentUpdatedCachedPeerData: [PeerId: (previous: CachedPeerData?, updated: CachedPeerData)],
currentUpdatedPeerPresences: [PeerId: PeerPresence], currentUpdatedPeerPresences: [PeerId: PeerPresence],
currentUpdatedPeerChatListEmbeddedStates: Set<PeerId>, currentUpdatedPeerChatListEmbeddedStates: Set<PeerId>,
currentUpdatedTotalUnreadStates: [PeerGroupId: ChatListTotalUnreadState], currentUpdatedTotalUnreadStates: [PeerGroupId: ChatListTotalUnreadState],

View File

@ -77,11 +77,11 @@ public final class SeedConfiguration {
public let decodeMessageThreadInfo: (CodableEntry) -> Message.AssociatedThreadInfo? public let decodeMessageThreadInfo: (CodableEntry) -> Message.AssociatedThreadInfo?
public let decodeAutoremoveTimeout: (CachedPeerData) -> Int32? public let decodeAutoremoveTimeout: (CachedPeerData) -> Int32?
public let decodeDisplayPeerAsRegularChat: (CachedPeerData) -> Bool public let decodeDisplayPeerAsRegularChat: (CachedPeerData) -> Bool
public let decodeAssociatedChatListPeerId: (CachedPeerData) -> PeerId?
public let isPeerUpgradeMessage: (Message) -> Bool public let isPeerUpgradeMessage: (Message) -> Bool
public let automaticThreadIndexInfo: (PeerId, Int64) -> StoredMessageHistoryThreadInfo? public let automaticThreadIndexInfo: (PeerId, Int64) -> StoredMessageHistoryThreadInfo?
public let customTagsFromAttributes: ([MessageAttribute]) -> [MemoryBuffer] public let customTagsFromAttributes: ([MessageAttribute]) -> [MemoryBuffer]
public let displaySavedMessagesAsTopicListPreferencesKey: ValueBoxKey public let displaySavedMessagesAsTopicListPreferencesKey: ValueBoxKey
public let chatListPeerMergeIntoTargetId: (Peer) -> PeerId?
public init( public init(
globalMessageIdsPeerIdNamespaces: Set<GlobalMessageIdsNamespace>, globalMessageIdsPeerIdNamespaces: Set<GlobalMessageIdsNamespace>,
@ -109,11 +109,11 @@ public final class SeedConfiguration {
decodeMessageThreadInfo: @escaping (CodableEntry) -> Message.AssociatedThreadInfo?, decodeMessageThreadInfo: @escaping (CodableEntry) -> Message.AssociatedThreadInfo?,
decodeAutoremoveTimeout: @escaping (CachedPeerData) -> Int32?, decodeAutoremoveTimeout: @escaping (CachedPeerData) -> Int32?,
decodeDisplayPeerAsRegularChat: @escaping (CachedPeerData) -> Bool, decodeDisplayPeerAsRegularChat: @escaping (CachedPeerData) -> Bool,
decodeAssociatedChatListPeerId: @escaping (CachedPeerData) -> PeerId?,
isPeerUpgradeMessage: @escaping (Message) -> Bool, isPeerUpgradeMessage: @escaping (Message) -> Bool,
automaticThreadIndexInfo: @escaping (PeerId, Int64) -> StoredMessageHistoryThreadInfo?, automaticThreadIndexInfo: @escaping (PeerId, Int64) -> StoredMessageHistoryThreadInfo?,
customTagsFromAttributes: @escaping ([MessageAttribute]) -> [MemoryBuffer], customTagsFromAttributes: @escaping ([MessageAttribute]) -> [MemoryBuffer],
displaySavedMessagesAsTopicListPreferencesKey: ValueBoxKey, displaySavedMessagesAsTopicListPreferencesKey: ValueBoxKey
chatListPeerMergeIntoTargetId: @escaping (Peer) -> PeerId?
) { ) {
self.globalMessageIdsPeerIdNamespaces = globalMessageIdsPeerIdNamespaces self.globalMessageIdsPeerIdNamespaces = globalMessageIdsPeerIdNamespaces
self.initializeChatListWithHole = initializeChatListWithHole self.initializeChatListWithHole = initializeChatListWithHole
@ -136,10 +136,10 @@ public final class SeedConfiguration {
self.decodeMessageThreadInfo = decodeMessageThreadInfo self.decodeMessageThreadInfo = decodeMessageThreadInfo
self.decodeAutoremoveTimeout = decodeAutoremoveTimeout self.decodeAutoremoveTimeout = decodeAutoremoveTimeout
self.decodeDisplayPeerAsRegularChat = decodeDisplayPeerAsRegularChat self.decodeDisplayPeerAsRegularChat = decodeDisplayPeerAsRegularChat
self.decodeAssociatedChatListPeerId = decodeAssociatedChatListPeerId
self.isPeerUpgradeMessage = isPeerUpgradeMessage self.isPeerUpgradeMessage = isPeerUpgradeMessage
self.automaticThreadIndexInfo = automaticThreadIndexInfo self.automaticThreadIndexInfo = automaticThreadIndexInfo
self.customTagsFromAttributes = customTagsFromAttributes self.customTagsFromAttributes = customTagsFromAttributes
self.displaySavedMessagesAsTopicListPreferencesKey = displaySavedMessagesAsTopicListPreferencesKey self.displaySavedMessagesAsTopicListPreferencesKey = displaySavedMessagesAsTopicListPreferencesKey
self.chatListPeerMergeIntoTargetId = chatListPeerMergeIntoTargetId
} }
} }

View File

@ -187,6 +187,14 @@ public let telegramPostboxSeedConfiguration: SeedConfiguration = {
} }
return false return false
}, },
decodeAssociatedChatListPeerId: { cachedData in
if let cachedData = cachedData as? CachedUserData {
if case let .known(value) = cachedData.linkedBotChannelId {
return value
}
}
return nil
},
isPeerUpgradeMessage: { message in isPeerUpgradeMessage: { message in
for media in message.media { for media in message.media {
if let action = media as? TelegramMediaAction { if let action = media as? TelegramMediaAction {
@ -245,13 +253,7 @@ public let telegramPostboxSeedConfiguration: SeedConfiguration = {
return result return result
}, },
displaySavedMessagesAsTopicListPreferencesKey: PreferencesKeys.displaySavedChatsAsTopics(), displaySavedMessagesAsTopicListPreferencesKey: PreferencesKeys.displaySavedChatsAsTopics()
chatListPeerMergeIntoTargetId: { peer in
if let peer = peer as? TelegramChannel, let linkedBotId = peer.linkedBotId {
return linkedBotId
}
return nil
}
) )
}() }()