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

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