Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

middleware.js 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. localParticipantRejected,
  38. participantRejected
  39. } from './actions';
  40. import {
  41. ASKED_TO_UNMUTE_SOUND_ID, AUDIO_MODERATION_NOTIFICATION_ID,
  42. CS_MODERATION_NOTIFICATION_ID,
  43. VIDEO_MODERATION_NOTIFICATION_ID
  44. } from './constants';
  45. import {
  46. isEnabledFromState,
  47. isParticipantApproved,
  48. isParticipantPending
  49. } from './functions';
  50. import { ASKED_TO_UNMUTE_FILE } from './sounds';
  51. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  52. const { type } = action;
  53. const { conference } = getConferenceState(getState());
  54. switch (type) {
  55. case APP_WILL_MOUNT: {
  56. dispatch(registerSound(ASKED_TO_UNMUTE_SOUND_ID, ASKED_TO_UNMUTE_FILE));
  57. break;
  58. }
  59. case APP_WILL_UNMOUNT: {
  60. dispatch(unregisterSound(ASKED_TO_UNMUTE_SOUND_ID));
  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. uid = AUDIO_MODERATION_NOTIFICATION_ID;
  71. break;
  72. }
  73. case MEDIA_TYPE.VIDEO: {
  74. titleKey = 'notify.moderationInEffectVideoTitle';
  75. uid = VIDEO_MODERATION_NOTIFICATION_ID;
  76. break;
  77. }
  78. case MEDIA_TYPE.PRESENTER: {
  79. titleKey = 'notify.moderationInEffectCSTitle';
  80. uid = CS_MODERATION_NOTIFICATION_ID;
  81. break;
  82. }
  83. }
  84. dispatch(showNotification({
  85. customActionNameKey: 'notify.raiseHandAction',
  86. customActionHandler: () => batch(() => {
  87. dispatch(raiseHand(true));
  88. dispatch(hideNotification(uid));
  89. }),
  90. descriptionKey,
  91. sticky: true,
  92. titleKey,
  93. uid
  94. }));
  95. break;
  96. }
  97. case REQUEST_DISABLE_AUDIO_MODERATION: {
  98. conference.disableAVModeration(MEDIA_TYPE.AUDIO);
  99. break;
  100. }
  101. case REQUEST_DISABLE_VIDEO_MODERATION: {
  102. conference.disableAVModeration(MEDIA_TYPE.VIDEO);
  103. break;
  104. }
  105. case REQUEST_ENABLE_AUDIO_MODERATION: {
  106. conference.enableAVModeration(MEDIA_TYPE.AUDIO);
  107. break;
  108. }
  109. case REQUEST_ENABLE_VIDEO_MODERATION: {
  110. conference.enableAVModeration(MEDIA_TYPE.VIDEO);
  111. break;
  112. }
  113. case PARTICIPANT_UPDATED: {
  114. const state = getState();
  115. const audioModerationEnabled = isEnabledFromState(MEDIA_TYPE.AUDIO, state);
  116. const participant = action.participant;
  117. if (participant && audioModerationEnabled) {
  118. if (isLocalParticipantModerator(state)) {
  119. // this is handled only by moderators
  120. if (participant.raisedHand) {
  121. // if participant raises hand show notification
  122. !isParticipantApproved(participant.id, MEDIA_TYPE.AUDIO)(state)
  123. && dispatch(participantPendingAudio(participant));
  124. } else {
  125. // if participant lowers hand hide notification
  126. isParticipantPending(participant, MEDIA_TYPE.AUDIO)(state)
  127. && dispatch(dismissPendingAudioParticipant(participant));
  128. }
  129. } else if (participant.id === getLocalParticipant(state).id
  130. && /* the new role */ isParticipantModerator(participant)) {
  131. // this is the granted moderator case
  132. getRemoteParticipants(state).forEach(p => {
  133. p.raisedHand && !isParticipantApproved(p.id, MEDIA_TYPE.AUDIO)(state)
  134. && dispatch(participantPendingAudio(p));
  135. });
  136. }
  137. }
  138. break;
  139. }
  140. }
  141. return next(action);
  142. });
  143. /**
  144. * Registers a change handler for state['features/base/conference'].conference to
  145. * set the event listeners needed for the A/V moderation feature to operate.
  146. */
  147. StateListenerRegistry.register(
  148. state => state['features/base/conference'].conference,
  149. (conference, { dispatch }, previousConference) => {
  150. if (conference && !previousConference) {
  151. // local participant is allowed to unmute
  152. conference.on(JitsiConferenceEvents.AV_MODERATION_APPROVED, ({ mediaType }) => {
  153. dispatch(localParticipantApproved(mediaType));
  154. // Audio & video moderation are both enabled at the same time.
  155. // Avoid displaying 2 different notifications.
  156. if (mediaType === MEDIA_TYPE.AUDIO) {
  157. dispatch(showNotification({
  158. titleKey: 'notify.hostAskedUnmute',
  159. sticky: true,
  160. customActionNameKey: 'notify.unmute',
  161. customActionHandler: () => dispatch(muteLocal(false, MEDIA_TYPE.AUDIO))
  162. }));
  163. dispatch(playSound(ASKED_TO_UNMUTE_SOUND_ID));
  164. }
  165. });
  166. conference.on(JitsiConferenceEvents.AV_MODERATION_REJECTED, ({ mediaType }) => {
  167. dispatch(localParticipantRejected(mediaType));
  168. });
  169. conference.on(JitsiConferenceEvents.AV_MODERATION_CHANGED, ({ enabled, mediaType, actor }) => {
  170. enabled ? dispatch(enableModeration(mediaType, actor)) : dispatch(disableModeration(mediaType, actor));
  171. });
  172. // this is received by moderators
  173. conference.on(
  174. JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_APPROVED,
  175. ({ participant, mediaType }) => {
  176. const { _id: id } = participant;
  177. batch(() => {
  178. // store in the whitelist
  179. dispatch(participantApproved(id, mediaType));
  180. // remove from pending list
  181. dispatch(dismissPendingParticipant(id, mediaType));
  182. });
  183. });
  184. // this is received by moderators
  185. conference.on(
  186. JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_REJECTED,
  187. ({ participant, mediaType }) => {
  188. const { _id: id } = participant;
  189. dispatch(participantRejected(id, mediaType));
  190. });
  191. }
  192. });