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

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