您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.native.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // @flow
  2. import i18next from 'i18next';
  3. import { NativeEventEmitter, NativeModules } from 'react-native';
  4. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT, getAppProp } from '../base/app';
  5. import { MiddlewareRegistry } from '../base/redux';
  6. import { invite } from './actions';
  7. import {
  8. BEGIN_ADD_PEOPLE,
  9. _SET_EMITTER_SUBSCRIPTIONS
  10. } from './actionTypes';
  11. import {
  12. getInviteResultsForQuery,
  13. isAddPeopleEnabled,
  14. isDialOutEnabled
  15. } from './functions';
  16. import './middleware.any';
  17. /**
  18. * The react-native module of the feature invite.
  19. */
  20. const { Invite } = NativeModules;
  21. /**
  22. * The middleware of the feature invite specific to mobile/react-native.
  23. *
  24. * @param {Store} store - The redux store.
  25. * @returns {Function}
  26. */
  27. Invite && MiddlewareRegistry.register(store => next => action => {
  28. switch (action.type) {
  29. case _SET_EMITTER_SUBSCRIPTIONS:
  30. return _setEmitterSubscriptions(store, next, action);
  31. case APP_WILL_MOUNT:
  32. return _appWillMount(store, next, action);
  33. case APP_WILL_UNMOUNT: {
  34. const result = next(action);
  35. store.dispatch({
  36. type: _SET_EMITTER_SUBSCRIPTIONS,
  37. emitterSubscriptions: undefined
  38. });
  39. return result;
  40. }
  41. case BEGIN_ADD_PEOPLE:
  42. return _beginAddPeople(store, next, action);
  43. }
  44. return next(action);
  45. });
  46. /**
  47. * Notifies the feature jwt that the action {@link APP_WILL_MOUNT} is being
  48. * dispatched within a specific redux {@code store}.
  49. *
  50. * @param {Store} store - The redux store in which the specified {@code action}
  51. * is being dispatched.
  52. * @param {Dispatch} next - The redux dispatch function to dispatch the
  53. * specified {@code action} to the specified {@code store}.
  54. * @param {Action} action - The redux action {@code APP_WILL_MOUNT} which is
  55. * being dispatched in the specified {@code store}.
  56. * @private
  57. * @returns {*} The value returned by {@code next(action)}.
  58. */
  59. function _appWillMount({ dispatch, getState }, next, action) {
  60. const result = next(action);
  61. const emitter = new NativeEventEmitter(Invite);
  62. const context = {
  63. dispatch,
  64. getState
  65. };
  66. dispatch({
  67. type: _SET_EMITTER_SUBSCRIPTIONS,
  68. emitterSubscriptions: [
  69. emitter.addListener(
  70. 'org.jitsi.meet:features/invite#invite',
  71. _onInvite,
  72. context),
  73. emitter.addListener(
  74. 'org.jitsi.meet:features/invite#performQuery',
  75. _onPerformQuery,
  76. context)
  77. ]
  78. });
  79. return result;
  80. }
  81. /**
  82. * Notifies the feature invite that the action {@link BEGIN_ADD_PEOPLE} is being
  83. * dispatched within a specific redux {@code store}.
  84. *
  85. * @param {Store} store - The redux store in which the specified {@code action}
  86. * is being dispatched.
  87. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  88. * specified {@code action} to the specified {@code store}.
  89. * @param {Action} action - The redux action {@code BEGIN_ADD_PEOPLE} which is
  90. * being dispatched in the specified {@code store}.
  91. * @private
  92. * @returns {*} The value returned by {@code next(action)}.
  93. */
  94. function _beginAddPeople(store, next, action) {
  95. const result = next(action);
  96. // The JavaScript App needs to provide uniquely identifying information to
  97. // the native Invite module so that the latter may match the former to the
  98. // native JitsiMeetView which hosts it.
  99. const externalAPIScope = getAppProp(store, 'externalAPIScope');
  100. externalAPIScope && Invite.beginAddPeople(externalAPIScope);
  101. return result;
  102. }
  103. /**
  104. * Handles the {@code invite} event of the feature invite i.e. invites specific
  105. * invitees to the current, ongoing conference.
  106. *
  107. * @param {Object} event - The details of the event.
  108. * @returns {void}
  109. */
  110. function _onInvite({ addPeopleControllerScope, externalAPIScope, invitees }) {
  111. const { dispatch, getState } = this; // eslint-disable-line no-invalid-this
  112. // If there are multiple JitsiMeetView instances alive, they will all get
  113. // the event, since there is a single bridge, so make sure we don't act if
  114. // the event is not for us.
  115. if (getAppProp(getState, 'externalAPIScope') !== externalAPIScope) {
  116. return;
  117. }
  118. dispatch(invite(invitees))
  119. .then(failedInvitees =>
  120. Invite.inviteSettled(
  121. externalAPIScope,
  122. addPeopleControllerScope,
  123. failedInvitees));
  124. }
  125. /**
  126. * Handles the {@code performQuery} event of the feature invite i.e. queries for
  127. * invitees who may subsequently be invited to the current, ongoing conference.
  128. *
  129. * @param {Object} event - The details of the event.
  130. * @returns {void}
  131. */
  132. function _onPerformQuery(
  133. { addPeopleControllerScope, externalAPIScope, query }) {
  134. const { getState } = this; // eslint-disable-line no-invalid-this
  135. const state = getState();
  136. // If there are multiple JitsiMeetView instances alive, they will all get
  137. // the event, since there is a single bridge, so make sure we don't act if
  138. // the event is not for us.
  139. if (getAppProp(state, 'externalAPIScope') !== externalAPIScope) {
  140. return;
  141. }
  142. const {
  143. dialOutAuthUrl,
  144. peopleSearchQueryTypes,
  145. peopleSearchUrl
  146. } = state['features/base/config'];
  147. const options = {
  148. dialOutAuthUrl,
  149. addPeopleEnabled: isAddPeopleEnabled(state),
  150. dialOutEnabled: isDialOutEnabled(state),
  151. jwt: state['features/base/jwt'].jwt,
  152. peopleSearchQueryTypes,
  153. peopleSearchUrl
  154. };
  155. getInviteResultsForQuery(query, options)
  156. .catch(() => [])
  157. .then(results => {
  158. const translatedResults = results.map(result => {
  159. if (result.type === 'phone') {
  160. result.title = i18next.t('addPeople.telephone', {
  161. number: result.number
  162. });
  163. if (result.showCountryCodeReminder) {
  164. result.subtitle = i18next.t(
  165. 'addPeople.countryReminder'
  166. );
  167. }
  168. }
  169. return result;
  170. }).filter(result => result.type !== 'phone' || result.allowed);
  171. Invite.receivedResults(
  172. externalAPIScope,
  173. addPeopleControllerScope,
  174. query,
  175. translatedResults);
  176. });
  177. }
  178. /**
  179. * Notifies the feature invite that the action
  180. * {@link _SET_EMITTER_SUBSCRIPTIONS} is being dispatched within a specific
  181. * redux {@code store}.
  182. *
  183. * @param {Store} store - The redux store in which the specified {@code action}
  184. * is being dispatched.
  185. * @param {Dispatch} next - The redux dispatch function to dispatch the
  186. * specified {@code action} to the specified {@code store}.
  187. * @param {Action} action - The redux action {@code _SET_EMITTER_SUBSCRIPTIONS}
  188. * which is being dispatched in the specified {@code store}.
  189. * @private
  190. * @returns {*}
  191. */
  192. function _setEmitterSubscriptions({ getState }, next, action) {
  193. const { emitterSubscriptions } = getState()['features/invite'];
  194. if (emitterSubscriptions) {
  195. for (const subscription of emitterSubscriptions) {
  196. subscription.remove();
  197. }
  198. }
  199. return next(action);
  200. }