Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

reducer.ts 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. // @ts-ignore
  7. } from '../base/participants';
  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. videoModerationEnabled: boolean;
  31. audioWhitelist: { [id: string]: boolean };
  32. videoWhitelist: { [id: string]: boolean };
  33. pendingAudio: Array<{ id: string }>;
  34. pendingVideo: Array<{ id: string }>;
  35. audioUnmuteApproved?: boolean|undefined;
  36. videoUnmuteApproved?: boolean|undefined;
  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: any, state: any = {}) {
  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('features/av-moderation', (state: IAVModerationState = initialState, action: any) => {
  68. switch (action.type) {
  69. case DISABLE_MODERATION: {
  70. const newState = action.mediaType === MEDIA_TYPE.AUDIO
  71. ? {
  72. audioModerationEnabled: false,
  73. audioUnmuteApproved: undefined
  74. } : {
  75. videoModerationEnabled: false,
  76. videoUnmuteApproved: undefined
  77. };
  78. return {
  79. ...state,
  80. ...newState,
  81. audioWhitelist: {},
  82. videoWhitelist: {},
  83. pendingAudio: [],
  84. pendingVideo: []
  85. };
  86. }
  87. case ENABLE_MODERATION: {
  88. const newState = action.mediaType === MEDIA_TYPE.AUDIO
  89. ? { audioModerationEnabled: true } : { videoModerationEnabled: true };
  90. return {
  91. ...state,
  92. ...newState
  93. };
  94. }
  95. case LOCAL_PARTICIPANT_APPROVED: {
  96. const newState = action.mediaType === MEDIA_TYPE.AUDIO
  97. ? { audioUnmuteApproved: true } : { videoUnmuteApproved: true };
  98. return {
  99. ...state,
  100. ...newState
  101. };
  102. }
  103. case LOCAL_PARTICIPANT_REJECTED: {
  104. const newState = action.mediaType === MEDIA_TYPE.AUDIO
  105. ? { audioUnmuteApproved: false } : { videoUnmuteApproved: false };
  106. return {
  107. ...state,
  108. ...newState
  109. };
  110. }
  111. case PARTICIPANT_PENDING_AUDIO: {
  112. const { participant } = action;
  113. // Add participant to pendingAudio array only if it's not already added
  114. if (!state.pendingAudio.find(pending => pending.id === participant.id)) {
  115. const updated = [ ...state.pendingAudio ];
  116. updated.push(participant);
  117. return {
  118. ...state,
  119. pendingAudio: updated
  120. };
  121. }
  122. return state;
  123. }
  124. case PARTICIPANT_UPDATED: {
  125. const participant = action.participant;
  126. const { audioModerationEnabled, videoModerationEnabled } = state;
  127. let hasStateChanged = false;
  128. // skips changing the reference of pendingAudio or pendingVideo,
  129. // if there is no change in the elements
  130. if (audioModerationEnabled) {
  131. hasStateChanged = _updatePendingParticipant(MEDIA_TYPE.AUDIO, participant, state);
  132. }
  133. if (videoModerationEnabled) {
  134. hasStateChanged = hasStateChanged || _updatePendingParticipant(MEDIA_TYPE.VIDEO, participant, state);
  135. }
  136. // If the state has changed we need to return a new object reference in order to trigger subscriber updates.
  137. if (hasStateChanged) {
  138. return {
  139. ...state
  140. };
  141. }
  142. return state;
  143. }
  144. case PARTICIPANT_LEFT: {
  145. const participant = action.participant;
  146. const { audioModerationEnabled, videoModerationEnabled } = state;
  147. let hasStateChanged = false;
  148. // skips changing the reference of pendingAudio or pendingVideo,
  149. // if there is no change in the elements
  150. if (audioModerationEnabled) {
  151. const newPendingAudio = state.pendingAudio.filter(pending => pending.id !== participant.id);
  152. if (state.pendingAudio.length !== newPendingAudio.length) {
  153. state.pendingAudio = newPendingAudio;
  154. hasStateChanged = true;
  155. }
  156. }
  157. if (videoModerationEnabled) {
  158. const newPendingVideo = state.pendingVideo.filter(pending => pending.id !== participant.id);
  159. if (state.pendingVideo.length !== newPendingVideo.length) {
  160. state.pendingVideo = newPendingVideo;
  161. hasStateChanged = true;
  162. }
  163. }
  164. // If the state has changed we need to return a new object reference in order to trigger subscriber updates.
  165. if (hasStateChanged) {
  166. return {
  167. ...state
  168. };
  169. }
  170. return state;
  171. }
  172. case DISMISS_PENDING_PARTICIPANT: {
  173. const { id, mediaType } = action;
  174. if (mediaType === MEDIA_TYPE.AUDIO) {
  175. return {
  176. ...state,
  177. pendingAudio: state.pendingAudio.filter(pending => pending.id !== id)
  178. };
  179. }
  180. if (mediaType === MEDIA_TYPE.VIDEO) {
  181. return {
  182. ...state,
  183. pendingVideo: state.pendingVideo.filter(pending => pending.id !== id)
  184. };
  185. }
  186. return state;
  187. }
  188. case PARTICIPANT_APPROVED: {
  189. const { mediaType, id } = action;
  190. if (mediaType === MEDIA_TYPE.AUDIO) {
  191. return {
  192. ...state,
  193. audioWhitelist: {
  194. ...state.audioWhitelist,
  195. [id]: true
  196. }
  197. };
  198. }
  199. if (mediaType === MEDIA_TYPE.VIDEO) {
  200. return {
  201. ...state,
  202. videoWhitelist: {
  203. ...state.videoWhitelist,
  204. [id]: true
  205. }
  206. };
  207. }
  208. return state;
  209. }
  210. case PARTICIPANT_REJECTED: {
  211. const { mediaType, id } = action;
  212. if (mediaType === MEDIA_TYPE.AUDIO) {
  213. return {
  214. ...state,
  215. audioWhitelist: {
  216. ...state.audioWhitelist,
  217. [id]: false
  218. }
  219. };
  220. }
  221. if (mediaType === MEDIA_TYPE.VIDEO) {
  222. return {
  223. ...state,
  224. videoWhitelist: {
  225. ...state.videoWhitelist,
  226. [id]: false
  227. }
  228. };
  229. }
  230. return state;
  231. }
  232. }
  233. return state;
  234. });