You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.native.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // @flow
  2. import i18next from 'i18next';
  3. import { NativeEventEmitter, NativeModules } from 'react-native';
  4. import { MiddlewareRegistry } from '../base/redux';
  5. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app';
  6. import { getInviteURL } from '../base/connection';
  7. import { inviteVideoRooms } from '../videosipgw';
  8. import {
  9. BEGIN_ADD_PEOPLE,
  10. _SET_EMITTER_SUBSCRIPTIONS
  11. } from './actionTypes';
  12. import {
  13. getInviteResultsForQuery,
  14. isAddPeopleEnabled,
  15. isDialOutEnabled,
  16. sendInvitesForItems
  17. } from './functions';
  18. import './middleware.any';
  19. /**
  20. * The react-native module of the feature invite.
  21. */
  22. const { Invite } = NativeModules;
  23. /**
  24. * The middleware of the feature invite specific to mobile/react-native.
  25. *
  26. * @param {Store} store - The redux store.
  27. * @returns {Function}
  28. */
  29. Invite && MiddlewareRegistry.register(store => next => action => {
  30. switch (action.type) {
  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#performQuery',
  71. _onPerformQuery,
  72. context),
  73. emitter.addListener(
  74. 'org.jitsi.meet:features/invite#invite',
  75. _onInvite,
  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({ getState }, 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 { app } = getState()['features/app'];
  100. if (app) {
  101. const { externalAPIScope } = app.props;
  102. if (externalAPIScope) {
  103. Invite.beginAddPeople(externalAPIScope);
  104. }
  105. }
  106. return result;
  107. }
  108. /**
  109. * Handles the {@code invite} event of the feature invite i.e. invites specific
  110. * invitees to the current, ongoing conference.
  111. *
  112. * @param {Object} event - The details of the event.
  113. * @returns {void}
  114. */
  115. function _onInvite(
  116. { addPeopleControllerScope, externalAPIScope, invitees }) {
  117. const { getState } = this; // eslint-disable-line no-invalid-this
  118. const state = getState();
  119. const { conference } = state['features/base/conference'];
  120. const { inviteServiceUrl } = state['features/base/config'];
  121. const options = {
  122. conference,
  123. inviteServiceUrl,
  124. inviteUrl: getInviteURL(state),
  125. inviteVideoRooms,
  126. jwt: state['features/base/jwt'].jwt
  127. };
  128. sendInvitesForItems(invitees, options)
  129. .then(failedInvitees =>
  130. Invite.inviteSettled(
  131. externalAPIScope,
  132. addPeopleControllerScope,
  133. failedInvitees));
  134. }
  135. /**
  136. * Handles the {@code performQuery} event of the feature invite i.e. queries for
  137. * invitees who may subsequently be invited to the current, ongoing conference.
  138. *
  139. * @param {Object} event - The details of the event.
  140. * @returns {void}
  141. */
  142. function _onPerformQuery(
  143. { addPeopleControllerScope, externalAPIScope, query }) {
  144. const { getState } = this; // eslint-disable-line no-invalid-this
  145. const state = getState();
  146. const {
  147. dialOutAuthUrl,
  148. peopleSearchQueryTypes,
  149. peopleSearchUrl
  150. } = state['features/base/config'];
  151. const options = {
  152. dialOutAuthUrl,
  153. addPeopleEnabled: isAddPeopleEnabled(state),
  154. dialOutEnabled: isDialOutEnabled(state),
  155. jwt: state['features/base/jwt'].jwt,
  156. peopleSearchQueryTypes,
  157. peopleSearchUrl
  158. };
  159. getInviteResultsForQuery(query, options)
  160. .catch(() => [])
  161. .then(results => {
  162. const translatedResults = results.map(result => {
  163. if (result.type === 'phone') {
  164. result.title = i18next.t('addPeople.telephone', {
  165. number: result.number
  166. });
  167. if (result.showCountryCodeReminder) {
  168. result.subtitle = i18next.t(
  169. 'addPeople.countryReminder'
  170. );
  171. }
  172. }
  173. return result;
  174. }).filter(result => result.type !== 'phone' || result.allowed);
  175. Invite.receivedResults(
  176. externalAPIScope,
  177. addPeopleControllerScope,
  178. query,
  179. translatedResults);
  180. });
  181. }