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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. getParticipantDisplayName,
  8. isLocalParticipantModerator,
  9. PARTICIPANT_UPDATED,
  10. raiseHand
  11. } from '../base/participants';
  12. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  13. import {
  14. hideNotification,
  15. NOTIFICATION_TIMEOUT,
  16. showNotification
  17. } from '../notifications';
  18. import {
  19. DISABLE_MODERATION,
  20. ENABLE_MODERATION,
  21. LOCAL_PARTICIPANT_MODERATION_NOTIFICATION,
  22. REQUEST_DISABLE_MODERATION,
  23. REQUEST_ENABLE_MODERATION
  24. } from './actionTypes';
  25. import {
  26. disableModeration,
  27. dismissPendingParticipant,
  28. dismissPendingAudioParticipant,
  29. enableModeration,
  30. localParticipantApproved,
  31. participantApproved,
  32. participantPendingAudio
  33. } from './actions';
  34. import {
  35. isEnabledFromState,
  36. isParticipantApproved,
  37. isParticipantPending
  38. } from './functions';
  39. const VIDEO_MODERATION_NOTIFICATION_ID = 'video-moderation';
  40. const AUDIO_MODERATION_NOTIFICATION_ID = 'audio-moderation';
  41. const CS_MODERATION_NOTIFICATION_ID = 'video-moderation';
  42. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  43. const { actor, mediaType, type } = action;
  44. switch (type) {
  45. case DISABLE_MODERATION:
  46. case ENABLE_MODERATION: {
  47. // Audio & video moderation are both enabled at the same time.
  48. // Avoid displaying 2 different notifications.
  49. if (mediaType === MEDIA_TYPE.VIDEO) {
  50. const titleKey = type === ENABLE_MODERATION
  51. ? 'notify.moderationStartedTitle'
  52. : 'notify.moderationStoppedTitle';
  53. dispatch(showNotification({
  54. descriptionKey: actor ? 'notify.moderationToggleDescription' : undefined,
  55. descriptionArguments: actor ? {
  56. participantDisplayName: getParticipantDisplayName(getState, actor.getId())
  57. } : undefined,
  58. titleKey
  59. }, NOTIFICATION_TIMEOUT));
  60. }
  61. break;
  62. }
  63. case LOCAL_PARTICIPANT_MODERATION_NOTIFICATION: {
  64. let descriptionKey;
  65. let titleKey;
  66. let uid;
  67. switch (action.mediaType) {
  68. case MEDIA_TYPE.AUDIO: {
  69. titleKey = 'notify.moderationInEffectTitle';
  70. descriptionKey = 'notify.moderationInEffectDescription';
  71. uid = AUDIO_MODERATION_NOTIFICATION_ID;
  72. break;
  73. }
  74. case MEDIA_TYPE.VIDEO: {
  75. titleKey = 'notify.moderationInEffectVideoTitle';
  76. descriptionKey = 'notify.moderationInEffectVideoDescription';
  77. uid = VIDEO_MODERATION_NOTIFICATION_ID;
  78. break;
  79. }
  80. case MEDIA_TYPE.PRESENTER: {
  81. titleKey = 'notify.moderationInEffectCSTitle';
  82. descriptionKey = 'notify.moderationInEffectCSDescription';
  83. uid = CS_MODERATION_NOTIFICATION_ID;
  84. break;
  85. }
  86. }
  87. dispatch(showNotification({
  88. customActionNameKey: 'notify.raiseHandAction',
  89. customActionHandler: () => batch(() => {
  90. dispatch(raiseHand(true));
  91. dispatch(hideNotification(uid));
  92. }),
  93. descriptionKey,
  94. sticky: true,
  95. titleKey,
  96. uid
  97. }));
  98. break;
  99. }
  100. case REQUEST_DISABLE_MODERATION: {
  101. const { conference } = getConferenceState(getState());
  102. conference.disableAVModeration(MEDIA_TYPE.AUDIO);
  103. conference.disableAVModeration(MEDIA_TYPE.VIDEO);
  104. break;
  105. }
  106. case REQUEST_ENABLE_MODERATION: {
  107. const { conference } = getConferenceState(getState());
  108. conference.enableAVModeration(MEDIA_TYPE.AUDIO);
  109. conference.enableAVModeration(MEDIA_TYPE.VIDEO);
  110. break;
  111. }
  112. case PARTICIPANT_UPDATED: {
  113. const state = getState();
  114. const audioModerationEnabled = isEnabledFromState(MEDIA_TYPE.AUDIO, state);
  115. // this is handled only by moderators
  116. if (audioModerationEnabled && isLocalParticipantModerator(state)) {
  117. const participant = action.participant;
  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. }
  128. break;
  129. }
  130. }
  131. return next(action);
  132. });
  133. /**
  134. * Registers a change handler for state['features/base/conference'].conference to
  135. * set the event listeners needed for the A/V moderation feature to operate.
  136. */
  137. StateListenerRegistry.register(
  138. state => state['features/base/conference'].conference,
  139. (conference, { dispatch }, previousConference) => {
  140. if (conference && !previousConference) {
  141. // local participant is allowed to unmute
  142. conference.on(JitsiConferenceEvents.AV_MODERATION_APPROVED, ({ mediaType }) => {
  143. dispatch(localParticipantApproved(mediaType));
  144. // Audio & video moderation are both enabled at the same time.
  145. // Avoid displaying 2 different notifications.
  146. if (mediaType === MEDIA_TYPE.VIDEO) {
  147. dispatch(showNotification({
  148. titleKey: 'notify.unmute',
  149. descriptionKey: 'notify.hostAskedUnmute',
  150. sticky: true
  151. }));
  152. }
  153. });
  154. conference.on(JitsiConferenceEvents.AV_MODERATION_CHANGED, ({ enabled, mediaType, actor }) => {
  155. enabled ? dispatch(enableModeration(mediaType, actor)) : dispatch(disableModeration(mediaType, actor));
  156. });
  157. // this is received by moderators
  158. conference.on(
  159. JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_APPROVED,
  160. ({ participant, mediaType }) => {
  161. const { _id: id } = participant;
  162. batch(() => {
  163. // store in the whitelist
  164. dispatch(participantApproved(id, mediaType));
  165. // remove from pending list
  166. dispatch(dismissPendingParticipant(id, mediaType));
  167. });
  168. });
  169. }
  170. });