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.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* @flow */
  2. import { NativeModules } from 'react-native';
  3. import {
  4. CONFERENCE_FAILED,
  5. CONFERENCE_JOINED,
  6. CONFERENCE_LEFT,
  7. CONFERENCE_WILL_JOIN,
  8. CONFERENCE_WILL_LEAVE,
  9. JITSI_CONFERENCE_URL_KEY
  10. } from '../../base/conference';
  11. import { LOAD_CONFIG_ERROR } from '../../base/config';
  12. import { MiddlewareRegistry } from '../../base/redux';
  13. import { toURLString } from '../../base/util';
  14. import { ENTER_PICTURE_IN_PICTURE } from '../picture-in-picture';
  15. /**
  16. * Middleware that captures Redux actions and uses the ExternalAPI module to
  17. * turn them into native events so the application knows about them.
  18. *
  19. * @param {Store} store - Redux store.
  20. * @returns {Function}
  21. */
  22. MiddlewareRegistry.register(store => next => action => {
  23. const result = next(action);
  24. switch (action.type) {
  25. case CONFERENCE_FAILED: {
  26. const { error, ...data } = action;
  27. // XXX Certain CONFERENCE_FAILED errors are recoverable i.e. they have
  28. // prevented the user from joining a specific conference but the app may
  29. // be able to eventually join the conference. For example, the app will
  30. // ask the user for a password upon
  31. // JitsiConferenceErrors.PASSWORD_REQUIRED and will retry joining the
  32. // conference afterwards. Such errors are to not reach the native
  33. // counterpart of the External API (or at least not in the
  34. // fatality/finality semantics attributed to
  35. // conferenceFailed:/onConferenceFailed).
  36. if (!error.recoverable) {
  37. _sendConferenceEvent(store, /* action */ {
  38. error: _toErrorString(error),
  39. ...data
  40. });
  41. }
  42. break;
  43. }
  44. case CONFERENCE_JOINED:
  45. case CONFERENCE_LEFT:
  46. case CONFERENCE_WILL_JOIN:
  47. case CONFERENCE_WILL_LEAVE:
  48. _sendConferenceEvent(store, action);
  49. break;
  50. case ENTER_PICTURE_IN_PICTURE:
  51. _sendEvent(store, _getSymbolDescription(action.type), /* data */ {});
  52. break;
  53. case LOAD_CONFIG_ERROR: {
  54. const { error, locationURL, type } = action;
  55. _sendEvent(store, _getSymbolDescription(type), /* data */ {
  56. error: _toErrorString(error),
  57. url: toURLString(locationURL)
  58. });
  59. break;
  60. }
  61. }
  62. return result;
  63. });
  64. /**
  65. * Returns a {@code String} representation of a specific error {@code Object}.
  66. *
  67. * @param {Error|Object|string} error - The error {@code Object} to return a
  68. * {@code String} representation of.
  69. * @returns {string} A {@code String} representation of the specified
  70. * {@code error}.
  71. */
  72. function _toErrorString(
  73. error: Error | { message: ?string, name: ?string } | string) {
  74. // XXX In lib-jitsi-meet and jitsi-meet we utilize errors in the form of
  75. // strings, Error instances, and plain objects which resemble Error.
  76. return (
  77. error
  78. ? typeof error === 'string'
  79. ? error
  80. : Error.prototype.toString.apply(error)
  81. : '');
  82. }
  83. /**
  84. * Gets the description of a specific {@code Symbol}.
  85. *
  86. * @param {Symbol} symbol - The {@code Symbol} to retrieve the description of.
  87. * @private
  88. * @returns {string} The description of {@code symbol}.
  89. */
  90. function _getSymbolDescription(symbol: Symbol) {
  91. let description = symbol.toString();
  92. if (description.startsWith('Symbol(') && description.endsWith(')')) {
  93. description = description.slice(7, -1);
  94. }
  95. // The polyfill es6-symbol that we use does not appear to comply with the
  96. // Symbol standard and, merely, adds @@ at the beginning of the description.
  97. if (description.startsWith('@@')) {
  98. description = description.slice(2);
  99. }
  100. return description;
  101. }
  102. /**
  103. * Sends an event to the native counterpart of the External API for a specific
  104. * conference-related redux action.
  105. *
  106. * @param {Store} store - The redux store.
  107. * @param {Action} action - The redux action.
  108. * @returns {void}
  109. */
  110. function _sendConferenceEvent(
  111. store: Object,
  112. action: {
  113. conference: Object,
  114. type: Symbol,
  115. url: ?string
  116. }) {
  117. const { conference, type, ...data } = action;
  118. // For these (redux) actions, conference identifies a JitsiConference
  119. // instance. The external API cannot transport such an object so we have to
  120. // transport an "equivalent".
  121. if (conference) {
  122. data.url = toURLString(conference[JITSI_CONFERENCE_URL_KEY]);
  123. }
  124. _swallowEvent(store, action, data)
  125. || _sendEvent(store, _getSymbolDescription(type), data);
  126. }
  127. /**
  128. * Sends a specific event to the native counterpart of the External API. Native
  129. * apps may listen to such events via the mechanisms provided by the (native)
  130. * mobile Jitsi Meet SDK.
  131. *
  132. * @param {Object} store - The redux store.
  133. * @param {string} name - The name of the event to send.
  134. * @param {Object} data - The details/specifics of the event to send determined
  135. * by/associated with the specified {@code name}.
  136. * @private
  137. * @returns {void}
  138. */
  139. function _sendEvent(
  140. { getState }: { getState: Function },
  141. name: string,
  142. data: Object) {
  143. // The JavaScript App needs to provide uniquely identifying information to
  144. // the native ExternalAPI module so that the latter may match the former to
  145. // the native JitsiMeetView which hosts it.
  146. const { app } = getState()['features/app'];
  147. if (app) {
  148. const { externalAPIScope } = app.props;
  149. if (externalAPIScope) {
  150. NativeModules.ExternalAPI.sendEvent(name, data, externalAPIScope);
  151. }
  152. }
  153. }
  154. /**
  155. * Determines whether to not send a {@code CONFERENCE_LEFT} event to the native
  156. * counterpart of the External API.
  157. *
  158. * @param {Object} store - The redux store.
  159. * @param {Action} action - The redux action which is causing the sending of the
  160. * event.
  161. * @param {Object} data - The details/specifics of the event to send determined
  162. * by/associated with the specified {@code action}.
  163. * @returns {boolean} If the specified event is to not be sent, {@code true};
  164. * otherwise, {@code false}.
  165. */
  166. function _swallowConferenceLeft({ getState }, action, { url }) {
  167. // XXX Internally, we work with JitsiConference instances. Externally
  168. // though, we deal with URL strings. The relation between the two is many to
  169. // one so it's technically and practically possible (by externally loading
  170. // the same URL string multiple times) to try to send CONFERENCE_LEFT
  171. // externally for a URL string which identifies a JitsiConference that the
  172. // app is internally legitimately working with.
  173. if (url) {
  174. const stateFeaturesBaseConference
  175. = getState()['features/base/conference'];
  176. // eslint-disable-next-line guard-for-in
  177. for (const p in stateFeaturesBaseConference) {
  178. const v = stateFeaturesBaseConference[p];
  179. // Does the value of the base/conference's property look like a
  180. // JitsiConference?
  181. if (v && typeof v === 'object') {
  182. const vURL = v[JITSI_CONFERENCE_URL_KEY];
  183. if (vURL && vURL.toString() === url) {
  184. return true;
  185. }
  186. }
  187. }
  188. }
  189. return false;
  190. }
  191. /**
  192. * Determines whether to not send a specific event to the native counterpart of
  193. * the External API.
  194. *
  195. * @param {Object} store - The redux store.
  196. * @param {Action} action - The redux action which is causing the sending of the
  197. * event.
  198. * @param {Object} data - The details/specifics of the event to send determined
  199. * by/associated with the specified {@code action}.
  200. * @returns {boolean} If the specified event is to not be sent, {@code true};
  201. * otherwise, {@code false}.
  202. */
  203. function _swallowEvent(store, action, data) {
  204. switch (action.type) {
  205. case CONFERENCE_LEFT:
  206. return _swallowConferenceLeft(store, action, data);
  207. default:
  208. return false;
  209. }
  210. }