您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.ts 5.5KB

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