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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. * Is the participants pane open.
  124. *
  125. * @param {Object} state - Global state.
  126. * @returns {boolean} Is the participants pane open.
  127. */
  128. export const getParticipantsPaneOpen = (state: Object) => Boolean(getState(state)?.isOpen);
  129. /**
  130. * Returns the type of quick action button to be displayed for a participant.
  131. * The button is displayed when hovering a participant from the participant list.
  132. *
  133. * @param {Object} participant - The participant.
  134. * @param {boolean} isAudioMuted - If audio is muted for the participant.
  135. * @param {Object} state - The redux state.
  136. * @returns {string} - The type of the quick action button.
  137. */
  138. export function getQuickActionButtonType(participant: Object, isAudioMuted: Boolean, state: Object) {
  139. // handled only by moderators
  140. if (isLocalParticipantModerator(state)) {
  141. if (!isAudioMuted) {
  142. return QUICK_ACTION_BUTTON.MUTE;
  143. }
  144. if (isSupported()(state)) {
  145. return QUICK_ACTION_BUTTON.ASK_TO_UNMUTE;
  146. }
  147. }
  148. return QUICK_ACTION_BUTTON.NONE;
  149. }
  150. /**
  151. * Returns true if the invite button should be rendered.
  152. *
  153. * @param {Object} state - Global state.
  154. * @returns {boolean}
  155. */
  156. export const shouldRenderInviteButton = (state: Object) => {
  157. const { disableInviteFunctions } = toState(state)['features/base/config'];
  158. const flagEnabled = getFeatureFlag(state, INVITE_ENABLED, true);
  159. const inBreakoutRoom = isInBreakoutRoom(state);
  160. return flagEnabled && !disableInviteFunctions && !inBreakoutRoom;
  161. };
  162. /**
  163. * Selector for retrieving ids of participants in the order that they are displayed in the filmstrip (with the
  164. * exception of participants with raised hand). The participants are reordered as follows.
  165. * 1. Dominant speaker.
  166. * 2. Local participant.
  167. * 3. Participants with raised hand.
  168. * 4. Participants with screenshare sorted alphabetically by their display name.
  169. * 5. Shared video participants.
  170. * 6. Recent speakers sorted alphabetically by their display name.
  171. * 7. Rest of the participants sorted alphabetically by their display name.
  172. *
  173. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  174. * {@code getState} function to be used to retrieve the state features/base/participants.
  175. * @returns {Array<string>}
  176. */
  177. export function getSortedParticipantIds(stateful: Object | Function): Array<string> {
  178. const { id } = getLocalParticipant(stateful);
  179. const remoteParticipants = getRemoteParticipantsSorted(stateful);
  180. const reorderedParticipants = new Set(remoteParticipants);
  181. const raisedHandParticipants = getRaiseHandsQueue(stateful).map(({ id: particId }) => particId);
  182. const remoteRaisedHandParticipants = new Set(raisedHandParticipants || []);
  183. const dominantSpeaker = getDominantSpeakerParticipant(stateful);
  184. for (const participant of remoteRaisedHandParticipants.keys()) {
  185. // Avoid duplicates.
  186. if (reorderedParticipants.has(participant)) {
  187. reorderedParticipants.delete(participant);
  188. }
  189. }
  190. const dominant = [];
  191. const dominantId = dominantSpeaker?.id;
  192. const local = remoteRaisedHandParticipants.has(id) ? [] : [ id ];
  193. // In case dominat speaker has raised hand, keep the order in the raised hand queue.
  194. // In case they don't have raised hand, goes first in the participants list.
  195. if (dominantId && dominantId !== id && !remoteRaisedHandParticipants.has(dominantId)) {
  196. reorderedParticipants.delete(dominantId);
  197. dominant.push(dominantId);
  198. }
  199. // Move self and participants with raised hand to the top of the list.
  200. return [
  201. ...dominant,
  202. ...local,
  203. ...Array.from(remoteRaisedHandParticipants.keys()),
  204. ...Array.from(reorderedParticipants.keys())
  205. ];
  206. }
  207. /**
  208. * Checks if a participant matches the search string.
  209. *
  210. * @param {Object} participant - The participant to be checked.
  211. * @param {string} searchString - The participants search string.
  212. * @returns {boolean}
  213. */
  214. export function participantMatchesSearch(participant: Object, searchString: string) {
  215. if (searchString === '') {
  216. return true;
  217. }
  218. const names = normalizeAccents(participant?.name || participant?.displayName || '')
  219. .toLowerCase()
  220. .split(' ');
  221. const lowerCaseSearchString = searchString.toLowerCase();
  222. for (const name of names) {
  223. if (name.startsWith(lowerCaseSearchString)) {
  224. return true;
  225. }
  226. }
  227. return false;
  228. }