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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // @flow
  2. import { getGravatarURL } from '@jitsi/js-utils/avatar';
  3. import { JitsiParticipantConnectionStatus } from '../lib-jitsi-meet';
  4. import { MEDIA_TYPE, shouldRenderVideoTrack } from '../media';
  5. import { toState } from '../redux';
  6. import { getTrackByMediaTypeAndParticipant } from '../tracks';
  7. import { createDeferred } from '../util';
  8. import {
  9. JIGASI_PARTICIPANT_ICON,
  10. MAX_DISPLAY_NAME_LENGTH,
  11. PARTICIPANT_ROLE
  12. } from './constants';
  13. import { preloadImage } from './preloadImage';
  14. declare var config: Object;
  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 */
  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 => {
  30. return participant && participant.email ? getGravatarURL(participant.email) : null;
  31. }
  32. ];
  33. /* eslint-enable arrow-body-style */
  34. /**
  35. * Resolves the first loadable avatar URL for a participant.
  36. *
  37. * @param {Object} participant - The participant to resolve avatars for.
  38. * @returns {Promise}
  39. */
  40. export function getFirstLoadableAvatarUrl(participant: Object) {
  41. const deferred = createDeferred();
  42. const fullPromise = deferred.promise
  43. .then(() => _getFirstLoadableAvatarUrl(participant))
  44. .then(src => {
  45. if (AVATAR_QUEUE.length) {
  46. const next = AVATAR_QUEUE.shift();
  47. next.resolve();
  48. }
  49. return src;
  50. });
  51. if (AVATAR_QUEUE.length) {
  52. AVATAR_QUEUE.push(deferred);
  53. } else {
  54. deferred.resolve();
  55. }
  56. return fullPromise;
  57. }
  58. /**
  59. * Returns local participant from Redux state.
  60. *
  61. * @param {(Function|Object|Participant[])} stateful - The redux state
  62. * features/base/participants, the (whole) redux state, or redux's
  63. * {@code getState} function to be used to retrieve the state
  64. * features/base/participants.
  65. * @returns {(Participant|undefined)}
  66. */
  67. export function getLocalParticipant(stateful: Object | Function) {
  68. const participants = _getAllParticipants(stateful);
  69. return participants.find(p => p.local);
  70. }
  71. /**
  72. * Normalizes a display name so then no invalid values (padding, length...etc)
  73. * can be set.
  74. *
  75. * @param {string} name - The display name to set.
  76. * @returns {string}
  77. */
  78. export function getNormalizedDisplayName(name: string) {
  79. if (!name || !name.trim()) {
  80. return undefined;
  81. }
  82. return name.trim().substring(0, MAX_DISPLAY_NAME_LENGTH);
  83. }
  84. /**
  85. * Returns participant by ID from Redux state.
  86. *
  87. * @param {(Function|Object|Participant[])} stateful - The redux state
  88. * features/base/participants, the (whole) redux state, or redux's
  89. * {@code getState} function to be used to retrieve the state
  90. * features/base/participants.
  91. * @param {string} id - The ID of the participant to retrieve.
  92. * @private
  93. * @returns {(Participant|undefined)}
  94. */
  95. export function getParticipantById(
  96. stateful: Object | Function, id: string): ?Object {
  97. const participants = _getAllParticipants(stateful);
  98. return participants.find(p => p.id === id);
  99. }
  100. /**
  101. * Returns a count of the known participants in the passed in redux state,
  102. * excluding any fake participants.
  103. *
  104. * @param {(Function|Object|Participant[])} stateful - The redux state
  105. * features/base/participants, the (whole) redux state, or redux's
  106. * {@code getState} function to be used to retrieve the state
  107. * features/base/participants.
  108. * @returns {number}
  109. */
  110. export function getParticipantCount(stateful: Object | Function) {
  111. return getParticipants(stateful).length;
  112. }
  113. /**
  114. * Returns a count of the known participants in the passed in redux state,
  115. * including fake participants.
  116. *
  117. * @param {(Function|Object|Participant[])} stateful - The redux state
  118. * features/base/participants, the (whole) redux state, or redux's
  119. * {@code getState} function to be used to retrieve the state
  120. * features/base/participants.
  121. * @returns {number}
  122. */
  123. export function getParticipantCountWithFake(stateful: Object | Function) {
  124. return _getAllParticipants(stateful).length;
  125. }
  126. /**
  127. * Returns participant's display name.
  128. *
  129. * FIXME: Remove the hardcoded strings once interfaceConfig is stored in redux
  130. * and merge with a similarly named method in {@code conference.js}.
  131. *
  132. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  133. * {@code getState} function to be used to retrieve the state.
  134. * @param {string} id - The ID of the participant's display name to retrieve.
  135. * @returns {string}
  136. */
  137. export function getParticipantDisplayName(
  138. stateful: Object | Function,
  139. id: string) {
  140. const participant = getParticipantById(stateful, id);
  141. if (participant) {
  142. if (participant.name) {
  143. return participant.name;
  144. }
  145. if (participant.local) {
  146. return typeof interfaceConfig === 'object'
  147. ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME
  148. : 'me';
  149. }
  150. }
  151. return typeof interfaceConfig === 'object'
  152. ? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME
  153. : 'Fellow Jitster';
  154. }
  155. /**
  156. * Returns the presence status of a participant associated with the passed id.
  157. *
  158. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  159. * {@code getState} function to be used to retrieve the state.
  160. * @param {string} id - The id of the participant.
  161. * @returns {string} - The presence status.
  162. */
  163. export function getParticipantPresenceStatus(
  164. stateful: Object | Function, id: string) {
  165. if (!id) {
  166. return undefined;
  167. }
  168. const participantById = getParticipantById(stateful, id);
  169. if (!participantById) {
  170. return undefined;
  171. }
  172. return participantById.presence;
  173. }
  174. /**
  175. * Selectors for getting all known participants with fake participants filtered
  176. * out.
  177. *
  178. * @param {(Function|Object|Participant[])} stateful - The redux state
  179. * features/base/participants, the (whole) redux state, or redux's
  180. * {@code getState} function to be used to retrieve the state
  181. * features/base/participants.
  182. * @returns {Participant[]}
  183. */
  184. export function getParticipants(stateful: Object | Function) {
  185. return _getAllParticipants(stateful).filter(p => !p.isFakeParticipant);
  186. }
  187. /**
  188. * Returns the participant which has its pinned state set to truthy.
  189. *
  190. * @param {(Function|Object|Participant[])} stateful - The redux state
  191. * features/base/participants, the (whole) redux state, or redux's
  192. * {@code getState} function to be used to retrieve the state
  193. * features/base/participants.
  194. * @returns {(Participant|undefined)}
  195. */
  196. export function getPinnedParticipant(stateful: Object | Function) {
  197. return _getAllParticipants(stateful).find(p => p.pinned);
  198. }
  199. /**
  200. * Returns array of participants from Redux state.
  201. *
  202. * @param {(Function|Object|Participant[])} stateful - The redux state
  203. * features/base/participants, the (whole) redux state, or redux's
  204. * {@code getState} function to be used to retrieve the state
  205. * features/base/participants.
  206. * @private
  207. * @returns {Participant[]}
  208. */
  209. function _getAllParticipants(stateful) {
  210. return (
  211. Array.isArray(stateful)
  212. ? stateful
  213. : toState(stateful)['features/base/participants'] || []);
  214. }
  215. /**
  216. * Returns the youtube fake participant.
  217. * At the moment it is considered the youtube participant the only fake participant in the list.
  218. *
  219. * @param {(Function|Object|Participant[])} stateful - The redux state
  220. * features/base/participants, the (whole) redux state, or redux's
  221. * {@code getState} function to be used to retrieve the state
  222. * features/base/participants.
  223. * @private
  224. * @returns {Participant}
  225. */
  226. export function getYoutubeParticipant(stateful: Object | Function) {
  227. const participants = _getAllParticipants(stateful);
  228. return participants.filter(p => p.isFakeParticipant)[0];
  229. }
  230. /**
  231. * Returns true if the participant is a moderator.
  232. *
  233. * @param {string} participant - Participant object.
  234. * @returns {boolean}
  235. */
  236. export function isParticipantModerator(participant: Object) {
  237. return participant?.role === PARTICIPANT_ROLE.MODERATOR;
  238. }
  239. /**
  240. * Returns true if all of the meeting participants are moderators.
  241. *
  242. * @param {Object|Function} stateful -Object or function that can be resolved
  243. * to the Redux state.
  244. * @returns {boolean}
  245. */
  246. export function isEveryoneModerator(stateful: Object | Function) {
  247. const participants = _getAllParticipants(stateful);
  248. return participants.every(isParticipantModerator);
  249. }
  250. /**
  251. * Checks a value and returns true if it's a preloaded icon object.
  252. *
  253. * @param {?string | ?Object} icon - The icon to check.
  254. * @returns {boolean}
  255. */
  256. export function isIconUrl(icon: ?string | ?Object) {
  257. return Boolean(icon) && typeof icon === 'object';
  258. }
  259. /**
  260. * Returns true if the current local participant is a moderator in the
  261. * conference.
  262. *
  263. * @param {Object|Function} stateful - Object or function that can be resolved
  264. * to the Redux state.
  265. * @param {?boolean} ignoreToken - When true we ignore the token check.
  266. * @returns {boolean}
  267. */
  268. export function isLocalParticipantModerator(
  269. stateful: Object | Function,
  270. ignoreToken: ?boolean = false) {
  271. const state = toState(stateful);
  272. const localParticipant = getLocalParticipant(state);
  273. if (!localParticipant) {
  274. return false;
  275. }
  276. return (
  277. localParticipant.role === PARTICIPANT_ROLE.MODERATOR
  278. && (ignoreToken
  279. || !state['features/base/config'].enableUserRolesBasedOnToken
  280. || !state['features/base/jwt'].isGuest));
  281. }
  282. /**
  283. * Returns true if the video of the participant should be rendered.
  284. * NOTE: This is currently only used on mobile.
  285. *
  286. * @param {Object|Function} stateful - Object or function that can be resolved
  287. * to the Redux state.
  288. * @param {string} id - The ID of the participant.
  289. * @returns {boolean}
  290. */
  291. export function shouldRenderParticipantVideo(stateful: Object | Function, id: string) {
  292. const state = toState(stateful);
  293. const participant = getParticipantById(state, id);
  294. if (!participant) {
  295. return false;
  296. }
  297. /* First check if we have an unmuted video track. */
  298. const videoTrack
  299. = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  300. if (!shouldRenderVideoTrack(videoTrack, /* waitForVideoStarted */ false)) {
  301. return false;
  302. }
  303. /* Then check if the participant connection is active. */
  304. const connectionStatus = participant.connectionStatus || JitsiParticipantConnectionStatus.ACTIVE;
  305. if (connectionStatus !== JitsiParticipantConnectionStatus.ACTIVE) {
  306. return false;
  307. }
  308. /* Then check if audio-only mode is not active. */
  309. const audioOnly = state['features/base/audio-only'].enabled;
  310. if (!audioOnly) {
  311. return true;
  312. }
  313. /* Last, check if the participant is sharing their screen and they are on stage. */
  314. const screenShares = state['features/video-layout'].screenShares || [];
  315. const largeVideoParticipantId = state['features/large-video'].participantId;
  316. const participantIsInLargeVideoWithScreen
  317. = participant.id === largeVideoParticipantId && screenShares.includes(participant.id);
  318. return participantIsInLargeVideoWithScreen;
  319. }
  320. /**
  321. * Resolves the first loadable avatar URL for a participant.
  322. *
  323. * @param {Object} participant - The participant to resolve avatars for.
  324. * @returns {?string}
  325. */
  326. async function _getFirstLoadableAvatarUrl(participant) {
  327. for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
  328. const url = AVATAR_CHECKER_FUNCTIONS[i](participant);
  329. if (url) {
  330. if (AVATAR_CHECKED_URLS.has(url)) {
  331. if (AVATAR_CHECKED_URLS.get(url)) {
  332. return url;
  333. }
  334. } else {
  335. try {
  336. const finalUrl = await preloadImage(url);
  337. AVATAR_CHECKED_URLS.set(finalUrl, true);
  338. return finalUrl;
  339. } catch (e) {
  340. AVATAR_CHECKED_URLS.set(url, false);
  341. }
  342. }
  343. }
  344. }
  345. return undefined;
  346. }