Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.native.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 { setPrejoinPageVisibility } from '../prejoin';
  17. import {
  18. CANCEL_LOGIN,
  19. STOP_WAIT_FOR_OWNER,
  20. UPGRADE_ROLE_FINISHED,
  21. WAIT_FOR_OWNER
  22. } from './actionTypes';
  23. import {
  24. openLoginDialog,
  25. openWaitForOwnerDialog,
  26. stopWaitForOwner,
  27. waitForOwner } from './actions.native';
  28. import { LoginDialog, WaitForOwnerDialog } from './components';
  29. /**
  30. * Middleware that captures connection or conference failed errors and controls
  31. * {@link WaitForOwnerDialog} and {@link LoginDialog}.
  32. *
  33. * FIXME Some of the complexity was introduced by the lack of dialog stacking.
  34. *
  35. * @param {Store} store - Redux store.
  36. * @returns {Function}
  37. */
  38. MiddlewareRegistry.register(store => next => action => {
  39. switch (action.type) {
  40. case CANCEL_LOGIN: {
  41. const { dispatch, getState } = store;
  42. const { thenableWithCancel } = getState()['features/authentication'];
  43. thenableWithCancel && thenableWithCancel.cancel();
  44. // The LoginDialog can be opened on top of "wait for owner". The app
  45. // should navigate only if LoginDialog was open without the
  46. // WaitForOwnerDialog.
  47. if (!isDialogOpen(store, WaitForOwnerDialog)) {
  48. if (_isWaitingForOwner(store)) {
  49. // Instead of hiding show the new one.
  50. const result = next(action);
  51. dispatch(openWaitForOwnerDialog());
  52. return result;
  53. }
  54. // Go back to the app's entry point.
  55. _hideLoginDialog(store);
  56. const state = getState();
  57. const { authRequired, conference } = state['features/base/conference'];
  58. const { passwordRequired } = state['features/base/connection'];
  59. // Only end the meeting if we are not already inside and trying to upgrade.
  60. // NOTE: Despite it's confusing name, `passwordRequired` implies an XMPP
  61. // connection auth error.
  62. if ((passwordRequired || authRequired) && !conference) {
  63. dispatch(appNavigate(undefined));
  64. }
  65. }
  66. break;
  67. }
  68. case CONFERENCE_FAILED: {
  69. const { error } = action;
  70. // XXX The feature authentication affords recovery from
  71. // CONFERENCE_FAILED caused by
  72. // JitsiConferenceErrors.AUTHENTICATION_REQUIRED.
  73. let recoverable;
  74. if (error.name === JitsiConferenceErrors.AUTHENTICATION_REQUIRED) {
  75. if (typeof error.recoverable === 'undefined') {
  76. error.recoverable = true;
  77. }
  78. recoverable = error.recoverable;
  79. }
  80. if (recoverable) {
  81. store.dispatch(waitForOwner());
  82. } else {
  83. store.dispatch(stopWaitForOwner());
  84. }
  85. break;
  86. }
  87. case CONFERENCE_JOINED:
  88. if (_isWaitingForOwner(store)) {
  89. store.dispatch(stopWaitForOwner());
  90. }
  91. _hideLoginDialog(store);
  92. break;
  93. case CONFERENCE_LEFT:
  94. store.dispatch(stopWaitForOwner());
  95. break;
  96. case CONNECTION_ESTABLISHED:
  97. _hideLoginDialog(store);
  98. break;
  99. case CONNECTION_FAILED: {
  100. const { error } = action;
  101. if (error
  102. && error.name === JitsiConnectionErrors.PASSWORD_REQUIRED
  103. && typeof error.recoverable === 'undefined') {
  104. error.recoverable = true;
  105. store.dispatch(setPrejoinPageVisibility(false));
  106. store.dispatch(openLoginDialog());
  107. }
  108. break;
  109. }
  110. case STOP_WAIT_FOR_OWNER:
  111. _clearExistingWaitForOwnerTimeout(store);
  112. store.dispatch(hideDialog(WaitForOwnerDialog));
  113. break;
  114. case UPGRADE_ROLE_FINISHED: {
  115. const { error, progress } = action;
  116. if (!error && progress === 1) {
  117. _hideLoginDialog(store);
  118. }
  119. break;
  120. }
  121. case WAIT_FOR_OWNER: {
  122. _clearExistingWaitForOwnerTimeout(store);
  123. const { handler, timeoutMs } = action;
  124. action.waitForOwnerTimeoutID = setTimeout(handler, timeoutMs);
  125. // The WAIT_FOR_OWNER action is cyclic and we don't want to hide the
  126. // login dialog every few seconds.
  127. isDialogOpen(store, LoginDialog)
  128. || store.dispatch(openWaitForOwnerDialog());
  129. break;
  130. }
  131. }
  132. return next(action);
  133. });
  134. /**
  135. * Will clear the wait for conference owner timeout handler if any is currently
  136. * set.
  137. *
  138. * @param {Object} store - The redux store.
  139. * @returns {void}
  140. */
  141. function _clearExistingWaitForOwnerTimeout(
  142. { getState }: { getState: Function }) {
  143. const { waitForOwnerTimeoutID } = getState()['features/authentication'];
  144. waitForOwnerTimeoutID && clearTimeout(waitForOwnerTimeoutID);
  145. }
  146. /**
  147. * Hides {@link LoginDialog} if it's currently displayed.
  148. *
  149. * @param {Object} store - The redux store.
  150. * @returns {void}
  151. */
  152. function _hideLoginDialog({ dispatch }: { dispatch: Dispatch<any> }) {
  153. dispatch(hideDialog(LoginDialog));
  154. }
  155. /**
  156. * Checks if the cyclic "wait for conference owner" task is currently scheduled.
  157. *
  158. * @param {Object} store - The redux store.
  159. * @returns {boolean}
  160. */
  161. function _isWaitingForOwner({ getState }: { getState: Function }) {
  162. return Boolean(getState()['features/authentication'].waitForOwnerTimeoutID);
  163. }