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.web.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // @flow
  2. import { maybeRedirectToWelcomePage } from '../app/actions';
  3. import {
  4. CONFERENCE_FAILED,
  5. CONFERENCE_JOINED,
  6. CONFERENCE_LEFT
  7. } from '../base/conference';
  8. import { CONNECTION_ESTABLISHED } from '../base/connection';
  9. import { hideDialog, isDialogOpen } from '../base/dialog';
  10. import {
  11. JitsiConferenceErrors
  12. } from '../base/lib-jitsi-meet';
  13. import { MiddlewareRegistry } from '../base/redux';
  14. import {
  15. CANCEL_LOGIN,
  16. STOP_WAIT_FOR_OWNER,
  17. WAIT_FOR_OWNER
  18. } from './actionTypes';
  19. import {
  20. stopWaitForOwner,
  21. waitForOwner
  22. } from './actions.native';
  23. import {
  24. hideLoginDialog,
  25. openWaitForOwnerDialog
  26. } from './actions.web';
  27. import { LoginDialog, WaitForOwnerDialog } from './components';
  28. /**
  29. * Middleware that captures connection or conference failed errors and controls
  30. * {@link WaitForOwnerDialog} and {@link LoginDialog}.
  31. *
  32. * FIXME Some of the complexity was introduced by the lack of dialog stacking.
  33. *
  34. * @param {Store} store - Redux store.
  35. * @returns {Function}
  36. */
  37. MiddlewareRegistry.register(store => next => action => {
  38. switch (action.type) {
  39. case CANCEL_LOGIN: {
  40. if (!isDialogOpen(store, WaitForOwnerDialog)) {
  41. if (isWaitingForOwner(store)) {
  42. store.dispatch(openWaitForOwnerDialog());
  43. return next(action);
  44. }
  45. store.dispatch(hideLoginDialog());
  46. store.dispatch(maybeRedirectToWelcomePage());
  47. }
  48. break;
  49. }
  50. case CONFERENCE_FAILED: {
  51. const { error } = action;
  52. let recoverable;
  53. if (error.name === JitsiConferenceErrors.AUTHENTICATION_REQUIRED) {
  54. if (typeof error.recoverable === 'undefined') {
  55. error.recoverable = true;
  56. }
  57. recoverable = error.recoverable;
  58. }
  59. if (recoverable) {
  60. store.dispatch(waitForOwner());
  61. } else {
  62. store.dispatch(stopWaitForOwner());
  63. }
  64. break;
  65. }
  66. case CONFERENCE_JOINED:
  67. if (isWaitingForOwner(store)) {
  68. store.dispatch(stopWaitForOwner());
  69. }
  70. store.dispatch(hideLoginDialog);
  71. break;
  72. case CONFERENCE_LEFT:
  73. store.dispatch(stopWaitForOwner());
  74. break;
  75. case CONNECTION_ESTABLISHED:
  76. store.dispatch(hideLoginDialog);
  77. break;
  78. case STOP_WAIT_FOR_OWNER:
  79. clearExistingWaitForOwnerTimeout(store);
  80. store.dispatch(hideDialog(WaitForOwnerDialog));
  81. break;
  82. case WAIT_FOR_OWNER: {
  83. clearExistingWaitForOwnerTimeout(store);
  84. const { handler, timeoutMs } = action;
  85. action.waitForOwnerTimeoutID = setTimeout(handler, timeoutMs);
  86. isDialogOpen(store, LoginDialog)
  87. || store.dispatch(openWaitForOwnerDialog());
  88. break;
  89. }
  90. }
  91. return next(action);
  92. });
  93. /**
  94. * Will clear the wait for conference owner timeout handler if any is currently
  95. * set.
  96. *
  97. * @param {Object} store - The redux store.
  98. * @returns {void}
  99. */
  100. function clearExistingWaitForOwnerTimeout(
  101. { getState }: { getState: Function }) {
  102. const { waitForOwnerTimeoutID } = getState()['features/authentication'];
  103. waitForOwnerTimeoutID && clearTimeout(waitForOwnerTimeoutID);
  104. }
  105. /**
  106. * Checks if the cyclic "wait for conference owner" task is currently scheduled.
  107. *
  108. * @param {Object} store - The redux store.
  109. * @returns {void}
  110. */
  111. function isWaitingForOwner({ getState }: { getState: Function }) {
  112. return getState()['features/authentication'].waitForOwnerTimeoutID;
  113. }