NCIntentController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import <Intents/INInteraction.h>
  6. #import <Intents/INSendMessageIntent.h>
  7. #import <Intents/INSendMessageIntent+UserNotifications.h>
  8. #import <Intents/INSpeakableString.h>
  9. #import <Intents/INOutgoingMessageType.h>
  10. #import <Intents/INImage.h>
  11. #import <Intents/INPerson.h>
  12. #import <Intents/INPersonHandle.h>
  13. #import <IntentsUI/INImage+IntentsUI.h>
  14. #import <SDWebImage/SDWebImageManager.h>
  15. #import "NCIntentController.h"
  16. #import "NCAPIController.h"
  17. #import "NCDatabaseManager.h"
  18. #import "NextcloudTalk-Swift.h"
  19. @implementation NCIntentController
  20. + (NCIntentController *)sharedInstance
  21. {
  22. static dispatch_once_t once;
  23. static NCIntentController *sharedInstance;
  24. dispatch_once(&once, ^{
  25. sharedInstance = [[self alloc] init];
  26. });
  27. return sharedInstance;
  28. }
  29. - (void)getInteractionForRoom:(NCRoom *)room withTitle:(NSString *)title withCompletionBlock:(GetInteractionForRoomCompletionBlock)block
  30. {
  31. (void)[[AvatarManager shared] getAvatarFor:room with:UIUserInterfaceStyleLight completionBlock:^(UIImage *avatarImage) {
  32. if (!avatarImage) {
  33. if (block) {
  34. block(nil);
  35. }
  36. return;
  37. }
  38. if (avatarImage.sd_isVector) {
  39. // INImage does not support SVGs -> render them
  40. avatarImage = [[AvatarManager shared] createRenderedImageWithImage:avatarImage];
  41. }
  42. INSpeakableString *groupName = [[INSpeakableString alloc] initWithSpokenPhrase:title];
  43. INPersonHandle *handle = [[INPersonHandle alloc] initWithValue:nil type:INPersonHandleTypeUnknown];
  44. INImage *image = [INImage imageWithUIImage:avatarImage];
  45. INPerson *person = [[INPerson alloc]
  46. initWithPersonHandle:handle
  47. nameComponents:nil
  48. displayName:title
  49. image:image
  50. contactIdentifier:nil
  51. customIdentifier:nil];
  52. INSendMessageIntent *sendMessageIntent = [[INSendMessageIntent alloc] initWithRecipients:nil
  53. outgoingMessageType:INOutgoingMessageTypeOutgoingMessageText
  54. content:nil
  55. speakableGroupName:groupName
  56. conversationIdentifier:room.internalId
  57. serviceName:nil
  58. sender:person
  59. attachments:nil];
  60. INInteraction *interaction = [[INInteraction alloc] initWithIntent:sendMessageIntent response:nil];
  61. interaction.direction = INInteractionDirectionIncoming;
  62. [interaction donateInteractionWithCompletion:^(NSError * _Nullable error) {
  63. if (block) {
  64. if (error) {
  65. NSLog(@"Interaction donation failed: %@", error.description);
  66. block(nil);
  67. } else {
  68. block(sendMessageIntent);
  69. }
  70. }
  71. }];
  72. }];
  73. }
  74. - (void)donateSendMessageIntentForRoom:(NCRoom *)room
  75. {
  76. INSpeakableString *groupName = [[INSpeakableString alloc] initWithSpokenPhrase:room.displayName];
  77. INSendMessageIntent *sendMessageIntent = [[INSendMessageIntent alloc] initWithRecipients:nil
  78. outgoingMessageType:INOutgoingMessageTypeOutgoingMessageText
  79. content:nil
  80. speakableGroupName:groupName
  81. conversationIdentifier:room.internalId
  82. serviceName:nil
  83. sender:nil
  84. attachments:nil];
  85. (void)[[AvatarManager shared] getAvatarFor:room with:UIUserInterfaceStyleLight completionBlock:^(UIImage *image) {
  86. if (image) {
  87. if (image.sd_isVector) {
  88. // INImage does not support SVGs -> render them
  89. image = [[AvatarManager shared] createRenderedImageWithImage:image];
  90. }
  91. INImage *intentImage = [INImage imageWithUIImage:image];
  92. [sendMessageIntent setImage:intentImage forParameterNamed:@"speakableGroupName"];
  93. [self donateMessageSentIntent:sendMessageIntent];
  94. }
  95. }];
  96. }
  97. - (void)donateMessageSentIntent:(INSendMessageIntent *)sendMessageIntent
  98. {
  99. INInteraction *interaction = [[INInteraction alloc] initWithIntent:sendMessageIntent response:nil];
  100. [interaction donateInteractionWithCompletion:^(NSError * _Nullable error) {
  101. if (error) {
  102. NSLog(@"Failed to donate sendMessageIntent: %@", [error description]);
  103. } else {
  104. NSLog(@"SendMessageIntent successfully donated");
  105. }
  106. }];
  107. }
  108. @end