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

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