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.

functions.ts 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import { IReduxState } from '../app/types';
  2. import {
  3. isEnabledFromState,
  4. isLocalParticipantApprovedFromState,
  5. isParticipantApproved,
  6. isSupported
  7. } from '../av-moderation/functions';
  8. import { IStateful } from '../base/app/types';
  9. import { getCurrentConference } from '../base/conference/functions';
  10. import { INVITE_ENABLED, PARTICIPANTS_ENABLED } from '../base/flags/constants';
  11. import { getFeatureFlag } from '../base/flags/functions';
  12. import { MEDIA_TYPE, type MediaType } from '../base/media/constants';
  13. import {
  14. getDominantSpeakerParticipant,
  15. getLocalParticipant,
  16. getRaiseHandsQueue,
  17. getRemoteParticipantsSorted,
  18. isLocalParticipantModerator,
  19. isParticipantModerator
  20. } from '../base/participants/functions';
  21. import { IParticipant } from '../base/participants/types';
  22. import { toState } from '../base/redux/functions';
  23. import { normalizeAccents } from '../base/util/strings';
  24. import { BREAKOUT_ROOMS_RENAME_FEATURE } from '../breakout-rooms/constants';
  25. import { isInBreakoutRoom } from '../breakout-rooms/functions';
  26. import { MEDIA_STATE, QUICK_ACTION_BUTTON, REDUCER_KEY } from './constants';
  27. /**
  28. * Checks if a participant is force muted.
  29. *
  30. * @param {IParticipant|undefined} participant - The participant.
  31. * @param {MediaType} mediaType - The media type.
  32. * @param {IReduxState} state - The redux state.
  33. * @returns {MediaState}
  34. */
  35. export function isForceMuted(participant: IParticipant | undefined, mediaType: MediaType, state: IReduxState) {
  36. if (isEnabledFromState(mediaType, state)) {
  37. if (participant?.local) {
  38. return !isLocalParticipantApprovedFromState(mediaType, state);
  39. }
  40. // moderators cannot be force muted
  41. if (isParticipantModerator(participant)) {
  42. return false;
  43. }
  44. return !isParticipantApproved(participant?.id ?? '', mediaType)(state);
  45. }
  46. return false;
  47. }
  48. /**
  49. * Determines the audio media state (the mic icon) for a participant.
  50. *
  51. * @param {IParticipant} participant - The participant.
  52. * @param {boolean} muted - The mute state of the participant.
  53. * @param {IReduxState} state - The redux state.
  54. * @returns {MediaState}
  55. */
  56. export function getParticipantAudioMediaState(participant: IParticipant | undefined,
  57. muted: Boolean, state: IReduxState) {
  58. const dominantSpeaker = getDominantSpeakerParticipant(state);
  59. if (participant?.isSilent) {
  60. return MEDIA_STATE.NONE;
  61. }
  62. if (muted) {
  63. if (isForceMuted(participant, MEDIA_TYPE.AUDIO, state)) {
  64. return MEDIA_STATE.FORCE_MUTED;
  65. }
  66. return MEDIA_STATE.MUTED;
  67. }
  68. if (participant === dominantSpeaker) {
  69. return MEDIA_STATE.DOMINANT_SPEAKER;
  70. }
  71. return MEDIA_STATE.UNMUTED;
  72. }
  73. /**
  74. * Determines the video media state (the mic icon) for a participant.
  75. *
  76. * @param {IParticipant} participant - The participant.
  77. * @param {boolean} muted - The mute state of the participant.
  78. * @param {IReduxState} state - The redux state.
  79. * @returns {MediaState}
  80. */
  81. export function getParticipantVideoMediaState(participant: IParticipant | undefined,
  82. muted: Boolean, state: IReduxState) {
  83. if (muted) {
  84. if (isForceMuted(participant, MEDIA_TYPE.VIDEO, state)) {
  85. return MEDIA_STATE.FORCE_MUTED;
  86. }
  87. return MEDIA_STATE.MUTED;
  88. }
  89. return MEDIA_STATE.UNMUTED;
  90. }
  91. /**
  92. * Returns this feature's root state.
  93. *
  94. * @param {IReduxState} state - Global state.
  95. * @returns {Object} Feature state.
  96. */
  97. const getState = (state: IReduxState) => state[REDUCER_KEY];
  98. /**
  99. * Returns the participants pane config.
  100. *
  101. * @param {IStateful} stateful - The redux store, the redux
  102. * {@code getState} function, or the redux state itself.
  103. * @returns {Object}
  104. */
  105. export const getParticipantsPaneConfig = (stateful: IStateful) => {
  106. const state = toState(stateful);
  107. const { participantsPane = {} } = state['features/base/config'];
  108. return participantsPane;
  109. };
  110. /**
  111. * Is the participants pane open.
  112. *
  113. * @param {IReduxState} state - Global state.
  114. * @returns {boolean} Is the participants pane open.
  115. */
  116. export const getParticipantsPaneOpen = (state: IReduxState) => Boolean(getState(state)?.isOpen);
  117. /**
  118. * Returns the type of quick action button to be displayed for a participant.
  119. * The button is displayed when hovering a participant from the participant list.
  120. *
  121. * @param {IParticipant} participant - The participant.
  122. * @param {boolean} isAudioMuted - If audio is muted for the participant.
  123. * @param {boolean} isVideoMuted - If audio is muted for the participant.
  124. * @param {IReduxState} state - The redux state.
  125. * @returns {string} - The type of the quick action button.
  126. */
  127. export function getQuickActionButtonType(
  128. participant: IParticipant | undefined,
  129. isAudioMuted: Boolean,
  130. isVideoMuted: Boolean,
  131. state: IReduxState) {
  132. // handled only by moderators
  133. const isVideoForceMuted = isForceMuted(participant, MEDIA_TYPE.VIDEO, state);
  134. const isParticipantSilent = participant?.isSilent || false;
  135. if (isLocalParticipantModerator(state)) {
  136. if (!isAudioMuted && !isParticipantSilent) {
  137. return QUICK_ACTION_BUTTON.MUTE;
  138. }
  139. if (!isVideoMuted) {
  140. return QUICK_ACTION_BUTTON.STOP_VIDEO;
  141. }
  142. if (isVideoForceMuted) {
  143. return QUICK_ACTION_BUTTON.ALLOW_VIDEO;
  144. }
  145. if (isSupported()(state) && !isParticipantSilent) {
  146. return QUICK_ACTION_BUTTON.ASK_TO_UNMUTE;
  147. }
  148. }
  149. return QUICK_ACTION_BUTTON.NONE;
  150. }
  151. /**
  152. * Returns true if the invite button should be rendered.
  153. *
  154. * @param {IReduxState} state - Global state.
  155. * @returns {boolean}
  156. */
  157. export const shouldRenderInviteButton = (state: IReduxState) => {
  158. const { disableInviteFunctions } = toState(state)['features/base/config'];
  159. const flagEnabled = getFeatureFlag(state, INVITE_ENABLED, true);
  160. const inBreakoutRoom = isInBreakoutRoom(state);
  161. return flagEnabled && !disableInviteFunctions && !inBreakoutRoom;
  162. };
  163. /**
  164. * Selector for retrieving ids of participants in the order that they are displayed in the filmstrip (with the
  165. * exception of participants with raised hand). The participants are reordered as follows.
  166. * 1. Dominant speaker.
  167. * 2. Local participant.
  168. * 3. Participants with raised hand.
  169. * 4. Participants with screenshare sorted alphabetically by their display name.
  170. * 5. Shared video participants.
  171. * 6. Recent speakers sorted alphabetically by their display name.
  172. * 7. Rest of the participants sorted alphabetically by their display name.
  173. *
  174. * @param {IStateful} stateful - The (whole) redux state, or redux's
  175. * {@code getState} function to be used to retrieve the state features/base/participants.
  176. * @returns {Array<string>}
  177. */
  178. export function getSortedParticipantIds(stateful: IStateful) {
  179. const id = getLocalParticipant(stateful)?.id;
  180. const remoteParticipants = getRemoteParticipantsSorted(stateful);
  181. const reorderedParticipants = new Set(remoteParticipants);
  182. const raisedHandParticipants = getRaiseHandsQueue(stateful).map(({ id: particId }) => particId);
  183. const remoteRaisedHandParticipants = new Set(raisedHandParticipants || []);
  184. const dominantSpeaker = getDominantSpeakerParticipant(stateful);
  185. for (const participant of remoteRaisedHandParticipants.keys()) {
  186. // Avoid duplicates.
  187. if (reorderedParticipants.has(participant)) {
  188. reorderedParticipants.delete(participant);
  189. }
  190. }
  191. const dominant = [];
  192. const dominantId = dominantSpeaker?.id;
  193. const local = remoteRaisedHandParticipants.has(id ?? '') ? [] : [ id ];
  194. // In case dominat speaker has raised hand, keep the order in the raised hand queue.
  195. // In case they don't have raised hand, goes first in the participants list.
  196. if (dominantId && dominantId !== id && !remoteRaisedHandParticipants.has(dominantId)) {
  197. reorderedParticipants.delete(dominantId);
  198. dominant.push(dominantId);
  199. }
  200. // Move self and participants with raised hand to the top of the list.
  201. return [
  202. ...dominant,
  203. ...local,
  204. ...Array.from(remoteRaisedHandParticipants.keys()),
  205. ...Array.from(reorderedParticipants.keys())
  206. ];
  207. }
  208. /**
  209. * Checks if a participant matches the search string.
  210. *
  211. * @param {Object} participant - The participant to be checked.
  212. * @param {string} searchString - The participants search string.
  213. * @returns {boolean}
  214. */
  215. export function participantMatchesSearch(participant: IParticipant | undefined
  216. | { displayName?: string; name?: string; },
  217. searchString: string) {
  218. if (searchString === '') {
  219. return true;
  220. }
  221. const participantName = normalizeAccents(participant?.name || participant?.displayName || '')
  222. .toLowerCase();
  223. const lowerCaseSearchString = searchString.trim().toLowerCase();
  224. return participantName.includes(lowerCaseSearchString);
  225. }
  226. /**
  227. * Returns whether the more actions button is visible.
  228. *
  229. * @param {IReduxState} state - Global state.
  230. * @returns {boolean}
  231. */
  232. export const isMoreActionsVisible = (state: IReduxState) => {
  233. const isLocalModerator = isLocalParticipantModerator(state);
  234. const inBreakoutRoom = isInBreakoutRoom(state);
  235. const { hideMoreActionsButton } = getParticipantsPaneConfig(state);
  236. return inBreakoutRoom ? false : !hideMoreActionsButton && isLocalModerator;
  237. };
  238. /**
  239. * Returns whether the mute all button is visible.
  240. *
  241. * @param {IReduxState} state - Global state.
  242. * @returns {boolean}
  243. */
  244. export const isMuteAllVisible = (state: IReduxState) => {
  245. const isLocalModerator = isLocalParticipantModerator(state);
  246. const inBreakoutRoom = isInBreakoutRoom(state);
  247. const { hideMuteAllButton } = getParticipantsPaneConfig(state);
  248. return inBreakoutRoom ? false : !hideMuteAllButton && isLocalModerator;
  249. };
  250. /**
  251. * Returns true if renaming the currently joined breakout room is allowed and false otherwise.
  252. *
  253. * @param {IReduxState} state - The redux state.
  254. * @returns {boolean} - True if renaming the currently joined breakout room is allowed and false otherwise.
  255. */
  256. export function isCurrentRoomRenamable(state: IReduxState) {
  257. return isInBreakoutRoom(state) && isBreakoutRoomRenameAllowed(state);
  258. }
  259. /**
  260. * Returns true if renaming a breakout room is allowed and false otherwise.
  261. *
  262. * @param {IReduxState} state - The redux state.
  263. * @returns {boolean} - True if renaming a breakout room is allowed and false otherwise.
  264. */
  265. export function isBreakoutRoomRenameAllowed(state: IReduxState) {
  266. const isLocalModerator = isLocalParticipantModerator(state);
  267. const conference = getCurrentConference(state);
  268. const isRenameBreakoutRoomsSupported
  269. = conference?.getBreakoutRooms()?.isFeatureSupported(BREAKOUT_ROOMS_RENAME_FEATURE) ?? false;
  270. return isLocalModerator && isRenameBreakoutRoomsSupported;
  271. }
  272. /**
  273. * Returns true if participants is enabled and false otherwise.
  274. *
  275. * @param {IStateful} stateful - The redux store, the redux
  276. * {@code getState} function, or the redux state itself.
  277. * @returns {boolean}
  278. */
  279. export const isParticipantsPaneEnabled = (stateful: IStateful) => {
  280. const state = toState(stateful);
  281. const { enabled = true } = getParticipantsPaneConfig(state);
  282. return Boolean(getFeatureFlag(state, PARTICIPANTS_ENABLED, true) && enabled);
  283. };