Target SiriIntents: Split IntentHandler into smaller files (#6203)

* Add protocol `ContactResolving`

* Let the `IntentHandler` implement `ContactResolving` (#6203)

Nothing has changed about the implementation itself; this prepares
the separation of this logic into a dedicated unit.

* Prepare the separation of the contact resolver from the intents handler (#6203)

* Move the implementation of `ContactResolving` to a dedicated class (#6203)

* Move `ContactResolver` to a dedicated file (#6203)

* Prepare the separation of the `StartAudioCallIntentHandler` from `IntentsHandler` (#6203)

* Move the implementation of `INStartAudioCallIntentHandling` to a dedicated class (#6203)

* Prepare the separation of the `StartVideoCallIntentHandler` from `IntentsHandler` (#6203)

* Move the implementation of `INStartVideoCallIntentHandling` to a dedicated class (#6203)

* Prepare the separation of the `SendMessageIntentHandler` from `IntentsHandler` (#6203)

* Move the implementation of `INSendMessageIntentHandling` to a dedicated class (#6203)

* Remove unused property (#6203)

* Return `nil` if the requested intent cannot be handled (#6203)

* Initialize the intent handlers _after_ everything else is configured (#6203)

In `init()`, there might be some configuration being done that is
required for the handlers.

* Add changelog entry

* Move curly braces in Objective-C to dedicated lines

This ensures that the code follows the style that is present in other Objective-C files.

Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com>

* Inject the `ContactResolver` into the intent handlers during initialization

In #6365, @pixlwave pointed out that

> If the resolver ever gained a cache or stored properties it would
> help keep the memory usage down in the extension

* Prefer forward-declaration over import in Objective-C header files

Per @pixlwave, this helps prevent build failures:

> We had random cycle errors while building a while back and it was
> eventually solved by removing all imports of `GeneratedInterface-Swift.h`
> in every [Objective-C header] file.

Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com>
This commit is contained in:
wtimme
2022-07-06 10:39:38 +02:00
committed by GitHub
parent b9e52d063b
commit e2c62179a7
11 changed files with 674 additions and 378 deletions

View File

@@ -0,0 +1,26 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <Foundation/Foundation.h>
#import "GeneratedInterface-Swift.h"
NS_ASSUME_NONNULL_BEGIN
@interface ContactResolver: NSObject <ContactResolving>
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,177 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "ContactResolver.h"
@import Intents;
#import "MXKAccountManager.h"
@implementation ContactResolver
- (void)resolveContacts:(nullable NSArray<INPerson *> *)contacts
withCompletion:(void (^)(NSArray<INPersonResolutionResult *> * _Nonnull))completion
{
if (contacts.count == 0)
{
completion(@[[INPersonResolutionResult needsValue]]);
return;
}
else
{
// We don't iterate over array of contacts from passed intent
// since it's hard to imagine scenario with several callee
// so we just extract the first one
INPerson *callee = contacts.firstObject;
// If this method is called after selection of the appropriate user, it will hold userId of an user to whom we must call
NSString *selectedUserId;
// Check if the user has selected right room among several direct rooms from previous resolution process run
if (callee.customIdentifier.length)
{
// If callee will have the same name as one of the contact in the system contacts app
// Siri will pass us this contact in the intent.contacts array and we must provide the same count of
// resolution results as elements count in the intent.contact.
// So we just pass the same result at all iterations
NSMutableArray *resolutionResults = [NSMutableArray array];
for (NSInteger i = 0; i < contacts.count; ++i)
[resolutionResults addObject:[INPersonResolutionResult successWithResolvedPerson:callee]];
completion(resolutionResults);
return;
}
else
{
// This resolution process run after selecting appropriate user among suggested user list
selectedUserId = callee.personHandle.value;
}
MXKAccount *account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
if (account)
{
MXFileStore *fileStore = [[MXFileStore alloc] initWithCredentials:account.mxCredentials];
[fileStore.roomSummaryStore fetchAllSummaries:^(NSArray<id<MXRoomSummaryProtocol>> * _Nonnull summaries) {
// Contains userIds of all users with whom the current user has direct chats
// Use set to avoid duplicates
NSMutableSet<NSString *> *directUserIds = [NSMutableSet set];
// Contains room summaries for all direct rooms connected with particular userId
NSMutableDictionary<NSString *, NSMutableArray<id<MXRoomSummaryProtocol>> *> *roomSummaries = [NSMutableDictionary dictionary];
for (id<MXRoomSummaryProtocol> summary in summaries)
{
// TODO: We also need to check if joined room members count equals 2
// It is pointlessly to save rooms with 1 joined member or room with more than 2 joined members
if (summary.isDirect)
{
NSString *directUserId = summary.directUserId;
// Collect room summaries only for specified user
if (selectedUserId && ![directUserId isEqualToString:selectedUserId])
continue;
// Save userId
[directUserIds addObject:directUserId];
// Save associated with diretUserId room summary
NSMutableArray<id<MXRoomSummaryProtocol>> *userRoomSummaries = roomSummaries[directUserId];
if (userRoomSummaries)
[userRoomSummaries addObject:summary];
else
roomSummaries[directUserId] = [NSMutableArray arrayWithObject:summary];
}
}
[fileStore asyncUsersWithUserIds:directUserIds.allObjects success:^(NSArray<MXUser *> * _Nonnull users) {
// Find users whose display name contains string presented us by Siri
NSMutableArray<MXUser *> *matchingUsers = [NSMutableArray array];
for (MXUser *user in users)
{
if (!user.displayname)
continue;
if (!NSEqualRanges([callee.displayName rangeOfString:user.displayname options:NSCaseInsensitiveSearch], (NSRange){NSNotFound,0}))
{
[matchingUsers addObject:user];
}
}
NSMutableArray<INPerson *> *persons = [NSMutableArray array];
if (matchingUsers.count == 1)
{
MXUser *user = matchingUsers.firstObject;
// Provide to the user a list of direct rooms to choose from
NSArray<id<MXRoomSummaryProtocol>> *summaries = roomSummaries[user.userId];
for (id<MXRoomSummaryProtocol> summary in summaries)
{
INPersonHandle *personHandle = [[INPersonHandle alloc] initWithValue:user.userId type:INPersonHandleTypeUnknown];
// For rooms we try to use room display name
NSString *displayName = summary.displayname ? summary.displayname : user.displayname;
INPerson *person = [[INPerson alloc] initWithPersonHandle:personHandle
nameComponents:nil
displayName:displayName
image:nil
contactIdentifier:nil
customIdentifier:summary.roomId];
[persons addObject:person];
}
}
else if (matchingUsers.count > 1)
{
// Provide to the user a list of users to choose from
// This is the case when there are several users with the same name
for (MXUser *user in matchingUsers)
{
INPersonHandle *personHandle = [[INPersonHandle alloc] initWithValue:user.userId type:INPersonHandleTypeUnknown];
INPerson *person = [[INPerson alloc] initWithPersonHandle:personHandle
nameComponents:nil
displayName:user.displayname
image:nil
contactIdentifier:nil
customIdentifier:nil];
[persons addObject:person];
}
}
if (persons.count == 0)
{
completion(@[[INPersonResolutionResult unsupported]]);
}
else if (persons.count == 1)
{
completion(@[[INPersonResolutionResult successWithResolvedPerson:persons.firstObject]]);
}
else
{
completion(@[[INPersonResolutionResult disambiguationWithPeopleToDisambiguate:persons]]);
}
} failure:nil];
}];
}
else
{
completion(@[[INPersonResolutionResult notRequired]]);
}
}
}
@end

View File

@@ -0,0 +1,22 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Intents
@objc protocol ContactResolving {
func resolveContacts(_ contacts: [INPerson]?,
withCompletion completion: @escaping ([INPersonResolutionResult]) -> Void)
}

View File

@@ -18,21 +18,23 @@
#import "GeneratedInterface-Swift.h"
#import "MXKAccountManager.h"
#import "ContactResolver.h"
#import "StartAudioCallIntentHandler.h"
#import "StartVideoCallIntentHandler.h"
#import "SendMessageIntentHandler.h"
#if __has_include(<MatrixSDK/MXJingleCallStack.h>)
#define CALL_STACK_JINGLE
#endif
@interface IntentHandler () <INStartAudioCallIntentHandling, INStartVideoCallIntentHandling, INSendMessageIntentHandling>
@interface IntentHandler ()
// Build Settings
@property (nonatomic) id<Configurable> configuration;
/**
The room that is currently being used to send a message. This is to ensure a
strong ref is maintained on the `MXRoom` until sending has completed.
*/
@property (nonatomic) MXRoom *selectedRoom;
@property (nonatomic) id<INStartAudioCallIntentHandling> startAudioCallIntentHandler;
@property (nonatomic) id<INStartVideoCallIntentHandling> startVideoCallIntentHandler;
@property (nonatomic) id<INSendMessageIntentHandling> sendMessageIntentHandler;
@end
@@ -65,386 +67,26 @@
Analytics *analytics = Analytics.shared;
[MXSDKOptions sharedInstance].analyticsDelegate = analytics;
[analytics startIfEnabled];
id<ContactResolving> contactResolver = [[ContactResolver alloc] init];
_startAudioCallIntentHandler = [[StartAudioCallIntentHandler alloc] initWithContactResolver:contactResolver];
_startVideoCallIntentHandler = [[StartVideoCallIntentHandler alloc] initWithContactResolver:contactResolver];
_sendMessageIntentHandler = [[SendMessageIntentHandler alloc] initWithContactResolver:contactResolver];
}
return self;
}
- (id)handlerForIntent:(INIntent *)intent
{
return self;
}
#pragma mark - INStartAudioCallIntentHandling
- (void)resolveContactsForStartAudioCall:(INStartAudioCallIntent *)intent withCompletion:(void (^)(NSArray<INPersonResolutionResult *> * _Nonnull))completion
{
[self resolveContacts:intent.contacts withCompletion:completion];
}
- (void)confirmStartAudioCall:(INStartAudioCallIntent *)intent completion:(void (^)(INStartAudioCallIntentResponse * _Nonnull))completion
{
INStartAudioCallIntentResponse *response = nil;
MXKAccount *account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
if (account)
{
#if defined MX_CALL_STACK_OPENWEBRTC || defined MX_CALL_STACK_ENDPOINT || defined CALL_STACK_JINGLE
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INStartAudioCallIntent class])];
response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeReady userActivity:userActivity];
#else
response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeFailureCallingServiceNotAvailable userActivity:nil];
#endif
}
else
{
// User hasn't logged in
response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeFailureAppConfigurationRequired userActivity:nil];
if ([intent isKindOfClass:[INStartAudioCallIntent class]]) {
return self.startAudioCallIntentHandler;
} else if ([intent isKindOfClass:[INStartVideoCallIntent class]]) {
return self.startVideoCallIntentHandler;
} else if ([intent isKindOfClass:[INSendMessageIntent class]]) {
return self.sendMessageIntentHandler;
}
completion(response);
}
- (void)handleStartAudioCall:(INStartAudioCallIntent *)intent completion:(void (^)(INStartAudioCallIntentResponse * _Nonnull))completion
{
INStartAudioCallIntentResponse *response = nil;
INPerson *person = intent.contacts.firstObject;
if (person && person.customIdentifier)
{
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass(INStartAudioCallIntent.class)];
userActivity.userInfo = @{ @"roomID" : person.customIdentifier };
response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeContinueInApp
userActivity:userActivity];
}
else
{
response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeFailure userActivity:nil];
}
completion(response);
}
#pragma mark - INStartVideoCallIntentHandling
- (void)resolveContactsForStartVideoCall:(INStartVideoCallIntent *)intent withCompletion:(void (^)(NSArray<INPersonResolutionResult *> * _Nonnull))completion
{
[self resolveContacts:intent.contacts withCompletion:completion];
}
- (void)confirmStartVideoCall:(INStartVideoCallIntent *)intent completion:(void (^)(INStartVideoCallIntentResponse * _Nonnull))completion
{
INStartVideoCallIntentResponse *response = nil;
MXKAccount *account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
if (account)
{
#if defined MX_CALL_STACK_OPENWEBRTC || defined MX_CALL_STACK_ENDPOINT || defined CALL_STACK_JINGLE
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INStartVideoCallIntent class])];
response = [[INStartVideoCallIntentResponse alloc] initWithCode:INStartVideoCallIntentResponseCodeReady userActivity:userActivity];
#else
response = [[INStartVideoCallIntentResponse alloc] initWithCode:INStartVideoCallIntentResponseCodeFailureCallingServiceNotAvailable userActivity:nil];
#endif
}
else
{
// User hasn't logged in
response = [[INStartVideoCallIntentResponse alloc] initWithCode:INStartVideoCallIntentResponseCodeFailureRequiringAppLaunch userActivity:nil];
}
completion(response);
}
- (void)handleStartVideoCall:(INStartVideoCallIntent *)intent completion:(void (^)(INStartVideoCallIntentResponse * _Nonnull))completion
{
INStartVideoCallIntentResponse *response = nil;
INPerson *person = intent.contacts.firstObject;
if (person && person.customIdentifier)
{
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass(INStartVideoCallIntent.class)];
userActivity.userInfo = @{ @"roomID" : person.customIdentifier };
response = [[INStartVideoCallIntentResponse alloc] initWithCode:INStartVideoCallIntentResponseCodeContinueInApp
userActivity:userActivity];
}
else
{
response = [[INStartVideoCallIntentResponse alloc] initWithCode:INStartVideoCallIntentResponseCodeFailure userActivity:nil];
}
completion(response);
}
#pragma mark - INSendMessageIntentHandling
- (void)resolveRecipientsForSendMessage:(INSendMessageIntent *)intent completion:(void (^)(NSArray<INSendMessageRecipientResolutionResult *> * _Nonnull))completion
{
[self resolveContacts:intent.recipients withCompletion:completion];
}
- (void)resolveContentForSendMessage:(INSendMessageIntent *)intent withCompletion:(void (^)(INStringResolutionResult * _Nonnull))completion
{
NSString *message = intent.content;
if (message && ![message isEqualToString:@""])
completion([INStringResolutionResult successWithResolvedString:message]);
else
completion([INStringResolutionResult needsValue]);
}
- (void)confirmSendMessage:(INSendMessageIntent *)intent completion:(void (^)(INSendMessageIntentResponse * _Nonnull))completion
{
INSendMessageIntentResponse *response = nil;
MXKAccount *account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
if (account)
{
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INSendMessageIntent class])];
response = [[INSendMessageIntentResponse alloc] initWithCode:INSendMessageIntentResponseCodeReady userActivity:userActivity];
}
else
{
// User hasn't logged in
response = [[INSendMessageIntentResponse alloc] initWithCode:INSendMessageIntentResponseCodeFailureRequiringAppLaunch userActivity:nil];
}
completion(response);
}
- (void)handleSendMessage:(INSendMessageIntent *)intent completion:(void (^)(INSendMessageIntentResponse * _Nonnull))completion
{
void (^completeWithCode)(INSendMessageIntentResponseCode) = ^(INSendMessageIntentResponseCode code) {
NSUserActivity *userActivity = nil;
if (code == INSendMessageIntentResponseCodeSuccess)
userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INSendMessageIntent class])];
INSendMessageIntentResponse *response = [[INSendMessageIntentResponse alloc] initWithCode:INSendMessageIntentResponseCodeSuccess
userActivity:userActivity];
completion(response);
};
INPerson *person = intent.recipients.firstObject;
if (person && person.customIdentifier)
{
MXKAccount *account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
MXFileStore *fileStore = [[MXFileStore alloc] initWithCredentials:account.mxCredentials];
[fileStore.roomSummaryStore fetchAllSummaries:^(NSArray<id<MXRoomSummaryProtocol>> * _Nonnull summaries) {
NSString *roomID = person.customIdentifier;
BOOL isEncrypted = NO;
for (id<MXRoomSummaryProtocol> summary in summaries)
{
if ([summary.roomId isEqualToString:roomID])
{
isEncrypted = summary.isEncrypted;
break;
}
}
if (isEncrypted)
{
[MXFileStore setPreloadOptions:0];
MXSession *session = [[MXSession alloc] initWithMatrixRestClient:account.mxRestClient];
MXWeakify(session);
[session setStore:fileStore success:^{
MXStrongifyAndReturnIfNil(session);
self.selectedRoom = [MXRoom loadRoomFromStore:fileStore withRoomId:roomID matrixSession:session];
// Do not warn for unknown devices. We have cross-signing now
session.crypto.warnOnUnknowDevices = NO;
MXWeakify(self);
[self.selectedRoom sendTextMessage:intent.content
threadId:nil
success:^(NSString *eventId) {
completeWithCode(INSendMessageIntentResponseCodeSuccess);
MXStrongifyAndReturnIfNil(self);
self.selectedRoom = nil;
} failure:^(NSError *error) {
completeWithCode(INSendMessageIntentResponseCodeFailure);
MXStrongifyAndReturnIfNil(self);
self.selectedRoom = nil;
}];
} failure:^(NSError *error) {
completeWithCode(INSendMessageIntentResponseCodeFailure);
}];
return;
}
[account.mxRestClient sendTextMessageToRoom:roomID
threadId:nil
text:intent.content
success:^(NSString *eventId) {
completeWithCode(INSendMessageIntentResponseCodeSuccess);
}
failure:^(NSError *error) {
completeWithCode(INSendMessageIntentResponseCodeFailure);
}];
}];
}
else
{
completeWithCode(INSendMessageIntentResponseCodeFailure);
}
}
#pragma mark - Private
- (void)resolveContacts:(nullable NSArray<INPerson *> *)contacts withCompletion:(void (^)(NSArray<INPersonResolutionResult *> * _Nonnull))completion
{
if (contacts.count == 0)
{
completion(@[[INPersonResolutionResult needsValue]]);
return;
}
else
{
// We don't iterate over array of contacts from passed intent
// since it's hard to imagine scenario with several callee
// so we just extract the first one
INPerson *callee = contacts.firstObject;
// If this method is called after selection of the appropriate user, it will hold userId of an user to whom we must call
NSString *selectedUserId;
// Check if the user has selected right room among several direct rooms from previous resolution process run
if (callee.customIdentifier.length)
{
// If callee will have the same name as one of the contact in the system contacts app
// Siri will pass us this contact in the intent.contacts array and we must provide the same count of
// resolution results as elements count in the intent.contact.
// So we just pass the same result at all iterations
NSMutableArray *resolutionResults = [NSMutableArray array];
for (NSInteger i = 0; i < contacts.count; ++i)
[resolutionResults addObject:[INPersonResolutionResult successWithResolvedPerson:callee]];
completion(resolutionResults);
return;
}
else
{
// This resolution process run after selecting appropriate user among suggested user list
selectedUserId = callee.personHandle.value;
}
MXKAccount *account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
if (account)
{
MXFileStore *fileStore = [[MXFileStore alloc] initWithCredentials:account.mxCredentials];
[fileStore.roomSummaryStore fetchAllSummaries:^(NSArray<id<MXRoomSummaryProtocol>> * _Nonnull summaries) {
// Contains userIds of all users with whom the current user has direct chats
// Use set to avoid duplicates
NSMutableSet<NSString *> *directUserIds = [NSMutableSet set];
// Contains room summaries for all direct rooms connected with particular userId
NSMutableDictionary<NSString *, NSMutableArray<id<MXRoomSummaryProtocol>> *> *roomSummaries = [NSMutableDictionary dictionary];
for (id<MXRoomSummaryProtocol> summary in summaries)
{
// TODO: We also need to check if joined room members count equals 2
// It is pointlessly to save rooms with 1 joined member or room with more than 2 joined members
if (summary.isDirect)
{
NSString *directUserId = summary.directUserId;
// Collect room summaries only for specified user
if (selectedUserId && ![directUserId isEqualToString:selectedUserId])
continue;
// Save userId
[directUserIds addObject:directUserId];
// Save associated with diretUserId room summary
NSMutableArray<id<MXRoomSummaryProtocol>> *userRoomSummaries = roomSummaries[directUserId];
if (userRoomSummaries)
[userRoomSummaries addObject:summary];
else
roomSummaries[directUserId] = [NSMutableArray arrayWithObject:summary];
}
}
[fileStore asyncUsersWithUserIds:directUserIds.allObjects success:^(NSArray<MXUser *> * _Nonnull users) {
// Find users whose display name contains string presented us by Siri
NSMutableArray<MXUser *> *matchingUsers = [NSMutableArray array];
for (MXUser *user in users)
{
if (!user.displayname)
continue;
if (!NSEqualRanges([callee.displayName rangeOfString:user.displayname options:NSCaseInsensitiveSearch], (NSRange){NSNotFound,0}))
{
[matchingUsers addObject:user];
}
}
NSMutableArray<INPerson *> *persons = [NSMutableArray array];
if (matchingUsers.count == 1)
{
MXUser *user = matchingUsers.firstObject;
// Provide to the user a list of direct rooms to choose from
NSArray<id<MXRoomSummaryProtocol>> *summaries = roomSummaries[user.userId];
for (id<MXRoomSummaryProtocol> summary in summaries)
{
INPersonHandle *personHandle = [[INPersonHandle alloc] initWithValue:user.userId type:INPersonHandleTypeUnknown];
// For rooms we try to use room display name
NSString *displayName = summary.displayname ? summary.displayname : user.displayname;
INPerson *person = [[INPerson alloc] initWithPersonHandle:personHandle
nameComponents:nil
displayName:displayName
image:nil
contactIdentifier:nil
customIdentifier:summary.roomId];
[persons addObject:person];
}
}
else if (matchingUsers.count > 1)
{
// Provide to the user a list of users to choose from
// This is the case when there are several users with the same name
for (MXUser *user in matchingUsers)
{
INPersonHandle *personHandle = [[INPersonHandle alloc] initWithValue:user.userId type:INPersonHandleTypeUnknown];
INPerson *person = [[INPerson alloc] initWithPersonHandle:personHandle
nameComponents:nil
displayName:user.displayname
image:nil
contactIdentifier:nil
customIdentifier:nil];
[persons addObject:person];
}
}
if (persons.count == 0)
{
completion(@[[INPersonResolutionResult unsupported]]);
}
else if (persons.count == 1)
{
completion(@[[INPersonResolutionResult successWithResolvedPerson:persons.firstObject]]);
}
else
{
completion(@[[INPersonResolutionResult disambiguationWithPeopleToDisambiguate:persons]]);
}
} failure:nil];
}];
}
else
{
completion(@[[INPersonResolutionResult notRequired]]);
}
}
return nil;
}
@end

