diff --git a/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift b/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift index da360a888a..a36674ae80 100644 --- a/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift +++ b/submodules/TelegramCore/Sources/State/AccountStateManagementUtils.swift @@ -1424,6 +1424,38 @@ private func finalStateWithUpdatesAndServerTime(postbox: Postbox, network: Netwo updatedState.addUpdateChatListFilterOrder(order: order) case let .updateDialogFilter(_, id, filter): updatedState.addUpdateChatListFilter(id: id, filter: filter) + case let .updateBotCommands(peer, botId, apiCommands): + let botPeerId = PeerId(namespace: Namespaces.Peer.CloudUser, id: PeerId.Id._internalFromInt32Value(botId)) + let commands: [BotCommand] = apiCommands.map { command in + switch command { + case let .botCommand(command, description): + return BotCommand(text: command, description: description) + } + } + updatedState.updateCachedPeerData(peer.peerId, { current in + if peer.peerId.namespace == Namespaces.Peer.CloudUser, let previous = current as? CachedUserData { + if let botInfo = previous.botInfo { + return previous.withUpdatedBotInfo(BotInfo(description: botInfo.description, commands: commands)) + } + } else if peer.peerId.namespace == Namespaces.Peer.CloudGroup, let previous = current as? CachedGroupData { + if let index = previous.botInfos.firstIndex(where: { $0.peerId == botPeerId }) { + var updatedBotInfos = previous.botInfos + let previousBotInfo = updatedBotInfos[index] + updatedBotInfos.remove(at: index) + updatedBotInfos.insert(CachedPeerBotInfo(peerId: botPeerId, botInfo: BotInfo(description: previousBotInfo.botInfo.description, commands: commands)), at: index) + return previous.withUpdatedBotInfos(updatedBotInfos) + } + } else if peer.peerId.namespace == Namespaces.Peer.CloudChannel, let previous = current as? CachedChannelData { + if let index = previous.botInfos.firstIndex(where: { $0.peerId == botPeerId }) { + var updatedBotInfos = previous.botInfos + let previousBotInfo = updatedBotInfos[index] + updatedBotInfos.remove(at: index) + updatedBotInfos.insert(CachedPeerBotInfo(peerId: botPeerId, botInfo: BotInfo(description: previousBotInfo.botInfo.description, commands: commands)), at: index) + return previous.withUpdatedBotInfos(updatedBotInfos) + } + } + return current + }) default: break }