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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // @flow
  2. import {
  3. getAvatarURL as _getAvatarURL,
  4. getGravatarURL
  5. } from 'js-utils/avatar';
  6. import { toState } from '../redux';
  7. import { JitsiParticipantConnectionStatus } from '../lib-jitsi-meet';
  8. import { MEDIA_TYPE, shouldRenderVideoTrack } from '../media';
  9. import { getTrackByMediaTypeAndParticipant } from '../tracks';
  10. import { createDeferred } from '../util';
  11. import {
  12. DEFAULT_AVATAR_RELATIVE_PATH,
  13. LOCAL_PARTICIPANT_DEFAULT_ID,
  14. MAX_DISPLAY_NAME_LENGTH,
  15. PARTICIPANT_ROLE
  16. } from './constants';
  17. import { preloadImage } from './preloadImage';
  18. declare var config: Object;
  19. declare var interfaceConfig: Object;
  20. /**
  21. * Temp structures for avatar urls to be checked/preloaded.
  22. */
  23. const AVATAR_QUEUE = [];
  24. const AVATAR_CHECKED_URLS = new Map();
  25. /* eslint-disable arrow-body-style */
  26. const AVATAR_CHECKER_FUNCTIONS = [
  27. participant => {
  28. return participant && participant.avatarURL ? participant.avatarURL : null;
  29. },
  30. participant => {
  31. return participant && participant.email ? getGravatarURL(participant.email) : null;
  32. }
  33. ];
  34. /* eslint-enable arrow-body-style */
  35. /**
  36. * Returns the URL of the image for the avatar of a specific participant.
  37. *
  38. * @param {Participant} [participant] - The participant to return the avatar URL
  39. * of.
  40. * @param {string} [participant.avatarID] - Participant's avatar ID.
  41. * @param {string} [participant.avatarURL] - Participant's avatar URL.
  42. * @param {string} [participant.email] - Participant's e-mail address.
  43. * @param {string} [participant.id] - Participant's ID.
  44. * @public
  45. * @returns {string} The URL of the image for the avatar of the specified
  46. * participant.
  47. */
  48. export function getAvatarURL({ avatarID, avatarURL, email, id }: {
  49. avatarID: string,
  50. avatarURL: string,
  51. email: string,
  52. id: string
  53. }) {
  54. // If disableThirdPartyRequests disables third-party avatar services, we are
  55. // restricted to a stock image of ours.
  56. if (typeof config === 'object' && config.disableThirdPartyRequests) {
  57. return DEFAULT_AVATAR_RELATIVE_PATH;
  58. }
  59. // If an avatarURL is specified, then obviously there's nothing to generate.
  60. if (avatarURL) {
  61. return avatarURL;
  62. }
  63. // The deployment is allowed to choose the avatar service which is to
  64. // generate the random avatars.
  65. const avatarService
  66. = typeof interfaceConfig === 'object'
  67. && interfaceConfig.RANDOM_AVATAR_URL_PREFIX
  68. ? {
  69. urlPrefix: interfaceConfig.RANDOM_AVATAR_URL_PREFIX,
  70. urlSuffix: interfaceConfig.RANDOM_AVATAR_URL_SUFFIX }
  71. : undefined;
  72. // eslint-disable-next-line object-property-newline
  73. return _getAvatarURL({ avatarID, email, id }, avatarService);
  74. }
  75. /**
  76. * Returns the avatarURL for the participant associated with the passed in
  77. * participant ID.
  78. *
  79. * @param {(Function|Object|Participant[])} stateful - The redux state
  80. * features/base/participants, the (whole) redux state, or redux's
  81. * {@code getState} function to be used to retrieve the state
  82. * features/base/participants.
  83. * @param {string} id - The ID of the participant to retrieve.
  84. * @param {boolean} isLocal - An optional parameter indicating whether or not
  85. * the partcipant id is for the local user. If true, a different logic flow is
  86. * used find the local user, ignoring the id value as it can change through the
  87. * beginning and end of a call.
  88. * @returns {(string|undefined)}
  89. */
  90. export function getAvatarURLByParticipantId(
  91. stateful: Object | Function,
  92. id: string = LOCAL_PARTICIPANT_DEFAULT_ID) {
  93. const participant = getParticipantById(stateful, id);
  94. return participant && getAvatarURL(participant);
  95. }
  96. /**
  97. * Resolves the first loadable avatar URL for a participant.
  98. *
  99. * @param {Object} participant - The participant to resolve avatars for.
  100. * @returns {Promise}
  101. */
  102. export function getFirstLoadableAvatarUrl(participant: Object) {
  103. const deferred = createDeferred();
  104. const fullPromise = deferred.promise
  105. .then(() => _getFirstLoadableAvatarUrl(participant))
  106. .then(src => {
  107. if (AVATAR_QUEUE.length) {
  108. const next = AVATAR_QUEUE.shift();
  109. next.resolve();
  110. }
  111. return src;
  112. });
  113. if (AVATAR_QUEUE.length) {
  114. AVATAR_QUEUE.push(deferred);
  115. } else {
  116. deferred.resolve();
  117. }
  118. return fullPromise;
  119. }
  120. /**
  121. * Returns local participant from Redux state.
  122. *
  123. * @param {(Function|Object|Participant[])} stateful - The redux state
  124. * features/base/participants, the (whole) redux state, or redux's
  125. * {@code getState} function to be used to retrieve the state
  126. * features/base/participants.
  127. * @returns {(Participant|undefined)}
  128. */
  129. export function getLocalParticipant(stateful: Object | Function) {
  130. const participants = _getAllParticipants(stateful);
  131. return participants.find(p => p.local);
  132. }
  133. /**
  134. * Normalizes a display name so then no invalid values (padding, length...etc)
  135. * can be set.
  136. *
  137. * @param {string} name - The display name to set.
  138. * @returns {string}
  139. */
  140. export function getNormalizedDisplayName(name: string) {
  141. if (!name || !name.trim()) {
  142. return undefined;
  143. }
  144. return name.trim().substring(0, MAX_DISPLAY_NAME_LENGTH);
  145. }
  146. /**
  147. * Returns participant by ID from Redux state.
  148. *
  149. * @param {(Function|Object|Participant[])} stateful - The redux state
  150. * features/base/participants, the (whole) redux state, or redux's
  151. * {@code getState} function to be used to retrieve the state
  152. * features/base/participants.
  153. * @param {string} id - The ID of the participant to retrieve.
  154. * @private
  155. * @returns {(Participant|undefined)}
  156. */
  157. export function getParticipantById(
  158. stateful: Object | Function, id: string): ?Object {
  159. const participants = _getAllParticipants(stateful);
  160. return participants.find(p => p.id === id);
  161. }
  162. /**
  163. * Returns a count of the known participants in the passed in redux state,
  164. * excluding any fake participants.
  165. *
  166. * @param {(Function|Object|Participant[])} stateful - The redux state
  167. * features/base/participants, the (whole) redux state, or redux's
  168. * {@code getState} function to be used to retrieve the state
  169. * features/base/participants.
  170. * @returns {number}
  171. */
  172. export function getParticipantCount(stateful: Object | Function) {
  173. return getParticipants(stateful).length;
  174. }
  175. /**
  176. * Returns a count of the known participants in the passed in redux state,
  177. * including fake participants.
  178. *
  179. * @param {(Function|Object|Participant[])} stateful - The redux state
  180. * features/base/participants, the (whole) redux state, or redux's
  181. * {@code getState} function to be used to retrieve the state
  182. * features/base/participants.
  183. * @returns {number}
  184. */
  185. export function getParticipantCountWithFake(stateful: Object | Function) {
  186. return _getAllParticipants(stateful).length;
  187. }
  188. /**
  189. * Returns participant's display name.
  190. *
  191. * FIXME: Remove the hardcoded strings once interfaceConfig is stored in redux
  192. * and merge with a similarly named method in {@code conference.js}.
  193. *
  194. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  195. * {@code getState} function to be used to retrieve the state.
  196. * @param {string} id - The ID of the participant's display name to retrieve.
  197. * @returns {string}
  198. */
  199. export function getParticipantDisplayName(
  200. stateful: Object | Function,
  201. id: string) {
  202. const participant = getParticipantById(stateful, id);
  203. if (participant) {
  204. if (participant.name) {
  205. return participant.name;
  206. }
  207. if (participant.local) {
  208. return typeof interfaceConfig === 'object'
  209. ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME
  210. : 'me';
  211. }
  212. }
  213. return typeof interfaceConfig === 'object'
  214. ? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME
  215. : 'Fellow Jitster';
  216. }
  217. /**
  218. * Returns the presence status of a participant associated with the passed id.
  219. *
  220. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  221. * {@code getState} function to be used to retrieve the state.
  222. * @param {string} id - The id of the participant.
  223. * @returns {string} - The presence status.
  224. */
  225. export function getParticipantPresenceStatus(
  226. stateful: Object | Function, id: string) {
  227. if (!id) {
  228. return undefined;
  229. }
  230. const participantById = getParticipantById(stateful, id);
  231. if (!participantById) {
  232. return undefined;
  233. }
  234. return participantById.presence;
  235. }
  236. /**
  237. * Selectors for getting all known participants with fake participants filtered
  238. * out.
  239. *
  240. * @param {(Function|Object|Participant[])} stateful - The redux state
  241. * features/base/participants, the (whole) redux state, or redux's
  242. * {@code getState} function to be used to retrieve the state
  243. * features/base/participants.
  244. * @returns {Participant[]}
  245. */
  246. export function getParticipants(stateful: Object | Function) {
  247. return _getAllParticipants(stateful).filter(p => !p.isFakeParticipant);
  248. }
  249. /**
  250. * Returns the participant which has its pinned state set to truthy.
  251. *
  252. * @param {(Function|Object|Participant[])} stateful - The redux state
  253. * features/base/participants, the (whole) redux state, or redux's
  254. * {@code getState} function to be used to retrieve the state
  255. * features/base/participants.
  256. * @returns {(Participant|undefined)}
  257. */
  258. export function getPinnedParticipant(stateful: Object | Function) {
  259. return _getAllParticipants(stateful).find(p => p.pinned);
  260. }
  261. /**
  262. * Returns array of participants from Redux state.
  263. *
  264. * @param {(Function|Object|Participant[])} stateful - The redux state
  265. * features/base/participants, the (whole) redux state, or redux's
  266. * {@code getState} function to be used to retrieve the state
  267. * features/base/participants.
  268. * @private
  269. * @returns {Participant[]}
  270. */
  271. function _getAllParticipants(stateful) {
  272. return (
  273. Array.isArray(stateful)
  274. ? stateful
  275. : toState(stateful)['features/base/participants'] || []);
  276. }
  277. /**
  278. * Returns true if all of the meeting participants are moderators.
  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 isEveryoneModerator(stateful: Object | Function) {
  285. const participants = _getAllParticipants(stateful);
  286. for (const participant of participants) {
  287. if (participant.role !== PARTICIPANT_ROLE.MODERATOR) {
  288. return false;
  289. }
  290. }
  291. return true;
  292. }
  293. /**
  294. * Returns true if the current local participant is a moderator in the
  295. * conference.
  296. *
  297. * @param {Object|Function} stateful - Object or function that can be resolved
  298. * to the Redux state.
  299. * @param {?boolean} ignoreToken - When true we ignore the token check.
  300. * @returns {boolean}
  301. */
  302. export function isLocalParticipantModerator(
  303. stateful: Object | Function,
  304. ignoreToken: ?boolean = false) {
  305. const state = toState(stateful);
  306. const localParticipant = getLocalParticipant(state);
  307. if (!localParticipant) {
  308. return false;
  309. }
  310. return (
  311. localParticipant.role === PARTICIPANT_ROLE.MODERATOR
  312. && (ignoreToken
  313. || !state['features/base/config'].enableUserRolesBasedOnToken
  314. || !state['features/base/jwt'].isGuest));
  315. }
  316. /**
  317. * Returns true if the video of the participant should be rendered.
  318. *
  319. * @param {Object|Function} stateful - Object or function that can be resolved
  320. * to the Redux state.
  321. * @param {string} id - The ID of the participant.
  322. * @returns {boolean}
  323. */
  324. export function shouldRenderParticipantVideo(
  325. stateful: Object | Function, id: string) {
  326. const state = toState(stateful);
  327. const participant = getParticipantById(state, id);
  328. if (!participant) {
  329. return false;
  330. }
  331. const audioOnly = state['features/base/conference'].audioOnly;
  332. const connectionStatus = participant.connectionStatus
  333. || JitsiParticipantConnectionStatus.ACTIVE;
  334. const videoTrack = getTrackByMediaTypeAndParticipant(
  335. state['features/base/tracks'],
  336. MEDIA_TYPE.VIDEO,
  337. id);
  338. // Is the video to be rendered?
  339. // FIXME It's currently impossible to have true as the value of
  340. // waitForVideoStarted because videoTrack's state videoStarted will be
  341. // updated only after videoTrack is rendered.
  342. // XXX Note that, unlike on web, we don't render video when the
  343. // connection status is interrupted, this is because the renderer
  344. // doesn't retain the last frame forever, so we would end up with a
  345. // black screen.
  346. const waitForVideoStarted = false;
  347. return !audioOnly
  348. && (connectionStatus
  349. === JitsiParticipantConnectionStatus.ACTIVE)
  350. && shouldRenderVideoTrack(videoTrack, waitForVideoStarted);
  351. }
  352. /**
  353. * Resolves the first loadable avatar URL for a participant.
  354. *
  355. * @param {Object} participant - The participant to resolve avatars for.
  356. * @returns {?string}
  357. */
  358. async function _getFirstLoadableAvatarUrl(participant) {
  359. for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
  360. const url = AVATAR_CHECKER_FUNCTIONS[i](participant);
  361. if (url) {
  362. if (AVATAR_CHECKED_URLS.has(url)) {
  363. if (AVATAR_CHECKED_URLS.get(url)) {
  364. return url;
  365. }
  366. } else {
  367. try {
  368. const finalUrl = await preloadImage(url);
  369. AVATAR_CHECKED_URLS.set(finalUrl, true);
  370. return finalUrl;
  371. } catch (e) {
  372. AVATAR_CHECKED_URLS.set(url, false);
  373. }
  374. }
  375. }
  376. }
  377. return undefined;
  378. }