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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. // @flow
  2. import { getGravatarURL } from '@jitsi/js-utils/avatar';
  3. import type { Store } from 'redux';
  4. import { JitsiParticipantConnectionStatus } from '../lib-jitsi-meet';
  5. import { MEDIA_TYPE, shouldRenderVideoTrack } from '../media';
  6. import { toState } from '../redux';
  7. import { getTrackByMediaTypeAndParticipant } from '../tracks';
  8. import { createDeferred } from '../util';
  9. import {
  10. JIGASI_PARTICIPANT_ICON,
  11. MAX_DISPLAY_NAME_LENGTH,
  12. PARTICIPANT_ROLE
  13. } from './constants';
  14. import { preloadImage } from './preloadImage';
  15. declare var interfaceConfig: Object;
  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 ?? 'https://www.gravatar.com/avatar/';
  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(src => {
  52. if (AVATAR_QUEUE.length) {
  53. const next = AVATAR_QUEUE.shift();
  54. next.resolve();
  55. }
  56. return src;
  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. * FIXME: Remove the hardcoded strings once interfaceConfig is stored in redux
  174. * and merge with a similarly named method in {@code conference.js}.
  175. *
  176. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  177. * {@code getState} function to be used to retrieve the state.
  178. * @param {string} id - The ID of the participant's display name to retrieve.
  179. * @returns {string}
  180. */
  181. export function getParticipantDisplayName(
  182. stateful: Object | Function,
  183. id: string) {
  184. const participant = getParticipantById(stateful, id);
  185. if (participant) {
  186. if (participant.name) {
  187. return participant.name;
  188. }
  189. if (participant.local) {
  190. return typeof interfaceConfig === 'object'
  191. ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME
  192. : 'me';
  193. }
  194. }
  195. return typeof interfaceConfig === 'object'
  196. ? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME
  197. : 'Fellow Jitster';
  198. }
  199. /**
  200. * Returns the presence status of a participant associated with the passed id.
  201. *
  202. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  203. * {@code getState} function to be used to retrieve the state.
  204. * @param {string} id - The id of the participant.
  205. * @returns {string} - The presence status.
  206. */
  207. export function getParticipantPresenceStatus(
  208. stateful: Object | Function, id: string) {
  209. if (!id) {
  210. return undefined;
  211. }
  212. const participantById = getParticipantById(stateful, id);
  213. if (!participantById) {
  214. return undefined;
  215. }
  216. return participantById.presence;
  217. }
  218. /**
  219. * Returns true if there is at least 1 participant with screen sharing feature and false otherwise.
  220. *
  221. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  222. * {@code getState} function to be used to retrieve the state.
  223. * @returns {boolean}
  224. */
  225. export function haveParticipantWithScreenSharingFeature(stateful: Object | Function) {
  226. return toState(stateful)['features/base/participants'].haveParticipantWithScreenSharingFeature;
  227. }
  228. /**
  229. * Selectors for getting all remote participants.
  230. *
  231. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  232. * {@code getState} function to be used to retrieve the state
  233. * features/base/participants.
  234. * @returns {Map<string, Object>}
  235. */
  236. export function getRemoteParticipants(stateful: Object | Function) {
  237. return toState(stateful)['features/base/participants'].remote;
  238. }
  239. /**
  240. * Returns the participant which has its pinned state set to truthy.
  241. *
  242. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  243. * {@code getState} function to be used to retrieve the state
  244. * features/base/participants.
  245. * @returns {(Participant|undefined)}
  246. */
  247. export function getPinnedParticipant(stateful: Object | Function) {
  248. const state = toState(stateful)['features/base/participants'];
  249. const { pinnedParticipant } = state;
  250. if (!pinnedParticipant) {
  251. return undefined;
  252. }
  253. return getParticipantById(stateful, pinnedParticipant);
  254. }
  255. /**
  256. * Returns true if the participant is a moderator.
  257. *
  258. * @param {string} participant - Participant object.
  259. * @returns {boolean}
  260. */
  261. export function isParticipantModerator(participant: Object) {
  262. return participant?.role === PARTICIPANT_ROLE.MODERATOR;
  263. }
  264. /**
  265. * Returns the dominant speaker participant.
  266. *
  267. * @param {(Function|Object)} stateful - The (whole) redux state or redux's
  268. * {@code getState} function to be used to retrieve the state features/base/participants.
  269. * @returns {Participant} - The participant from the redux store.
  270. */
  271. export function getDominantSpeakerParticipant(stateful: Object | Function) {
  272. const state = toState(stateful)['features/base/participants'];
  273. const { dominantSpeaker } = state;
  274. if (!dominantSpeaker) {
  275. return undefined;
  276. }
  277. return getParticipantById(stateful, dominantSpeaker);
  278. }
  279. /**
  280. * Returns true if all of the meeting participants are moderators.
  281. *
  282. * @param {Object|Function} stateful -Object or function that can be resolved
  283. * to the Redux state.
  284. * @returns {boolean}
  285. */
  286. export function isEveryoneModerator(stateful: Object | Function) {
  287. const state = toState(stateful)['features/base/participants'];
  288. return state.everyoneIsModerator === true;
  289. }
  290. /**
  291. * Checks a value and returns true if it's a preloaded icon object.
  292. *
  293. * @param {?string | ?Object} icon - The icon to check.
  294. * @returns {boolean}
  295. */
  296. export function isIconUrl(icon: ?string | ?Object) {
  297. return Boolean(icon) && (typeof icon === 'object' || typeof icon === 'function');
  298. }
  299. /**
  300. * Returns true if the current local participant is a moderator in the
  301. * conference.
  302. *
  303. * @param {Object|Function} stateful - Object or function that can be resolved
  304. * to the Redux state.
  305. * @returns {boolean}
  306. */
  307. export function isLocalParticipantModerator(stateful: Object | Function) {
  308. const state = toState(stateful)['features/base/participants'];
  309. const { local } = state;
  310. if (!local) {
  311. return false;
  312. }
  313. return isParticipantModerator(local);
  314. }
  315. /**
  316. * Returns true if the video of the participant should be rendered.
  317. * NOTE: This is currently only used on mobile.
  318. *
  319. * @param {Object|Function} stateful - Object or function that can be resolved
  320. * to the Redux state.
  321. * @param {string} id - The ID of the participant.
  322. * @returns {boolean}
  323. */
  324. export function shouldRenderParticipantVideo(stateful: Object | Function, id: string) {
  325. const state = toState(stateful);
  326. const participant = getParticipantById(state, id);
  327. if (!participant) {
  328. return false;
  329. }
  330. /* First check if we have an unmuted video track. */
  331. const videoTrack
  332. = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  333. if (!shouldRenderVideoTrack(videoTrack, /* waitForVideoStarted */ false)) {
  334. return false;
  335. }
  336. /* Then check if the participant connection is active. */
  337. const connectionStatus = participant.connectionStatus || JitsiParticipantConnectionStatus.ACTIVE;
  338. if (connectionStatus !== JitsiParticipantConnectionStatus.ACTIVE) {
  339. return false;
  340. }
  341. /* Then check if audio-only mode is not active. */
  342. const audioOnly = state['features/base/audio-only'].enabled;
  343. if (!audioOnly) {
  344. return true;
  345. }
  346. /* Last, check if the participant is sharing their screen and they are on stage. */
  347. const remoteScreenShares = state['features/video-layout'].remoteScreenShares || [];
  348. const largeVideoParticipantId = state['features/large-video'].participantId;
  349. const participantIsInLargeVideoWithScreen
  350. = participant.id === largeVideoParticipantId && remoteScreenShares.includes(participant.id);
  351. return participantIsInLargeVideoWithScreen;
  352. }
  353. /**
  354. * Resolves the first loadable avatar URL for a participant.
  355. *
  356. * @param {Object} participant - The participant to resolve avatars for.
  357. * @param {Store} store - Redux store.
  358. * @returns {?string}
  359. */
  360. async function _getFirstLoadableAvatarUrl(participant, store) {
  361. for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
  362. const url = AVATAR_CHECKER_FUNCTIONS[i](participant, store);
  363. if (url !== null) {
  364. if (AVATAR_CHECKED_URLS.has(url)) {
  365. if (AVATAR_CHECKED_URLS.get(url)) {
  366. return url;
  367. }
  368. } else {
  369. try {
  370. const finalUrl = await preloadImage(url);
  371. AVATAR_CHECKED_URLS.set(finalUrl, true);
  372. return finalUrl;
  373. } catch (e) {
  374. AVATAR_CHECKED_URLS.set(url, false);
  375. }
  376. }
  377. }
  378. }
  379. return undefined;
  380. }
  381. /**
  382. * Selector for retrieving sorted participants by display name.
  383. *
  384. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  385. * {@code getState} function to be used to retrieve the state
  386. * features/base/participants.
  387. * @returns {Array<Object>}
  388. */
  389. export function getSortedParticipants(stateful: Object | Function) {
  390. const localParticipant = getLocalParticipant(stateful);
  391. const remoteParticipants = getRemoteParticipants(stateful);
  392. const raisedHandParticipantIds = getRaiseHandsQueue(stateful);
  393. const items = [];
  394. const dominantSpeaker = getDominantSpeakerParticipant(stateful);
  395. const raisedHandParticipants = [];
  396. raisedHandParticipantIds
  397. .map(id => remoteParticipants.get(id) || localParticipant)
  398. .forEach(p => {
  399. if (p !== dominantSpeaker) {
  400. raisedHandParticipants.push(p);
  401. }
  402. });
  403. remoteParticipants.forEach(p => {
  404. if (p !== dominantSpeaker && !raisedHandParticipantIds.find(id => p.id === id)) {
  405. items.push(p);
  406. }
  407. });
  408. if (!raisedHandParticipantIds.find(id => localParticipant.id === id)) {
  409. items.push(localParticipant);
  410. }
  411. items.sort((a, b) =>
  412. getParticipantDisplayName(stateful, a.id).localeCompare(getParticipantDisplayName(stateful, b.id))
  413. );
  414. items.unshift(...raisedHandParticipants);
  415. if (dominantSpeaker && dominantSpeaker !== localParticipant) {
  416. items.unshift(dominantSpeaker);
  417. }
  418. return items;
  419. }
  420. /**
  421. * Selector for retrieving ids of alphabetically sorted participants by name.
  422. *
  423. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  424. * {@code getState} function to be used to retrieve the state
  425. * features/base/participants.
  426. * @returns {Array<string>}
  427. */
  428. export function getSortedParticipantIds(stateful: Object | Function): Array<string> {
  429. const participantIds = getSortedParticipants(stateful).map((p): Object => p.id);
  430. return participantIds;
  431. }
  432. /**
  433. * Get the participants queue with raised hands.
  434. *
  435. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  436. * {@code getState} function to be used to retrieve the state
  437. * features/base/participants.
  438. * @returns {Array<string>}
  439. */
  440. export function getRaiseHandsQueue(stateful: Object | Function): Array<string> {
  441. const { raisedHandsQueue } = toState(stateful)['features/base/participants'];
  442. return raisedHandsQueue;
  443. }