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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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|Participant[])} stateful - The redux state
  69. * features/base/participants, the (whole) redux state, or redux's
  70. * {@code getState} function to be used to retrieve the state
  71. * features/base/participants.
  72. * @returns {(Participant|undefined)}
  73. */
  74. export function getLocalParticipant(stateful: Object | Function) {
  75. const participants = _getAllParticipants(stateful);
  76. return participants.find(p => p.local);
  77. }
  78. /**
  79. * Normalizes a display name so then no invalid values (padding, length...etc)
  80. * can be set.
  81. *
  82. * @param {string} name - The display name to set.
  83. * @returns {string}
  84. */
  85. export function getNormalizedDisplayName(name: string) {
  86. if (!name || !name.trim()) {
  87. return undefined;
  88. }
  89. return name.trim().substring(0, MAX_DISPLAY_NAME_LENGTH);
  90. }
  91. /**
  92. * Returns participant by ID from Redux state.
  93. *
  94. * @param {(Function|Object|Participant[])} stateful - The redux state
  95. * features/base/participants, the (whole) redux state, or redux's
  96. * {@code getState} function to be used to retrieve the state
  97. * features/base/participants.
  98. * @param {string} id - The ID of the participant to retrieve.
  99. * @private
  100. * @returns {(Participant|undefined)}
  101. */
  102. export function getParticipantById(
  103. stateful: Object | Function, id: string): ?Object {
  104. const participants = _getAllParticipants(stateful);
  105. return participants.find(p => p.id === id);
  106. }
  107. /**
  108. * Returns a count of the known participants in the passed in redux state,
  109. * excluding any fake participants.
  110. *
  111. * @param {(Function|Object|Participant[])} stateful - The redux state
  112. * features/base/participants, the (whole) redux state, or redux's
  113. * {@code getState} function to be used to retrieve the state
  114. * features/base/participants.
  115. * @returns {number}
  116. */
  117. export function getParticipantCount(stateful: Object | Function) {
  118. return getParticipants(stateful).length;
  119. }
  120. /**
  121. * Returns a count of the known participants in the passed in redux state,
  122. * including fake participants.
  123. *
  124. * @param {(Function|Object|Participant[])} stateful - The redux state
  125. * features/base/participants, the (whole) redux state, or redux's
  126. * {@code getState} function to be used to retrieve the state
  127. * features/base/participants.
  128. * @returns {number}
  129. */
  130. export function getParticipantCountWithFake(stateful: Object | Function) {
  131. return _getAllParticipants(stateful).length;
  132. }
  133. /**
  134. * Returns participant's display name.
  135. *
  136. * FIXME: Remove the hardcoded strings once interfaceConfig is stored in redux
  137. * and merge with a similarly named method in {@code conference.js}.
  138. *
  139. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  140. * {@code getState} function to be used to retrieve the state.
  141. * @param {string} id - The ID of the participant's display name to retrieve.
  142. * @returns {string}
  143. */
  144. export function getParticipantDisplayName(
  145. stateful: Object | Function,
  146. id: string) {
  147. const participant = getParticipantById(stateful, id);
  148. if (participant) {
  149. if (participant.name) {
  150. return participant.name;
  151. }
  152. if (participant.local) {
  153. return typeof interfaceConfig === 'object'
  154. ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME
  155. : 'me';
  156. }
  157. }
  158. return typeof interfaceConfig === 'object'
  159. ? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME
  160. : 'Fellow Jitster';
  161. }
  162. /**
  163. * Returns the presence status of a participant associated with the passed id.
  164. *
  165. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  166. * {@code getState} function to be used to retrieve the state.
  167. * @param {string} id - The id of the participant.
  168. * @returns {string} - The presence status.
  169. */
  170. export function getParticipantPresenceStatus(
  171. stateful: Object | Function, id: string) {
  172. if (!id) {
  173. return undefined;
  174. }
  175. const participantById = getParticipantById(stateful, id);
  176. if (!participantById) {
  177. return undefined;
  178. }
  179. return participantById.presence;
  180. }
  181. /**
  182. * Selectors for getting all known participants with fake participants filtered
  183. * out.
  184. *
  185. * @param {(Function|Object|Participant[])} stateful - The redux state
  186. * features/base/participants, the (whole) redux state, or redux's
  187. * {@code getState} function to be used to retrieve the state
  188. * features/base/participants.
  189. * @returns {Participant[]}
  190. */
  191. export function getParticipants(stateful: Object | Function) {
  192. return _getAllParticipants(stateful).filter(p => !p.isFakeParticipant);
  193. }
  194. /**
  195. * Returns the participant which has its pinned state set to truthy.
  196. *
  197. * @param {(Function|Object|Participant[])} stateful - The redux state
  198. * features/base/participants, the (whole) redux state, or redux's
  199. * {@code getState} function to be used to retrieve the state
  200. * features/base/participants.
  201. * @returns {(Participant|undefined)}
  202. */
  203. export function getPinnedParticipant(stateful: Object | Function) {
  204. return _getAllParticipants(stateful).find(p => p.pinned);
  205. }
  206. /**
  207. * Returns array of participants from Redux state.
  208. *
  209. * @param {(Function|Object|Participant[])} stateful - The redux state
  210. * features/base/participants, the (whole) redux state, or redux's
  211. * {@code getState} function to be used to retrieve the state
  212. * features/base/participants.
  213. * @private
  214. * @returns {Participant[]}
  215. */
  216. function _getAllParticipants(stateful) {
  217. return (
  218. Array.isArray(stateful)
  219. ? stateful
  220. : toState(stateful)['features/base/participants'] || []);
  221. }
  222. /**
  223. * Returns the youtube fake participant.
  224. * At the moment it is considered the youtube participant the only fake participant in the list.
  225. *
  226. * @param {(Function|Object|Participant[])} stateful - The redux state
  227. * features/base/participants, the (whole) redux state, or redux's
  228. * {@code getState} function to be used to retrieve the state
  229. * features/base/participants.
  230. * @private
  231. * @returns {Participant}
  232. */
  233. export function getYoutubeParticipant(stateful: Object | Function) {
  234. const participants = _getAllParticipants(stateful);
  235. return participants.filter(p => p.isFakeParticipant)[0];
  236. }
  237. /**
  238. * Returns true if the participant is a moderator.
  239. *
  240. * @param {string} participant - Participant object.
  241. * @returns {boolean}
  242. */
  243. export function isParticipantModerator(participant: Object) {
  244. return participant?.role === PARTICIPANT_ROLE.MODERATOR;
  245. }
  246. /**
  247. * Returns true if all of the meeting participants are moderators.
  248. *
  249. * @param {Object|Function} stateful -Object or function that can be resolved
  250. * to the Redux state.
  251. * @returns {boolean}
  252. */
  253. export function isEveryoneModerator(stateful: Object | Function) {
  254. const participants = _getAllParticipants(stateful);
  255. return participants.every(isParticipantModerator);
  256. }
  257. /**
  258. * Checks a value and returns true if it's a preloaded icon object.
  259. *
  260. * @param {?string | ?Object} icon - The icon to check.
  261. * @returns {boolean}
  262. */
  263. export function isIconUrl(icon: ?string | ?Object) {
  264. return Boolean(icon) && (typeof icon === 'object' || typeof icon === 'function');
  265. }
  266. /**
  267. * Returns true if the current local participant is a moderator in the
  268. * conference.
  269. *
  270. * @param {Object|Function} stateful - Object or function that can be resolved
  271. * to the Redux state.
  272. * @returns {boolean}
  273. */
  274. export function isLocalParticipantModerator(stateful: Object | Function) {
  275. const state = toState(stateful);
  276. const localParticipant = getLocalParticipant(state);
  277. if (!localParticipant) {
  278. return false;
  279. }
  280. return localParticipant.role === PARTICIPANT_ROLE.MODERATOR;
  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. * @param {Store} store - Redux store.
  325. * @returns {?string}
  326. */
  327. async function _getFirstLoadableAvatarUrl(participant, store) {
  328. for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
  329. const url = AVATAR_CHECKER_FUNCTIONS[i](participant, store);
  330. if (url) {
  331. if (AVATAR_CHECKED_URLS.has(url)) {
  332. if (AVATAR_CHECKED_URLS.get(url)) {
  333. return url;
  334. }
  335. } else {
  336. try {
  337. const finalUrl = await preloadImage(url);
  338. AVATAR_CHECKED_URLS.set(finalUrl, true);
  339. return finalUrl;
  340. } catch (e) {
  341. AVATAR_CHECKED_URLS.set(url, false);
  342. }
  343. }
  344. }
  345. }
  346. return undefined;
  347. }