Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

add INSendMessageIntent donations #906

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
89 changes: 89 additions & 0 deletions MatrixKit/Models/Room/MXKRoomDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
limitations under the License.
*/

@import Intents;

#import "MXKRoomDataSource.h"

@import MatrixSDK;
Expand Down Expand Up @@ -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;
Expand All @@ -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<INPerson *> *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)
{
Expand Down Expand Up @@ -1822,6 +1863,14 @@ - (void)sendImage:(UIImage *)image success:(void (^)(NSString *))success failure
}
}

if (@available(ios 15, *)) {
NSArray<INPerson *> *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];
}

Expand Down Expand Up @@ -1874,6 +1923,15 @@ - (void)sendVideoAsset:(AVAsset *)videoAsset withThumbnail:(UIImage *)videoThumb
{
__block MXEvent *localEchoEvent = nil;

// Donate an intent
if (@available(iOS 15.0, *)) {
NSArray<INPerson *> *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)
Expand All @@ -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<INPerson *> *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)
Expand All @@ -1907,6 +1976,17 @@ - (void)sendVoiceMessage:(NSURL *)audioFileLocalURL
{
__block MXEvent *localEchoEvent = nil;

// Donate an intent
if (@available(iOS 15.0, *)) {
NSArray<INPerson *> *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)
Expand All @@ -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<INPerson *> *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)
Expand Down