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.native.js 4.9KB

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