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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. UPGRADE_ROLE_FINISHED,
  20. WAIT_FOR_OWNER
  21. } from './actionTypes';
  22. import {
  23. openLoginDialog,
  24. openWaitForOwnerDialog,
  25. stopWaitForOwner,
  26. waitForOwner } 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. const { authRequired, conference } = getState()['features/base/conference'];
  56. // Only end the meeting if we are not already inside and trying to upgrade.
  57. if (authRequired && !conference) {
  58. // FIXME Like cancelWaitForOwner, dispatch conferenceLeft to notify
  59. // the external-api.
  60. dispatch(appNavigate(undefined));
  61. }
  62. }
  63. break;
  64. }
  65. case CONFERENCE_FAILED: {
  66. const { error } = action;
  67. // XXX The feature authentication affords recovery from
  68. // CONFERENCE_FAILED caused by
  69. // JitsiConferenceErrors.AUTHENTICATION_REQUIRED.
  70. let recoverable;
  71. if (error.name === JitsiConferenceErrors.AUTHENTICATION_REQUIRED) {
  72. if (typeof error.recoverable === 'undefined') {
  73. error.recoverable = true;
  74. }
  75. recoverable = error.recoverable;
  76. }
  77. if (recoverable) {
  78. store.dispatch(waitForOwner());
  79. } else {
  80. store.dispatch(stopWaitForOwner());
  81. }
  82. break;
  83. }
  84. case CONFERENCE_JOINED:
  85. if (_isWaitingForOwner(store)) {
  86. store.dispatch(stopWaitForOwner());
  87. }
  88. _hideLoginDialog(store);
  89. break;
  90. case CONFERENCE_LEFT:
  91. store.dispatch(stopWaitForOwner());
  92. break;
  93. case CONNECTION_ESTABLISHED:
  94. _hideLoginDialog(store);
  95. break;
  96. case CONNECTION_FAILED: {
  97. const { error } = action;
  98. if (error
  99. && error.name === JitsiConnectionErrors.PASSWORD_REQUIRED
  100. && typeof error.recoverable === 'undefined') {
  101. error.recoverable = true;
  102. store.dispatch(openLoginDialog());
  103. }
  104. break;
  105. }
  106. case STOP_WAIT_FOR_OWNER:
  107. _clearExistingWaitForOwnerTimeout(store);
  108. store.dispatch(hideDialog(WaitForOwnerDialog));
  109. break;
  110. case UPGRADE_ROLE_FINISHED: {
  111. const { error, progress } = action;
  112. if (!error && progress === 1) {
  113. _hideLoginDialog(store);
  114. }
  115. break;
  116. }
  117. case WAIT_FOR_OWNER: {
  118. _clearExistingWaitForOwnerTimeout(store);
  119. const { handler, timeoutMs } = action;
  120. action.waitForOwnerTimeoutID = setTimeout(handler, timeoutMs);
  121. // The WAIT_FOR_OWNER action is cyclic and we don't want to hide the
  122. // login dialog every few seconds.
  123. isDialogOpen(store, LoginDialog)
  124. || store.dispatch(openWaitForOwnerDialog());
  125. break;
  126. }
  127. }
  128. return next(action);
  129. });
  130. /**
  131. * Will clear the wait for conference owner timeout handler if any is currently
  132. * set.
  133. *
  134. * @param {Object} store - The redux store.
  135. * @returns {void}
  136. */
  137. function _clearExistingWaitForOwnerTimeout(
  138. { getState }: { getState: Function }) {
  139. const { waitForOwnerTimeoutID } = getState()['features/authentication'];
  140. waitForOwnerTimeoutID && clearTimeout(waitForOwnerTimeoutID);
  141. }
  142. /**
  143. * Hides {@link LoginDialog} if it's currently displayed.
  144. *
  145. * @param {Object} store - The redux store.
  146. * @returns {void}
  147. */
  148. function _hideLoginDialog({ dispatch }: { dispatch: Dispatch<any> }) {
  149. dispatch(hideDialog(LoginDialog));
  150. }
  151. /**
  152. * Checks if the cyclic "wait for conference owner" task is currently scheduled.
  153. *
  154. * @param {Object} store - The redux store.
  155. * @returns {boolean}
  156. */
  157. function _isWaitingForOwner({ getState }: { getState: Function }) {
  158. return Boolean(getState()['features/authentication'].waitForOwnerTimeoutID);
  159. }