Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

functions.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // @flow
  2. import { getGravatarURL } from '@jitsi/js-utils/avatar';
  3. import type { Store } from 'redux';
  4. import { GRAVATAR_BASE_URL, isCORSAvatarURL } from '../avatar';
  5. import { JitsiParticipantConnectionStatus } from '../lib-jitsi-meet';
  6. import { MEDIA_TYPE, shouldRenderVideoTrack } from '../media';
  7. import { toState } from '../redux';
  8. import { getTrackByMediaTypeAndParticipant } from '../tracks';
  9. import { createDeferred } from '../util';
  10. import {
  11. JIGASI_PARTICIPANT_ICON,
  12. MAX_DISPLAY_NAME_LENGTH,
  13. PARTICIPANT_ROLE
  14. } from './constants';
  15. import { preloadImage } from './preloadImage';
  16. /**
  17. * Temp structures for avatar urls to be checked/preloaded.
  18. */
  19. const AVATAR_QUEUE = [];
  20. const AVATAR_CHECKED_URLS = new Map();
  21. /* eslint-disable arrow-body-style, no-unused-vars */
  22. const AVATAR_CHECKER_FUNCTIONS = [
  23. (participant, _) => {
  24. return participant && participant.isJigasi ? JIGASI_PARTICIPANT_ICON : null;
  25. },
  26. (participant, _) => {
  27. return participant && participant.avatarURL ? participant.avatarURL : null;
  28. },
  29. (participant, store) => {
  30. if (participant && participant.email) {
  31. // TODO: remove once libravatar has deployed their new scaled up infra. -saghul
  32. const gravatarBaseURL
  33. = store.getState()['features/base/config'].gravatarBaseURL ?? GRAVATAR_BASE_URL;
  34. return getGravatarURL(participant.email, gravatarBaseURL);
  35. }
  36. return null;
  37. }
  38. ];
  39. /* eslint-enable arrow-body-style, no-unused-vars */
  40. /**
  41. * Resolves the first loadable avatar URL for a participant.
  42. *
  43. * @param {Object} participant - The participant to resolve avatars for.
  44. * @param {Store} store - Redux store.
  45. * @returns {Promise}
  46. */
  47. export function getFirstLoadableAvatarUrl(participant: Object, store: Store<any, any>) {
  48. const deferred = createDeferred();
  49. const fullPromise = deferred.promise
  50. .then(() => _getFirstLoadableAvatarUrl(participant, store))
  51. .then(result => {
  52. if (AVATAR_QUEUE.length) {
  53. const next = AVATAR_QUEUE.shift();
  54. next.resolve();
  55. }
  56. return result;
  57. });
  58. if (AVATAR_QUEUE.length) {
  59. AVATAR_QUEUE.push(deferred);
  60. } else {
  61. deferred.resolve();
  62. }
  63. return fullPromise;
  64. }
  65. /**
  66. * Returns local participant from Redux state.
  67. *
  68. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  69. * {@code getState} function to be used to retrieve the state
  70. * features/base/participants.
  71. * @returns {(Participant|undefined)}
  72. */
  73. export function getLocalParticipant(stateful: Object | Function) {
  74. const state = toState(stateful)['features/base/participants'];
  75. return state.local;
  76. }
  77. /**
  78. * Normalizes a display name so then no invalid values (padding, length...etc)
  79. * can be set.
  80. *
  81. * @param {string} name - The display name to set.
  82. * @returns {string}
  83. */
  84. export function getNormalizedDisplayName(name: string) {
  85. if (!name || !name.trim()) {
  86. return undefined;
  87. }
  88. return name.trim().substring(0, MAX_DISPLAY_NAME_LENGTH);
  89. }
  90. /**
  91. * Returns participant by ID from Redux state.
  92. *
  93. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  94. * {@code getState} function to be used to retrieve the state
  95. * features/base/participants.
  96. * @param {string} id - The ID of the participant to retrieve.
  97. * @private
  98. * @returns {(Participant|undefined)}
  99. */
  100. export function getParticipantById(
  101. stateful: Object | Function, id: string): ?Object {
  102. const state = toState(stateful)['features/base/participants'];
  103. const { local, remote } = state;
  104. return remote.get(id) || (local?.id === id ? local : undefined);
  105. }
  106. /**
  107. * Returns the participant with the ID matching the passed ID or the local participant if the ID is
  108. * undefined.
  109. *
  110. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  111. * {@code getState} function to be used to retrieve the state
  112. * features/base/participants.
  113. * @param {string|undefined} [participantID] - An optional partipantID argument.
  114. * @returns {Participant|undefined}
  115. */
  116. export function getParticipantByIdOrUndefined(stateful: Object | Function, participantID: ?string) {
  117. return participantID ? getParticipantById(stateful, participantID) : getLocalParticipant(stateful);
  118. }
  119. /**
  120. * Returns a count of the known participants in the passed in redux state,
  121. * excluding any fake participants.
  122. *
  123. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  124. * {@code getState} function to be used to retrieve the state
  125. * features/base/participants.
  126. * @returns {number}
  127. */
  128. export function getParticipantCount(stateful: Object | Function) {
  129. const state = toState(stateful)['features/base/participants'];
  130. const { local, remote, fakeParticipants } = state;
  131. return remote.size - fakeParticipants.size + (local ? 1 : 0);
  132. }
  133. /**
  134. * Returns the Map with fake participants.
  135. *
  136. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  137. * {@code getState} function to be used to retrieve the state
  138. * features/base/participants.
  139. * @returns {Map<string, Participant>} - The Map with fake participants.
  140. */
  141. export function getFakeParticipants(stateful: Object | Function) {
  142. return toState(stateful)['features/base/participants'].fakeParticipants;
  143. }
  144. /**
  145. * Returns a count of the known remote participants in the passed in redux state.
  146. *
  147. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  148. * {@code getState} function to be used to retrieve the state
  149. * features/base/participants.
  150. * @returns {number}
  151. */
  152. export function getRemoteParticipantCount(stateful: Object | Function) {
  153. const state = toState(stateful)['features/base/participants'];
  154. return state.remote.size;
  155. }
  156. /**
  157. * Returns a count of the known participants in the passed in redux state,
  158. * including fake participants.
  159. *
  160. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  161. * {@code getState} function to be used to retrieve the state
  162. * features/base/participants.
  163. * @returns {number}
  164. */
  165. export function getParticipantCountWithFake(stateful: Object | Function) {
  166. const state = toState(stateful)['features/base/participants'];
  167. const { local, remote } = state;
  168. return remote.size + (local ? 1 : 0);
  169. }
  170. /**
  171. * Returns participant's 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.
  175. * @param {string} id - The ID of the participant's display name to retrieve.
  176. * @returns {string}
  177. */
  178. export function getParticipantDisplayName(
  179. stateful: Object | Function,
  180. id: string) {
  181. const participant = getParticipantById(stateful, id);
  182. const {
  183. defaultLocalDisplayName,
  184. defaultRemoteDisplayName
  185. } = toState(stateful)['features/base/config'];
  186. if (participant) {
  187. if (participant.name) {
  188. return participant.name;
  189. }
  190. if (participant.local) {
  191. return defaultLocalDisplayName;
  192. }
  193. }
  194. return defaultRemoteDisplayName;
  195. }
  196. /**
  197. * Returns the presence status of a participant associated with the passed id.
  198. *
  199. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  200. * {@code getState} function to be used to retrieve the state.
  201. * @param {string} id - The id of the participant.
  202. * @returns {string} - The presence status.
  203. */
  204. export function getParticipantPresenceStatus(
  205. stateful: Object | Function, id: string) {
  206. if (!id) {
  207. return undefined;
  208. }
  209. const participantById = getParticipantById(stateful, id);
  210. if (!participantById) {
  211. return undefined;
  212. }
  213. return participantById.presence;
  214. }
  215. /**
  216. * Returns true if there is at least 1 participant with screen sharing feature and false otherwise.
  217. *
  218. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  219. * {@code getState} function to be used to retrieve the state.
  220. * @returns {boolean}
  221. */
  222. export function haveParticipantWithScreenSharingFeature(stateful: Object | Function) {
  223. return toState(stateful)['features/base/participants'].haveParticipantWithScreenSharingFeature;
  224. }
  225. /**
  226. * Selectors for getting all remote participants.
  227. *
  228. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  229. * {@code getState} function to be used to retrieve the state
  230. * features/base/participants.
  231. * @returns {Map<string, Object>}
  232. */
  233. export function getRemoteParticipants(stateful: Object | Function) {
  234. return toState(stateful)['features/base/participants'].remote;
  235. }
  236. /**
  237. * Selectors for the getting the remote participants in the order that they are displayed in the filmstrip.
  238. *
  239. @param {(Function|Object)} stateful - The (whole) redux state, or redux's {@code getState} function to be used to
  240. * retrieve the state features/filmstrip.
  241. * @returns {Array<string>}
  242. */
  243. export function getRemoteParticipantsSorted(stateful: Object | Function) {
  244. return toState(stateful)['features/filmstrip'].remoteParticipants;
  245. }
  246. /**
  247. * Returns the participant which has its pinned state set to truthy.
  248. *
  249. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  250. * {@code getState} function to be used to retrieve the state
  251. * features/base/participants.
  252. * @returns {(Participant|undefined)}
  253. */
  254. export function getPinnedParticipant(stateful: Object | Function) {
  255. const state = toState(stateful)['features/base/participants'];
  256. const { pinnedParticipant } = state;
  257. if (!pinnedParticipant) {
  258. return undefined;
  259. }
  260. return getParticipantById(stateful, pinnedParticipant);
  261. }
  262. /**
  263. * Returns true if the participant is a moderator.
  264. *
  265. * @param {string} participant - Participant object.
  266. * @returns {boolean}
  267. */
  268. export function isParticipantModerator(participant: Object) {
  269. return participant?.role === PARTICIPANT_ROLE.MODERATOR;
  270. }
  271. /**
  272. * Returns the dominant speaker participant.
  273. *
  274. * @param {(Function|Object)} stateful - The (whole) redux state or redux's
  275. * {@code getState} function to be used to retrieve the state features/base/participants.
  276. * @returns {Participant} - The participant from the redux store.
  277. */
  278. export function getDominantSpeakerParticipant(stateful: Object | Function) {
  279. const state = toState(stateful)['features/base/participants'];
  280. const { dominantSpeaker } = state;
  281. if (!dominantSpeaker) {
  282. return undefined;
  283. }
  284. return getParticipantById(stateful, dominantSpeaker);
  285. }
  286. /**
  287. * Returns true if all of the meeting participants are moderators.
  288. *
  289. * @param {Object|Function} stateful -Object or function that can be resolved
  290. * to the Redux state.
  291. * @returns {boolean}
  292. */
  293. export function isEveryoneModerator(stateful: Object | Function) {
  294. const state = toState(stateful)['features/base/participants'];
  295. return state.everyoneIsModerator === true;
  296. }
  297. /**
  298. * Checks a value and returns true if it's a preloaded icon object.
  299. *
  300. * @param {?string | ?Object} icon - The icon to check.
  301. * @returns {boolean}
  302. */
  303. export function isIconUrl(icon: ?string | ?Object) {
  304. return Boolean(icon) && (typeof icon === 'object' || typeof icon === 'function');
  305. }
  306. /**
  307. * Returns true if the current local participant is a moderator in the
  308. * conference.
  309. *
  310. * @param {Object|Function} stateful - Object or function that can be resolved
  311. * to the Redux state.
  312. * @returns {boolean}
  313. */
  314. export function isLocalParticipantModerator(stateful: Object | Function) {
  315. const state = toState(stateful)['features/base/participants'];
  316. const { local } = state;
  317. if (!local) {
  318. return false;
  319. }
  320. return isParticipantModerator(local);
  321. }
  322. /**
  323. * Returns true if the video of the participant should be rendered.
  324. * NOTE: This is currently only used on mobile.
  325. *
  326. * @param {Object|Function} stateful - Object or function that can be resolved
  327. * to the Redux state.
  328. * @param {string} id - The ID of the participant.
  329. * @returns {boolean}
  330. */
  331. export function shouldRenderParticipantVideo(stateful: Object | Function, id: string) {
  332. const state = toState(stateful);
  333. const participant = getParticipantById(state, id);
  334. if (!participant) {
  335. return false;
  336. }
  337. /* First check if we have an unmuted video track. */
  338. const videoTrack
  339. = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  340. if (!shouldRenderVideoTrack(videoTrack, /* waitForVideoStarted */ false)) {
  341. return false;
  342. }
  343. /* Then check if the participant connection is active. */
  344. const connectionStatus = participant.connectionStatus || JitsiParticipantConnectionStatus.ACTIVE;
  345. if (connectionStatus !== JitsiParticipantConnectionStatus.ACTIVE) {
  346. return false;
  347. }
  348. /* Then check if audio-only mode is not active. */
  349. const audioOnly = state['features/base/audio-only'].enabled;
  350. if (!audioOnly) {
  351. return true;
  352. }
  353. /* Last, check if the participant is sharing their screen and they are on stage. */
  354. const remoteScreenShares = state['features/video-layout'].remoteScreenShares || [];
  355. const largeVideoParticipantId = state['features/large-video'].participantId;
  356. const participantIsInLargeVideoWithScreen
  357. = participant.id === largeVideoParticipantId && remoteScreenShares.includes(participant.id);
  358. return participantIsInLargeVideoWithScreen;
  359. }
  360. /**
  361. * Resolves the first loadable avatar URL for a participant.
  362. *
  363. * @param {Object} participant - The participant to resolve avatars for.
  364. * @param {Store} store - Redux store.
  365. * @returns {?string}
  366. */
  367. async function _getFirstLoadableAvatarUrl(participant, store) {
  368. for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
  369. const url = AVATAR_CHECKER_FUNCTIONS[i](participant, store);
  370. if (url !== null) {
  371. if (AVATAR_CHECKED_URLS.has(url)) {
  372. const { isLoadable, isUsingCORS } = AVATAR_CHECKED_URLS.get(url) || {};
  373. if (isLoadable) {
  374. return {
  375. isUsingCORS,
  376. src: url
  377. };
  378. }
  379. } else {
  380. try {
  381. const { corsAvatarURLs } = store.getState()['features/base/config'];
  382. const { isUsingCORS, src } = await preloadImage(url, isCORSAvatarURL(url, corsAvatarURLs));
  383. AVATAR_CHECKED_URLS.set(src, {
  384. isLoadable: true,
  385. isUsingCORS
  386. });
  387. return {
  388. isUsingCORS,
  389. src
  390. };
  391. } catch (e) {
  392. AVATAR_CHECKED_URLS.set(url, {
  393. isLoadable: false,
  394. isUsingCORS: false
  395. });
  396. }
  397. }
  398. }
  399. }
  400. return undefined;
  401. }
  402. /**
  403. * Get the participants queue with raised hands.
  404. *
  405. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  406. * {@code getState} function to be used to retrieve the state
  407. * features/base/participants.
  408. * @returns {Array<Object>}
  409. */
  410. export function getRaiseHandsQueue(stateful: Object | Function): Array<Object> {
  411. const { raisedHandsQueue } = toState(stateful)['features/base/participants'];
  412. return raisedHandsQueue;
  413. }
  414. /**
  415. * Returns whether the given participant has his hand raised or not.
  416. *
  417. * @param {Object} participant - The participant.
  418. * @returns {boolean} - Whether participant has raise hand or not.
  419. */
  420. export function hasRaisedHand(participant: Object): boolean {
  421. return Boolean(participant && participant.raisedHandTimestamp);
  422. }