diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/PendingStoryManager.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/PendingStoryManager.swift index cf8b471f2c..f78a3a6aa6 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/PendingStoryManager.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/PendingStoryManager.swift @@ -460,7 +460,7 @@ final class PendingStoryManager { }) } else { pendingItemContext.disposable = (_internal_uploadStoryImpl(postbox: self.postbox, network: self.network, accountPeerId: self.accountPeerId, stateManager: self.stateManager, messageMediaPreuploadManager: self.messageMediaPreuploadManager, revalidationContext: self.revalidationContext, auxiliaryMethods: self.auxiliaryMethods, toPeerId: toPeerId, stableId: stableId, media: firstItem.media, mediaAreas: firstItem.mediaAreas, text: firstItem.text, entities: firstItem.entities, embeddedStickers: firstItem.embeddedStickers, pin: firstItem.pin, privacy: firstItem.privacy, isForwardingDisabled: firstItem.isForwardingDisabled, period: Int(firstItem.period), randomId: firstItem.randomId, forwardInfo: firstItem.forwardInfo) - |> deliverOn(self.queue)).start(next: { [weak self] event in + |> deliverOn(self.queue)).start(next: { [weak self] event in guard let `self` = self else { return } diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/Stories.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/Stories.swift index 0ff890c31b..d26c411426 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/Stories.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/Stories.swift @@ -1028,6 +1028,7 @@ private struct PendingStoryIdMappingKey: Hashable { } private let pendingStoryIdMapping = Atomic<[PendingStoryIdMappingKey: Int32]>(value: [:]) +private let pendingBotPreviewIdMapping = Atomic<[PendingStoryIdMappingKey: MediaId]>(value: [:]) func _internal_lookUpPendingStoryIdMapping(peerId: PeerId, stableId: Int32) -> Int32? { return pendingStoryIdMapping.with { dict in @@ -1045,6 +1046,22 @@ private func _internal_putPendingStoryIdMapping(peerId: PeerId, stableId: Int32, } } +func _internal_lookUpPendingBotPreviewIdMapping(peerId: PeerId, stableId: Int32) -> MediaId? { + return pendingBotPreviewIdMapping.with { dict in + return dict[PendingStoryIdMappingKey(peerId: peerId, stableId: stableId)] + } +} + +private func _internal_putPendingBotPreviewIdMapping(peerId: PeerId, stableId: Int32, id: MediaId) { + let _ = pendingBotPreviewIdMapping.modify { dict in + var dict = dict + + dict[PendingStoryIdMappingKey(peerId: peerId, stableId: stableId)] = id + + return dict + } +} + func _internal_uploadStoryImpl( postbox: Postbox, network: Network, @@ -1329,6 +1346,10 @@ func _internal_uploadBotPreviewImpl( let addedItem = CachedUserData.BotPreview.Item(media: resultMediaValue, timestamp: date) + if let mediaId = resultMediaValue.id { + _internal_putPendingBotPreviewIdMapping(peerId: toPeerId, stableId: stableId, id: mediaId) + } + if language == nil { transaction.updatePeerCachedData(peerIds: Set([toPeerId]), update: { _, current in guard var current = current as? CachedUserData else { diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/StoryListContext.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/StoryListContext.swift index d92634a4df..e22036f50a 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/StoryListContext.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/StoryListContext.swift @@ -2196,6 +2196,13 @@ public final class BotPreviewStoryListContext: StoryListContext { self.nextId += 1 self.pendingIdMapping[item.stableId] = mappedId } + + if let mediaId = _internal_lookUpPendingBotPreviewIdMapping(peerId: self.peerId, stableId: item.stableId) { + if let botPreview, botPreview.items.contains(where: { $0.media.id == mediaId }) { + continue + } + } + if case let .botPreview(itemPeerId, itemLanguage) = item.target, itemPeerId == peerId, itemLanguage == language { items.append(State.Item( id: StoryId(peerId: peerId, id: mappedId), @@ -2551,8 +2558,24 @@ public final class BotPreviewStoryListContext: StoryListContext { } private func pushLanguageItems() { - var items = self.localItems + var items: [State.Item] = [] + for item in self.localItems { + var stableId: Int32? + inner: for (from, to) in self.pendingIdMapping { + if to == item.id.id { + stableId = from + break inner + } + } + if let stableId, let mediaId = _internal_lookUpPendingBotPreviewIdMapping(peerId: self.peerId, stableId: stableId) { + if self.remoteItems.contains(where: { $0.storyItem.media.id == mediaId }) { + continue + } + } + items.append(item) + } items.append(contentsOf: self.remoteItems) + self.stateValue = State( peerReference: self.stateValue.peerReference, items: items,