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.

reducer.ts 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { MEDIA_TYPE } from '../base/media/constants';
  2. import type { MediaType } from '../base/media/constants';
  3. import {
  4. PARTICIPANT_LEFT,
  5. PARTICIPANT_UPDATED
  6. } from '../base/participants/actionTypes';
  7. import { IParticipant } from '../base/participants/types';
  8. import ReducerRegistry from '../base/redux/ReducerRegistry';
  9. import {
  10. DISABLE_MODERATION,
  11. DISMISS_PENDING_PARTICIPANT,
  12. ENABLE_MODERATION,
  13. LOCAL_PARTICIPANT_APPROVED,
  14. LOCAL_PARTICIPANT_REJECTED,
  15. PARTICIPANT_APPROVED,
  16. PARTICIPANT_PENDING_AUDIO,
  17. PARTICIPANT_REJECTED
  18. } from './actionTypes';
  19. import { MEDIA_TYPE_TO_PENDING_STORE_KEY } from './constants';
  20. const initialState = {
  21. audioModerationEnabled: false,
  22. videoModerationEnabled: false,
  23. audioWhitelist: {},
  24. videoWhitelist: {},
  25. pendingAudio: [],
  26. pendingVideo: []
  27. };
  28. export interface IAVModerationState {
  29. audioModerationEnabled: boolean;
  30. audioUnmuteApproved?: boolean | undefined;
  31. audioWhitelist: { [id: string]: boolean; };
  32. pendingAudio: Array<{ id: string; }>;
  33. pendingVideo: Array<{ id: string; }>;
  34. videoModerationEnabled: boolean;
  35. videoUnmuteApproved?: boolean | undefined;
  36. videoWhitelist: { [id: string]: boolean; };
  37. }
  38. /**
  39. * Updates a participant in the state for the specified media type.
  40. *
  41. * @param {MediaType} mediaType - The media type.
  42. * @param {Object} participant - Information about participant to be modified.
  43. * @param {Object} state - The current state.
  44. * @private
  45. * @returns {boolean} - Whether state instance was modified.
  46. */
  47. function _updatePendingParticipant(mediaType: MediaType, participant: IParticipant, state: IAVModerationState) {
  48. let arrayItemChanged = false;
  49. const storeKey = MEDIA_TYPE_TO_PENDING_STORE_KEY[mediaType];
  50. const arr = state[storeKey];
  51. const newArr = arr.map((pending: { id: string; }) => {
  52. if (pending.id === participant.id) {
  53. arrayItemChanged = true;
  54. return {
  55. ...pending,
  56. ...participant
  57. };
  58. }
  59. return pending;
  60. });
  61. if (arrayItemChanged) {
  62. state[storeKey] = newArr;
  63. return true;
  64. }
  65. return false;
  66. }
  67. ReducerRegistry.register<IAVModerationState>('features/av-moderation',
  68. (state = initialState, action): IAVModerationState => {
  69. switch (action.type) {
  70. case DISABLE_MODERATION: {
  71. const newState = action.mediaType === MEDIA_TYPE.AUDIO
  72. ? {
  73. audioModerationEnabled: false,
  74. audioUnmuteApproved: undefined
  75. } : {
  76. videoModerationEnabled: false,
  77. videoUnmuteApproved: undefined
  78. };
  79. return {
  80. ...state,
  81. ...newState,
  82. audioWhitelist: {},
  83. videoWhitelist: {},
  84. pendingAudio: [],
  85. pendingVideo: []
  86. };
  87. }
  88. case ENABLE_MODERATION: {
  89. const newState = action.mediaType === MEDIA_TYPE.AUDIO
  90. ? { audioModerationEnabled: true } : { videoModerationEnabled: true };
  91. return {
  92. ...state,
  93. ...newState
  94. };
  95. }
  96. case LOCAL_PARTICIPANT_APPROVED: {
  97. const newState = action.mediaType === MEDIA_TYPE.AUDIO
  98. ? { audioUnmuteApproved: true } : { videoUnmuteApproved: true };
  99. return {
  100. ...state,
  101. ...newState
  102. };
  103. }
  104. case LOCAL_PARTICIPANT_REJECTED: {
  105. const newState = action.mediaType === MEDIA_TYPE.AUDIO
  106. ? { audioUnmuteApproved: false } : { videoUnmuteApproved: false };
  107. return {
  108. ...state,
  109. ...newState
  110. };
  111. }
  112. case PARTICIPANT_PENDING_AUDIO: {
  113. const { participant } = action;
  114. // Add participant to pendingAudio array only if it's not already added
  115. if (!state.pendingAudio.find(pending => pending.id === participant.id)) {
  116. const updated = [ ...state.pendingAudio ];
  117. updated.push(participant);
  118. return {
  119. ...state,
  120. pendingAudio: updated
  121. };
  122. }
  123. return state;
  124. }
  125. case PARTICIPANT_UPDATED: {
  126. const participant = action.participant;
  127. const { audioModerationEnabled, videoModerationEnabled } = state;
  128. let hasStateChanged = false;
  129. // skips changing the reference of pendingAudio or pendingVideo,
  130. // if there is no change in the elements
  131. if (audioModerationEnabled) {
  132. hasStateChanged = _updatePendingParticipant(MEDIA_TYPE.AUDIO, participant, state);
  133. }
  134. if (videoModerationEnabled) {
  135. hasStateChanged = hasStateChanged || _updatePendingParticipant(MEDIA_TYPE.VIDEO, participant, state);
  136. }
  137. // If the state has changed we need to return a new object reference in order to trigger subscriber updates.
  138. if (hasStateChanged) {
  139. return {
  140. ...state
  141. };
  142. }
  143. return state;
  144. }
  145. case PARTICIPANT_LEFT: {
  146. const participant = action.participant;
  147. const { audioModerationEnabled, videoModerationEnabled } = state;
  148. let hasStateChanged = false;
  149. // skips changing the reference of pendingAudio or pendingVideo,
  150. // if there is no change in the elements
  151. if (audioModerationEnabled) {
  152. const newPendingAudio = state.pendingAudio.filter(pending => pending.id !== participant.id);
  153. if (state.pendingAudio.length !== newPendingAudio.length) {
  154. state.pendingAudio = newPendingAudio;
  155. hasStateChanged = true;
  156. }
  157. }
  158. if (videoModerationEnabled) {
  159. const newPendingVideo = state.pendingVideo.filter(pending => pending.id !== participant.id);
  160. if (state.pendingVideo.length !== newPendingVideo.length) {
  161. state.pendingVideo = newPendingVideo;
  162. hasStateChanged = true;
  163. }
  164. }
  165. // If the state has changed we need to return a new object reference in order to trigger subscriber updates.
  166. if (hasStateChanged) {
  167. return {
  168. ...state
  169. };
  170. }
  171. return state;
  172. }
  173. case DISMISS_PENDING_PARTICIPANT: {
  174. const { id, mediaType } = action;
  175. if (mediaType === MEDIA_TYPE.AUDIO) {
  176. return {
  177. ...state,
  178. pendingAudio: state.pendingAudio.filter(pending => pending.id !== id)
  179. };
  180. }
  181. if (mediaType === MEDIA_TYPE.VIDEO) {
  182. return {
  183. ...state,
  184. pendingVideo: state.pendingVideo.filter(pending => pending.id !== id)
  185. };
  186. }
  187. return state;
  188. }
  189. case PARTICIPANT_APPROVED: {
  190. const { mediaType, id } = action;
  191. if (mediaType === MEDIA_TYPE.AUDIO) {
  192. return {
  193. ...state,
  194. audioWhitelist: {
  195. ...state.audioWhitelist,
  196. [id]: true
  197. }
  198. };
  199. }
  200. if (mediaType === MEDIA_TYPE.VIDEO) {
  201. return {
  202. ...state,
  203. videoWhitelist: {
  204. ...state.videoWhitelist,
  205. [id]: true
  206. }
  207. };
  208. }
  209. return state;
  210. }
  211. case PARTICIPANT_REJECTED: {
  212. const { mediaType, id } = action;
  213. if (mediaType === MEDIA_TYPE.AUDIO) {
  214. return {
  215. ...state,
  216. audioWhitelist: {
  217. ...state.audioWhitelist,
  218. [id]: false
  219. }
  220. };
  221. }
  222. if (mediaType === MEDIA_TYPE.VIDEO) {
  223. return {
  224. ...state,
  225. videoWhitelist: {
  226. ...state.videoWhitelist,
  227. [id]: false
  228. }
  229. };
  230. }
  231. return state;
  232. }
  233. }
  234. return state;
  235. });