diff --git a/Telegram/SiriIntents/IntentContacts.swift b/Telegram/SiriIntents/IntentContacts.swift index bc9e10590c2..f702c30f7e6 100644 --- a/Telegram/SiriIntents/IntentContacts.swift +++ b/Telegram/SiriIntents/IntentContacts.swift @@ -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 { return postbox.transaction { transaction -> TelegramUser? in if let user = transaction.getPeer(peerId) as? TelegramUser { diff --git a/Telegram/SiriIntents/IntentHandler.swift b/Telegram/SiriIntents/IntentHandler.swift index 46395e888b4..2447f2ef4dd 100644 --- a/Telegram/SiriIntents/IntentHandler.swift +++ b/Telegram/SiriIntents/IntentHandler.swift @@ -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)