From b9b6df4dfd3e344f4d5d4d1af7f0f5cc9cf4f4a1 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Mon, 29 Jul 2019 13:47:11 +0300 Subject: [PATCH] Fixed CarPlay reply action --- SiriIntents/IntentContacts.swift | 10 ++++++++++ SiriIntents/IntentHandler.swift | 34 +++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/SiriIntents/IntentContacts.swift b/SiriIntents/IntentContacts.swift index c16680033b..19ae8cf1e6 100644 --- a/SiriIntents/IntentContacts.swift +++ b/SiriIntents/IntentContacts.swift @@ -53,6 +53,16 @@ func matchingCloudContacts(postbox: Postbox, contacts: [MatchingDeviceContact]) } } +func matchingCloudContact(postbox: Postbox, peerId: PeerId) -> Signal { + return postbox.transaction { transaction -> TelegramUser? in + if let user = transaction.getPeer(peerId) as? TelegramUser { + return user + } else { + return nil + } + } +} + func personWithUser(stableId: String, user: TelegramUser) -> INPerson { var nameComponents = PersonNameComponents() nameComponents.givenName = user.firstName diff --git a/SiriIntents/IntentHandler.swift b/SiriIntents/IntentHandler.swift index 718db75ae3..c74f61d180 100644 --- a/SiriIntents/IntentHandler.swift +++ b/SiriIntents/IntentHandler.swift @@ -120,6 +120,9 @@ class IntentHandler: INExtension, INSendMessageIntentHandling, INSearchForMessag return .complete() } } + |> afterNext { account in + account.resetStateManagement() + } |> take(1) } self.accountPromise.set(account) @@ -222,7 +225,36 @@ class IntentHandler: INExtension, INSendMessageIntentHandling, INSearchForMessag // MARK: - INSendMessageIntentHandling func resolveRecipients(for intent: INSendMessageIntent, with completion: @escaping ([INPersonResolutionResult]) -> Void) { - self.resolve(persons: intent.recipients, with: completion) + if #available(iOSApplicationExtension 11.0, *) { + if let peerId = intent.conversationIdentifier.flatMap(Int64.init) { + let account = self.accountPromise.get() + + let signal = account + |> mapToSignal { account -> Signal in + return matchingCloudContact(postbox: account.postbox, peerId: PeerId(peerId)) + |> map { user -> INPerson? in + if let user = user { + return personWithUser(stableId: "tg\(peerId)", user: user) + } else { + return nil + } + } + } + + self.resolvePersonsDisposable.set((signal + |> deliverOnMainQueue).start(next: { person in + if let person = person { + completion([INPersonResolutionResult.success(with: person)]) + } else { + completion([INPersonResolutionResult.needsValue()]) + } + })) + } else { + self.resolve(persons: intent.recipients, with: completion) + } + } else { + self.resolve(persons: intent.recipients, with: completion) + } } func resolveContent(for intent: INSendMessageIntent, with completion: @escaping (INStringResolutionResult) -> Void) {