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 9.1KB

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