Swiftgram/TelegramCore/SynchronizePinnedChatsOperation.swift
2017-02-02 11:47:45 +03:00

38 lines
1.4 KiB
Swift

import Foundation
#if os(macOS)
import PostboxMac
#else
import Postbox
#endif
final class SynchronizePinnedChatsOperation: Coding {
let previousPeerIds: [PeerId]
init(previousPeerIds: [PeerId]) {
self.previousPeerIds = previousPeerIds
}
init(decoder: Decoder) {
self.previousPeerIds = PeerId.decodeArrayFromBuffer(decoder.decodeBytesForKey("p")!)
}
func encode(_ encoder: Encoder) {
let buffer = WriteBuffer()
PeerId.encodeArrayToBuffer(self.previousPeerIds, buffer: buffer)
encoder.encodeBytes(buffer, forKey: "p")
}
}
func addSynchronizePinnedChatsOperation(modifier: Modifier) {
var updateLocalIndex: Int32?
modifier.operationLogEnumerateEntries(peerId: PeerId(namespace: 0, id: 0), tag: OperationLogTags.SynchronizePinnedChats, { entry in
updateLocalIndex = entry.tagLocalIndex
return false
})
let operationContents = SynchronizePinnedChatsOperation(previousPeerIds: modifier.getPinnedPeerIds())
if let updateLocalIndex = updateLocalIndex {
let _ = modifier.operationLogRemoveEntry(peerId: PeerId(namespace: 0, id: 0), tag: OperationLogTags.SynchronizePinnedChats, tagLocalIndex: updateLocalIndex)
}
modifier.operationLogAddEntry(peerId: PeerId(namespace: 0, id: 0), tag: OperationLogTags.SynchronizePinnedChats, tagLocalIndex: .automatic, tagMergedIndex: .automatic, contents: operationContents)
}