From 7d3acf71fdd7c8a09e84910a666e1dbee2206de5 Mon Sep 17 00:00:00 2001 From: Finn Behrens Date: Fri, 17 Sep 2021 16:28:04 +0200 Subject: [PATCH] add INSendMessageIntent donations Signed-off-by: Finn Behrens --- MatrixKit/Models/Room/MXKRoomDataSource.m | 89 +++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/MatrixKit/Models/Room/MXKRoomDataSource.m b/MatrixKit/Models/Room/MXKRoomDataSource.m index 4486d9f4..8a94b7f8 100644 --- a/MatrixKit/Models/Room/MXKRoomDataSource.m +++ b/MatrixKit/Models/Room/MXKRoomDataSource.m @@ -17,6 +17,8 @@ limitations under the License. */ +@import Intents; + #import "MXKRoomDataSource.h" @import MatrixSDK; @@ -1698,6 +1700,36 @@ - (void)paginateToFillRect:(CGRect)rect direction:(MXTimelineDirection)direction #pragma mark - Sending + +- (INPerson *)intentRecipient API_AVAILABLE(ios(15)) +{ + + INPersonHandle *personHandle; + INPerson *person; + + if ([_room isDirect]) { + MXUser *user = [_room.mxSession userWithUserId:[_room directUserId]]; + + personHandle = [[INPersonHandle alloc] initWithValue:user.userId type:INPersonHandleTypeUnknown]; + // TODO: avata url to INImage + person = [[INPerson alloc] initWithPersonHandle:personHandle nameComponents:NULL displayName:user.displayname image:NULL contactIdentifier:NULL customIdentifier:user.userId isMe:NO suggestionType:INPersonSuggestionTypeInstantMessageAddress]; + } else { + personHandle = [[INPersonHandle alloc] initWithValue:_room.roomId type:INPersonHandleTypeUnknown]; + // TODO: root avatar url to INImage + person = [[INPerson alloc] initWithPersonHandle:personHandle nameComponents:NULL displayName:_room.summary.displayname image:NULL contactIdentifier:NULL customIdentifier:_room.roomId isMe:NO suggestionType:INPersonSuggestionTypeNone]; + } + + return person; +} + +- (nullable INSpeakableString *)intentGroupName API_AVAILABLE(ios(15)) +{ + if (![_room isDirect]) { + return [[INSpeakableString alloc] initWithSpokenPhrase:_room.summary.displayname]; + } + return NULL; +} + - (void)sendTextMessage:(NSString *)text success:(void (^)(NSString *))success failure:(void (^)(NSError *))failure { __block MXEvent *localEchoEvent = nil; @@ -1706,6 +1738,15 @@ - (void)sendTextMessage:(NSString *)text success:(void (^)(NSString *))success f NSString *sanitizedText = [self sanitizedMessageText:text]; NSString *html = [self htmlMessageFromSanitizedText:sanitizedText]; + // Donate an intent + if (@available(iOS 15.0, *)) { + NSArray *recipients = [[NSArray alloc] initWithObjects:[self intentRecipient], nil]; + INSendMessageIntent *intent = [[INSendMessageIntent alloc] initWithRecipients:recipients outgoingMessageType:INOutgoingMessageTypeOutgoingMessageText content:sanitizedText speakableGroupName:[self intentGroupName] conversationIdentifier:self->_roomId serviceName:@"matrix" sender:NULL]; + + INInteraction *interaction = [[INInteraction alloc] initWithIntent:intent response:NULL]; + [interaction donateInteractionWithCompletion:NULL]; + } + // Make the request to the homeserver if (isEmote) { @@ -1822,6 +1863,14 @@ - (void)sendImage:(UIImage *)image success:(void (^)(NSString *))success failure } } + if (@available(ios 15, *)) { + NSArray *recipients = [[NSArray alloc] initWithObjects:[self intentRecipient], nil]; + INSendMessageIntent *intent = [[INSendMessageIntent alloc] initWithRecipients:recipients outgoingMessageType:INOutgoingMessageTypeOutgoingMessageText content:NULL speakableGroupName:[self intentGroupName] conversationIdentifier:self->_roomId serviceName:@"matrix" sender:NULL]; + + INInteraction *interaction = [[INInteraction alloc] initWithIntent:intent response:NULL]; + [interaction donateInteractionWithCompletion:NULL]; + } + [self sendImageData:imageData withImageSize:image.size mimeType:mimetype andThumbnail:thumbnail success:success failure:failure]; } @@ -1874,6 +1923,15 @@ - (void)sendVideoAsset:(AVAsset *)videoAsset withThumbnail:(UIImage *)videoThumb { __block MXEvent *localEchoEvent = nil; + // Donate an intent + if (@available(iOS 15.0, *)) { + NSArray *recipients = [[NSArray alloc] initWithObjects:[self intentRecipient], nil]; + INSendMessageIntent *intent = [[INSendMessageIntent alloc] initWithRecipients:recipients outgoingMessageType:INOutgoingMessageTypeOutgoingMessageText content:NULL speakableGroupName:[self intentGroupName] conversationIdentifier:self->_roomId serviceName:@"matrix" sender:NULL]; + + INInteraction *interaction = [[INInteraction alloc] initWithIntent:intent response:NULL]; + [interaction donateInteractionWithCompletion:NULL]; + } + [_room sendVideoAsset:videoAsset withThumbnail:videoThumbnail localEcho:&localEchoEvent success:success failure:failure]; if (localEchoEvent) @@ -1888,6 +1946,17 @@ - (void)sendAudioFile:(NSURL *)audioFileLocalURL mimeType:mimeType success:(void { __block MXEvent *localEchoEvent = nil; + // Donate an intent + if (@available(iOS 15.0, *)) { + NSArray *recipients = [[NSArray alloc] initWithObjects:[self intentRecipient], nil]; + INSendMessageAttachment *attachment = [INSendMessageAttachment attachmentWithAudioMessageFile:[INFile fileWithFileURL:audioFileLocalURL filename:NULL typeIdentifier:mimeType]]; + + INSendMessageIntent *intent = [[INSendMessageIntent alloc] initWithRecipients:recipients outgoingMessageType:INOutgoingMessageTypeOutgoingMessageText content:NULL speakableGroupName:[self intentGroupName] conversationIdentifier:self->_roomId serviceName:@"matrix" sender:NULL attachments:[[NSArray alloc] initWithObjects:attachment, nil]]; + + INInteraction *interaction = [[INInteraction alloc] initWithIntent:intent response:NULL]; + [interaction donateInteractionWithCompletion:NULL]; + } + [_room sendAudioFile:audioFileLocalURL mimeType:mimeType localEcho:&localEchoEvent success:success failure:failure keepActualFilename:YES]; if (localEchoEvent) @@ -1907,6 +1976,17 @@ - (void)sendVoiceMessage:(NSURL *)audioFileLocalURL { __block MXEvent *localEchoEvent = nil; + // Donate an intent + if (@available(iOS 15.0, *)) { + NSArray *recipients = [[NSArray alloc] initWithObjects:[self intentRecipient], nil]; + INSendMessageAttachment *attachment = [INSendMessageAttachment attachmentWithAudioMessageFile:[INFile fileWithFileURL:audioFileLocalURL filename:NULL typeIdentifier:mimeType]]; + + INSendMessageIntent *intent = [[INSendMessageIntent alloc] initWithRecipients:recipients outgoingMessageType:INOutgoingMessageTypeOutgoingMessageText content:NULL speakableGroupName:[self intentGroupName] conversationIdentifier:self->_roomId serviceName:@"matrix" sender:NULL attachments:[[NSArray alloc] initWithObjects:attachment, nil]]; + + INInteraction *interaction = [[INInteraction alloc] initWithIntent:intent response:NULL]; + [interaction donateInteractionWithCompletion:NULL]; + } + [_room sendVoiceMessage:audioFileLocalURL mimeType:mimeType duration:duration samples:samples localEcho:&localEchoEvent success:success failure:failure keepActualFilename:YES]; if (localEchoEvent) @@ -1922,6 +2002,15 @@ - (void)sendFile:(NSURL *)fileLocalURL mimeType:(NSString*)mimeType success:(voi { __block MXEvent *localEchoEvent = nil; + // Donate an intent + if (@available(iOS 15.0, *)) { + NSArray *recipients = [[NSArray alloc] initWithObjects:[self intentRecipient], nil]; + INSendMessageIntent *intent = [[INSendMessageIntent alloc] initWithRecipients:recipients outgoingMessageType:INOutgoingMessageTypeOutgoingMessageText content:NULL speakableGroupName:[self intentGroupName] conversationIdentifier:self->_roomId serviceName:@"matrix" sender:NULL]; + + INInteraction *interaction = [[INInteraction alloc] initWithIntent:intent response:NULL]; + [interaction donateInteractionWithCompletion:NULL]; + } + [_room sendFile:fileLocalURL mimeType:mimeType localEcho:&localEchoEvent success:success failure:failure]; if (localEchoEvent)