diff --git a/submodules/TelegramCore/Sources/State/ManagedSynchronizeChatInputStateOperations.swift b/submodules/TelegramCore/Sources/State/ManagedSynchronizeChatInputStateOperations.swift index 1641156675..549f85b092 100644 --- a/submodules/TelegramCore/Sources/State/ManagedSynchronizeChatInputStateOperations.swift +++ b/submodules/TelegramCore/Sources/State/ManagedSynchronizeChatInputStateOperations.swift @@ -93,7 +93,7 @@ func managedSynchronizeChatInputStateOperations(postbox: Postbox, network: Netwo let signal = withTakenOperation(postbox: postbox, peerId: entry.peerId, tag: tag, tagLocalIndex: entry.tagLocalIndex, { transaction, entry -> Signal in if let entry = entry { if let operation = entry.contents as? SynchronizeChatInputStateOperation { - return synchronizeChatInputState(transaction: transaction, postbox: postbox, network: network, peerId: entry.peerId, operation: operation) + return synchronizeChatInputState(transaction: transaction, postbox: postbox, network: network, peerId: entry.peerId, threadId: operation.threadId, operation: operation) } else { assertionFailure() } @@ -125,9 +125,16 @@ func managedSynchronizeChatInputStateOperations(postbox: Postbox, network: Netwo } } -private func synchronizeChatInputState(transaction: Transaction, postbox: Postbox, network: Network, peerId: PeerId, operation: SynchronizeChatInputStateOperation) -> Signal { +private func synchronizeChatInputState(transaction: Transaction, postbox: Postbox, network: Network, peerId: PeerId, threadId: Int64?, operation: SynchronizeChatInputStateOperation) -> Signal { var inputState: SynchronizeableChatInputState? - if let peerChatInterfaceState = transaction.getPeerChatInterfaceState(peerId), let data = peerChatInterfaceState.data { + let peerChatInterfaceState: StoredPeerChatInterfaceState? + if let threadId = threadId { + peerChatInterfaceState = transaction.getPeerChatThreadInterfaceState(peerId, threadId: threadId) + } else { + peerChatInterfaceState = transaction.getPeerChatInterfaceState(peerId) + } + + if let peerChatInterfaceState = peerChatInterfaceState, let data = peerChatInterfaceState.data { inputState = (try? AdaptedPostboxDecoder().decode(InternalChatInterfaceState.self, from: data))?.synchronizeableInputState } @@ -141,7 +148,12 @@ private func synchronizeChatInputState(transaction: Transaction, postbox: Postbo flags |= (1 << 3) } } - return network.request(Api.functions.messages.saveDraft(flags: flags, replyToMsgId: inputState?.replyToMessageId?.id, topMsgId: nil, peer: inputPeer, message: inputState?.text ?? "", entities: apiEntitiesFromMessageTextEntities(inputState?.entities ?? [], associatedPeers: SimpleDictionary()))) + var topMsgId: Int32? + if let threadId = threadId { + flags |= (1 << 2) + topMsgId = Int32(clamping: threadId) + } + return network.request(Api.functions.messages.saveDraft(flags: flags, replyToMsgId: inputState?.replyToMessageId?.id, topMsgId: topMsgId, peer: inputPeer, message: inputState?.text ?? "", entities: apiEntitiesFromMessageTextEntities(inputState?.entities ?? [], associatedPeers: SimpleDictionary()))) |> delay(2.0, queue: Queue.concurrentDefaultQueue()) |> `catch` { _ -> Signal in return .single(.boolFalse) diff --git a/submodules/TelegramCore/Sources/State/SynchronizeChatInputStateOperation.swift b/submodules/TelegramCore/Sources/State/SynchronizeChatInputStateOperation.swift index 544d797775..b0721c6114 100644 --- a/submodules/TelegramCore/Sources/State/SynchronizeChatInputStateOperation.swift +++ b/submodules/TelegramCore/Sources/State/SynchronizeChatInputStateOperation.swift @@ -1,27 +1,41 @@ import Foundation import Postbox -func addSynchronizeChatInputStateOperation(transaction: Transaction, peerId: PeerId) { - var updateLocalIndex: Int32? +func addSynchronizeChatInputStateOperation(transaction: Transaction, peerId: PeerId, threadId: Int64?) { + var removeLocalIndices: [Int32] = [] let tag: PeerOperationLogTag = OperationLogTags.SynchronizeChatInputStates var previousOperation: SynchronizeChatInputStateOperation? transaction.operationLogEnumerateEntries(peerId: peerId, tag: tag, { entry in - updateLocalIndex = entry.tagLocalIndex if let operation = entry.contents as? SynchronizeChatInputStateOperation { - previousOperation = operation + if operation.threadId == threadId { + previousOperation = operation + removeLocalIndices.append(entry.tagLocalIndex) + return false + } + } else { + removeLocalIndices.append(entry.tagLocalIndex) } - return false + return true }) var previousState: SynchronizeableChatInputState? if let previousOperation = previousOperation { previousState = previousOperation.previousState - } else if let peerChatInterfaceState = transaction.getPeerChatInterfaceState(peerId), let data = peerChatInterfaceState.data { - previousState = (try? AdaptedPostboxDecoder().decode(InternalChatInterfaceState.self, from: data))?.synchronizeableInputState + } else { + let peerChatInterfaceState: StoredPeerChatInterfaceState? + if let threadId = threadId { + peerChatInterfaceState = transaction.getPeerChatThreadInterfaceState(peerId, threadId: threadId) + } else { + peerChatInterfaceState = transaction.getPeerChatInterfaceState(peerId) + } + + if let peerChatInterfaceState = peerChatInterfaceState, let data = peerChatInterfaceState.data { + previousState = (try? AdaptedPostboxDecoder().decode(InternalChatInterfaceState.self, from: data))?.synchronizeableInputState + } } - let operationContents = SynchronizeChatInputStateOperation(previousState: previousState) - if let updateLocalIndex = updateLocalIndex { - let _ = transaction.operationLogRemoveEntry(peerId: peerId, tag: tag, tagLocalIndex: updateLocalIndex) + let operationContents = SynchronizeChatInputStateOperation(previousState: previousState, threadId: threadId) + for index in removeLocalIndices { + let _ = transaction.operationLogRemoveEntry(peerId: peerId, tag: tag, tagLocalIndex: index) } transaction.operationLogAddEntry(peerId: peerId, tag: tag, tagLocalIndex: .automatic, tagMergedIndex: .automatic, contents: operationContents) } diff --git a/submodules/TelegramCore/Sources/SyncCore/SyncCore_SynchronizeChatInputStateOperation.swift b/submodules/TelegramCore/Sources/SyncCore/SyncCore_SynchronizeChatInputStateOperation.swift index 7f3ad4bb48..325fec2c39 100644 --- a/submodules/TelegramCore/Sources/SyncCore/SyncCore_SynchronizeChatInputStateOperation.swift +++ b/submodules/TelegramCore/Sources/SyncCore/SyncCore_SynchronizeChatInputStateOperation.swift @@ -2,13 +2,16 @@ import Postbox public final class SynchronizeChatInputStateOperation: PostboxCoding { public let previousState: SynchronizeableChatInputState? + public let threadId: Int64? - public init(previousState: SynchronizeableChatInputState?) { + public init(previousState: SynchronizeableChatInputState?, threadId: Int64?) { self.previousState = previousState + self.threadId = threadId } public init(decoder: PostboxDecoder) { self.previousState = decoder.decode(SynchronizeableChatInputState.self, forKey: "p") + self.threadId = decoder.decodeOptionalInt64ForKey("threadId") } public func encode(_ encoder: PostboxEncoder) { @@ -17,5 +20,10 @@ public final class SynchronizeChatInputStateOperation: PostboxCoding { } else { encoder.encodeNil(forKey: "p") } + if let threadId = self.threadId { + encoder.encodeInt64(threadId, forKey: "threadId") + } else { + encoder.encodeNil(forKey: "threadId") + } } } diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift index c1d5488e0c..f648e7956b 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Peers/TelegramEnginePeers.swift @@ -689,6 +689,17 @@ public extension TelegramEngine { ) if let threadId = threadId { + var currentInputState: SynchronizeableChatInputState? + if let peerChatInterfaceState = transaction.getPeerChatThreadInterfaceState(peerId, threadId: threadId), let data = peerChatInterfaceState.data { + currentInputState = (try? AdaptedPostboxDecoder().decode(InternalChatInterfaceState.self, from: data))?.synchronizeableInputState + } + let updatedInputState = state.synchronizeableInputState + + if currentInputState != updatedInputState { + if peerId.namespace == Namespaces.Peer.CloudUser || peerId.namespace == Namespaces.Peer.CloudChannel || peerId.namespace == Namespaces.Peer.CloudGroup { + addSynchronizeChatInputStateOperation(transaction: transaction, peerId: peerId, threadId: threadId) + } + } transaction.setPeerChatThreadInterfaceState(peerId, threadId: threadId, state: storedState) } else { var currentInputState: SynchronizeableChatInputState? @@ -699,7 +710,7 @@ public extension TelegramEngine { if currentInputState != updatedInputState { if peerId.namespace == Namespaces.Peer.CloudUser || peerId.namespace == Namespaces.Peer.CloudChannel || peerId.namespace == Namespaces.Peer.CloudGroup { - addSynchronizeChatInputStateOperation(transaction: transaction, peerId: peerId) + addSynchronizeChatInputStateOperation(transaction: transaction, peerId: peerId, threadId: nil) } } transaction.setPeerChatInterfaceState(