Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Telegram/SiriIntents/IntentContacts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,33 @@ func matchingCloudContacts(postbox: Postbox, contacts: [MatchingDeviceContact])
}
}

func matchingOpenChatWithDeviceContacts(postbox: Postbox, contacts: [MatchingDeviceContact]) -> Signal<[(String, TelegramUser)], NoError> {
return postbox.transaction { transaction -> [(String, TelegramUser)] in
var result: [(String, TelegramUser)] = []
let contactPeerIds = transaction.getContactPeerIds()

outer: for peerId in transaction.chatListGetAllPeerIds(groupId: .root) {
if contactPeerIds.contains(peerId) {
continue
}
if peerId.namespace != Namespaces.Peer.CloudUser {
continue
}
guard let user = transaction.getPeer(peerId) as? TelegramUser, !user.isDeleted else {
continue
}
for contact in contacts {
if let contactPeerId = contact.peerId, contactPeerId == peerId {
result.append((contact.stableId, user))
continue outer
}
}
}

return result
}
}

func matchingCloudContact(postbox: Postbox, peerId: PeerId) -> Signal<TelegramUser?, NoError> {
return postbox.transaction { transaction -> TelegramUser? in
if let user = transaction.getPeer(peerId) as? TelegramUser {
Expand Down
13 changes: 13 additions & 0 deletions Telegram/SiriIntents/IntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,19 @@ class DefaultIntentHandler: INExtension, INSendMessageIntentHandling, INSearchFo
|> mapToSignal { account -> Signal<[(String, TelegramUser)], IntentContactsError> in
if let account = account {
return matchingCloudContacts(postbox: account.postbox, contacts: matchedContacts)
|> mapToSignal { cloudContacts -> Signal<[(String, TelegramUser)], NoError> in
let matchedStableIds = Set(cloudContacts.map { $0.0 })
let unmatchedContacts = matchedContacts.filter { !matchedStableIds.contains($0.stableId) }
if unmatchedContacts.isEmpty {
return .single(cloudContacts)
}
return matchingOpenChatWithDeviceContacts(postbox: account.postbox, contacts: unmatchedContacts)
|> map { openChatContacts -> [(String, TelegramUser)] in
var result = cloudContacts
result.append(contentsOf: openChatContacts)
return result
}
}
|> castError(IntentContactsError.self)
} else {
return .fail(.generic)
Expand Down