Thread draft synchronization

This commit is contained in:
Ali
2022-10-16 17:06:30 +04:00
parent 8b84be981f
commit 7d62cee9f5
4 changed files with 61 additions and 16 deletions

View File

@@ -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)
}