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.8KB

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