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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // @flow
  2. import { appNavigate } from '../app';
  3. import {
  4. CONFERENCE_FAILED,
  5. CONFERENCE_JOINED,
  6. CONFERENCE_LEFT
  7. } from '../base/conference';
  8. import { CONNECTION_ESTABLISHED, CONNECTION_FAILED } from '../base/connection';
  9. import { hideDialog, isDialogOpen } from '../base/dialog';
  10. import {
  11. JitsiConferenceErrors,
  12. JitsiConnectionErrors
  13. } from '../base/lib-jitsi-meet';
  14. import { MiddlewareRegistry } from '../base/redux';
  15. import {
  16. _openLoginDialog,
  17. _openWaitForOwnerDialog,
  18. stopWaitForOwner,
  19. waitForOwner
  20. } from './actions';
  21. import {
  22. CANCEL_LOGIN,
  23. STOP_WAIT_FOR_OWNER,
  24. WAIT_FOR_OWNER
  25. } from './actionTypes';
  26. import { LoginDialog, WaitForOwnerDialog } from './components';
  27. /**
  28. * Middleware that captures connection or conference failed errors and controls
  29. * {@link WaitForOwnerDialog} and {@link LoginDialog}.
  30. *
  31. * FIXME Some of the complexity was introduced by the lack of dialog stacking.
  32. *
  33. * @param {Store} store - Redux store.
  34. * @returns {Function}
  35. */
  36. MiddlewareRegistry.register(store => next => action => {
  37. switch (action.type) {
  38. case CANCEL_LOGIN: {
  39. const { dispatch, getState } = store;
  40. const { thenableWithCancel } = getState()['features/authentication'];
  41. thenableWithCancel && thenableWithCancel.cancel();
  42. // The LoginDialog can be opened on top of "wait for owner". The app
  43. // should navigate only if LoginDialog was open without the
  44. // WaitForOwnerDialog.
  45. if (!isDialogOpen(store, WaitForOwnerDialog)) {
  46. if (_isWaitingForOwner(store)) {
  47. // Instead of hiding show the new one.
  48. const result = next(action);
  49. dispatch(_openWaitForOwnerDialog());
  50. return result;
  51. }
  52. // Go back to the app's entry point.
  53. _hideLoginDialog(store);
  54. dispatch(appNavigate(undefined));
  55. }
  56. break;
  57. }
  58. case CONFERENCE_FAILED: {
  59. const { error } = action;
  60. // XXX The feature authentication affords recovery from
  61. // CONFERENCE_FAILED caused by
  62. // JitsiConferenceErrors.AUTHENTICATION_REQUIRED.
  63. let recoverable;
  64. if (error.name === JitsiConferenceErrors.AUTHENTICATION_REQUIRED) {
  65. if (typeof error.recoverable === 'undefined') {
  66. error.recoverable = true;
  67. }
  68. recoverable = error.recoverable;
  69. }
  70. if (recoverable) {
  71. store.dispatch(waitForOwner());
  72. } else {
  73. store.dispatch(stopWaitForOwner());
  74. }
  75. break;
  76. }
  77. case CONFERENCE_JOINED:
  78. if (_isWaitingForOwner(store)) {
  79. store.dispatch(stopWaitForOwner());
  80. }
  81. _hideLoginDialog(store);
  82. break;
  83. case CONFERENCE_LEFT:
  84. store.dispatch(stopWaitForOwner());
  85. break;
  86. case CONNECTION_ESTABLISHED:
  87. _hideLoginDialog(store);
  88. break;
  89. case CONNECTION_FAILED: {
  90. const { error } = action;
  91. error
  92. && error.name === JitsiConnectionErrors.PASSWORD_REQUIRED
  93. && store.dispatch(_openLoginDialog());
  94. break;
  95. }
  96. case STOP_WAIT_FOR_OWNER:
  97. _clearExistingWaitForOwnerTimeout(store);
  98. store.dispatch(hideDialog(WaitForOwnerDialog));
  99. break;
  100. case WAIT_FOR_OWNER: {
  101. _clearExistingWaitForOwnerTimeout(store);
  102. const { handler, timeoutMs } = action;
  103. action.waitForOwnerTimeoutID = setTimeout(handler, timeoutMs);
  104. // The WAIT_FOR_OWNER action is cyclic and we don't want to hide the
  105. // login dialog every few seconds.
  106. isDialogOpen(store, LoginDialog)
  107. || store.dispatch(_openWaitForOwnerDialog());
  108. break;
  109. }
  110. }
  111. return next(action);
  112. });
  113. /**
  114. * Will clear the wait for conference owner timeout handler if any is currently
  115. * set.
  116. *
  117. * @param {Object} store - The redux store.
  118. * @returns {void}
  119. */
  120. function _clearExistingWaitForOwnerTimeout(
  121. { getState }: { getState: Function }) {
  122. const { waitForOwnerTimeoutID } = getState()['features/authentication'];
  123. waitForOwnerTimeoutID && clearTimeout(waitForOwnerTimeoutID);
  124. }
  125. /**
  126. * Hides {@link LoginDialog} if it's currently displayed.
  127. *
  128. * @param {Object} store - The redux store.
  129. * @returns {void}
  130. */
  131. function _hideLoginDialog({ dispatch }: { dispatch: Dispatch<*> }) {
  132. dispatch(hideDialog(LoginDialog));
  133. }
  134. /**
  135. * Checks if the cyclic "wait for conference owner" task is currently scheduled.
  136. *
  137. * @param {Object} store - The redux store.
  138. * @returns {boolean}
  139. */
  140. function _isWaitingForOwner({ getState }: { getState: Function }) {
  141. return Boolean(getState()['features/authentication'].waitForOwnerTimeoutID);
  142. }