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

middleware.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // @flow
  2. import { batch } from 'react-redux';
  3. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../base/app';
  4. import { CONFERENCE_FAILED, CONFERENCE_JOINED } from '../base/conference';
  5. import { JitsiConferenceErrors, JitsiConferenceEvents } from '../base/lib-jitsi-meet';
  6. import { getFirstLoadableAvatarUrl, getParticipantDisplayName } from '../base/participants';
  7. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  8. import { playSound, registerSound, unregisterSound } from '../base/sounds';
  9. import { isTestModeEnabled } from '../base/testing';
  10. import { NOTIFICATION_TYPE, showNotification } from '../notifications';
  11. import { shouldAutoKnock } from '../prejoin/functions';
  12. import { KNOCKING_PARTICIPANT_ARRIVED_OR_UPDATED } from './actionTypes';
  13. import {
  14. hideLobbyScreen,
  15. knockingParticipantLeft,
  16. openLobbyScreen,
  17. participantIsKnockingOrUpdated,
  18. setLobbyModeEnabled,
  19. startKnocking,
  20. setPasswordJoinFailed
  21. } from './actions';
  22. import { KNOCKING_PARTICIPANT_SOUND_ID } from './constants';
  23. import { KNOCKING_PARTICIPANT_FILE } from './sounds';
  24. declare var APP: Object;
  25. MiddlewareRegistry.register(store => next => action => {
  26. switch (action.type) {
  27. case APP_WILL_MOUNT:
  28. store.dispatch(registerSound(KNOCKING_PARTICIPANT_SOUND_ID, KNOCKING_PARTICIPANT_FILE));
  29. break;
  30. case APP_WILL_UNMOUNT:
  31. store.dispatch(unregisterSound(KNOCKING_PARTICIPANT_SOUND_ID));
  32. break;
  33. case CONFERENCE_FAILED:
  34. return _conferenceFailed(store, next, action);
  35. case CONFERENCE_JOINED:
  36. return _conferenceJoined(store, next, action);
  37. case KNOCKING_PARTICIPANT_ARRIVED_OR_UPDATED: {
  38. // We need the full update result to be in the store already
  39. const result = next(action);
  40. _findLoadableAvatarForKnockingParticipant(store, action.participant);
  41. return result;
  42. }
  43. }
  44. return next(action);
  45. });
  46. /**
  47. * Registers a change handler for state['features/base/conference'].conference to
  48. * set the event listeners needed for the lobby feature to operate.
  49. */
  50. StateListenerRegistry.register(
  51. state => state['features/base/conference'].conference,
  52. (conference, { dispatch, getState }, previousConference) => {
  53. if (conference && !previousConference) {
  54. conference.on(JitsiConferenceEvents.MEMBERS_ONLY_CHANGED, enabled => {
  55. dispatch(setLobbyModeEnabled(enabled));
  56. });
  57. conference.on(JitsiConferenceEvents.LOBBY_USER_JOINED, (id, name) => {
  58. batch(() => {
  59. dispatch(participantIsKnockingOrUpdated({
  60. id,
  61. name
  62. }));
  63. dispatch(playSound(KNOCKING_PARTICIPANT_SOUND_ID));
  64. if (typeof APP !== 'undefined') {
  65. APP.API.notifyKnockingParticipant({
  66. id,
  67. name
  68. });
  69. }
  70. });
  71. });
  72. conference.on(JitsiConferenceEvents.LOBBY_USER_UPDATED, (id, participant) => {
  73. dispatch(participantIsKnockingOrUpdated({
  74. ...participant,
  75. id
  76. }));
  77. });
  78. conference.on(JitsiConferenceEvents.LOBBY_USER_LEFT, id => {
  79. dispatch(knockingParticipantLeft(id));
  80. });
  81. conference.on(JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED, (origin, sender) =>
  82. _maybeSendLobbyNotification(origin, sender, {
  83. dispatch,
  84. getState
  85. })
  86. );
  87. }
  88. });
  89. /**
  90. * Function to handle the conference failed event and navigate the user to the lobby screen
  91. * based on the failure reason.
  92. *
  93. * @param {Object} store - The Redux store.
  94. * @param {Function} next - The Redux next function.
  95. * @param {Object} action - The Redux action.
  96. * @returns {Object}
  97. */
  98. function _conferenceFailed({ dispatch, getState }, next, action) {
  99. const { error } = action;
  100. const state = getState();
  101. const nonFirstFailure = Boolean(state['features/base/conference'].membersOnly);
  102. if (error.name === JitsiConferenceErrors.MEMBERS_ONLY_ERROR) {
  103. if (typeof error.recoverable === 'undefined') {
  104. error.recoverable = true;
  105. }
  106. const result = next(action);
  107. dispatch(openLobbyScreen());
  108. if (shouldAutoKnock(state)) {
  109. dispatch(startKnocking());
  110. }
  111. dispatch(setPasswordJoinFailed(nonFirstFailure));
  112. return result;
  113. }
  114. dispatch(hideLobbyScreen());
  115. if (error.name === JitsiConferenceErrors.CONFERENCE_ACCESS_DENIED) {
  116. dispatch(showNotification({
  117. appearance: NOTIFICATION_TYPE.ERROR,
  118. hideErrorSupportLink: true,
  119. titleKey: 'lobby.joinRejectedMessage'
  120. }));
  121. }
  122. return next(action);
  123. }
  124. /**
  125. * Handles cleanup of lobby state when a conference is joined.
  126. *
  127. * @param {Object} store - The Redux store.
  128. * @param {Function} next - The Redux next function.
  129. * @param {Object} action - The Redux action.
  130. * @returns {Object}
  131. */
  132. function _conferenceJoined({ dispatch }, next, action) {
  133. dispatch(hideLobbyScreen());
  134. return next(action);
  135. }
  136. /**
  137. * Finds the loadable avatar URL and updates the participant accordingly.
  138. *
  139. * @param {Object} store - The Redux store.
  140. * @param {Object} participant - The knocking participant.
  141. * @returns {void}
  142. */
  143. function _findLoadableAvatarForKnockingParticipant(store, { id }) {
  144. const { dispatch, getState } = store;
  145. const updatedParticipant = getState()['features/lobby'].knockingParticipants.find(p => p.id === id);
  146. const { disableThirdPartyRequests } = getState()['features/base/config'];
  147. if (!disableThirdPartyRequests && updatedParticipant && !updatedParticipant.loadableAvatarUrl) {
  148. getFirstLoadableAvatarUrl(updatedParticipant, store).then(loadableAvatarUrl => {
  149. if (loadableAvatarUrl) {
  150. dispatch(participantIsKnockingOrUpdated({
  151. loadableAvatarUrl,
  152. id
  153. }));
  154. }
  155. });
  156. }
  157. }
  158. /**
  159. * Check the endpoint message that arrived through the conference and
  160. * sends a lobby notification, if the message belongs to the feature.
  161. *
  162. * @param {Object} origin - The origin (initiator) of the message.
  163. * @param {Object} message - The actual message.
  164. * @param {Object} store - The Redux store.
  165. * @returns {void}
  166. */
  167. function _maybeSendLobbyNotification(origin, message, { dispatch, getState }) {
  168. if (!origin?._id || message?.type !== 'lobby-notify') {
  169. return;
  170. }
  171. const notificationProps: any = {
  172. descriptionArguments: {
  173. originParticipantName: getParticipantDisplayName(getState, origin._id),
  174. targetParticipantName: message.name
  175. },
  176. titleKey: 'lobby.notificationTitle'
  177. };
  178. switch (message.event) {
  179. case 'LOBBY-ENABLED':
  180. notificationProps.descriptionKey = `lobby.notificationLobby${message.value ? 'En' : 'Dis'}abled`;
  181. break;
  182. case 'LOBBY-ACCESS-GRANTED':
  183. notificationProps.descriptionKey = 'lobby.notificationLobbyAccessGranted';
  184. break;
  185. case 'LOBBY-ACCESS-DENIED':
  186. notificationProps.descriptionKey = 'lobby.notificationLobbyAccessDenied';
  187. break;
  188. }
  189. dispatch(showNotification(notificationProps, isTestModeEnabled(getState()) ? undefined : 5000));
  190. }