[WIP] Saved messages

This commit is contained in:
Isaac
2023-12-25 22:58:09 +04:00
parent 4651c39083
commit 0e75f18f14
65 changed files with 934 additions and 369 deletions

View File

@@ -1074,6 +1074,11 @@ public final class Transaction {
assert(!self.disposed)
self.postbox?.scanMessages(peerId: peerId, namespace: namespace, tag: tag, f)
}
public func scanMessages(peerId: PeerId, threadId: Int64, namespace: MessageId.Namespace, tag: MessageTags, _ f: (Message) -> Bool) {
assert(!self.disposed)
self.postbox?.scanMessages(peerId: peerId, threadId: threadId, namespace: namespace, tag: tag, f)
}
public func scanTopMessages(peerId: PeerId, namespace: MessageId.Namespace, limit: Int, _ f: (Message) -> Bool) {
assert(!self.disposed)
@@ -1336,6 +1341,26 @@ public final class Transaction {
public func getPeerStoryStats(peerId: PeerId) -> PeerStoryStats? {
return fetchPeerStoryStats(postbox: self.postbox!, peerId: peerId)
}
public func searchSubPeers(peerId: PeerId, query: String, indexNameMapping: [PeerId: [PeerIndexNameRepresentation]]) -> [Peer] {
let allThreads = self.postbox!.messageHistoryThreadIndexTable.getAll(peerId: peerId)
var matchingPeers: [(Peer, MessageIndex)] = []
for (threadId, index, _) in allThreads {
if let peer = self.postbox!.peerTable.get(PeerId(threadId)) {
if let mappings = indexNameMapping[peer.id] {
inner: for mapping in mappings {
if mapping.matchesByTokens(query) {
matchingPeers.append((peer, index))
break inner
}
}
} else if peer.indexName.matchesByTokens(query) {
matchingPeers.append((peer, index))
}
}
}
return matchingPeers.sorted(by: { $0.1 > $1.1 }).map(\.0)
}
}
public enum PostboxResult {
@@ -3937,6 +3962,28 @@ final class PostboxImpl {
}
}
}
fileprivate func scanMessages(peerId: PeerId, threadId: Int64, namespace: MessageId.Namespace, tag: MessageTags, _ f: (Message) -> Bool) {
var index = MessageIndex.lowerBound(peerId: peerId, namespace: namespace)
while true {
let indices = self.messageHistoryThreadTagsTable.laterIndices(tag: tag, threadId: threadId, peerId: peerId, namespace: namespace, index: index, includeFrom: false, count: 10)
for index in indices {
if let message = self.messageHistoryTable.getMessage(index) {
if !f(self.renderIntermediateMessage(message)) {
break
}
} else {
assertionFailure()
break
}
}
if let last = indices.last {
index = last
} else {
break
}
}
}
fileprivate func scanTopMessages(peerId: PeerId, namespace: MessageId.Namespace, limit: Int, _ f: (Message) -> Bool) {
let lowerBound = MessageIndex.lowerBound(peerId: peerId, namespace: namespace)