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.5KB

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