123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- /* @flow */
-
- import { MEDIA_TYPE } from '../base/media/constants';
- import type { MediaType } from '../base/media/constants';
- import {
- PARTICIPANT_LEFT,
- PARTICIPANT_UPDATED
- } from '../base/participants';
- import { ReducerRegistry } from '../base/redux';
-
- import {
- DISABLE_MODERATION,
- DISMISS_PENDING_PARTICIPANT,
- ENABLE_MODERATION,
- LOCAL_PARTICIPANT_APPROVED,
- PARTICIPANT_APPROVED,
- PARTICIPANT_PENDING_AUDIO
- } from './actionTypes';
- import { MEDIA_TYPE_TO_PENDING_STORE_KEY } from './constants';
-
- const initialState = {
- audioModerationEnabled: false,
- videoModerationEnabled: false,
- audioWhitelist: {},
- videoWhitelist: {},
- pendingAudio: [],
- pendingVideo: []
- };
-
- /**
- Updates a participant in the state for the specified media type.
- *
- * @param {MediaType} mediaType - The media type.
- * @param {Object} participant - Information about participant to be modified.
- * @param {Object} state - The current state.
- * @private
- * @returns {boolean} - Whether state instance was modified.
- */
- function _updatePendingParticipant(mediaType: MediaType, participant, state: Object = {}) {
- let arrayItemChanged = false;
- const storeKey = MEDIA_TYPE_TO_PENDING_STORE_KEY[mediaType];
- const arr = state[storeKey];
- const newArr = arr.map(pending => {
- if (pending.id === participant.id) {
- arrayItemChanged = true;
-
- return {
- ...pending,
- ...participant
- };
- }
-
- return pending;
- });
-
- if (arrayItemChanged) {
- state[storeKey] = newArr;
-
- return true;
- }
-
- return false;
- }
-
- ReducerRegistry.register('features/av-moderation', (state = initialState, action) => {
-
- switch (action.type) {
- case DISABLE_MODERATION: {
- const newState = action.mediaType === MEDIA_TYPE.AUDIO
- ? {
- audioModerationEnabled: false,
- audioUnmuteApproved: undefined
- } : {
- videoModerationEnabled: false,
- videoUnmuteApproved: undefined
- };
-
- return {
- ...state,
- ...newState,
- audioWhitelist: {},
- videoWhitelist: {},
- pendingAudio: [],
- pendingVideo: []
- };
- }
-
- case ENABLE_MODERATION: {
- const newState = action.mediaType === MEDIA_TYPE.AUDIO
- ? { audioModerationEnabled: true } : { videoModerationEnabled: true };
-
- return {
- ...state,
- ...newState
- };
- }
-
- case LOCAL_PARTICIPANT_APPROVED: {
- const newState = action.mediaType === MEDIA_TYPE.AUDIO
- ? { audioUnmuteApproved: true } : { videoUnmuteApproved: true };
-
- return {
- ...state,
- ...newState
- };
- }
-
- case PARTICIPANT_PENDING_AUDIO: {
- const { participant } = action;
-
- // Add participant to pendingAudio array only if it's not already added
- if (!state.pendingAudio.find(pending => pending.id === participant.id)) {
- const updated = [ ...state.pendingAudio ];
-
- updated.push(participant);
-
- return {
- ...state,
- pendingAudio: updated
- };
- }
-
- return state;
- }
-
- case PARTICIPANT_UPDATED: {
- const participant = action.participant;
- const { audioModerationEnabled, videoModerationEnabled } = state;
- let hasStateChanged = false;
-
- // skips changing the reference of pendingAudio or pendingVideo,
- // if there is no change in the elements
- if (audioModerationEnabled) {
- hasStateChanged = _updatePendingParticipant(MEDIA_TYPE.AUDIO, participant, state);
- }
-
- if (videoModerationEnabled) {
- hasStateChanged = _updatePendingParticipant(MEDIA_TYPE.VIDEO, participant, state);
- }
-
- // If the state has changed we need to return a new object reference in order to trigger subscriber updates.
- if (hasStateChanged) {
- return {
- ...state
- };
- }
-
- return state;
- }
- case PARTICIPANT_LEFT: {
- const participant = action.participant;
- const { audioModerationEnabled, videoModerationEnabled } = state;
- let hasStateChanged = false;
-
- // skips changing the reference of pendingAudio or pendingVideo,
- // if there is no change in the elements
- if (audioModerationEnabled) {
- const newPendingAudio = state.pendingAudio.filter(pending => pending.id !== participant.id);
-
- if (state.pendingAudio.length !== newPendingAudio.length) {
- state.pendingAudio = newPendingAudio;
- hasStateChanged = true;
- }
- }
-
- if (videoModerationEnabled) {
- const newPendingVideo = state.pendingVideo.filter(pending => pending.id !== participant.id);
-
- if (state.pendingVideo.length !== newPendingVideo.length) {
- state.pendingVideo = newPendingVideo;
- hasStateChanged = true;
- }
- }
-
- // If the state has changed we need to return a new object reference in order to trigger subscriber updates.
- if (hasStateChanged) {
- return {
- ...state
- };
- }
-
- return state;
- }
-
- case DISMISS_PENDING_PARTICIPANT: {
- const { participant, mediaType } = action;
-
- if (mediaType === MEDIA_TYPE.AUDIO) {
- return {
- ...state,
- pendingAudio: state.pendingAudio.filter(pending => pending.id !== participant.id)
- };
- }
-
- if (mediaType === MEDIA_TYPE.VIDEO) {
- return {
- ...state,
- pendingVideo: state.pendingVideo.filter(pending => pending.id !== participant.id)
- };
- }
-
- return state;
- }
-
- case PARTICIPANT_APPROVED: {
- const { mediaType, id } = action;
-
- if (mediaType === MEDIA_TYPE.AUDIO) {
- return {
- ...state,
- audioWhitelist: {
- ...state.audioWhitelist,
- [id]: true
- }
- };
- }
-
- if (mediaType === MEDIA_TYPE.VIDEO) {
- return {
- ...state,
- videoWhitelist: {
- ...state.videoWhitelist,
- [id]: true
- }
- };
- }
-
- return state;
- }
-
- }
-
- return state;
- });
|