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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // @flow
  2. import { NativeEventEmitter, NativeModules } from 'react-native';
  3. import { appNavigate } from '../../app/actions';
  4. import { APP_WILL_MOUNT } from '../../base/app/actionTypes';
  5. import {
  6. CONFERENCE_FAILED,
  7. CONFERENCE_JOINED,
  8. CONFERENCE_LEFT,
  9. CONFERENCE_WILL_JOIN,
  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 {
  17. CONNECTION_DISCONNECTED,
  18. CONNECTION_FAILED,
  19. JITSI_CONNECTION_CONFERENCE_KEY,
  20. JITSI_CONNECTION_URL_KEY,
  21. getURLWithoutParams
  22. } from '../../base/connection';
  23. import { SET_AUDIO_MUTED } from '../../base/media/actionTypes';
  24. import { PARTICIPANT_JOINED, PARTICIPANT_LEFT } from '../../base/participants';
  25. import { MiddlewareRegistry } from '../../base/redux';
  26. import { muteLocal } from '../../remote-video-menu/actions';
  27. import { ENTER_PICTURE_IN_PICTURE } from '../picture-in-picture';
  28. import { sendEvent } from './functions';
  29. /**
  30. * Event which will be emitted on the native side to indicate the conference
  31. * has ended either by user request or because an error was produced.
  32. */
  33. const CONFERENCE_TERMINATED = 'CONFERENCE_TERMINATED';
  34. const { ExternalAPI } = NativeModules;
  35. const eventEmitter = new NativeEventEmitter(ExternalAPI);
  36. /**
  37. * Middleware that captures Redux actions and uses the ExternalAPI module to
  38. * turn them into native events so the application knows about them.
  39. *
  40. * @param {Store} store - Redux store.
  41. * @returns {Function}
  42. */
  43. MiddlewareRegistry.register(store => next => action => {
  44. const result = next(action);
  45. const { type } = action;
  46. switch (type) {
  47. case APP_WILL_MOUNT:
  48. _registerForNativeEvents(store.dispatch);
  49. break;
  50. case CONFERENCE_FAILED: {
  51. const { error, ...data } = action;
  52. // XXX Certain CONFERENCE_FAILED errors are recoverable i.e. they have
  53. // prevented the user from joining a specific conference but the app may
  54. // be able to eventually join the conference. For example, the app will
  55. // ask the user for a password upon
  56. // JitsiConferenceErrors.PASSWORD_REQUIRED and will retry joining the
  57. // conference afterwards. Such errors are to not reach the native
  58. // counterpart of the External API (or at least not in the
  59. // fatality/finality semantics attributed to
  60. // conferenceFailed:/onConferenceFailed).
  61. if (!error.recoverable) {
  62. _sendConferenceEvent(store, /* action */ {
  63. error: _toErrorString(error),
  64. ...data
  65. });
  66. }
  67. break;
  68. }
  69. case CONFERENCE_JOINED:
  70. case CONFERENCE_LEFT:
  71. case CONFERENCE_WILL_JOIN:
  72. _sendConferenceEvent(store, action);
  73. break;
  74. case CONNECTION_DISCONNECTED: {
  75. // FIXME: This is a hack. See the description in the JITSI_CONNECTION_CONFERENCE_KEY constant definition.
  76. // Check if this connection was attached to any conference. If it wasn't, fake a CONFERENCE_TERMINATED event.
  77. const { connection } = action;
  78. const conference = connection[JITSI_CONNECTION_CONFERENCE_KEY];
  79. if (!conference) {
  80. // This action will arrive late, so the locationURL stored on the state is no longer valid.
  81. const locationURL = connection[JITSI_CONNECTION_URL_KEY];
  82. sendEvent(
  83. store,
  84. CONFERENCE_TERMINATED,
  85. /* data */ {
  86. url: _normalizeUrl(locationURL)
  87. });
  88. }
  89. break;
  90. }
  91. case CONNECTION_FAILED:
  92. !action.error.recoverable
  93. && _sendConferenceFailedOnConnectionError(store, action);
  94. break;
  95. case ENTER_PICTURE_IN_PICTURE:
  96. sendEvent(store, type, /* data */ {});
  97. break;
  98. case LOAD_CONFIG_ERROR: {
  99. const { error, locationURL } = action;
  100. sendEvent(
  101. store,
  102. CONFERENCE_TERMINATED,
  103. /* data */ {
  104. error: _toErrorString(error),
  105. url: _normalizeUrl(locationURL)
  106. });
  107. break;
  108. }
  109. case PARTICIPANT_JOINED:
  110. case PARTICIPANT_LEFT: {
  111. const { participant } = action;
  112. sendEvent(
  113. store,
  114. action.type,
  115. /* data */ {
  116. isLocal: participant.local,
  117. email: participant.email,
  118. name: participant.name,
  119. participantId: participant.id
  120. });
  121. break;
  122. }
  123. case SET_ROOM:
  124. _maybeTriggerEarlyConferenceWillJoin(store, action);
  125. break;
  126. case SET_AUDIO_MUTED:
  127. sendEvent(
  128. store,
  129. 'AUDIO_MUTED_CHANGED',
  130. /* data */ {
  131. muted: action.muted
  132. });
  133. break;
  134. }
  135. return result;
  136. });
  137. /**
  138. * Registers for events sent from the native side via NativeEventEmitter.
  139. *
  140. * @param {Dispatch} dispatch - The Redux dispatch function.
  141. * @private
  142. * @returns {void}
  143. */
  144. function _registerForNativeEvents(dispatch) {
  145. eventEmitter.addListener(ExternalAPI.HANG_UP, () => {
  146. dispatch(appNavigate(undefined));
  147. });
  148. eventEmitter.addListener(ExternalAPI.SET_AUDIO_MUTED, ({ muted }) => {
  149. dispatch(muteLocal(muted === 'true'));
  150. });
  151. }
  152. /**
  153. * Returns a {@code String} representation of a specific error {@code Object}.
  154. *
  155. * @param {Error|Object|string} error - The error {@code Object} to return a
  156. * {@code String} representation of.
  157. * @returns {string} A {@code String} representation of the specified
  158. * {@code error}.
  159. */
  160. function _toErrorString(
  161. error: Error | { message: ?string, name: ?string } | string) {
  162. // XXX In lib-jitsi-meet and jitsi-meet we utilize errors in the form of
  163. // strings, Error instances, and plain objects which resemble Error.
  164. return (
  165. error
  166. ? typeof error === 'string'
  167. ? error
  168. : Error.prototype.toString.apply(error)
  169. : '');
  170. }
  171. /**
  172. * If {@link SET_ROOM} action happens for a valid conference room this method
  173. * will emit an early {@link CONFERENCE_WILL_JOIN} event to let the external API
  174. * know that a conference is being joined. Before that happens a connection must
  175. * be created and only then base/conference feature would emit
  176. * {@link CONFERENCE_WILL_JOIN}. That is fine for the Jitsi Meet app, because
  177. * that's the a conference instance gets created, but it's too late for
  178. * the external API to learn that. The latter {@link CONFERENCE_WILL_JOIN} is
  179. * swallowed in {@link _swallowEvent}.
  180. *
  181. * @param {Store} store - The redux store.
  182. * @param {Action} action - The redux action.
  183. * @returns {void}
  184. */
  185. function _maybeTriggerEarlyConferenceWillJoin(store, action) {
  186. const { locationURL } = store.getState()['features/base/connection'];
  187. const { room } = action;
  188. isRoomValid(room) && locationURL && sendEvent(
  189. store,
  190. CONFERENCE_WILL_JOIN,
  191. /* data */ {
  192. url: _normalizeUrl(locationURL)
  193. });
  194. }
  195. /**
  196. * Normalizes the given URL for presentation over the external API.
  197. *
  198. * @param {URL} url -The URL to normalize.
  199. * @returns {string} - The normalized URL as a string.
  200. */
  201. function _normalizeUrl(url: URL) {
  202. return getURLWithoutParams(url).href;
  203. }
  204. /**
  205. * Sends an event to the native counterpart of the External API for a specific
  206. * conference-related redux action.
  207. *
  208. * @param {Store} store - The redux store.
  209. * @param {Action} action - The redux action.
  210. * @returns {void}
  211. */
  212. function _sendConferenceEvent(
  213. store: Object,
  214. action: {
  215. conference: Object,
  216. type: string,
  217. url: ?string
  218. }) {
  219. const { conference, type, ...data } = action;
  220. // For these (redux) actions, conference identifies a JitsiConference
  221. // instance. The external API cannot transport such an object so we have to
  222. // transport an "equivalent".
  223. if (conference) {
  224. data.url = _normalizeUrl(conference[JITSI_CONFERENCE_URL_KEY]);
  225. }
  226. if (_swallowEvent(store, action, data)) {
  227. return;
  228. }
  229. let type_;
  230. switch (type) {
  231. case CONFERENCE_FAILED:
  232. case CONFERENCE_LEFT:
  233. type_ = CONFERENCE_TERMINATED;
  234. break;
  235. default:
  236. type_ = type;
  237. break;
  238. }
  239. sendEvent(store, type_, data);
  240. }
  241. /**
  242. * Sends {@link CONFERENCE_TERMINATED} event when the {@link CONNECTION_FAILED}
  243. * occurs. It should be done only if the connection fails before the conference
  244. * instance is created. Otherwise the eventual failure event is supposed to be
  245. * emitted by the base/conference feature.
  246. *
  247. * @param {Store} store - The redux store.
  248. * @param {Action} action - The redux action.
  249. * @returns {void}
  250. */
  251. function _sendConferenceFailedOnConnectionError(store, action) {
  252. const { locationURL } = store.getState()['features/base/connection'];
  253. const { connection } = action;
  254. locationURL
  255. && forEachConference(
  256. store,
  257. // If there's any conference in the base/conference state then the
  258. // base/conference feature is supposed to emit a failure.
  259. conference => conference.getConnection() !== connection)
  260. && sendEvent(
  261. store,
  262. CONFERENCE_TERMINATED,
  263. /* data */ {
  264. url: _normalizeUrl(locationURL),
  265. error: action.error.name
  266. });
  267. }
  268. /**
  269. * Determines whether to not send a {@code CONFERENCE_LEFT} event to the native
  270. * counterpart of the External API.
  271. *
  272. * @param {Object} store - The redux store.
  273. * @param {Action} action - The redux action which is causing the sending of the
  274. * event.
  275. * @param {Object} data - The details/specifics of the event to send determined
  276. * by/associated with the specified {@code action}.
  277. * @returns {boolean} If the specified event is to not be sent, {@code true};
  278. * otherwise, {@code false}.
  279. */
  280. function _swallowConferenceLeft({ getState }, action, { url }) {
  281. // XXX Internally, we work with JitsiConference instances. Externally
  282. // though, we deal with URL strings. The relation between the two is many to
  283. // one so it's technically and practically possible (by externally loading
  284. // the same URL string multiple times) to try to send CONFERENCE_LEFT
  285. // externally for a URL string which identifies a JitsiConference that the
  286. // app is internally legitimately working with.
  287. let swallowConferenceLeft = false;
  288. url
  289. && forEachConference(getState, (conference, conferenceURL) => {
  290. if (conferenceURL && conferenceURL.toString() === url) {
  291. swallowConferenceLeft = true;
  292. }
  293. return !swallowConferenceLeft;
  294. });
  295. return swallowConferenceLeft;
  296. }
  297. /**
  298. * Determines whether to not send a specific event to the native counterpart of
  299. * the External API.
  300. *
  301. * @param {Object} store - The redux store.
  302. * @param {Action} action - The redux action which is causing the sending of the
  303. * event.
  304. * @param {Object} data - The details/specifics of the event to send determined
  305. * by/associated with the specified {@code action}.
  306. * @returns {boolean} If the specified event is to not be sent, {@code true};
  307. * otherwise, {@code false}.
  308. */
  309. function _swallowEvent(store, action, data) {
  310. switch (action.type) {
  311. case CONFERENCE_LEFT:
  312. return _swallowConferenceLeft(store, action, data);
  313. case CONFERENCE_WILL_JOIN:
  314. // CONFERENCE_WILL_JOIN is dispatched to the external API on SET_ROOM,
  315. // before the connection is created, so we need to swallow the original
  316. // one emitted by base/conference.
  317. return true;
  318. default:
  319. return false;
  320. }
  321. }