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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // @flow
  2. import {
  3. createConnectionEvent,
  4. inIframe,
  5. sendAnalytics
  6. } from '../analytics';
  7. import { SET_ROOM } from '../base/conference';
  8. import {
  9. CONNECTION_ESTABLISHED,
  10. CONNECTION_FAILED,
  11. getURLWithoutParams
  12. } from '../base/connection';
  13. import { MiddlewareRegistry } from '../base/redux';
  14. import { reloadNow } from './actions';
  15. import { _getRouteToRender } from './getRouteToRender';
  16. MiddlewareRegistry.register(store => next => action => {
  17. switch (action.type) {
  18. case CONNECTION_ESTABLISHED:
  19. return _connectionEstablished(store, next, action);
  20. case CONNECTION_FAILED:
  21. return _connectionFailed(store, next, action);
  22. case SET_ROOM:
  23. return _setRoom(store, next, action);
  24. }
  25. return next(action);
  26. });
  27. /**
  28. * Notifies the feature app that the action {@link CONNECTION_ESTABLISHED} is
  29. * being dispatched within a specific redux {@code store}.
  30. *
  31. * @param {Store} store - The redux store in which the specified {@code action}
  32. * is being dispatched.
  33. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  34. * specified {@code action} to the specified {@code store}.
  35. * @param {Action} action - The redux action {@code CONNECTION_ESTABLISHED}
  36. * which is being dispatched in the specified {@code store}.
  37. * @private
  38. * @returns {Object} The new state that is the result of the reduction of the
  39. * specified {@code action}.
  40. */
  41. function _connectionEstablished(store, next, action) {
  42. const result = next(action);
  43. // In the Web app we explicitly do not want to display the hash and
  44. // query/search URL params. Unfortunately, window.location and, more
  45. // importantly, its params are used not only in jitsi-meet but also in
  46. // lib-jitsi-meet. Consequenlty, the time to remove the params is
  47. // determined by when no one needs them anymore.
  48. const { history, location } = window;
  49. if (inIframe()) {
  50. return;
  51. }
  52. if (history
  53. && location
  54. && history.length
  55. && typeof history.replaceState === 'function') {
  56. const replacement = getURLWithoutParams(location);
  57. if (location !== replacement) {
  58. history.replaceState(
  59. history.state,
  60. (document && document.title) || '',
  61. replacement);
  62. }
  63. }
  64. return result;
  65. }
  66. /**
  67. * CONNECTION_FAILED action side effects.
  68. *
  69. * @param {Object} store - The Redux store.
  70. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the specified {@code action} to
  71. * the specified {@code store}.
  72. * @param {Action} action - The redux action {@code CONNECTION_FAILED} which is being dispatched in the specified
  73. * {@code store}.
  74. * @returns {Object}
  75. * @private
  76. */
  77. function _connectionFailed({ dispatch, getState }, next, action) {
  78. // In the case of a split-brain error, reload early and prevent further
  79. // handling of the action.
  80. if (_isMaybeSplitBrainError(getState, action)) {
  81. dispatch(reloadNow());
  82. return;
  83. }
  84. return next(action);
  85. }
  86. /**
  87. * Returns whether or not a CONNECTION_FAILED action is for a possible split brain error. A split brain error occurs
  88. * when at least two users join a conference on different bridges. It is assumed the split brain scenario occurs very
  89. * early on in the call.
  90. *
  91. * @param {Function} getState - The redux function for fetching the current state.
  92. * @param {Action} action - The redux action {@code CONNECTION_FAILED} which is being dispatched in the specified
  93. * {@code store}.
  94. * @private
  95. * @returns {boolean}
  96. */
  97. function _isMaybeSplitBrainError(getState, action) {
  98. const { error } = action;
  99. const isShardChangedError = error
  100. && error.message === 'item-not-found'
  101. && error.details
  102. && error.details.shard_changed;
  103. if (isShardChangedError) {
  104. const state = getState();
  105. const { timeEstablished } = state['features/base/connection'];
  106. const { _immediateReloadThreshold } = state['features/base/config'];
  107. const timeSinceConnectionEstablished = timeEstablished && Date.now() - timeEstablished;
  108. const reloadThreshold = typeof _immediateReloadThreshold === 'number' ? _immediateReloadThreshold : 1500;
  109. const isWithinSplitBrainThreshold = !timeEstablished || timeSinceConnectionEstablished <= reloadThreshold;
  110. sendAnalytics(createConnectionEvent('failed', {
  111. ...error,
  112. connectionEstablished: timeEstablished,
  113. splitBrain: isWithinSplitBrainThreshold,
  114. timeSinceConnectionEstablished
  115. }));
  116. return isWithinSplitBrainThreshold;
  117. }
  118. return false;
  119. }
  120. /**
  121. * Navigates to a route in accord with a specific redux state.
  122. *
  123. * @param {Store} store - The redux store which determines/identifies the route
  124. * to navigate to.
  125. * @private
  126. * @returns {void}
  127. */
  128. function _navigate({ getState }) {
  129. const state = getState();
  130. const { app } = state['features/base/app'];
  131. _getRouteToRender(state).then(route => app._navigate(route));
  132. }
  133. /**
  134. * Notifies the feature app that the action {@link SET_ROOM} is being dispatched
  135. * within a specific redux {@code store}.
  136. *
  137. * @param {Store} store - The redux store in which the specified {@code action}
  138. * is being dispatched.
  139. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  140. * specified {@code action} to the specified {@code store}.
  141. * @param {Action} action - The redux action, {@code SET_ROOM}, which is being
  142. * dispatched in the specified {@code store}.
  143. * @private
  144. * @returns {Object} The new state that is the result of the reduction of the
  145. * specified {@code action}.
  146. */
  147. function _setRoom(store, next, action) {
  148. const result = next(action);
  149. _navigate(store);
  150. return result;
  151. }