View File

@@ -0,0 +1,29 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <Foundation/Foundation.h>
@import Intents;
@protocol ContactResolving;
NS_ASSUME_NONNULL_BEGIN
@interface SendMessageIntentHandler : NSObject <INSendMessageIntentHandling>
- (instancetype)initWithContactResolver:(id<ContactResolving>)contactResolver;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,161 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "SendMessageIntentHandler.h"
#import "ContactResolver.h"
#import "MXKAccountManager.h"
#import "GeneratedInterface-Swift.h"
@interface SendMessageIntentHandler ()
@property (nonatomic) id<ContactResolving> contactResolver;
/**
The room that is currently being used to send a message. This is to ensure a
strong ref is maintained on the `MXRoom` until sending has completed.
*/
@property (nonatomic) MXRoom *selectedRoom;
@end
@implementation SendMessageIntentHandler
- (instancetype)initWithContactResolver:(id<ContactResolving>)contactResolver
{
if (self = [super init]) {
_contactResolver = contactResolver;
}
return self;
}
#pragma mark - INSendMessageIntentHandling
- (void)resolveRecipientsForSendMessage:(INSendMessageIntent *)intent completion:(void (^)(NSArray<INSendMessageRecipientResolutionResult *> * _Nonnull))completion
{
[self.contactResolver resolveContacts:intent.recipients withCompletion:completion];
}
- (void)resolveContentForSendMessage:(INSendMessageIntent *)intent withCompletion:(void (^)(INStringResolutionResult * _Nonnull))completion
{
NSString *message = intent.content;
if (message && ![message isEqualToString:@""])
completion([INStringResolutionResult successWithResolvedString:message]);
else
completion([INStringResolutionResult needsValue]);
}
- (void)confirmSendMessage:(INSendMessageIntent *)intent completion:(void (^)(INSendMessageIntentResponse * _Nonnull))completion
{
INSendMessageIntentResponse *response = nil;
MXKAccount *account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
if (account)
{
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INSendMessageIntent class])];
response = [[INSendMessageIntentResponse alloc] initWithCode:INSendMessageIntentResponseCodeReady userActivity:userActivity];
}
else
{
// User hasn't logged in
response = [[INSendMessageIntentResponse alloc] initWithCode:INSendMessageIntentResponseCodeFailureRequiringAppLaunch userActivity:nil];
}
completion(response);
}
- (void)handleSendMessage:(INSendMessageIntent *)intent completion:(void (^)(INSendMessageIntentResponse * _Nonnull))completion
{
void (^completeWithCode)(INSendMessageIntentResponseCode) = ^(INSendMessageIntentResponseCode code) {
NSUserActivity *userActivity = nil;
if (code == INSendMessageIntentResponseCodeSuccess)
userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INSendMessageIntent class])];
INSendMessageIntentResponse *response = [[INSendMessageIntentResponse alloc] initWithCode:INSendMessageIntentResponseCodeSuccess
userActivity:userActivity];
completion(response);
};
INPerson *person = intent.recipients.firstObject;
if (person && person.customIdentifier)
{
MXKAccount *account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
MXFileStore *fileStore = [[MXFileStore alloc] initWithCredentials:account.mxCredentials];
[fileStore.roomSummaryStore fetchAllSummaries:^(NSArray<id<MXRoomSummaryProtocol>> * _Nonnull summaries) {
NSString *roomID = person.customIdentifier;
BOOL isEncrypted = NO;
for (id<MXRoomSummaryProtocol> summary in summaries)
{
if ([summary.roomId isEqualToString:roomID])
{
isEncrypted = summary.isEncrypted;
break;
}
}
if (isEncrypted)
{
[MXFileStore setPreloadOptions:0];
MXSession *session = [[MXSession alloc] initWithMatrixRestClient:account.mxRestClient];
MXWeakify(session);
[session setStore:fileStore success:^{
MXStrongifyAndReturnIfNil(session);
self.selectedRoom = [MXRoom loadRoomFromStore:fileStore withRoomId:roomID matrixSession:session];
// Do not warn for unknown devices. We have cross-signing now
session.crypto.warnOnUnknowDevices = NO;
MXWeakify(self);
[self.selectedRoom sendTextMessage:intent.content
threadId:nil
success:^(NSString *eventId) {
completeWithCode(INSendMessageIntentResponseCodeSuccess);
MXStrongifyAndReturnIfNil(self);
self.selectedRoom = nil;
} failure:^(NSError *error) {
completeWithCode(INSendMessageIntentResponseCodeFailure);
MXStrongifyAndReturnIfNil(self);
self.selectedRoom = nil;
}];
} failure:^(NSError *error) {
completeWithCode(INSendMessageIntentResponseCodeFailure);
}];
return;
}
[account.mxRestClient sendTextMessageToRoom:roomID
threadId:nil
text:intent.content
success:^(NSString *eventId) {
completeWithCode(INSendMessageIntentResponseCodeSuccess);
}
failure:^(NSError *error) {
completeWithCode(INSendMessageIntentResponseCodeFailure);
}];
}];
}
else
{
completeWithCode(INSendMessageIntentResponseCodeFailure);
}
}
@end

