Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

functions.ts 11KB

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