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.ts 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { AnyAction } from 'redux';
  2. // @ts-expect-error
  3. import UIEvents from '../../../service/UI/UIEvents';
  4. import { IStore } from '../app/types';
  5. import {
  6. CONFERENCE_FAILED,
  7. CONFERENCE_JOINED,
  8. LOCK_STATE_CHANGED,
  9. SET_PASSWORD_FAILED
  10. } from '../base/conference/actionTypes';
  11. import { hideDialog } from '../base/dialog/actions';
  12. import { JitsiConferenceErrors } from '../base/lib-jitsi-meet';
  13. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  14. import { showNotification } from '../notifications/actions';
  15. import { NOTIFICATION_TIMEOUT_TYPE } from '../notifications/constants';
  16. import { _openPasswordRequiredPrompt } from './actions';
  17. // eslint-disable-next-line lines-around-comment
  18. // @ts-ignore
  19. import { PasswordRequiredPrompt } from './components';
  20. import { LOCKED_REMOTELY } from './constants';
  21. import logger from './logger';
  22. /**
  23. * Middleware that captures conference failed and checks for password required
  24. * error and requests a dialog for user to enter password.
  25. *
  26. * @param {Store} store - The redux store.
  27. * @returns {Function}
  28. */
  29. MiddlewareRegistry.register(store => next => action => {
  30. switch (action.type) {
  31. case CONFERENCE_FAILED:
  32. return _conferenceFailed(store, next, action);
  33. case CONFERENCE_JOINED:
  34. return _conferenceJoined(store, next, action);
  35. case LOCK_STATE_CHANGED: {
  36. // TODO Remove this logic when all components interested in the lock
  37. // state change event are moved into react/redux.
  38. if (typeof APP !== 'undefined') {
  39. APP.UI.emitEvent(UIEvents.TOGGLE_ROOM_LOCK, action.locked);
  40. }
  41. const previousLockedState = store.getState()['features/base/conference'].locked;
  42. const result = next(action);
  43. const currentLockedState = store.getState()['features/base/conference'].locked;
  44. if (currentLockedState === LOCKED_REMOTELY) {
  45. store.dispatch(
  46. showNotification({
  47. titleKey: 'notify.passwordSetRemotely'
  48. }, NOTIFICATION_TIMEOUT_TYPE.SHORT));
  49. } else if (previousLockedState === LOCKED_REMOTELY && !currentLockedState) {
  50. store.dispatch(
  51. showNotification({
  52. titleKey: 'notify.passwordRemovedRemotely'
  53. }, NOTIFICATION_TIMEOUT_TYPE.SHORT));
  54. }
  55. return result;
  56. }
  57. case SET_PASSWORD_FAILED:
  58. return _setPasswordFailed(store, next, action);
  59. }
  60. return next(action);
  61. });
  62. /**
  63. * Handles cleanup of lock prompt state when a conference is joined.
  64. *
  65. * @param {Store} store - The redux store in which the specified action is being
  66. * dispatched.
  67. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  68. * specified action to the specified store.
  69. * @param {Action} action - The redux action {@code CONFERENCE_JOINED} which
  70. * specifies the details associated with joining the conference.
  71. * @private
  72. * @returns {*}
  73. */
  74. function _conferenceJoined({ dispatch }: IStore, next: Function, action: AnyAction) {
  75. dispatch(hideDialog(PasswordRequiredPrompt));
  76. return next(action);
  77. }
  78. /**
  79. * Handles errors that occur when a conference fails.
  80. *
  81. * @param {Store} store - The redux store in which the specified action is being
  82. * dispatched.
  83. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  84. * specified action to the specified store.
  85. * @param {Action} action - The redux action {@code CONFERENCE_FAILED} which
  86. * specifies the details associated with the error and the failed conference.
  87. * @private
  88. * @returns {*}
  89. */
  90. function _conferenceFailed({ dispatch }: IStore, next: Function, action: AnyAction) {
  91. const { conference, error } = action;
  92. if (conference && error.name === JitsiConferenceErrors.PASSWORD_REQUIRED) {
  93. // XXX The feature room-lock affords recovery after CONFERENCE_FAILED
  94. // caused by JitsiConferenceErrors.PASSWORD_REQUIRED.
  95. if (typeof error.recoverable === 'undefined') {
  96. error.recoverable = true;
  97. }
  98. if (error.recoverable) {
  99. dispatch(_openPasswordRequiredPrompt(conference));
  100. }
  101. } else {
  102. dispatch(hideDialog(PasswordRequiredPrompt));
  103. }
  104. return next(action);
  105. }
  106. /**
  107. * Handles errors that occur when a password fails to be set.
  108. *
  109. * @param {Store} store - The redux store in which the specified action is being
  110. * dispatched.
  111. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  112. * specified action to the specified store.
  113. * @param {Action} action - The redux action {@code SET_PASSWORD_ERROR} which
  114. * has the error type that should be handled.
  115. * @private
  116. * @returns {*}
  117. */
  118. function _setPasswordFailed(store: IStore, next: Function, action: AnyAction) {
  119. if (typeof APP !== 'undefined') {
  120. // TODO Remove this logic when displaying of error messages on web is
  121. // handled through react/redux.
  122. const { error } = action;
  123. let descriptionKey;
  124. let titleKey;
  125. if (error === JitsiConferenceErrors.PASSWORD_NOT_SUPPORTED) {
  126. logger.warn('room passwords not supported');
  127. descriptionKey = 'dialog.passwordNotSupported';
  128. titleKey = 'dialog.passwordNotSupportedTitle';
  129. } else {
  130. logger.warn('setting password failed', error);
  131. descriptionKey = 'dialog.lockMessage';
  132. titleKey = 'dialog.lockTitle';
  133. }
  134. APP.UI.messageHandler.showError({
  135. descriptionKey,
  136. titleKey
  137. });
  138. }
  139. return next(action);
  140. }