選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

middleware.js 8.0KB

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