View File

@@ -0,0 +1,29 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <Foundation/Foundation.h>
@import Intents;
@protocol ContactResolving;
NS_ASSUME_NONNULL_BEGIN
@interface StartAudioCallIntentHandler : NSObject <INStartAudioCallIntentHandling>
- (instancetype)initWithContactResolver:(id<ContactResolving>)contactResolver;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,90 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "StartAudioCallIntentHandler.h"
#import "MXKAccountManager.h"
#import "ContactResolver.h"
#import "GeneratedInterface-Swift.h"
@interface StartAudioCallIntentHandler ()
@property (nonatomic) id<ContactResolving> contactResolver;
@end
@implementation StartAudioCallIntentHandler
- (instancetype)initWithContactResolver:(id<ContactResolving>)contactResolver
{
if (self = [super init]) {
_contactResolver = contactResolver;
}
return self;
}
#pragma mark - INStartAudioCallIntentHandling
- (void)resolveContactsForStartAudioCall:(INStartAudioCallIntent *)intent withCompletion:(void (^)(NSArray<INPersonResolutionResult *> * _Nonnull))completion
{
[self.contactResolver resolveContacts:intent.contacts withCompletion:completion];
}
- (void)confirmStartAudioCall:(INStartAudioCallIntent *)intent completion:(void (^)(INStartAudioCallIntentResponse * _Nonnull))completion
{
INStartAudioCallIntentResponse *response = nil;
MXKAccount *account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
if (account)
{
#if defined MX_CALL_STACK_OPENWEBRTC || defined MX_CALL_STACK_ENDPOINT || defined CALL_STACK_JINGLE
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INStartAudioCallIntent class])];
response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeReady userActivity:userActivity];
#else
response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeFailureCallingServiceNotAvailable userActivity:nil];
#endif
}
else
{
// User hasn't logged in
response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeFailureAppConfigurationRequired userActivity:nil];
}
completion(response);
}
- (void)handleStartAudioCall:(INStartAudioCallIntent *)intent completion:(void (^)(INStartAudioCallIntentResponse * _Nonnull))completion
{
INStartAudioCallIntentResponse *response = nil;
INPerson *person = intent.contacts.firstObject;
if (person && person.customIdentifier)
{
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass(INStartAudioCallIntent.class)];
userActivity.userInfo = @{ @"roomID" : person.customIdentifier };
response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeContinueInApp
userActivity:userActivity];
}
else
{
response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeFailure userActivity:nil];
}
completion(response);
}
@end

