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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // @flow
  2. import { NativeModules } from 'react-native';
  3. import { getAppProp } from '../../base/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. It should be done only if the connection fails before the conference
  170. * instance is created. Otherwise the eventual failure event is supposed to be
  171. * emitted by the base/conference feature.
  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. const { connection } = action;
  180. locationURL
  181. && forEachConference(
  182. store,
  183. // If there's any conference in the base/conference state then the
  184. // base/conference feature is supposed to emit a failure.
  185. conference => conference.getConnection() !== connection)
  186. && _sendEvent(
  187. store,
  188. _getSymbolDescription(CONFERENCE_FAILED),
  189. /* data */ {
  190. url: toURLString(locationURL),
  191. error: action.error.name
  192. });
  193. }
  194. /**
  195. * Sends a specific event to the native counterpart of the External API. Native
  196. * apps may listen to such events via the mechanisms provided by the (native)
  197. * mobile Jitsi Meet SDK.
  198. *
  199. * @param {Object} store - The redux store.
  200. * @param {string} name - The name of the event to send.
  201. * @param {Object} data - The details/specifics of the event to send determined
  202. * by/associated with the specified {@code name}.
  203. * @private
  204. * @returns {void}
  205. */
  206. function _sendEvent(store: Object, name: string, data: Object) {
  207. // The JavaScript App needs to provide uniquely identifying information to
  208. // the native ExternalAPI module so that the latter may match the former to
  209. // the native JitsiMeetView which hosts it.
  210. const externalAPIScope = getAppProp(store, 'externalAPIScope');
  211. externalAPIScope
  212. && NativeModules.ExternalAPI.sendEvent(name, data, externalAPIScope);
  213. }
  214. /**
  215. * Determines whether to not send a {@code CONFERENCE_LEFT} event to the native
  216. * counterpart of the External API.
  217. *
  218. * @param {Object} store - The redux store.
  219. * @param {Action} action - The redux action which is causing the sending of the
  220. * event.
  221. * @param {Object} data - The details/specifics of the event to send determined
  222. * by/associated with the specified {@code action}.
  223. * @returns {boolean} If the specified event is to not be sent, {@code true};
  224. * otherwise, {@code false}.
  225. */
  226. function _swallowConferenceLeft({ getState }, action, { url }) {
  227. // XXX Internally, we work with JitsiConference instances. Externally
  228. // though, we deal with URL strings. The relation between the two is many to
  229. // one so it's technically and practically possible (by externally loading
  230. // the same URL string multiple times) to try to send CONFERENCE_LEFT
  231. // externally for a URL string which identifies a JitsiConference that the
  232. // app is internally legitimately working with.
  233. let swallowConferenceLeft = false;
  234. url
  235. && forEachConference(getState, (conference, conferenceURL) => {
  236. if (conferenceURL && conferenceURL.toString() === url) {
  237. swallowConferenceLeft = true;
  238. }
  239. return !swallowConferenceLeft;
  240. });
  241. return swallowConferenceLeft;
  242. }
  243. /**
  244. * Determines whether to not send a specific event to the native counterpart of
  245. * the External API.
  246. *
  247. * @param {Object} store - The redux store.
  248. * @param {Action} action - The redux action which is causing the sending of the
  249. * event.
  250. * @param {Object} data - The details/specifics of the event to send determined
  251. * by/associated with the specified {@code action}.
  252. * @returns {boolean} If the specified event is to not be sent, {@code true};
  253. * otherwise, {@code false}.
  254. */
  255. function _swallowEvent(store, action, data) {
  256. switch (action.type) {
  257. case CONFERENCE_LEFT:
  258. return _swallowConferenceLeft(store, action, data);
  259. case CONFERENCE_WILL_JOIN:
  260. // CONFERENCE_WILL_JOIN is dispatched to the external API on SET_ROOM,
  261. // before the connection is created, so we need to swallow the original
  262. // one emitted by base/conference.
  263. return true;
  264. default:
  265. return false;
  266. }
  267. }