Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

reducer.ts 7.7KB

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