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.

functions.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // @flow
  2. import { MEDIA_TYPE, type MediaType } from '../base/media/constants';
  3. import { getParticipantById, isLocalParticipantModerator } from '../base/participants/functions';
  4. import { MEDIA_TYPE_TO_WHITELIST_STORE_KEY, MEDIA_TYPE_TO_PENDING_STORE_KEY } from './constants';
  5. /**
  6. * Returns this feature's root state.
  7. *
  8. * @param {Object} state - Global state.
  9. * @returns {Object} Feature state.
  10. */
  11. const getState = state => state['features/av-moderation'];
  12. /**
  13. * Returns whether moderation is enabled per media type.
  14. *
  15. * @param {MEDIA_TYPE} mediaType - The media type to check.
  16. * @param {Object} state - Global state.
  17. * @returns {null|boolean|*}
  18. */
  19. export const isEnabledFromState = (mediaType: MediaType, state: Object) =>
  20. (mediaType === MEDIA_TYPE.AUDIO
  21. ? getState(state)?.audioModerationEnabled
  22. : getState(state)?.videoModerationEnabled) === true;
  23. /**
  24. * Returns whether moderation is enabled per media type.
  25. *
  26. * @param {MEDIA_TYPE} mediaType - The media type to check.
  27. * @returns {null|boolean|*}
  28. */
  29. export const isEnabled = (mediaType: MediaType) => (state: Object) => isEnabledFromState(mediaType, state);
  30. /**
  31. * Returns whether local participant is approved to unmute a media type.
  32. *
  33. * @param {MEDIA_TYPE} mediaType - The media type to check.
  34. * @param {Object} state - Global state.
  35. * @returns {boolean}
  36. */
  37. export const isLocalParticipantApprovedFromState = (mediaType: MediaType, state: Object) => {
  38. const approved = (mediaType === MEDIA_TYPE.AUDIO
  39. ? getState(state).audioUnmuteApproved
  40. : getState(state).videoUnmuteApproved) === true;
  41. return approved || isLocalParticipantModerator(state);
  42. };
  43. /**
  44. * Returns whether local participant is approved to unmute a media type.
  45. *
  46. * @param {MEDIA_TYPE} mediaType - The media type to check.
  47. * @returns {null|boolean|*}
  48. */
  49. export const isLocalParticipantApproved = (mediaType: MediaType) =>
  50. (state: Object) =>
  51. isLocalParticipantApprovedFromState(mediaType, state);
  52. /**
  53. * Returns a selector creator which determines if the participant is approved or not for a media type.
  54. *
  55. * @param {string} id - The participant id.
  56. * @param {MEDIA_TYPE} mediaType - The media type to check.
  57. * @returns {boolean}
  58. */
  59. export const isParticipantApproved = (id: string, mediaType: MediaType) => (state: Object) => {
  60. const storeKey = MEDIA_TYPE_TO_WHITELIST_STORE_KEY[mediaType];
  61. return Boolean(getState(state)[storeKey][id]);
  62. };
  63. /**
  64. * Returns a selector creator which determines if the participant is pending or not for a media type.
  65. *
  66. * @param {string} id - The participant id.
  67. * @param {MEDIA_TYPE} mediaType - The media type to check.
  68. * @returns {boolean}
  69. */
  70. export const isParticipantPending = (id: string, mediaType: MediaType) => (state: Object) => {
  71. const storeKey = MEDIA_TYPE_TO_PENDING_STORE_KEY[mediaType];
  72. const arr = getState(state)[storeKey];
  73. return Boolean(arr.find(pending => pending === id));
  74. };
  75. /**
  76. * Selector which returns a list with all the participants asking to audio unmute.
  77. * This is visible ony for the moderator.
  78. *
  79. * @param {Object} state - The global state.
  80. * @returns {Array<Object>}
  81. */
  82. export const getParticipantsAskingToAudioUnmute = (state: Object) => {
  83. if (isLocalParticipantModerator(state)) {
  84. const ids = getState(state).pendingAudio;
  85. return ids.map(id => getParticipantById(state, id)).filter(Boolean);
  86. }
  87. return [];
  88. };
  89. /**
  90. * Returns true if a special notification can be displayed when a participant
  91. * tries to unmute.
  92. *
  93. * @param {MediaType} mediaType - 'audio' or 'video' media type.
  94. * @param {Object} state - The global state.
  95. * @returns {boolean}
  96. */
  97. export const shouldShowModeratedNotification = (mediaType: MediaType, state: Object) =>
  98. isEnabledFromState(mediaType, state)
  99. && !isLocalParticipantApprovedFromState(mediaType, state);