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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // @flow
  2. import { getGravatarURL } from '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 all of the meeting participants are moderators.
  232. *
  233. * @param {Object|Function} stateful -Object or function that can be resolved
  234. * to the Redux state.
  235. * @returns {boolean}
  236. */
  237. export function isEveryoneModerator(stateful: Object | Function) {
  238. const participants = _getAllParticipants(stateful);
  239. for (const participant of participants) {
  240. if (participant.role !== PARTICIPANT_ROLE.MODERATOR) {
  241. return false;
  242. }
  243. }
  244. return true;
  245. }
  246. /**
  247. * Checks a value and returns true if it's a preloaded icon object.
  248. *
  249. * @param {?string | ?Object} icon - The icon to check.
  250. * @returns {boolean}
  251. */
  252. export function isIconUrl(icon: ?string | ?Object) {
  253. return Boolean(icon) && typeof icon === 'object';
  254. }
  255. /**
  256. * Returns true if the current local participant is a moderator in the
  257. * conference.
  258. *
  259. * @param {Object|Function} stateful - Object or function that can be resolved
  260. * to the Redux state.
  261. * @param {?boolean} ignoreToken - When true we ignore the token check.
  262. * @returns {boolean}
  263. */
  264. export function isLocalParticipantModerator(
  265. stateful: Object | Function,
  266. ignoreToken: ?boolean = false) {
  267. const state = toState(stateful);
  268. const localParticipant = getLocalParticipant(state);
  269. if (!localParticipant) {
  270. return false;
  271. }
  272. return (
  273. localParticipant.role === PARTICIPANT_ROLE.MODERATOR
  274. && (ignoreToken
  275. || !state['features/base/config'].enableUserRolesBasedOnToken
  276. || !state['features/base/jwt'].isGuest));
  277. }
  278. /**
  279. * Returns true if the video of the participant should be rendered.
  280. * NOTE: This is currently only used on mobile.
  281. *
  282. * @param {Object|Function} stateful - Object or function that can be resolved
  283. * to the Redux state.
  284. * @param {string} id - The ID of the participant.
  285. * @returns {boolean}
  286. */
  287. export function shouldRenderParticipantVideo(stateful: Object | Function, id: string) {
  288. const state = toState(stateful);
  289. const participant = getParticipantById(state, id);
  290. if (!participant) {
  291. return false;
  292. }
  293. /* First check if we have an unmuted video track. */
  294. const videoTrack
  295. = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  296. if (!shouldRenderVideoTrack(videoTrack, /* waitForVideoStarted */ false)) {
  297. return false;
  298. }
  299. /* Then check if the participant connection is active. */
  300. const connectionStatus = participant.connectionStatus || JitsiParticipantConnectionStatus.ACTIVE;
  301. if (connectionStatus !== JitsiParticipantConnectionStatus.ACTIVE) {
  302. return false;
  303. }
  304. /* Then check if audio-only mode is not active. */
  305. const audioOnly = state['features/base/audio-only'].enabled;
  306. if (!audioOnly) {
  307. return true;
  308. }
  309. /* Last, check if the participant is sharing their screen and they are on stage. */
  310. const screenShares = state['features/video-layout'].screenShares || [];
  311. const largeVideoParticipantId = state['features/large-video'].participantId;
  312. const participantIsInLargeVideoWithScreen
  313. = participant.id === largeVideoParticipantId && screenShares.includes(participant.id);
  314. return participantIsInLargeVideoWithScreen;
  315. }
  316. /**
  317. * Resolves the first loadable avatar URL for a participant.
  318. *
  319. * @param {Object} participant - The participant to resolve avatars for.
  320. * @returns {?string}
  321. */
  322. async function _getFirstLoadableAvatarUrl(participant) {
  323. for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
  324. const url = AVATAR_CHECKER_FUNCTIONS[i](participant);
  325. if (url) {
  326. if (AVATAR_CHECKED_URLS.has(url)) {
  327. if (AVATAR_CHECKED_URLS.get(url)) {
  328. return url;
  329. }
  330. } else {
  331. try {
  332. const finalUrl = await preloadImage(url);
  333. AVATAR_CHECKED_URLS.set(finalUrl, true);
  334. return finalUrl;
  335. } catch (e) {
  336. AVATAR_CHECKED_URLS.set(url, false);
  337. }
  338. }
  339. }
  340. }
  341. return undefined;
  342. }