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.js 7.7KB

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