Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

functions.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. * Curried version of getParticipantDisplayName.
  164. *
  165. * @see {@link getParticipantDisplayName}
  166. * @param {string} id - The ID of the participant's display name to retrieve.
  167. * @returns {Function}
  168. */
  169. export const getParticipantDisplayNameWithId = (id: string) =>
  170. (state: Object | Function) =>
  171. getParticipantDisplayName(state, id);
  172. /**
  173. * Returns the presence status of a participant associated with the passed id.
  174. *
  175. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  176. * {@code getState} function to be used to retrieve the state.
  177. * @param {string} id - The id of the participant.
  178. * @returns {string} - The presence status.
  179. */
  180. export function getParticipantPresenceStatus(
  181. stateful: Object | Function, id: string) {
  182. if (!id) {
  183. return undefined;
  184. }
  185. const participantById = getParticipantById(stateful, id);
  186. if (!participantById) {
  187. return undefined;
  188. }
  189. return participantById.presence;
  190. }
  191. /**
  192. * Selectors for getting all known participants with fake participants filtered
  193. * out.
  194. *
  195. * @param {(Function|Object|Participant[])} stateful - The redux state
  196. * features/base/participants, the (whole) redux state, or redux's
  197. * {@code getState} function to be used to retrieve the state
  198. * features/base/participants.
  199. * @returns {Participant[]}
  200. */
  201. export function getParticipants(stateful: Object | Function) {
  202. return _getAllParticipants(stateful).filter(p => !p.isFakeParticipant);
  203. }
  204. /**
  205. * Returns the participant which has its pinned state set to truthy.
  206. *
  207. * @param {(Function|Object|Participant[])} stateful - The redux state
  208. * features/base/participants, the (whole) redux state, or redux's
  209. * {@code getState} function to be used to retrieve the state
  210. * features/base/participants.
  211. * @returns {(Participant|undefined)}
  212. */
  213. export function getPinnedParticipant(stateful: Object | Function) {
  214. return _getAllParticipants(stateful).find(p => p.pinned);
  215. }
  216. /**
  217. * Returns array of participants from Redux state.
  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. function _getAllParticipants(stateful) {
  227. return (
  228. Array.isArray(stateful)
  229. ? stateful
  230. : toState(stateful)['features/base/participants'] || []);
  231. }
  232. /**
  233. * Returns the youtube fake participant.
  234. * At the moment it is considered the youtube participant the only fake participant in the list.
  235. *
  236. * @param {(Function|Object|Participant[])} stateful - The redux state
  237. * features/base/participants, the (whole) redux state, or redux's
  238. * {@code getState} function to be used to retrieve the state
  239. * features/base/participants.
  240. * @private
  241. * @returns {Participant}
  242. */
  243. export function getYoutubeParticipant(stateful: Object | Function) {
  244. const participants = _getAllParticipants(stateful);
  245. return participants.filter(p => p.isFakeParticipant)[0];
  246. }
  247. /**
  248. * Returns true if the participant is a moderator.
  249. *
  250. * @param {string} participant - Participant object.
  251. * @returns {boolean}
  252. */
  253. export function isParticipantModerator(participant: Object) {
  254. return participant?.role === PARTICIPANT_ROLE.MODERATOR;
  255. }
  256. /**
  257. * Returns true if all of the meeting participants are moderators.
  258. *
  259. * @param {Object|Function} stateful -Object or function that can be resolved
  260. * to the Redux state.
  261. * @returns {boolean}
  262. */
  263. export function isEveryoneModerator(stateful: Object | Function) {
  264. const participants = _getAllParticipants(stateful);
  265. return participants.every(isParticipantModerator);
  266. }
  267. /**
  268. * Checks a value and returns true if it's a preloaded icon object.
  269. *
  270. * @param {?string | ?Object} icon - The icon to check.
  271. * @returns {boolean}
  272. */
  273. export function isIconUrl(icon: ?string | ?Object) {
  274. return Boolean(icon) && (typeof icon === 'object' || typeof icon === 'function');
  275. }
  276. /**
  277. * Returns true if the current local participant is a moderator in the
  278. * conference.
  279. *
  280. * @param {Object|Function} stateful - Object or function that can be resolved
  281. * to the Redux state.
  282. * @returns {boolean}
  283. */
  284. export function isLocalParticipantModerator(stateful: Object | Function) {
  285. const state = toState(stateful);
  286. const localParticipant = getLocalParticipant(state);
  287. if (!localParticipant) {
  288. return false;
  289. }
  290. return localParticipant.role === PARTICIPANT_ROLE.MODERATOR;
  291. }
  292. /**
  293. * Returns true if the video of the participant should be rendered.
  294. * NOTE: This is currently only used on mobile.
  295. *
  296. * @param {Object|Function} stateful - Object or function that can be resolved
  297. * to the Redux state.
  298. * @param {string} id - The ID of the participant.
  299. * @returns {boolean}
  300. */
  301. export function shouldRenderParticipantVideo(stateful: Object | Function, id: string) {
  302. const state = toState(stateful);
  303. const participant = getParticipantById(state, id);
  304. if (!participant) {
  305. return false;
  306. }
  307. /* First check if we have an unmuted video track. */
  308. const videoTrack
  309. = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  310. if (!shouldRenderVideoTrack(videoTrack, /* waitForVideoStarted */ false)) {
  311. return false;
  312. }
  313. /* Then check if the participant connection is active. */
  314. const connectionStatus = participant.connectionStatus || JitsiParticipantConnectionStatus.ACTIVE;
  315. if (connectionStatus !== JitsiParticipantConnectionStatus.ACTIVE) {
  316. return false;
  317. }
  318. /* Then check if audio-only mode is not active. */
  319. const audioOnly = state['features/base/audio-only'].enabled;
  320. if (!audioOnly) {
  321. return true;
  322. }
  323. /* Last, check if the participant is sharing their screen and they are on stage. */
  324. const remoteScreenShares = state['features/video-layout'].remoteScreenShares || [];
  325. const largeVideoParticipantId = state['features/large-video'].participantId;
  326. const participantIsInLargeVideoWithScreen
  327. = participant.id === largeVideoParticipantId && remoteScreenShares.includes(participant.id);
  328. return participantIsInLargeVideoWithScreen;
  329. }
  330. /**
  331. * Resolves the first loadable avatar URL for a participant.
  332. *
  333. * @param {Object} participant - The participant to resolve avatars for.
  334. * @param {Store} store - Redux store.
  335. * @returns {?string}
  336. */
  337. async function _getFirstLoadableAvatarUrl(participant, store) {
  338. for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
  339. const url = AVATAR_CHECKER_FUNCTIONS[i](participant, store);
  340. if (url) {
  341. if (AVATAR_CHECKED_URLS.has(url)) {
  342. if (AVATAR_CHECKED_URLS.get(url)) {
  343. return url;
  344. }
  345. } else {
  346. try {
  347. const finalUrl = await preloadImage(url);
  348. AVATAR_CHECKED_URLS.set(finalUrl, true);
  349. return finalUrl;
  350. } catch (e) {
  351. AVATAR_CHECKED_URLS.set(url, false);
  352. }
  353. }
  354. }
  355. }
  356. return undefined;
  357. }