Search Local Peers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
fileprivate func searchPeers(query: String) -> [RenderedPeer] {
var peerIds = Set<PeerId>()
var chatPeers: [RenderedPeer] = []
// search from peerNameIndexTable
var (chatPeerIds, contactPeerIds) = self.peerNameIndexTable.matchingPeerIds(tokens: (regular: stringIndexTokens(query, transliteration: .none), transliterated: stringIndexTokens(query, transliteration: .transliterated)), categories: [.chats, .contacts], chatListIndexTable: self.chatListIndexTable, contactTable: self.contactsTable)
// search reverse associated peerIds from reverseAssociatedPeerTable
var additionalChatPeerIds: [PeerId] = []
for peerId in chatPeerIds {
for associatedId in self.reverseAssociatedPeerTable.get(peerId: peerId) {
let inclusionIndex = self.chatListIndexTable.get(peerId: associatedId)
if inclusionIndex.includedIndex(peerId: associatedId) != nil {
additionalChatPeerIds.append(associatedId)
}
}
}
chatPeerIds.append(contentsOf: additionalChatPeerIds)
// prepare peers
for peerId in chatPeerIds {
if let peer = self.peerTable.get(peerId) {
var peers = SimpleDictionary<PeerId, Peer>()
peers[peer.id] = peer
// prepare associated peer
if let associatedPeerId = peer.associatedPeerId {
if let associatedPeer = self.peerTable.get(associatedPeerId) {
peers[associatedPeer.id] = associatedPeer
}
}
chatPeers.append(RenderedPeer(peerId: peer.id, peers: peers))
peerIds.insert(peerId)
}
}
// prepare contacts
var contactPeers: [RenderedPeer] = []
for peerId in contactPeerIds {
if !peerIds.contains(peerId) {
if let peer = self.peerTable.get(peerId) {
var peers = SimpleDictionary<PeerId, Peer>()
peers[peer.id] = peer
contactPeers.append(RenderedPeer(peerId: peer.id, peers: peers))
}
}
}
contactPeers.sort(by: { lhs, rhs in
lhs.peers[lhs.peerId]!.indexName.indexName(.lastNameFirst) < rhs.peers[rhs.peerId]!.indexName.indexName(.lastNameFirst)
})
return chatPeers + contactPeers
}
Seach Remote Peers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
func _internal_searchPeers(account: Account, query: String) -> Signal<([FoundPeer], [FoundPeer]), NoError> {
let searchResult = account.network.request(Api.functions.contacts.search(q: query, limit: 20), automaticFloodWait: false)
|> map(Optional.init)
|> `catch` { _ in
return Signal<Api.contacts.Found?, NoError>.single(nil)
}
let processedSearchResult = searchResult
|> mapToSignal { result -> Signal<([FoundPeer], [FoundPeer]), NoError> in
if let result = result {
switch result {
case let .found(myResults, results, chats, users):
return account.postbox.transaction { transaction -> ([FoundPeer], [FoundPeer]) in
var peers: [PeerId: Peer] = [:]
var subscribers: [PeerId: Int32] = [:]
// handle users
for user in users {
if let user = TelegramUser.merge(transaction.getPeer(user.peerId) as? TelegramUser, rhs: user) {
peers[user.id] = user
}
}
// handle chats
for chat in chats {
if let groupOrChannel = parseTelegramGroupOrChannel(chat: chat) {
peers[groupOrChannel.id] = groupOrChannel
switch chat {
/*feed*/
case let .channel(_, _, _, _, _, _, _, _, _, _, _, participantsCount):
if let participantsCount = participantsCount {
// participants count
subscribers[groupOrChannel.id] = participantsCount
}
default:
break
}
}
}
// update database
updatePeers(transaction: transaction, peers: Array(peers.values), update: { _, updated in
return updated
})
// prepare myResult
var renderedMyPeers: [FoundPeer] = []
for result in myResults {
let peerId: PeerId = result.peerId
if let peer = peers[peerId] {
if let group = peer as? TelegramGroup, group.migrationReference != nil {
continue
}
renderedMyPeers.append(FoundPeer(peer: peer, subscribers: subscribers[peerId]))
}
}
// prepare results
var renderedPeers: [FoundPeer] = []
for result in results {
let peerId: PeerId = result.peerId
if let peer = peers[peerId] {
if let group = peer as? TelegramGroup, group.migrationReference != nil {
continue
}
renderedPeers.append(FoundPeer(peer: peer, subscribers: subscribers[peerId]))
}
}
return (renderedMyPeers, renderedPeers)
}
}
} else {
return .single(([], []))
}
}
return processedSearchResult
}