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 6.3KB

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