View File

@@ -0,0 +1,29 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <Foundation/Foundation.h>
@import Intents;
@protocol ContactResolving;
NS_ASSUME_NONNULL_BEGIN
@interface StartVideoCallIntentHandler : NSObject <INStartVideoCallIntentHandling>
- (instancetype)initWithContactResolver:(id<ContactResolving>)contactResolver;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,90 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "StartVideoCallIntentHandler.h"
#import "ContactResolver.h"
#import "MXKAccountManager.h"
#import "GeneratedInterface-Swift.h"
@interface StartVideoCallIntentHandler ()
@property (nonatomic) id<ContactResolving> contactResolver;
@end
@implementation StartVideoCallIntentHandler
- (instancetype)initWithContactResolver:(id<ContactResolving>)contactResolver
{
if (self = [super init]) {
_contactResolver = contactResolver;
}
return self;
}
#pragma mark - INStartVideoCallIntentHandling
- (void)resolveContactsForStartVideoCall:(INStartVideoCallIntent *)intent withCompletion:(void (^)(NSArray<INPersonResolutionResult *> * _Nonnull))completion
{
[self.contactResolver resolveContacts:intent.contacts withCompletion:completion];
}
- (void)confirmStartVideoCall:(INStartVideoCallIntent *)intent completion:(void (^)(INStartVideoCallIntentResponse * _Nonnull))completion
{
INStartVideoCallIntentResponse *response = nil;
MXKAccount *account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
if (account)
{
#if defined MX_CALL_STACK_OPENWEBRTC || defined MX_CALL_STACK_ENDPOINT || defined CALL_STACK_JINGLE
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INStartVideoCallIntent class])];
response = [[INStartVideoCallIntentResponse alloc] initWithCode:INStartVideoCallIntentResponseCodeReady userActivity:userActivity];
#else
response = [[INStartVideoCallIntentResponse alloc] initWithCode:INStartVideoCallIntentResponseCodeFailureCallingServiceNotAvailable userActivity:nil];
#endif
}
else
{
// User hasn't logged in
response = [[INStartVideoCallIntentResponse alloc] initWithCode:INStartVideoCallIntentResponseCodeFailureRequiringAppLaunch userActivity:nil];
}
completion(response);
}
- (void)handleStartVideoCall:(INStartVideoCallIntent *)intent completion:(void (^)(INStartVideoCallIntentResponse * _Nonnull))completion
{
INStartVideoCallIntentResponse *response = nil;
INPerson *person = intent.contacts.firstObject;
if (person && person.customIdentifier)
{
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass(INStartVideoCallIntent.class)];
userActivity.userInfo = @{ @"roomID" : person.customIdentifier };
response = [[INStartVideoCallIntentResponse alloc] initWithCode:INStartVideoCallIntentResponseCodeContinueInApp
userActivity:userActivity];
}
else
{
response = [[INStartVideoCallIntentResponse alloc] initWithCode:INStartVideoCallIntentResponseCodeFailure userActivity:nil];
}
completion(response);
}
@end

1
changelog.d/6203.build Normal file
View File

@@ -0,0 +1 @@
Split `IntentHandler` into smaller, dedicated entities