您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 config: Object;
  16. declare var interfaceConfig: Object;
  17. /**
  18. * Temp structures for avatar urls to be checked/preloaded.
  19. */
  20. const AVATAR_QUEUE = [];
  21. const AVATAR_CHECKED_URLS = new Map();
  22. /* eslint-disable arrow-body-style, no-unused-vars */
  23. const AVATAR_CHECKER_FUNCTIONS = [
  24. (participant, _) => {
  25. return participant && participant.isJigasi ? JIGASI_PARTICIPANT_ICON : null;
  26. },
  27. (participant, _) => {
  28. return participant && participant.avatarURL ? participant.avatarURL : null;
  29. },
  30. (participant, store) => {
  31. if (participant && participant.email) {
  32. // TODO: remove once libravatar has deployed their new scaled up infra. -saghul
  33. const gravatarBaseURL
  34. = store.getState()['features/base/config'].gravatarBaseURL ?? 'https://www.gravatar.com/avatar/';
  35. return getGravatarURL(participant.email, gravatarBaseURL);
  36. }
  37. return null;
  38. }
  39. ];
  40. /* eslint-enable arrow-body-style, no-unused-vars */
  41. /**
  42. * Resolves the first loadable avatar URL for a participant.
  43. *
  44. * @param {Object} participant - The participant to resolve avatars for.
  45. * @param {Store} store - Redux store.
  46. * @returns {Promise}
  47. */
  48. export function getFirstLoadableAvatarUrl(participant: Object, store: Store<any, any>) {
  49. const deferred = createDeferred();
  50. const fullPromise = deferred.promise
  51. .then(() => _getFirstLoadableAvatarUrl(participant, store))
  52. .then(src => {
  53. if (AVATAR_QUEUE.length) {
  54. const next = AVATAR_QUEUE.shift();
  55. next.resolve();
  56. }
  57. return src;
  58. });
  59. if (AVATAR_QUEUE.length) {
  60. AVATAR_QUEUE.push(deferred);
  61. } else {
  62. deferred.resolve();
  63. }
  64. return fullPromise;
  65. }
  66. /**
  67. * Returns local participant from Redux state.
  68. *
  69. * @param {(Function|Object|Participant[])} stateful - The redux state
  70. * features/base/participants, the (whole) redux state, or redux's
  71. * {@code getState} function to be used to retrieve the state
  72. * features/base/participants.
  73. * @returns {(Participant|undefined)}
  74. */
  75. export function getLocalParticipant(stateful: Object | Function) {
  76. const participants = _getAllParticipants(stateful);
  77. return participants.find(p => p.local);
  78. }
  79. /**
  80. * Normalizes a display name so then no invalid values (padding, length...etc)
  81. * can be set.
  82. *
  83. * @param {string} name - The display name to set.
  84. * @returns {string}
  85. */
  86. export function getNormalizedDisplayName(name: string) {
  87. if (!name || !name.trim()) {
  88. return undefined;
  89. }
  90. return name.trim().substring(0, MAX_DISPLAY_NAME_LENGTH);
  91. }
  92. /**
  93. * Returns participant by ID from Redux state.
  94. *
  95. * @param {(Function|Object|Participant[])} stateful - The redux state
  96. * features/base/participants, the (whole) redux state, or redux's
  97. * {@code getState} function to be used to retrieve the state
  98. * features/base/participants.
  99. * @param {string} id - The ID of the participant to retrieve.
  100. * @private
  101. * @returns {(Participant|undefined)}
  102. */
  103. export function getParticipantById(
  104. stateful: Object | Function, id: string): ?Object {
  105. const participants = _getAllParticipants(stateful);
  106. return participants.find(p => p.id === id);
  107. }
  108. /**
  109. * Returns a count of the known participants in the passed in redux state,
  110. * excluding any fake participants.
  111. *
  112. * @param {(Function|Object|Participant[])} stateful - The redux state
  113. * features/base/participants, the (whole) redux state, or redux's
  114. * {@code getState} function to be used to retrieve the state
  115. * features/base/participants.
  116. * @returns {number}
  117. */
  118. export function getParticipantCount(stateful: Object | Function) {
  119. return getParticipants(stateful).length;
  120. }
  121. /**
  122. * Returns a count of the known participants in the passed in redux state,
  123. * including fake participants.
  124. *
  125. * @param {(Function|Object|Participant[])} stateful - The redux state
  126. * features/base/participants, the (whole) redux state, or redux's
  127. * {@code getState} function to be used to retrieve the state
  128. * features/base/participants.
  129. * @returns {number}
  130. */
  131. export function getParticipantCountWithFake(stateful: Object | Function) {
  132. return _getAllParticipants(stateful).length;
  133. }
  134. /**
  135. * Returns participant's display name.
  136. *
  137. * FIXME: Remove the hardcoded strings once interfaceConfig is stored in redux
  138. * and merge with a similarly named method in {@code conference.js}.
  139. *
  140. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  141. * {@code getState} function to be used to retrieve the state.
  142. * @param {string} id - The ID of the participant's display name to retrieve.
  143. * @returns {string}
  144. */
  145. export function getParticipantDisplayName(
  146. stateful: Object | Function,
  147. id: string) {
  148. const participant = getParticipantById(stateful, id);
  149. if (participant) {
  150. if (participant.name) {
  151. return participant.name;
  152. }
  153. if (participant.local) {
  154. return typeof interfaceConfig === 'object'
  155. ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME
  156. : 'me';
  157. }
  158. }
  159. return typeof interfaceConfig === 'object'
  160. ? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME
  161. : 'Fellow Jitster';
  162. }
  163. /**
  164. * Returns the presence status of a participant associated with the passed id.
  165. *
  166. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  167. * {@code getState} function to be used to retrieve the state.
  168. * @param {string} id - The id of the participant.
  169. * @returns {string} - The presence status.
  170. */
  171. export function getParticipantPresenceStatus(
  172. stateful: Object | Function, id: string) {
  173. if (!id) {
  174. return undefined;
  175. }
  176. const participantById = getParticipantById(stateful, id);
  177. if (!participantById) {
  178. return undefined;
  179. }
  180. return participantById.presence;
  181. }
  182. /**
  183. * Selectors for getting all known participants with fake participants filtered
  184. * out.
  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[]}
  191. */
  192. export function getParticipants(stateful: Object | Function) {
  193. return _getAllParticipants(stateful).filter(p => !p.isFakeParticipant);
  194. }
  195. /**
  196. * Returns the participant which has its pinned state set to truthy.
  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. * @returns {(Participant|undefined)}
  203. */
  204. export function getPinnedParticipant(stateful: Object | Function) {
  205. return _getAllParticipants(stateful).find(p => p.pinned);
  206. }
  207. /**
  208. * Returns array of participants from Redux state.
  209. *
  210. * @param {(Function|Object|Participant[])} stateful - The redux state
  211. * features/base/participants, the (whole) redux state, or redux's
  212. * {@code getState} function to be used to retrieve the state
  213. * features/base/participants.
  214. * @private
  215. * @returns {Participant[]}
  216. */
  217. function _getAllParticipants(stateful) {
  218. return (
  219. Array.isArray(stateful)
  220. ? stateful
  221. : toState(stateful)['features/base/participants'] || []);
  222. }
  223. /**
  224. * Returns the youtube fake participant.
  225. * At the moment it is considered the youtube participant the only fake participant in the list.
  226. *
  227. * @param {(Function|Object|Participant[])} stateful - The redux state
  228. * features/base/participants, the (whole) redux state, or redux's
  229. * {@code getState} function to be used to retrieve the state
  230. * features/base/participants.
  231. * @private
  232. * @returns {Participant}
  233. */
  234. export function getYoutubeParticipant(stateful: Object | Function) {
  235. const participants = _getAllParticipants(stateful);
  236. return participants.filter(p => p.isFakeParticipant)[0];
  237. }
  238. /**
  239. * Returns true if the participant is a moderator.
  240. *
  241. * @param {string} participant - Participant object.
  242. * @returns {boolean}
  243. */
  244. export function isParticipantModerator(participant: Object) {
  245. return participant?.role === PARTICIPANT_ROLE.MODERATOR;
  246. }
  247. /**
  248. * Returns true if all of the meeting participants are moderators.
  249. *
  250. * @param {Object|Function} stateful -Object or function that can be resolved
  251. * to the Redux state.
  252. * @returns {boolean}
  253. */
  254. export function isEveryoneModerator(stateful: Object | Function) {
  255. const participants = _getAllParticipants(stateful);
  256. return participants.every(isParticipantModerator);
  257. }
  258. /**
  259. * Checks a value and returns true if it's a preloaded icon object.
  260. *
  261. * @param {?string | ?Object} icon - The icon to check.
  262. * @returns {boolean}
  263. */
  264. export function isIconUrl(icon: ?string | ?Object) {
  265. return Boolean(icon) && typeof icon === 'object';
  266. }
  267. /**
  268. * Returns true if the current local participant is a moderator in the
  269. * conference.
  270. *
  271. * @param {Object|Function} stateful - Object or function that can be resolved
  272. * to the Redux state.
  273. * @returns {boolean}
  274. */
  275. export function isLocalParticipantModerator(stateful: Object | Function) {
  276. const state = toState(stateful);
  277. const localParticipant = getLocalParticipant(state);
  278. if (!localParticipant) {
  279. return false;
  280. }
  281. return localParticipant.role === PARTICIPANT_ROLE.MODERATOR;
  282. }
  283. /**
  284. * Returns true if the video of the participant should be rendered.
  285. * NOTE: This is currently only used on mobile.
  286. *
  287. * @param {Object|Function} stateful - Object or function that can be resolved
  288. * to the Redux state.
  289. * @param {string} id - The ID of the participant.
  290. * @returns {boolean}
  291. */
  292. export function shouldRenderParticipantVideo(stateful: Object | Function, id: string) {
  293. const state = toState(stateful);
  294. const participant = getParticipantById(state, id);
  295. if (!participant) {
  296. return false;
  297. }
  298. /* First check if we have an unmuted video track. */
  299. const videoTrack
  300. = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  301. if (!shouldRenderVideoTrack(videoTrack, /* waitForVideoStarted */ false)) {
  302. return false;
  303. }
  304. /* Then check if the participant connection is active. */
  305. const connectionStatus = participant.connectionStatus || JitsiParticipantConnectionStatus.ACTIVE;
  306. if (connectionStatus !== JitsiParticipantConnectionStatus.ACTIVE) {
  307. return false;
  308. }
  309. /* Then check if audio-only mode is not active. */
  310. const audioOnly = state['features/base/audio-only'].enabled;
  311. if (!audioOnly) {
  312. return true;
  313. }
  314. /* Last, check if the participant is sharing their screen and they are on stage. */
  315. const screenShares = state['features/video-layout'].screenShares || [];
  316. const largeVideoParticipantId = state['features/large-video'].participantId;
  317. const participantIsInLargeVideoWithScreen
  318. = participant.id === largeVideoParticipantId && screenShares.includes(participant.id);
  319. return participantIsInLargeVideoWithScreen;
  320. }
  321. /**
  322. * Resolves the first loadable avatar URL for a participant.
  323. *
  324. * @param {Object} participant - The participant to resolve avatars for.
  325. * @param {Store} store - Redux store.
  326. * @returns {?string}
  327. */
  328. async function _getFirstLoadableAvatarUrl(participant, store) {
  329. for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
  330. const url = AVATAR_CHECKER_FUNCTIONS[i](participant, store);
  331. if (url) {
  332. if (AVATAR_CHECKED_URLS.has(url)) {
  333. if (AVATAR_CHECKED_URLS.get(url)) {
  334. return url;
  335. }
  336. } else {
  337. try {
  338. const finalUrl = await preloadImage(url);
  339. AVATAR_CHECKED_URLS.set(finalUrl, true);
  340. return finalUrl;
  341. } catch (e) {
  342. AVATAR_CHECKED_URLS.set(url, false);
  343. }
  344. }
  345. }
  346. }
  347. return undefined;
  348. }