Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

InviteSearch.m 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright @ 2018-present Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <React/RCTBridge.h>
  17. #import <React/RCTEventEmitter.h>
  18. #import <React/RCTUtils.h>
  19. #import "JitsiMeetView+Private.h"
  20. #import "InviteSearch.h"
  21. // The events emitted/supported by InviteSearch:
  22. static NSString * const InviteSearchPerformQueryAction = @"performQueryAction";
  23. static NSString * const InviteSearchPerformSubmitInviteAction = @"performSubmitInviteAction";
  24. @interface InviteSearch : RCTEventEmitter
  25. @end
  26. @interface InviteSearchController ()
  27. @property (nonatomic, readonly) NSString* _Nonnull identifier;
  28. @property (nonatomic, strong) NSMutableDictionary* _Nonnull items;
  29. @property (nonatomic, nullable, weak) InviteSearch* module;
  30. - (instancetype)initWithSearchModule:(InviteSearch *)module;
  31. - (void)didReceiveResults:(NSArray<NSDictionary*> * _Nonnull)results
  32. forQuery:(NSString * _Nonnull)query;
  33. - (void)inviteDidSucceed;
  34. - (void)inviteDidFailForItems:(NSArray<NSDictionary *> *)items;
  35. @end
  36. @implementation InviteSearch
  37. static NSMutableDictionary* searchControllers;
  38. RCT_EXTERN void RCTRegisterModule(Class);
  39. + (void)load {
  40. RCTRegisterModule(self);
  41. searchControllers = [[NSMutableDictionary alloc] init];
  42. }
  43. + (NSString *)moduleName {
  44. return @"InviteSearch";
  45. }
  46. - (NSArray<NSString *> *)supportedEvents {
  47. return @[
  48. InviteSearchPerformQueryAction,
  49. InviteSearchPerformSubmitInviteAction
  50. ];
  51. }
  52. /**
  53. * Calls the corresponding JitsiMeetView's delegate to request that the native
  54. * invite search be presented.
  55. *
  56. * @param scope
  57. */
  58. RCT_EXPORT_METHOD(launchNativeInvite:(NSString *)scope) {
  59. // The JavaScript App needs to provide uniquely identifying information to
  60. // the native module so that the latter may match the former to the native
  61. // JitsiMeetView which hosts it.
  62. JitsiMeetView *view = [JitsiMeetView viewForExternalAPIScope:scope];
  63. if (!view) {
  64. return;
  65. }
  66. id<JitsiMeetViewDelegate> delegate = view.delegate;
  67. if (!delegate) {
  68. return;
  69. }
  70. if ([delegate respondsToSelector:@selector(launchNativeInviteForSearchController:)]) {
  71. InviteSearchController* searchController = [searchControllers objectForKey:scope];
  72. if (!searchController) {
  73. searchController = [self makeInviteSearchController];
  74. }
  75. [delegate launchNativeInviteForSearchController:searchController];
  76. }
  77. }
  78. RCT_EXPORT_METHOD(inviteSucceeded:(NSString *)inviteScope) {
  79. InviteSearchController* searchController = [searchControllers objectForKey:inviteScope];
  80. [searchController inviteDidSucceed];
  81. [searchControllers removeObjectForKey:inviteScope];
  82. }
  83. RCT_EXPORT_METHOD(inviteFailedForItems:(NSArray<NSDictionary *> *)items inviteScope:(NSString *)inviteScope) {
  84. InviteSearchController* searchController = [searchControllers objectForKey:inviteScope];
  85. [searchController inviteDidFailForItems:items];
  86. }
  87. RCT_EXPORT_METHOD(receivedResults:(NSArray *)results forQuery:(NSString *)query inviteScope:(NSString *)inviteScope) {
  88. InviteSearchController* searchController = [searchControllers objectForKey:inviteScope];
  89. [searchController didReceiveResults:results forQuery:query];
  90. }
  91. - (InviteSearchController *)makeInviteSearchController {
  92. InviteSearchController* searchController = [[InviteSearchController alloc] initWithSearchModule:self];
  93. [searchControllers setObject:searchController forKey:searchController.identifier];
  94. return searchController;
  95. }
  96. - (void)performQuery:(NSString * _Nonnull)query inviteScope:(NSString * _Nonnull)inviteScope {
  97. [self sendEventWithName:InviteSearchPerformQueryAction body:@{ @"query": query, @"inviteScope": inviteScope }];
  98. }
  99. - (void)cancelSearchForInviteScope:(NSString * _Nonnull)inviteScope {
  100. [searchControllers removeObjectForKey:inviteScope];
  101. }
  102. - (void)submitSelectedItems:(NSArray<NSDictionary *> * _Nonnull)items inviteScope:(NSString * _Nonnull)inviteScope {
  103. [self sendEventWithName:InviteSearchPerformSubmitInviteAction body:@{ @"selectedItems": items, @"inviteScope": inviteScope }];
  104. }
  105. @end
  106. @implementation InviteSearchController
  107. - (instancetype)initWithSearchModule:(InviteSearch *)module {
  108. self = [super init];
  109. if (self) {
  110. _identifier = [[NSUUID UUID] UUIDString];
  111. self.items = [[NSMutableDictionary alloc] init];
  112. self.module = module;
  113. }
  114. return self;
  115. }
  116. - (void)performQuery:(NSString *)query {
  117. [self.module performQuery:query inviteScope:self.identifier];
  118. }
  119. - (void)cancelSearch {
  120. [self.module cancelSearchForInviteScope:self.identifier];
  121. }
  122. - (void)submitSelectedItemIds:(NSArray<NSString *> * _Nonnull)ids {
  123. NSMutableArray* items = [[NSMutableArray alloc] init];
  124. for (NSString* itemId in ids) {
  125. id item = [self.items objectForKey:itemId];
  126. if (item) {
  127. [items addObject:item];
  128. }
  129. }
  130. [self.module submitSelectedItems:items inviteScope:self.identifier];
  131. }
  132. - (void)didReceiveResults:(NSArray<NSDictionary *> *)results forQuery:(NSString *)query {
  133. for (NSDictionary* item in results) {
  134. NSString* itemId = item[@"id"];
  135. NSString* itemType = item[@"type"];
  136. if (itemId) {
  137. [self.items setObject:item forKey:itemId];
  138. } else if (itemType != nil && [itemType isEqualToString: @"phone"]) {
  139. NSString* number = item[@"number"];
  140. if (number) {
  141. [self.items setObject:item forKey:number];
  142. }
  143. }
  144. }
  145. [self.delegate inviteSearchController:self didReceiveResults:results forQuery:query];
  146. }
  147. - (void)inviteDidSucceed {
  148. [self.delegate inviteDidSucceedForSearchController:self];
  149. }
  150. - (void)inviteDidFailForItems:(NSArray<NSDictionary *> *)items {
  151. if (!items) {
  152. items = @[];
  153. }
  154. [self.delegate inviteDidFailForItems:items fromSearchController:self];
  155. }
  156. @end