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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { appNavigate } from '../app';
  2. import {
  3. CONFERENCE_FAILED,
  4. CONFERENCE_JOINED,
  5. CONFERENCE_LEFT
  6. } from '../base/conference';
  7. import { CONNECTION_ESTABLISHED, CONNECTION_FAILED } from '../base/connection';
  8. import { hideDialog, isDialogOpen } from '../base/dialog';
  9. import {
  10. JitsiConferenceErrors,
  11. JitsiConnectionErrors
  12. } from '../base/lib-jitsi-meet';
  13. import { MiddlewareRegistry } from '../base/redux';
  14. import {
  15. _openLoginDialog,
  16. _openWaitForOwnerDialog,
  17. stopWaitForOwner,
  18. waitForOwner
  19. } from './actions';
  20. import {
  21. CANCEL_LOGIN,
  22. CANCEL_WAIT_FOR_OWNER,
  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 { thenableWithCancel }
  40. = store.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. store.dispatch(_openWaitForOwnerDialog());
  50. return result;
  51. }
  52. // Go back to the app's entry point.
  53. _hideLoginDialog(store);
  54. store.dispatch(appNavigate(undefined));
  55. }
  56. break;
  57. }
  58. case CANCEL_WAIT_FOR_OWNER: {
  59. const result = next(action);
  60. store.dispatch(stopWaitForOwner());
  61. store.dispatch(appNavigate(undefined));
  62. return result;
  63. }
  64. case CONFERENCE_FAILED:
  65. if (action.error === JitsiConferenceErrors.AUTHENTICATION_REQUIRED) {
  66. store.dispatch(waitForOwner());
  67. } else {
  68. store.dispatch(stopWaitForOwner());
  69. }
  70. break;
  71. case CONFERENCE_JOINED:
  72. if (_isWaitingForOwner(store)) {
  73. store.dispatch(stopWaitForOwner());
  74. }
  75. _hideLoginDialog(store);
  76. break;
  77. case CONFERENCE_LEFT:
  78. store.dispatch(stopWaitForOwner());
  79. break;
  80. case CONNECTION_ESTABLISHED:
  81. _hideLoginDialog(store);
  82. break;
  83. case CONNECTION_FAILED: {
  84. const { error } = action;
  85. error
  86. && error.name === JitsiConnectionErrors.PASSWORD_REQUIRED
  87. && store.dispatch(_openLoginDialog());
  88. break;
  89. }
  90. case STOP_WAIT_FOR_OWNER:
  91. _clearExistingWaitForOwnerTimeout(store);
  92. store.dispatch(hideDialog(WaitForOwnerDialog));
  93. break;
  94. case WAIT_FOR_OWNER: {
  95. _clearExistingWaitForOwnerTimeout(store);
  96. const { handler, timeoutMs } = action;
  97. action.waitForOwnerTimeoutID = setTimeout(handler, timeoutMs);
  98. // The WAIT_FOR_OWNER action is cyclic and we don't want to hide the
  99. // login dialog every few seconds.
  100. isDialogOpen(store, LoginDialog)
  101. || store.dispatch(_openWaitForOwnerDialog());
  102. break;
  103. }
  104. }
  105. return next(action);
  106. });
  107. /**
  108. * Will clear the wait for conference owner timeout handler if any is currently
  109. * set.
  110. *
  111. * @param {Object} store - The redux store.
  112. * @returns {void}
  113. */
  114. function _clearExistingWaitForOwnerTimeout({ getState }) {
  115. const { waitForOwnerTimeoutID } = getState()['features/authentication'];
  116. waitForOwnerTimeoutID && clearTimeout(waitForOwnerTimeoutID);
  117. }
  118. /**
  119. * Hides {@link LoginDialog} if it's currently displayed.
  120. *
  121. * @param {Object} store - The redux store.
  122. * @returns {void}
  123. */
  124. function _hideLoginDialog({ dispatch }) {
  125. dispatch(hideDialog(LoginDialog));
  126. }
  127. /**
  128. * Checks if the cyclic "wait for conference owner" task is currently scheduled.
  129. *
  130. * @param {Object} store - The redux store.
  131. * @returns {boolean}
  132. */
  133. function _isWaitingForOwner({ getState }) {
  134. return Boolean(getState()['features/authentication'].waitForOwnerTimeoutID);
  135. }