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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. * Selectors for the getting the remote participants in the order that they are displayed in the filmstrip.
  241. *
  242. @param {(Function|Object)} stateful - The (whole) redux state, or redux's {@code getState} function to be used to
  243. * retrieve the state features/filmstrip.
  244. * @returns {Array<string>}
  245. */
  246. export function getRemoteParticipantsSorted(stateful: Object | Function) {
  247. return toState(stateful)['features/filmstrip'].remoteParticipants;
  248. }
  249. /**
  250. * Returns the participant which has its pinned state set to truthy.
  251. *
  252. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  253. * {@code getState} function to be used to retrieve the state
  254. * features/base/participants.
  255. * @returns {(Participant|undefined)}
  256. */
  257. export function getPinnedParticipant(stateful: Object | Function) {
  258. const state = toState(stateful)['features/base/participants'];
  259. const { pinnedParticipant } = state;
  260. if (!pinnedParticipant) {
  261. return undefined;
  262. }
  263. return getParticipantById(stateful, pinnedParticipant);
  264. }
  265. /**
  266. * Returns true if the participant is a moderator.
  267. *
  268. * @param {string} participant - Participant object.
  269. * @returns {boolean}
  270. */
  271. export function isParticipantModerator(participant: Object) {
  272. return participant?.role === PARTICIPANT_ROLE.MODERATOR;
  273. }
  274. /**
  275. * Returns the dominant speaker participant.
  276. *
  277. * @param {(Function|Object)} stateful - The (whole) redux state or redux's
  278. * {@code getState} function to be used to retrieve the state features/base/participants.
  279. * @returns {Participant} - The participant from the redux store.
  280. */
  281. export function getDominantSpeakerParticipant(stateful: Object | Function) {
  282. const state = toState(stateful)['features/base/participants'];
  283. const { dominantSpeaker } = state;
  284. if (!dominantSpeaker) {
  285. return undefined;
  286. }
  287. return getParticipantById(stateful, dominantSpeaker);
  288. }
  289. /**
  290. * Returns true if all of the meeting participants are moderators.
  291. *
  292. * @param {Object|Function} stateful -Object or function that can be resolved
  293. * to the Redux state.
  294. * @returns {boolean}
  295. */
  296. export function isEveryoneModerator(stateful: Object | Function) {
  297. const state = toState(stateful)['features/base/participants'];
  298. return state.everyoneIsModerator === true;
  299. }
  300. /**
  301. * Checks a value and returns true if it's a preloaded icon object.
  302. *
  303. * @param {?string | ?Object} icon - The icon to check.
  304. * @returns {boolean}
  305. */
  306. export function isIconUrl(icon: ?string | ?Object) {
  307. return Boolean(icon) && (typeof icon === 'object' || typeof icon === 'function');
  308. }
  309. /**
  310. * Returns true if the current local participant is a moderator in the
  311. * conference.
  312. *
  313. * @param {Object|Function} stateful - Object or function that can be resolved
  314. * to the Redux state.
  315. * @returns {boolean}
  316. */
  317. export function isLocalParticipantModerator(stateful: Object | Function) {
  318. const state = toState(stateful)['features/base/participants'];
  319. const { local } = state;
  320. if (!local) {
  321. return false;
  322. }
  323. return isParticipantModerator(local);
  324. }
  325. /**
  326. * Returns true if the video of the participant should be rendered.
  327. * NOTE: This is currently only used on mobile.
  328. *
  329. * @param {Object|Function} stateful - Object or function that can be resolved
  330. * to the Redux state.
  331. * @param {string} id - The ID of the participant.
  332. * @returns {boolean}
  333. */
  334. export function shouldRenderParticipantVideo(stateful: Object | Function, id: string) {
  335. const state = toState(stateful);
  336. const participant = getParticipantById(state, id);
  337. if (!participant) {
  338. return false;
  339. }
  340. /* First check if we have an unmuted video track. */
  341. const videoTrack
  342. = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  343. if (!shouldRenderVideoTrack(videoTrack, /* waitForVideoStarted */ false)) {
  344. return false;
  345. }
  346. /* Then check if the participant connection is active. */
  347. const connectionStatus = participant.connectionStatus || JitsiParticipantConnectionStatus.ACTIVE;
  348. if (connectionStatus !== JitsiParticipantConnectionStatus.ACTIVE) {
  349. return false;
  350. }
  351. /* Then check if audio-only mode is not active. */
  352. const audioOnly = state['features/base/audio-only'].enabled;
  353. if (!audioOnly) {
  354. return true;
  355. }
  356. /* Last, check if the participant is sharing their screen and they are on stage. */
  357. const remoteScreenShares = state['features/video-layout'].remoteScreenShares || [];
  358. const largeVideoParticipantId = state['features/large-video'].participantId;
  359. const participantIsInLargeVideoWithScreen
  360. = participant.id === largeVideoParticipantId && remoteScreenShares.includes(participant.id);
  361. return participantIsInLargeVideoWithScreen;
  362. }
  363. /**
  364. * Resolves the first loadable avatar URL for a participant.
  365. *
  366. * @param {Object} participant - The participant to resolve avatars for.
  367. * @param {Store} store - Redux store.
  368. * @returns {?string}
  369. */
  370. async function _getFirstLoadableAvatarUrl(participant, store) {
  371. for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
  372. const url = AVATAR_CHECKER_FUNCTIONS[i](participant, store);
  373. if (url !== null) {
  374. if (AVATAR_CHECKED_URLS.has(url)) {
  375. if (AVATAR_CHECKED_URLS.get(url)) {
  376. return url;
  377. }
  378. } else {
  379. try {
  380. const finalUrl = await preloadImage(url);
  381. AVATAR_CHECKED_URLS.set(finalUrl, true);
  382. return finalUrl;
  383. } catch (e) {
  384. AVATAR_CHECKED_URLS.set(url, false);
  385. }
  386. }
  387. }
  388. }
  389. return undefined;
  390. }
  391. /**
  392. * Selector for retrieving ids of participants in the order that they are displayed in the filmstrip (with the
  393. * exception of participants with raised hand). The participants are reordered as follows.
  394. * 1. Local participant.
  395. * 2. Participants with raised hand.
  396. * 3. Participants with screenshare sorted alphabetically by their display name.
  397. * 4. Shared video participants.
  398. * 5. Recent speakers sorted alphabetically by their display name.
  399. * 6. Rest of the participants sorted alphabetically by their display name.
  400. *
  401. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  402. * {@code getState} function to be used to retrieve the state features/base/participants.
  403. * @returns {Array<string>}
  404. */
  405. export function getSortedParticipantIds(stateful: Object | Function): Array<string> {
  406. const { id } = getLocalParticipant(stateful);
  407. const remoteParticipants = getRemoteParticipantsSorted(stateful);
  408. const reorderedParticipants = new Set(remoteParticipants);
  409. const raisedHandParticipants = getRaiseHandsQueue(stateful);
  410. const remoteRaisedHandParticipants = new Set(raisedHandParticipants || []);
  411. for (const participant of remoteRaisedHandParticipants.keys()) {
  412. // Avoid duplicates.
  413. if (reorderedParticipants.has(participant)) {
  414. reorderedParticipants.delete(participant);
  415. } else {
  416. remoteRaisedHandParticipants.delete(participant);
  417. }
  418. }
  419. // Remove self.
  420. remoteRaisedHandParticipants.has(id) && remoteRaisedHandParticipants.delete(id);
  421. // Move self and participants with raised hand to the top of the list.
  422. return [
  423. id,
  424. ...Array.from(remoteRaisedHandParticipants.keys()),
  425. ...Array.from(reorderedParticipants.keys())
  426. ];
  427. }
  428. /**
  429. * Get the participants queue with raised hands.
  430. *
  431. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  432. * {@code getState} function to be used to retrieve the state
  433. * features/base/participants.
  434. * @returns {Array<string>}
  435. */
  436. export function getRaiseHandsQueue(stateful: Object | Function): Array<string> {
  437. const { raisedHandsQueue } = toState(stateful)['features/base/participants'];
  438. return raisedHandsQueue;
  439. }