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.ts 8.9KB

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