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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // @flow
  2. import { getGravatarURL } from 'js-utils/avatar';
  3. import { toState } from '../redux';
  4. import { JitsiParticipantConnectionStatus } from '../lib-jitsi-meet';
  5. import { MEDIA_TYPE, shouldRenderVideoTrack } from '../media';
  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 true if all of the meeting participants are moderators.
  217. *
  218. * @param {Object|Function} stateful -Object or function that can be resolved
  219. * to the Redux state.
  220. * @returns {boolean}
  221. */
  222. export function isEveryoneModerator(stateful: Object | Function) {
  223. const participants = _getAllParticipants(stateful);
  224. for (const participant of participants) {
  225. if (participant.role !== PARTICIPANT_ROLE.MODERATOR) {
  226. return false;
  227. }
  228. }
  229. return true;
  230. }
  231. /**
  232. * Checks a URL string and returns true if it's an icon url.
  233. *
  234. * @param {string?} url - The URL string to check.
  235. * @returns {boolean}
  236. */
  237. export function isIconUrl(url: ?string) {
  238. return Boolean(url && url.match(/icon:\/\/(.+)/i));
  239. }
  240. /**
  241. * Returns true if the current local participant is a moderator in the
  242. * conference.
  243. *
  244. * @param {Object|Function} stateful - Object or function that can be resolved
  245. * to the Redux state.
  246. * @param {?boolean} ignoreToken - When true we ignore the token check.
  247. * @returns {boolean}
  248. */
  249. export function isLocalParticipantModerator(
  250. stateful: Object | Function,
  251. ignoreToken: ?boolean = false) {
  252. const state = toState(stateful);
  253. const localParticipant = getLocalParticipant(state);
  254. if (!localParticipant) {
  255. return false;
  256. }
  257. return (
  258. localParticipant.role === PARTICIPANT_ROLE.MODERATOR
  259. && (ignoreToken
  260. || !state['features/base/config'].enableUserRolesBasedOnToken
  261. || !state['features/base/jwt'].isGuest));
  262. }
  263. /**
  264. * Returns true if the video of the participant should be rendered.
  265. *
  266. * @param {Object|Function} stateful - Object or function that can be resolved
  267. * to the Redux state.
  268. * @param {string} id - The ID of the participant.
  269. * @returns {boolean}
  270. */
  271. export function shouldRenderParticipantVideo(
  272. stateful: Object | Function, id: string) {
  273. const state = toState(stateful);
  274. const participant = getParticipantById(state, id);
  275. if (!participant) {
  276. return false;
  277. }
  278. const audioOnly = state['features/base/audio-only'].enabled;
  279. const connectionStatus = participant.connectionStatus
  280. || JitsiParticipantConnectionStatus.ACTIVE;
  281. const videoTrack = getTrackByMediaTypeAndParticipant(
  282. state['features/base/tracks'],
  283. MEDIA_TYPE.VIDEO,
  284. id);
  285. // Is the video to be rendered?
  286. // FIXME It's currently impossible to have true as the value of
  287. // waitForVideoStarted because videoTrack's state videoStarted will be
  288. // updated only after videoTrack is rendered.
  289. // XXX Note that, unlike on web, we don't render video when the
  290. // connection status is interrupted, this is because the renderer
  291. // doesn't retain the last frame forever, so we would end up with a
  292. // black screen.
  293. const waitForVideoStarted = false;
  294. return !audioOnly
  295. && (connectionStatus
  296. === JitsiParticipantConnectionStatus.ACTIVE)
  297. && shouldRenderVideoTrack(videoTrack, waitForVideoStarted);
  298. }
  299. /**
  300. * Resolves the first loadable avatar URL for a participant.
  301. *
  302. * @param {Object} participant - The participant to resolve avatars for.
  303. * @returns {?string}
  304. */
  305. async function _getFirstLoadableAvatarUrl(participant) {
  306. for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
  307. const url = AVATAR_CHECKER_FUNCTIONS[i](participant);
  308. if (url) {
  309. if (AVATAR_CHECKED_URLS.has(url)) {
  310. if (AVATAR_CHECKED_URLS.get(url)) {
  311. return url;
  312. }
  313. } else {
  314. try {
  315. const finalUrl = await preloadImage(url);
  316. AVATAR_CHECKED_URLS.set(finalUrl, true);
  317. return finalUrl;
  318. } catch (e) {
  319. AVATAR_CHECKED_URLS.set(url, false);
  320. }
  321. }
  322. }
  323. }
  324. return undefined;
  325. }