Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { AnyAction } from 'redux';
  2. import { createConnectionEvent } from '../analytics/AnalyticsEvents';
  3. import { sendAnalytics } from '../analytics/functions';
  4. import { appWillNavigate } from '../base/app/actions';
  5. import { SET_ROOM } from '../base/conference/actionTypes';
  6. import { CONNECTION_ESTABLISHED, CONNECTION_FAILED } from '../base/connection/actionTypes';
  7. import { getURLWithoutParams } from '../base/connection/utils';
  8. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  9. import { inIframe } from '../base/util/iframeUtils';
  10. import { reloadNow } from './actions';
  11. import { _getRouteToRender } from './getRouteToRender';
  12. import { IStore } from './types';
  13. MiddlewareRegistry.register(store => next => action => {
  14. switch (action.type) {
  15. case CONNECTION_ESTABLISHED:
  16. return _connectionEstablished(store, next, action);
  17. case CONNECTION_FAILED:
  18. return _connectionFailed(store, next, action);
  19. case SET_ROOM:
  20. return _setRoom(store, next, action);
  21. }
  22. return next(action);
  23. });
  24. /**
  25. * Notifies the feature app that the action {@link CONNECTION_ESTABLISHED} is
  26. * being dispatched within a specific redux {@code store}.
  27. *
  28. * @param {Store} store - The redux store in which the specified {@code action}
  29. * is being dispatched.
  30. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  31. * specified {@code action} to the specified {@code store}.
  32. * @param {Action} action - The redux action {@code CONNECTION_ESTABLISHED}
  33. * which is being dispatched in the specified {@code store}.
  34. * @private
  35. * @returns {Object} The new state that is the result of the reduction of the
  36. * specified {@code action}.
  37. */
  38. function _connectionEstablished(store: IStore, next: Function, action: AnyAction) {
  39. const result = next(action);
  40. // In the Web app we explicitly do not want to display the hash and
  41. // query/search URL params. Unfortunately, window.location and, more
  42. // importantly, its params are used not only in jitsi-meet but also in
  43. // lib-jitsi-meet. Consequently, the time to remove the params is
  44. // determined by when no one needs them anymore.
  45. // @ts-ignore
  46. const { history, location } = window;
  47. if (inIframe()) {
  48. return;
  49. }
  50. if (history
  51. && location
  52. && history.length
  53. && typeof history.replaceState === 'function') {
  54. // @ts-ignore
  55. const replacement = getURLWithoutParams(location);
  56. // @ts-ignore
  57. if (location !== replacement) {
  58. history.replaceState(
  59. history.state,
  60. 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 }: IStore, next: Function, action: AnyAction) {
  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: IStore['getState'], action: AnyAction) {
  98. const { error } = action;
  99. const isShardChangedError = error
  100. && error.message === 'item-not-found'
  101. && error.details?.shard_changed;
  102. if (isShardChangedError) {
  103. const state = getState();
  104. const { timeEstablished } = state['features/base/connection'];
  105. const { _immediateReloadThreshold } = state['features/base/config'];
  106. const timeSinceConnectionEstablished = Number(timeEstablished && Date.now() - timeEstablished);
  107. const reloadThreshold = typeof _immediateReloadThreshold === 'number' ? _immediateReloadThreshold : 1500;
  108. const isWithinSplitBrainThreshold = !timeEstablished || timeSinceConnectionEstablished <= reloadThreshold;
  109. sendAnalytics(createConnectionEvent('failed', {
  110. ...error,
  111. connectionEstablished: timeEstablished,
  112. splitBrain: isWithinSplitBrainThreshold,
  113. timeSinceConnectionEstablished
  114. }));
  115. return isWithinSplitBrainThreshold;
  116. }
  117. return false;
  118. }
  119. /**
  120. * Navigates to a route in accord with a specific redux state.
  121. *
  122. * @param {Store} store - The redux store which determines/identifies the route
  123. * to navigate to.
  124. * @private
  125. * @returns {void}
  126. */
  127. function _navigate({ dispatch, getState }: IStore) {
  128. const state = getState();
  129. const { app } = state['features/base/app'];
  130. _getRouteToRender(state).then((route: Object) => {
  131. dispatch(appWillNavigate(app, route));
  132. return app._navigate(route);
  133. });
  134. }
  135. /**
  136. * Notifies the feature app that the action {@link SET_ROOM} is being dispatched
  137. * within a specific redux {@code store}.
  138. *
  139. * @param {Store} store - The redux store in which the specified {@code action}
  140. * is being dispatched.
  141. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  142. * specified {@code action} to the specified {@code store}.
  143. * @param {Action} action - The redux action, {@code SET_ROOM}, which is being
  144. * dispatched in the specified {@code store}.
  145. * @private
  146. * @returns {Object} The new state that is the result of the reduction of the
  147. * specified {@code action}.
  148. */
  149. function _setRoom(store: IStore, next: Function, action: AnyAction) {
  150. const result = next(action);
  151. _navigate(store);
  152. return result;
  153. }