Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.ts 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import { batch } from 'react-redux';
  2. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../base/app/actionTypes';
  3. import { getConferenceState } from '../base/conference/functions';
  4. import { JitsiConferenceEvents } from '../base/lib-jitsi-meet';
  5. import { MEDIA_TYPE, MediaType } from '../base/media/constants';
  6. import { PARTICIPANT_UPDATED } from '../base/participants/actionTypes';
  7. import { raiseHand } from '../base/participants/actions';
  8. import {
  9. getLocalParticipant,
  10. getRemoteParticipants,
  11. hasRaisedHand,
  12. isLocalParticipantModerator,
  13. isParticipantModerator
  14. } from '../base/participants/functions';
  15. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  16. import StateListenerRegistry from '../base/redux/StateListenerRegistry';
  17. import { playSound, registerSound, unregisterSound } from '../base/sounds/actions';
  18. import { hideNotification, showNotification } from '../notifications/actions';
  19. import { NOTIFICATION_TIMEOUT_TYPE } from '../notifications/constants';
  20. import { muteLocal } from '../video-menu/actions.any';
  21. import {
  22. DISABLE_MODERATION,
  23. ENABLE_MODERATION,
  24. LOCAL_PARTICIPANT_APPROVED,
  25. LOCAL_PARTICIPANT_MODERATION_NOTIFICATION,
  26. LOCAL_PARTICIPANT_REJECTED,
  27. PARTICIPANT_APPROVED,
  28. PARTICIPANT_REJECTED,
  29. REQUEST_DISABLE_AUDIO_MODERATION,
  30. REQUEST_DISABLE_VIDEO_MODERATION,
  31. REQUEST_ENABLE_AUDIO_MODERATION,
  32. REQUEST_ENABLE_VIDEO_MODERATION
  33. } from './actionTypes';
  34. import {
  35. disableModeration,
  36. dismissPendingAudioParticipant,
  37. dismissPendingParticipant,
  38. enableModeration,
  39. localParticipantApproved,
  40. localParticipantRejected,
  41. participantApproved,
  42. participantPendingAudio,
  43. participantRejected
  44. } from './actions';
  45. import {
  46. ASKED_TO_UNMUTE_NOTIFICATION_ID,
  47. ASKED_TO_UNMUTE_SOUND_ID,
  48. AUDIO_MODERATION_NOTIFICATION_ID,
  49. CS_MODERATION_NOTIFICATION_ID,
  50. VIDEO_MODERATION_NOTIFICATION_ID
  51. } from './constants';
  52. import {
  53. isEnabledFromState,
  54. isParticipantApproved,
  55. isParticipantPending
  56. } from './functions';
  57. import { ASKED_TO_UNMUTE_FILE } from './sounds';
  58. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  59. const { type } = action;
  60. const { conference } = getConferenceState(getState());
  61. switch (type) {
  62. case APP_WILL_MOUNT: {
  63. dispatch(registerSound(ASKED_TO_UNMUTE_SOUND_ID, ASKED_TO_UNMUTE_FILE));
  64. break;
  65. }
  66. case APP_WILL_UNMOUNT: {
  67. dispatch(unregisterSound(ASKED_TO_UNMUTE_SOUND_ID));
  68. break;
  69. }
  70. case LOCAL_PARTICIPANT_MODERATION_NOTIFICATION: {
  71. let descriptionKey;
  72. let titleKey;
  73. let uid = '';
  74. const localParticipant = getLocalParticipant(getState);
  75. const raisedHand = hasRaisedHand(localParticipant);
  76. switch (action.mediaType) {
  77. case MEDIA_TYPE.AUDIO: {
  78. titleKey = 'notify.moderationInEffectTitle';
  79. uid = AUDIO_MODERATION_NOTIFICATION_ID;
  80. break;
  81. }
  82. case MEDIA_TYPE.VIDEO: {
  83. titleKey = 'notify.moderationInEffectVideoTitle';
  84. uid = VIDEO_MODERATION_NOTIFICATION_ID;
  85. break;
  86. }
  87. case MEDIA_TYPE.PRESENTER: {
  88. titleKey = 'notify.moderationInEffectCSTitle';
  89. uid = CS_MODERATION_NOTIFICATION_ID;
  90. break;
  91. }
  92. }
  93. dispatch(showNotification({
  94. customActionNameKey: [ 'notify.raiseHandAction' ],
  95. customActionHandler: [ () => batch(() => {
  96. !raisedHand && dispatch(raiseHand(true));
  97. dispatch(hideNotification(uid));
  98. }) ],
  99. descriptionKey,
  100. sticky: true,
  101. titleKey,
  102. uid
  103. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  104. break;
  105. }
  106. case REQUEST_DISABLE_AUDIO_MODERATION: {
  107. conference?.disableAVModeration(MEDIA_TYPE.AUDIO);
  108. break;
  109. }
  110. case REQUEST_DISABLE_VIDEO_MODERATION: {
  111. conference?.disableAVModeration(MEDIA_TYPE.VIDEO);
  112. break;
  113. }
  114. case REQUEST_ENABLE_AUDIO_MODERATION: {
  115. conference?.enableAVModeration(MEDIA_TYPE.AUDIO);
  116. break;
  117. }
  118. case REQUEST_ENABLE_VIDEO_MODERATION: {
  119. conference?.enableAVModeration(MEDIA_TYPE.VIDEO);
  120. break;
  121. }
  122. case PARTICIPANT_UPDATED: {
  123. const state = getState();
  124. const audioModerationEnabled = isEnabledFromState(MEDIA_TYPE.AUDIO, state);
  125. const participant = action.participant;
  126. if (participant && audioModerationEnabled) {
  127. if (isLocalParticipantModerator(state)) {
  128. // this is handled only by moderators
  129. if (hasRaisedHand(participant)) {
  130. // if participant raises hand show notification
  131. !isParticipantApproved(participant.id, MEDIA_TYPE.AUDIO)(state)
  132. && dispatch(participantPendingAudio(participant));
  133. } else {
  134. // if participant lowers hand hide notification
  135. isParticipantPending(participant, MEDIA_TYPE.AUDIO)(state)
  136. && dispatch(dismissPendingAudioParticipant(participant));
  137. }
  138. } else if (participant.id === getLocalParticipant(state)?.id
  139. && /* the new role */ isParticipantModerator(participant)) {
  140. // this is the granted moderator case
  141. getRemoteParticipants(state).forEach(p => {
  142. hasRaisedHand(p) && !isParticipantApproved(p.id, MEDIA_TYPE.AUDIO)(state)
  143. && dispatch(participantPendingAudio(p));
  144. });
  145. }
  146. }
  147. break;
  148. }
  149. case ENABLE_MODERATION: {
  150. if (typeof APP !== 'undefined') {
  151. APP.API.notifyModerationChanged(action.mediaType, true);
  152. }
  153. break;
  154. }
  155. case DISABLE_MODERATION: {
  156. if (typeof APP !== 'undefined') {
  157. APP.API.notifyModerationChanged(action.mediaType, false);
  158. }
  159. break;
  160. }
  161. case LOCAL_PARTICIPANT_APPROVED: {
  162. if (typeof APP !== 'undefined') {
  163. const local = getLocalParticipant(getState());
  164. APP.API.notifyParticipantApproved(local?.id, action.mediaType);
  165. }
  166. break;
  167. }
  168. case PARTICIPANT_APPROVED: {
  169. if (typeof APP !== 'undefined') {
  170. APP.API.notifyParticipantApproved(action.id, action.mediaType);
  171. }
  172. break;
  173. }
  174. case LOCAL_PARTICIPANT_REJECTED: {
  175. if (typeof APP !== 'undefined') {
  176. const local = getLocalParticipant(getState());
  177. APP.API.notifyParticipantRejected(local?.id, action.mediaType);
  178. }
  179. break;
  180. }
  181. case PARTICIPANT_REJECTED: {
  182. if (typeof APP !== 'undefined') {
  183. APP.API.notifyParticipantRejected(action.id, action.mediaType);
  184. }
  185. break;
  186. }
  187. }
  188. return next(action);
  189. });
  190. /**
  191. * Registers a change handler for state['features/base/conference'].conference to
  192. * set the event listeners needed for the A/V moderation feature to operate.
  193. */
  194. StateListenerRegistry.register(
  195. state => state['features/base/conference'].conference,
  196. (conference, { dispatch }, previousConference) => {
  197. if (conference && !previousConference) {
  198. // local participant is allowed to unmute
  199. conference.on(JitsiConferenceEvents.AV_MODERATION_APPROVED, ({ mediaType }: { mediaType: MediaType; }) => {
  200. dispatch(localParticipantApproved(mediaType));
  201. // Audio & video moderation are both enabled at the same time.
  202. // Avoid displaying 2 different notifications.
  203. if (mediaType === MEDIA_TYPE.AUDIO) {
  204. dispatch(showNotification({
  205. titleKey: 'notify.hostAskedUnmute',
  206. sticky: true,
  207. customActionNameKey: [ 'notify.unmute' ],
  208. customActionHandler: [ () => dispatch(muteLocal(false, MEDIA_TYPE.AUDIO)) ],
  209. uid: ASKED_TO_UNMUTE_NOTIFICATION_ID
  210. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  211. dispatch(playSound(ASKED_TO_UNMUTE_SOUND_ID));
  212. }
  213. });
  214. conference.on(JitsiConferenceEvents.AV_MODERATION_REJECTED, ({ mediaType }: { mediaType: MediaType; }) => {
  215. dispatch(localParticipantRejected(mediaType));
  216. });
  217. conference.on(JitsiConferenceEvents.AV_MODERATION_CHANGED, ({ enabled, mediaType, actor }: {
  218. actor: Object; enabled: boolean; mediaType: MediaType;
  219. }) => {
  220. enabled ? dispatch(enableModeration(mediaType, actor)) : dispatch(disableModeration(mediaType, actor));
  221. });
  222. // this is received by moderators
  223. conference.on(
  224. JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_APPROVED,
  225. ({ participant, mediaType }: { mediaType: MediaType; participant: { _id: string; }; }) => {
  226. const { _id: id } = participant;
  227. batch(() => {
  228. // store in the whitelist
  229. dispatch(participantApproved(id, mediaType));
  230. // remove from pending list
  231. dispatch(dismissPendingParticipant(id, mediaType));
  232. });
  233. });
  234. // this is received by moderators
  235. conference.on(
  236. JitsiConferenceEvents.AV_MODERATION_PARTICIPANT_REJECTED,
  237. ({ participant, mediaType }: { mediaType: MediaType; participant: { _id: string; }; }) => {
  238. const { _id: id } = participant;
  239. dispatch(participantRejected(id, mediaType));
  240. });
  241. }
  242. });