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

middleware.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // @flow
  2. import { batch } from 'react-redux';
  3. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../base/app';
  4. import { getConferenceState } from '../base/conference';
  5. import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
  6. import { MEDIA_TYPE } from '../base/media';
  7. import {
  8. getLocalParticipant,
  9. getRemoteParticipants,
  10. isLocalParticipantModerator,
  11. isParticipantModerator,
  12. PARTICIPANT_UPDATED,
  13. raiseHand
  14. } from '../base/participants';
  15. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  16. import { playSound, registerSound, unregisterSound } from '../base/sounds';
  17. import {
  18. hideNotification,
  19. showNotification
  20. } from '../notifications';
  21. import { muteLocal } from '../video-menu/actions.any';
  22. import {
  23. LOCAL_PARTICIPANT_MODERATION_NOTIFICATION,
  24. REQUEST_DISABLE_AUDIO_MODERATION,
  25. REQUEST_DISABLE_VIDEO_MODERATION,
  26. REQUEST_ENABLE_AUDIO_MODERATION,
  27. REQUEST_ENABLE_VIDEO_MODERATION
  28. } from './actionTypes';
  29. import {
  30. disableModeration,
  31. dismissPendingParticipant,
  32. dismissPendingAudioParticipant,
  33. enableModeration,
  34. localParticipantApproved,
  35. participantApproved,
  36. participantPendingAudio
  37. } from './actions';
  38. import {
  39. ASKED_TO_UNMUTE_SOUND_ID, AUDIO_MODERATION_NOTIFICATION_ID,
  40. CS_MODERATION_NOTIFICATION_ID,
  41. VIDEO_MODERATION_NOTIFICATION_ID
  42. } from './constants';
  43. import {
  44. isEnabledFromState,
  45. isParticipantApproved,
  46. isParticipantPending
  47. } from './functions';
  48. import { ASKED_TO_UNMUTE_FILE } from './sounds';
  49. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  50. const { type } = action;
  51. const { conference } = getConferenceState(getState());
  52. switch (type) {
  53. case APP_WILL_MOUNT: {
  54. dispatch(registerSound(ASKED_TO_UNMUTE_SOUND_ID, ASKED_TO_UNMUTE_FILE));
  55. break;
  56. }
  57. case APP_WILL_UNMOUNT: {
  58. dispatch(unregisterSound(ASKED_TO_UNMUTE_SOUND_ID));
  59. break;
  60. }
  61. case LOCAL_PARTICIPANT_MODERATION_NOTIFICATION: {
  62. let descriptionKey;
  63. let titleKey;
  64. let uid;
  65. switch (action.mediaType) {
  66. case MEDIA_TYPE.AUDIO: {
  67. titleKey = 'notify.moderationInEffectTitle';
  68. uid = AUDIO_MODERATION_NOTIFICATION_ID;
  69. break;
  70. }
  71. case MEDIA_TYPE.VIDEO: {
  72. titleKey = 'notify.moderationInEffectVideoTitle';
  73. uid = VIDEO_MODERATION_NOTIFICATION_ID;
  74. break;
  75. }
  76. case MEDIA_TYPE.PRESENTER: {
  77. titleKey = 'notify.moderationInEffectCSTitle';
  78. uid = CS_MODERATION_NOTIFICATION_ID;
  79. break;
  80. }
  81. }
  82. dispatch(showNotification({
  83. customActionNameKey: 'notify.raiseHandAction',
  84. customActionHandler: () => batch(() => {
  85. dispatch(raiseHand(true));
  86. dispatch(hideNotification(uid));
  87. }),
  88. descriptionKey,
  89. sticky: true,
  90. titleKey,
  91. uid
  92. }));
  93. break;
  94. }
  95. case REQUEST_DISABLE_AUDIO_MODERATION: {
  96. conference.disableAVModeration(MEDIA_TYPE.AUDIO);
  97. break;
  98. }
  99. case REQUEST_DISABLE_VIDEO_MODERATION: {
  100. conference.disableAVModeration(MEDIA_TYPE.VIDEO);
  101. break;
  102. }
  103. case REQUEST_ENABLE_AUDIO_MODERATION: {
  104. conference.enableAVModeration(MEDIA_TYPE.AUDIO);
  105. break;
  106. }
  107. case REQUEST_ENABLE_VIDEO_MODERATION: {
  108. conference.enableAVModeration(MEDIA_TYPE.VIDEO);
  109. break;
  110. }
  111. case PARTICIPANT_UPDATED: {
  112. const state = getState();
  113. const audioModerationEnabled = isEnabledFromState(MEDIA_TYPE.AUDIO, state);
  114. const participant = action.participant;
  115. if (participant && audioModerationEnabled) {
  116. if (isLocalParticipantModerator(state)) {
  117. // this is handled only by moderators
  118. if (participant.raisedHand) {
  119. // if participant raises hand show notification
  120. !isParticipantApproved(participant.id, MEDIA_TYPE.AUDIO)(state)
  121. && dispatch(participantPendingAudio(participant));
  122. } else {
  123. // if participant lowers hand hide notification
  124. isParticipantPending(participant, MEDIA_TYPE.AUDIO)(state)
  125. && dispatch(dismissPendingAudioParticipant(participant));
  126. }
  127. } else if (participant.id === getLocalParticipant(state).id
  128. && /* the new role */ isParticipantModerator(participant)) {
  129. // this is the granted moderator case
  130. getRemoteParticipants(state).forEach(p => {
  131. p.raisedHand && !isParticipantApproved(p.id, MEDIA_TYPE.AUDIO)(state)
  132. && dispatch(participantPendingAudio(p));
  133. });
  134. }
  135. }
  136. break;
  137. }
  138. }
  139. return next(action);
  140. });
  141. /**
  142. * Registers a change handler for state['features/base/conference'].conference to
  143. * set the event listeners needed for the A/V moderation feature to operate.
  144. */
  145. StateListenerRegistry.register(
  146. state => state['features/base/conference'].conference,
  147. (conference, { dispatch }, previousConference) => {
  148. if (conference && !previousConference) {
  149. // local participant is allowed to unmute
  150. conference.on(JitsiConferenceEvents.AV_MODERATION_APPROVED, ({ mediaType }) => {
  151. dispatch(localParticipantApproved(mediaType));
  152. // Audio & video moderation are both enabled at the same time.
  153. // Avoid displaying 2 different notifications.
  154. if (mediaType === MEDIA_TYPE.AUDIO) {
  155. dispatch(showNotification({
  156. titleKey: 'notify.hostAskedUnmute',
  157. sticky: true,
  158. customActionNameKey: 'notify.unmute',
  159. customActionHandler: () => dispatch(muteLocal(false, MEDIA_TYPE.AUDIO))
  160. }));
  161. dispatch(playSound(ASKED_TO_UNMUTE_SOUND_ID));
  162. }
  163. });
  164. conference.on(JitsiConferenceEvents.AV_MODERATION_CHANGED, ({ enabled, mediaType, actor }) => {
  165. enabled ? dispatch(enableModeration(mediaType, actor)) : dispatch(disableModeration(mediaType, actor));
  166. });
  167. // this is received by moderators
  168. conference.on(
  169. JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_APPROVED,
  170. ({ participant, mediaType }) => {
  171. const { _id: id } = participant;
  172. batch(() => {
  173. // store in the whitelist
  174. dispatch(participantApproved(id, mediaType));
  175. // remove from pending list
  176. dispatch(dismissPendingParticipant(id, mediaType));
  177. });
  178. });
  179. }
  180. });