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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. _sendConferenceFailedOnConnectionError(store, action);
  55. break;
  56. case ENTER_PICTURE_IN_PICTURE:
  57. _sendEvent(store, _getSymbolDescription(action.type), /* data */ {});
  58. break;
  59. case LOAD_CONFIG_ERROR: {
  60. const { error, locationURL, type } = action;
  61. _sendEvent(store, _getSymbolDescription(type), /* data */ {
  62. error: _toErrorString(error),
  63. url: toURLString(locationURL)
  64. });
  65. break;
  66. }
  67. case SET_ROOM:
  68. _maybeTriggerEarlyConferenceWillJoin(store, action);
  69. break;
  70. }
  71. return result;
  72. });
  73. /**
  74. * Returns a {@code String} representation of a specific error {@code Object}.
  75. *
  76. * @param {Error|Object|string} error - The error {@code Object} to return a
  77. * {@code String} representation of.
  78. * @returns {string} A {@code String} representation of the specified
  79. * {@code error}.
  80. */
  81. function _toErrorString(
  82. error: Error | { message: ?string, name: ?string } | string) {
  83. // XXX In lib-jitsi-meet and jitsi-meet we utilize errors in the form of
  84. // strings, Error instances, and plain objects which resemble Error.
  85. return (
  86. error
  87. ? typeof error === 'string'
  88. ? error
  89. : Error.prototype.toString.apply(error)
  90. : '');
  91. }
  92. /**
  93. * Gets the description of a specific {@code Symbol}.
  94. *
  95. * @param {Symbol} symbol - The {@code Symbol} to retrieve the description of.
  96. * @private
  97. * @returns {string} The description of {@code symbol}.
  98. */
  99. function _getSymbolDescription(symbol: Symbol) {
  100. let description = symbol.toString();
  101. if (description.startsWith('Symbol(') && description.endsWith(')')) {
  102. description = description.slice(7, -1);
  103. }
  104. // The polyfill es6-symbol that we use does not appear to comply with the
  105. // Symbol standard and, merely, adds @@ at the beginning of the description.
  106. if (description.startsWith('@@')) {
  107. description = description.slice(2);
  108. }
  109. return description;
  110. }
  111. /**
  112. * If {@link SET_ROOM} action happens for a valid conference room this method
  113. * will emit an early {@link CONFERENCE_WILL_JOIN} event to let the external API
  114. * know that a conference is being joined. Before that happens a connection must
  115. * be created and only then base/conference feature would emit
  116. * {@link CONFERENCE_WILL_JOIN}. That is fine for the Jitsi Meet app, because
  117. * that's the a conference instance gets created, but it's too late for
  118. * the external API to learn that. The latter {@link CONFERENCE_WILL_JOIN} is
  119. * swallowed in {@link _swallowEvent}.
  120. *
  121. * @param {Store} store - The redux store.
  122. * @param {Action} action - The redux action.
  123. * @returns {void}
  124. */
  125. function _maybeTriggerEarlyConferenceWillJoin(store, action) {
  126. const { locationURL } = store.getState()['features/base/connection'];
  127. const { room } = action;
  128. isRoomValid(room) && locationURL && _sendEvent(
  129. store,
  130. _getSymbolDescription(CONFERENCE_WILL_JOIN),
  131. /* data */ {
  132. url: toURLString(locationURL)
  133. });
  134. }
  135. /**
  136. * Sends an event to the native counterpart of the External API for a specific
  137. * conference-related redux action.
  138. *
  139. * @param {Store} store - The redux store.
  140. * @param {Action} action - The redux action.
  141. * @returns {void}
  142. */
  143. function _sendConferenceEvent(
  144. store: Object,
  145. action: {
  146. conference: Object,
  147. type: Symbol,
  148. url: ?string
  149. }) {
  150. const { conference, type, ...data } = action;
  151. // For these (redux) actions, conference identifies a JitsiConference
  152. // instance. The external API cannot transport such an object so we have to
  153. // transport an "equivalent".
  154. if (conference) {
  155. data.url = toURLString(conference[JITSI_CONFERENCE_URL_KEY]);
  156. }
  157. _swallowEvent(store, action, data)
  158. || _sendEvent(store, _getSymbolDescription(type), data);
  159. }
  160. /**
  161. * Sends {@link CONFERENCE_FAILED} event when the {@link CONNECTION_FAILED}
  162. * occurs. Otherwise the external API will not emit such event, because at this
  163. * point conference has not been created yet and the base/conference feature
  164. * will not emit it.
  165. *
  166. * @param {Store} store - The redux store.
  167. * @param {Action} action - The redux action.
  168. * @returns {void}
  169. */
  170. function _sendConferenceFailedOnConnectionError(store, action) {
  171. const { locationURL } = store.getState()['features/base/connection'];
  172. locationURL && _sendEvent(
  173. store,
  174. _getSymbolDescription(CONFERENCE_FAILED),
  175. /* data */ {
  176. url: toURLString(locationURL),
  177. error: action.error.name
  178. });
  179. }
  180. /**
  181. * Sends a specific event to the native counterpart of the External API. Native
  182. * apps may listen to such events via the mechanisms provided by the (native)
  183. * mobile Jitsi Meet SDK.
  184. *
  185. * @param {Object} store - The redux store.
  186. * @param {string} name - The name of the event to send.
  187. * @param {Object} data - The details/specifics of the event to send determined
  188. * by/associated with the specified {@code name}.
  189. * @private
  190. * @returns {void}
  191. */
  192. function _sendEvent(
  193. { getState }: { getState: Function },
  194. name: string,
  195. data: Object) {
  196. // The JavaScript App needs to provide uniquely identifying information to
  197. // the native ExternalAPI module so that the latter may match the former to
  198. // the native JitsiMeetView which hosts it.
  199. const { app } = getState()['features/app'];
  200. if (app) {
  201. const { externalAPIScope } = app.props;
  202. if (externalAPIScope) {
  203. NativeModules.ExternalAPI.sendEvent(name, data, externalAPIScope);
  204. }
  205. }
  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. if (url) {
  227. const stateFeaturesBaseConference
  228. = getState()['features/base/conference'];
  229. // eslint-disable-next-line guard-for-in
  230. for (const p in stateFeaturesBaseConference) {
  231. const v = stateFeaturesBaseConference[p];
  232. // Does the value of the base/conference's property look like a
  233. // JitsiConference?
  234. if (v && typeof v === 'object') {
  235. const vURL = v[JITSI_CONFERENCE_URL_KEY];
  236. if (vURL && vURL.toString() === url) {
  237. return true;
  238. }
  239. }
  240. }
  241. }
  242. return false;
  243. }
  244. /**
  245. * Determines whether to not send a specific event to the native counterpart of
  246. * the External API.
  247. *
  248. * @param {Object} store - The redux store.
  249. * @param {Action} action - The redux action which is causing the sending of the
  250. * event.
  251. * @param {Object} data - The details/specifics of the event to send determined
  252. * by/associated with the specified {@code action}.
  253. * @returns {boolean} If the specified event is to not be sent, {@code true};
  254. * otherwise, {@code false}.
  255. */
  256. function _swallowEvent(store, action, data) {
  257. switch (action.type) {
  258. case CONFERENCE_LEFT:
  259. return _swallowConferenceLeft(store, action, data);
  260. case CONFERENCE_WILL_JOIN:
  261. // CONFERENCE_WILL_JOIN is dispatched to the external API on SET_ROOM,
  262. // before the connection is created, so we need to swallow the original
  263. // one emitted by base/conference.
  264. return true;
  265. default:
  266. return false;
  267. }
  268. }