您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.js 19KB

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