From 9f89633d03f980290166210bca4ef4ee3e90dd3c Mon Sep 17 00:00:00 2001 From: Peter Date: Tue, 31 Jan 2017 15:14:32 +0300 Subject: [PATCH] no message --- Postbox/MessageHistoryTable.swift | 15 +++++++++++++++ Postbox/Postbox.swift | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/Postbox/MessageHistoryTable.swift b/Postbox/MessageHistoryTable.swift index b26e9a6e20..9f67a1e93c 100644 --- a/Postbox/MessageHistoryTable.swift +++ b/Postbox/MessageHistoryTable.swift @@ -293,6 +293,11 @@ final class MessageHistoryTable: Table { } } + func clearHistory(peerId: PeerId, operationsByPeerId: inout [PeerId: [MessageHistoryOperation]], unsentMessageOperations: inout [IntermediateMessageHistoryUnsentOperation], updatedPeerReadStateOperations: inout [PeerId: PeerReadStateSynchronizationOperation?]) { + let indices = self.allIndices(peerId) + self.removeMessages(indices.map { $0.id }, operationsByPeerId: &operationsByPeerId, unsentMessageOperations: &unsentMessageOperations, updatedPeerReadStateOperations: &updatedPeerReadStateOperations) + } + func fillHole(_ id: MessageId, fillType: HoleFill, tagMask: MessageTags?, messages: [StoreMessage], operationsByPeerId: inout [PeerId: [MessageHistoryOperation]], unsentMessageOperations: inout [IntermediateMessageHistoryUnsentOperation], updatedPeerReadStateOperations: inout [PeerId: PeerReadStateSynchronizationOperation?]) { var operations: [MessageHistoryIndexOperation] = [] self.messageHistoryIndexTable.fillHole(id, fillType: fillType, tagMask: tagMask, messages: self.internalStoreMessages(messages), operations: &operations) @@ -1669,6 +1674,16 @@ final class MessageHistoryTable: Table { return messageIds } + + func allIndices(_ peerId: PeerId) -> [MessageIndex] { + var indices: [MessageIndex] = [] + self.valueBox.range(self.table, start: self.key(MessageIndex.lowerBound(peerId: peerId)).predecessor, end: self.key(MessageIndex.upperBound(peerId: peerId)).successor, keys: { key in + let index = MessageIndex(id: MessageId(peerId: PeerId(key.getInt64(0)), namespace: key.getInt32(8 + 4), id: key.getInt32(8 + 4 + 4)), timestamp: key.getInt32(8)) + indices.append(index) + return true + }, limit: 0) + return indices + } func debugList(_ peerId: PeerId, peerTable: PeerTable) -> [RenderedMessageHistoryEntry] { var operationsByPeerId: [PeerId : [MessageHistoryOperation]] = [:] diff --git a/Postbox/Postbox.swift b/Postbox/Postbox.swift index 77f22c4739..5d36794e2e 100644 --- a/Postbox/Postbox.swift +++ b/Postbox/Postbox.swift @@ -47,6 +47,10 @@ public final class Modifier { self.postbox?.deleteMessages(messageIds) } + public func clearHistory(_ peerId: PeerId) { + self.postbox?.clearHistory(peerId) + } + public func deleteMessagesWithGlobalIds(_ ids: [Int32]) { if let postbox = self.postbox { let messageIds = postbox.messageIdsForGlobalIds(ids) @@ -753,6 +757,10 @@ public final class Postbox { self.messageHistoryTable.removeMessages(messageIds, operationsByPeerId: &self.currentOperationsByPeerId, unsentMessageOperations: ¤tUnsentOperations, updatedPeerReadStateOperations: &self.currentUpdatedSynchronizeReadStateOperations) } + fileprivate func clearHistory(_ peerId: PeerId) { + self.messageHistoryTable.clearHistory(peerId: peerId, operationsByPeerId: &self.currentOperationsByPeerId, unsentMessageOperations: ¤tUnsentOperations, updatedPeerReadStateOperations: &self.currentUpdatedSynchronizeReadStateOperations) + } + fileprivate func resetIncomingReadStates(_ states: [PeerId: [MessageId.Namespace: PeerReadState]]) { self.messageHistoryTable.resetIncomingReadStates(states, operationsByPeerId: &self.currentOperationsByPeerId, updatedPeerReadStateOperations: &self.currentUpdatedSynchronizeReadStateOperations) }