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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. MAX_DISPLAY_NAME_LENGTH,
  10. PARTICIPANT_ROLE
  11. } from './constants';
  12. import { preloadImage } from './preloadImage';
  13. declare var config: Object;
  14. declare var interfaceConfig: Object;
  15. /**
  16. * Temp structures for avatar urls to be checked/preloaded.
  17. */
  18. const AVATAR_QUEUE = [];
  19. const AVATAR_CHECKED_URLS = new Map();
  20. /* eslint-disable arrow-body-style */
  21. const AVATAR_CHECKER_FUNCTIONS = [
  22. participant => {
  23. return participant && participant.avatarURL ? participant.avatarURL : null;
  24. },
  25. participant => {
  26. return participant && participant.email ? getGravatarURL(participant.email) : null;
  27. }
  28. ];
  29. /* eslint-enable arrow-body-style */
  30. /**
  31. * Resolves the first loadable avatar URL for a participant.
  32. *
  33. * @param {Object} participant - The participant to resolve avatars for.
  34. * @returns {Promise}
  35. */
  36. export function getFirstLoadableAvatarUrl(participant: Object) {
  37. const deferred = createDeferred();
  38. const fullPromise = deferred.promise
  39. .then(() => _getFirstLoadableAvatarUrl(participant))
  40. .then(src => {
  41. if (AVATAR_QUEUE.length) {
  42. const next = AVATAR_QUEUE.shift();
  43. next.resolve();
  44. }
  45. return src;
  46. });
  47. if (AVATAR_QUEUE.length) {
  48. AVATAR_QUEUE.push(deferred);
  49. } else {
  50. deferred.resolve();
  51. }
  52. return fullPromise;
  53. }
  54. /**
  55. * Returns local participant from Redux state.
  56. *
  57. * @param {(Function|Object|Participant[])} stateful - The redux state
  58. * features/base/participants, the (whole) redux state, or redux's
  59. * {@code getState} function to be used to retrieve the state
  60. * features/base/participants.
  61. * @returns {(Participant|undefined)}
  62. */
  63. export function getLocalParticipant(stateful: Object | Function) {
  64. const participants = _getAllParticipants(stateful);
  65. return participants.find(p => p.local);
  66. }
  67. /**
  68. * Normalizes a display name so then no invalid values (padding, length...etc)
  69. * can be set.
  70. *
  71. * @param {string} name - The display name to set.
  72. * @returns {string}
  73. */
  74. export function getNormalizedDisplayName(name: string) {
  75. if (!name || !name.trim()) {
  76. return undefined;
  77. }
  78. return name.trim().substring(0, MAX_DISPLAY_NAME_LENGTH);
  79. }
  80. /**
  81. * Returns participant by ID from Redux state.
  82. *
  83. * @param {(Function|Object|Participant[])} stateful - The redux state
  84. * features/base/participants, the (whole) redux state, or redux's
  85. * {@code getState} function to be used to retrieve the state
  86. * features/base/participants.
  87. * @param {string} id - The ID of the participant to retrieve.
  88. * @private
  89. * @returns {(Participant|undefined)}
  90. */
  91. export function getParticipantById(
  92. stateful: Object | Function, id: string): ?Object {
  93. const participants = _getAllParticipants(stateful);
  94. return participants.find(p => p.id === id);
  95. }
  96. /**
  97. * Returns a count of the known participants in the passed in redux state,
  98. * excluding any fake participants.
  99. *
  100. * @param {(Function|Object|Participant[])} stateful - The redux state
  101. * features/base/participants, the (whole) redux state, or redux's
  102. * {@code getState} function to be used to retrieve the state
  103. * features/base/participants.
  104. * @returns {number}
  105. */
  106. export function getParticipantCount(stateful: Object | Function) {
  107. return getParticipants(stateful).length;
  108. }
  109. /**
  110. * Returns a count of the known participants in the passed in redux state,
  111. * including fake participants.
  112. *
  113. * @param {(Function|Object|Participant[])} stateful - The redux state
  114. * features/base/participants, the (whole) redux state, or redux's
  115. * {@code getState} function to be used to retrieve the state
  116. * features/base/participants.
  117. * @returns {number}
  118. */
  119. export function getParticipantCountWithFake(stateful: Object | Function) {
  120. return _getAllParticipants(stateful).length;
  121. }
  122. /**
  123. * Returns participant's display name.
  124. *
  125. * FIXME: Remove the hardcoded strings once interfaceConfig is stored in redux
  126. * and merge with a similarly named method in {@code conference.js}.
  127. *
  128. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  129. * {@code getState} function to be used to retrieve the state.
  130. * @param {string} id - The ID of the participant's display name to retrieve.
  131. * @returns {string}
  132. */
  133. export function getParticipantDisplayName(
  134. stateful: Object | Function,
  135. id: string) {
  136. const participant = getParticipantById(stateful, id);
  137. if (participant) {
  138. if (participant.name) {
  139. return participant.name;
  140. }
  141. if (participant.local) {
  142. return typeof interfaceConfig === 'object'
  143. ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME
  144. : 'me';
  145. }
  146. }
  147. return typeof interfaceConfig === 'object'
  148. ? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME
  149. : 'Fellow Jitster';
  150. }
  151. /**
  152. * Returns the presence status of a participant associated with the passed id.
  153. *
  154. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  155. * {@code getState} function to be used to retrieve the state.
  156. * @param {string} id - The id of the participant.
  157. * @returns {string} - The presence status.
  158. */
  159. export function getParticipantPresenceStatus(
  160. stateful: Object | Function, id: string) {
  161. if (!id) {
  162. return undefined;
  163. }
  164. const participantById = getParticipantById(stateful, id);
  165. if (!participantById) {
  166. return undefined;
  167. }
  168. return participantById.presence;
  169. }
  170. /**
  171. * Selectors for getting all known participants with fake participants filtered
  172. * out.
  173. *
  174. * @param {(Function|Object|Participant[])} stateful - The redux state
  175. * features/base/participants, the (whole) redux state, or redux's
  176. * {@code getState} function to be used to retrieve the state
  177. * features/base/participants.
  178. * @returns {Participant[]}
  179. */
  180. export function getParticipants(stateful: Object | Function) {
  181. return _getAllParticipants(stateful).filter(p => !p.isFakeParticipant);
  182. }
  183. /**
  184. * Returns the participant which has its pinned state set to truthy.
  185. *
  186. * @param {(Function|Object|Participant[])} stateful - The redux state
  187. * features/base/participants, the (whole) redux state, or redux's
  188. * {@code getState} function to be used to retrieve the state
  189. * features/base/participants.
  190. * @returns {(Participant|undefined)}
  191. */
  192. export function getPinnedParticipant(stateful: Object | Function) {
  193. return _getAllParticipants(stateful).find(p => p.pinned);
  194. }
  195. /**
  196. * Returns array of participants from Redux state.
  197. *
  198. * @param {(Function|Object|Participant[])} stateful - The redux state
  199. * features/base/participants, the (whole) redux state, or redux's
  200. * {@code getState} function to be used to retrieve the state
  201. * features/base/participants.
  202. * @private
  203. * @returns {Participant[]}
  204. */
  205. function _getAllParticipants(stateful) {
  206. return (
  207. Array.isArray(stateful)
  208. ? stateful
  209. : toState(stateful)['features/base/participants'] || []);
  210. }
  211. /**
  212. * Returns true if all of the meeting participants are moderators.
  213. *
  214. * @param {Object|Function} stateful -Object or function that can be resolved
  215. * to the Redux state.
  216. * @returns {boolean}
  217. */
  218. export function isEveryoneModerator(stateful: Object | Function) {
  219. const participants = _getAllParticipants(stateful);
  220. for (const participant of participants) {
  221. if (participant.role !== PARTICIPANT_ROLE.MODERATOR) {
  222. return false;
  223. }
  224. }
  225. return true;
  226. }
  227. /**
  228. * Returns true if the current local participant is a moderator in the
  229. * conference.
  230. *
  231. * @param {Object|Function} stateful - Object or function that can be resolved
  232. * to the Redux state.
  233. * @param {?boolean} ignoreToken - When true we ignore the token check.
  234. * @returns {boolean}
  235. */
  236. export function isLocalParticipantModerator(
  237. stateful: Object | Function,
  238. ignoreToken: ?boolean = false) {
  239. const state = toState(stateful);
  240. const localParticipant = getLocalParticipant(state);
  241. if (!localParticipant) {
  242. return false;
  243. }
  244. return (
  245. localParticipant.role === PARTICIPANT_ROLE.MODERATOR
  246. && (ignoreToken
  247. || !state['features/base/config'].enableUserRolesBasedOnToken
  248. || !state['features/base/jwt'].isGuest));
  249. }
  250. /**
  251. * Returns true if the video of the participant should be rendered.
  252. *
  253. * @param {Object|Function} stateful - Object or function that can be resolved
  254. * to the Redux state.
  255. * @param {string} id - The ID of the participant.
  256. * @returns {boolean}
  257. */
  258. export function shouldRenderParticipantVideo(
  259. stateful: Object | Function, id: string) {
  260. const state = toState(stateful);
  261. const participant = getParticipantById(state, id);
  262. if (!participant) {
  263. return false;
  264. }
  265. const audioOnly = state['features/base/conference'].audioOnly;
  266. const connectionStatus = participant.connectionStatus
  267. || JitsiParticipantConnectionStatus.ACTIVE;
  268. const videoTrack = getTrackByMediaTypeAndParticipant(
  269. state['features/base/tracks'],
  270. MEDIA_TYPE.VIDEO,
  271. id);
  272. // Is the video to be rendered?
  273. // FIXME It's currently impossible to have true as the value of
  274. // waitForVideoStarted because videoTrack's state videoStarted will be
  275. // updated only after videoTrack is rendered.
  276. // XXX Note that, unlike on web, we don't render video when the
  277. // connection status is interrupted, this is because the renderer
  278. // doesn't retain the last frame forever, so we would end up with a
  279. // black screen.
  280. const waitForVideoStarted = false;
  281. return !audioOnly
  282. && (connectionStatus
  283. === JitsiParticipantConnectionStatus.ACTIVE)
  284. && shouldRenderVideoTrack(videoTrack, waitForVideoStarted);
  285. }
  286. /**
  287. * Resolves the first loadable avatar URL for a participant.
  288. *
  289. * @param {Object} participant - The participant to resolve avatars for.
  290. * @returns {?string}
  291. */
  292. async function _getFirstLoadableAvatarUrl(participant) {
  293. for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
  294. const url = AVATAR_CHECKER_FUNCTIONS[i](participant);
  295. if (url) {
  296. if (AVATAR_CHECKED_URLS.has(url)) {
  297. if (AVATAR_CHECKED_URLS.get(url)) {
  298. return url;
  299. }
  300. } else {
  301. try {
  302. const finalUrl = await preloadImage(url);
  303. AVATAR_CHECKED_URLS.set(finalUrl, true);
  304. return finalUrl;
  305. } catch (e) {
  306. AVATAR_CHECKED_URLS.set(url, false);
  307. }
  308. }
  309. }
  310. }
  311. return undefined;
  312. }