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 9.7KB

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