2021-06-23 11:23:44 +00:00
|
|
|
import { MEDIA_TYPE } from '../base/media/constants';
|
2021-07-09 12:36:19 +00:00
|
|
|
import type { MediaType } from '../base/media/constants';
|
|
|
|
import {
|
|
|
|
PARTICIPANT_LEFT,
|
|
|
|
PARTICIPANT_UPDATED
|
2022-07-11 12:30:37 +00:00
|
|
|
|
2022-07-01 09:32:39 +00:00
|
|
|
// @ts-ignore
|
2021-07-09 12:36:19 +00:00
|
|
|
} from '../base/participants';
|
2022-07-01 09:32:39 +00:00
|
|
|
import ReducerRegistry from '../base/redux/ReducerRegistry';
|
2021-06-23 11:23:44 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
DISABLE_MODERATION,
|
|
|
|
DISMISS_PENDING_PARTICIPANT,
|
|
|
|
ENABLE_MODERATION,
|
|
|
|
LOCAL_PARTICIPANT_APPROVED,
|
2021-09-28 16:11:13 +00:00
|
|
|
LOCAL_PARTICIPANT_REJECTED,
|
2021-06-23 11:23:44 +00:00
|
|
|
PARTICIPANT_APPROVED,
|
2021-09-28 16:11:13 +00:00
|
|
|
PARTICIPANT_PENDING_AUDIO,
|
|
|
|
PARTICIPANT_REJECTED
|
2021-06-23 11:23:44 +00:00
|
|
|
} from './actionTypes';
|
2021-07-09 12:36:19 +00:00
|
|
|
import { MEDIA_TYPE_TO_PENDING_STORE_KEY } from './constants';
|
2021-06-23 11:23:44 +00:00
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
audioModerationEnabled: false,
|
|
|
|
videoModerationEnabled: false,
|
|
|
|
audioWhitelist: {},
|
|
|
|
videoWhitelist: {},
|
|
|
|
pendingAudio: [],
|
|
|
|
pendingVideo: []
|
|
|
|
};
|
|
|
|
|
2022-07-01 09:32:39 +00:00
|
|
|
export interface IAVModerationState {
|
|
|
|
audioModerationEnabled: boolean;
|
2022-07-20 08:47:01 +00:00
|
|
|
audioUnmuteApproved?: boolean|undefined;
|
2022-07-01 09:32:39 +00:00
|
|
|
audioWhitelist: { [id: string]: boolean };
|
|
|
|
pendingAudio: Array<{ id: string }>;
|
|
|
|
pendingVideo: Array<{ id: string }>;
|
2022-07-20 08:47:01 +00:00
|
|
|
videoModerationEnabled: boolean;
|
2022-07-01 09:32:39 +00:00
|
|
|
videoUnmuteApproved?: boolean|undefined;
|
2022-07-20 08:47:01 +00:00
|
|
|
videoWhitelist: { [id: string]: boolean };
|
2022-07-01 09:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 12:36:19 +00:00
|
|
|
/**
|
2022-07-01 09:32:39 +00:00
|
|
|
* Updates a participant in the state for the specified media type.
|
2021-07-09 12:36:19 +00:00
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
2022-07-01 09:32:39 +00:00
|
|
|
function _updatePendingParticipant(mediaType: MediaType, participant: any, state: any = {}) {
|
2021-07-09 12:36:19 +00:00
|
|
|
let arrayItemChanged = false;
|
|
|
|
const storeKey = MEDIA_TYPE_TO_PENDING_STORE_KEY[mediaType];
|
|
|
|
const arr = state[storeKey];
|
2022-07-11 12:30:37 +00:00
|
|
|
const newArr = arr.map((pending: { id: string}) => {
|
2021-07-09 12:36:19 +00:00
|
|
|
if (pending.id === participant.id) {
|
|
|
|
arrayItemChanged = true;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...pending,
|
|
|
|
...participant
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return pending;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (arrayItemChanged) {
|
|
|
|
state[storeKey] = newArr;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-07-01 09:32:39 +00:00
|
|
|
ReducerRegistry.register('features/av-moderation', (state: IAVModerationState = initialState, action: any) => {
|
2021-06-23 11:23:44 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-28 16:11:13 +00:00
|
|
|
case LOCAL_PARTICIPANT_REJECTED: {
|
|
|
|
const newState = action.mediaType === MEDIA_TYPE.AUDIO
|
|
|
|
? { audioUnmuteApproved: false } : { videoUnmuteApproved: false };
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
...newState
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-06-23 11:23:44 +00:00
|
|
|
case PARTICIPANT_PENDING_AUDIO: {
|
2021-07-09 12:36:19 +00:00
|
|
|
const { participant } = action;
|
2021-06-23 11:23:44 +00:00
|
|
|
|
2021-07-09 12:36:19 +00:00
|
|
|
// Add participant to pendingAudio array only if it's not already added
|
|
|
|
if (!state.pendingAudio.find(pending => pending.id === participant.id)) {
|
2021-06-23 11:23:44 +00:00
|
|
|
const updated = [ ...state.pendingAudio ];
|
|
|
|
|
2021-07-09 12:36:19 +00:00
|
|
|
updated.push(participant);
|
2021-06-23 11:23:44 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
pendingAudio: updated
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2021-07-09 12:36:19 +00:00
|
|
|
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) {
|
2021-08-06 19:36:08 +00:00
|
|
|
hasStateChanged = hasStateChanged || _updatePendingParticipant(MEDIA_TYPE.VIDEO, participant, state);
|
2021-07-09 12:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2021-06-23 11:23:44 +00:00
|
|
|
case DISMISS_PENDING_PARTICIPANT: {
|
2021-08-06 19:36:08 +00:00
|
|
|
const { id, mediaType } = action;
|
2021-06-23 11:23:44 +00:00
|
|
|
|
|
|
|
if (mediaType === MEDIA_TYPE.AUDIO) {
|
|
|
|
return {
|
|
|
|
...state,
|
2021-08-06 19:36:08 +00:00
|
|
|
pendingAudio: state.pendingAudio.filter(pending => pending.id !== id)
|
2021-06-23 11:23:44 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mediaType === MEDIA_TYPE.VIDEO) {
|
|
|
|
return {
|
|
|
|
...state,
|
2021-08-06 19:36:08 +00:00
|
|
|
pendingVideo: state.pendingVideo.filter(pending => pending.id !== id)
|
2021-06-23 11:23:44 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-28 16:11:13 +00:00
|
|
|
case PARTICIPANT_REJECTED: {
|
|
|
|
const { mediaType, id } = action;
|
|
|
|
|
|
|
|
if (mediaType === MEDIA_TYPE.AUDIO) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
audioWhitelist: {
|
|
|
|
...state.audioWhitelist,
|
|
|
|
[id]: false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mediaType === MEDIA_TYPE.VIDEO) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
videoWhitelist: {
|
|
|
|
...state.videoWhitelist,
|
|
|
|
[id]: false
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2021-06-23 11:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
});
|