jiti-meet/react/features/base/participants/functions.js

431 lines
13 KiB
JavaScript
Raw Normal View History

2017-10-13 19:31:05 +00:00
// @flow
2019-06-26 14:08:23 +00:00
import {
getAvatarURL as _getAvatarURL,
getGravatarURL
} from 'js-utils/avatar';
2017-09-01 21:25:48 +00:00
import { toState } from '../redux';
import { JitsiParticipantConnectionStatus } from '../lib-jitsi-meet';
import { MEDIA_TYPE, shouldRenderVideoTrack } from '../media';
import { getTrackByMediaTypeAndParticipant } from '../tracks';
2019-06-26 14:08:23 +00:00
import { createDeferred } from '../util';
import {
DEFAULT_AVATAR_RELATIVE_PATH,
LOCAL_PARTICIPANT_DEFAULT_ID,
2019-01-15 11:28:07 +00:00
MAX_DISPLAY_NAME_LENGTH,
PARTICIPANT_ROLE
} from './constants';
2019-06-26 14:08:23 +00:00
import { preloadImage } from './preloadImage';
2017-08-16 21:28:39 +00:00
2017-02-27 21:42:28 +00:00
declare var config: Object;
declare var interfaceConfig: Object;
2017-03-07 16:50:17 +00:00
2019-06-26 14:08:23 +00:00
/**
* Temp structures for avatar urls to be checked/preloaded.
*/
const AVATAR_QUEUE = [];
const AVATAR_CHECKED_URLS = new Map();
/* eslint-disable arrow-body-style */
const AVATAR_CHECKER_FUNCTIONS = [
participant => {
return participant && participant.avatarURL ? participant.avatarURL : null;
},
participant => {
return participant && participant.email ? getGravatarURL(participant.email) : null;
}
];
/* eslint-enable arrow-body-style */
2017-03-07 16:50:17 +00:00
/**
* Returns the URL of the image for the avatar of a specific participant.
*
* @param {Participant} [participant] - The participant to return the avatar URL
* of.
* @param {string} [participant.avatarID] - Participant's avatar ID.
* @param {string} [participant.avatarURL] - Participant's avatar URL.
* @param {string} [participant.email] - Participant's e-mail address.
* @param {string} [participant.id] - Participant's ID.
2017-09-01 21:25:48 +00:00
* @public
2017-03-07 16:50:17 +00:00
* @returns {string} The URL of the image for the avatar of the specified
* participant.
*/
export function getAvatarURL({ avatarID, avatarURL, email, id }: {
2017-09-01 21:25:48 +00:00
avatarID: string,
avatarURL: string,
email: string,
id: string
2017-09-01 21:25:48 +00:00
}) {
2017-03-07 16:50:17 +00:00
// If disableThirdPartyRequests disables third-party avatar services, we are
// restricted to a stock image of ours.
if (typeof config === 'object' && config.disableThirdPartyRequests) {
2017-08-16 21:28:39 +00:00
return DEFAULT_AVATAR_RELATIVE_PATH;
2017-03-07 16:50:17 +00:00
}
// If an avatarURL is specified, then obviously there's nothing to generate.
if (avatarURL) {
return avatarURL;
}
// The deployment is allowed to choose the avatar service which is to
// generate the random avatars.
const avatarService
= typeof interfaceConfig === 'object'
&& interfaceConfig.RANDOM_AVATAR_URL_PREFIX
? {
urlPrefix: interfaceConfig.RANDOM_AVATAR_URL_PREFIX,
urlSuffix: interfaceConfig.RANDOM_AVATAR_URL_SUFFIX }
: undefined;
2017-03-07 16:50:17 +00:00
// eslint-disable-next-line object-property-newline
return _getAvatarURL({ avatarID, email, id }, avatarService);
2017-03-07 16:50:17 +00:00
}
2017-02-27 21:42:28 +00:00
/**
* Returns the avatarURL for the participant associated with the passed in
* participant ID.
*
* @param {(Function|Object|Participant[])} stateful - The redux state
* features/base/participants, the (whole) redux state, or redux's
* {@code getState} function to be used to retrieve the state
* features/base/participants.
* @param {string} id - The ID of the participant to retrieve.
* @param {boolean} isLocal - An optional parameter indicating whether or not
* the partcipant id is for the local user. If true, a different logic flow is
* used find the local user, ignoring the id value as it can change through the
* beginning and end of a call.
* @returns {(string|undefined)}
*/
export function getAvatarURLByParticipantId(
stateful: Object | Function,
id: string = LOCAL_PARTICIPANT_DEFAULT_ID) {
const participant = getParticipantById(stateful, id);
return participant && getAvatarURL(participant);
}
2019-06-26 14:08:23 +00:00
/**
* Resolves the first loadable avatar URL for a participant.
*
* @param {Object} participant - The participant to resolve avatars for.
* @returns {Promise}
*/
export function getFirstLoadableAvatarUrl(participant: Object) {
const deferred = createDeferred();
const fullPromise = deferred.promise
.then(() => _getFirstLoadableAvatarUrl(participant))
.then(src => {
if (AVATAR_QUEUE.length) {
const next = AVATAR_QUEUE.shift();
next.resolve();
}
return src;
});
if (AVATAR_QUEUE.length) {
AVATAR_QUEUE.push(deferred);
} else {
deferred.resolve();
}
return fullPromise;
}
/**
* Returns local participant from Redux state.
*
2017-10-13 19:31:05 +00:00
* @param {(Function|Object|Participant[])} stateful - The redux state
* features/base/participants, the (whole) redux state, or redux's
2017-10-13 19:31:05 +00:00
* {@code getState} function to be used to retrieve the state
* features/base/participants.
* @returns {(Participant|undefined)}
*/
2017-10-13 19:31:05 +00:00
export function getLocalParticipant(stateful: Object | Function) {
const participants = _getAllParticipants(stateful);
return participants.find(p => p.local);
}
2019-01-15 11:28:07 +00:00
/**
* Normalizes a display name so then no invalid values (padding, length...etc)
* can be set.
*
* @param {string} name - The display name to set.
* @returns {string}
*/
export function getNormalizedDisplayName(name: string) {
if (!name || !name.trim()) {
return undefined;
}
return name.trim().substring(0, MAX_DISPLAY_NAME_LENGTH);
}
/**
* Returns participant by ID from Redux state.
*
2017-10-13 19:31:05 +00:00
* @param {(Function|Object|Participant[])} stateful - The redux state
* features/base/participants, the (whole) redux state, or redux's
2017-10-13 19:31:05 +00:00
* {@code getState} function to be used to retrieve the state
* features/base/participants.
* @param {string} id - The ID of the participant to retrieve.
* @private
* @returns {(Participant|undefined)}
*/
export function getParticipantById(
stateful: Object | Function, id: string): ?Object {
2017-10-13 19:31:05 +00:00
const participants = _getAllParticipants(stateful);
return participants.find(p => p.id === id);
}
/**
* Returns a count of the known participants in the passed in redux state,
* excluding any fake participants.
*
2017-10-13 19:31:05 +00:00
* @param {(Function|Object|Participant[])} stateful - The redux state
* features/base/participants, the (whole) redux state, or redux's
2017-10-13 19:31:05 +00:00
* {@code getState} function to be used to retrieve the state
* features/base/participants.
* @returns {number}
*/
2017-10-13 19:31:05 +00:00
export function getParticipantCount(stateful: Object | Function) {
return getParticipants(stateful).length;
}
/**
* Returns a count of the known participants in the passed in redux state,
* including fake participants.
*
* @param {(Function|Object|Participant[])} stateful - The redux state
* features/base/participants, the (whole) redux state, or redux's
* {@code getState} function to be used to retrieve the state
* features/base/participants.
* @returns {number}
*/
export function getParticipantCountWithFake(stateful: Object | Function) {
return _getAllParticipants(stateful).length;
}
/**
* Returns participant's display name.
*
* FIXME: Remove the hardcoded strings once interfaceConfig is stored in redux
* and merge with a similarly named method in {@code conference.js}.
*
* @param {(Function|Object)} stateful - The (whole) redux state, or redux's
* {@code getState} function to be used to retrieve the state.
* @param {string} id - The ID of the participant's display name to retrieve.
* @returns {string}
*/
export function getParticipantDisplayName(
stateful: Object | Function,
id: string) {
const participant = getParticipantById(stateful, id);
if (participant) {
if (participant.name) {
return participant.name;
}
if (participant.local) {
return typeof interfaceConfig === 'object'
? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME
: 'me';
}
}
return typeof interfaceConfig === 'object'
? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME
: 'Fellow Jitster';
}
/**
* Returns the presence status of a participant associated with the passed id.
*
* @param {(Function|Object)} stateful - The (whole) redux state, or redux's
* {@code getState} function to be used to retrieve the state.
* @param {string} id - The id of the participant.
* @returns {string} - The presence status.
*/
export function getParticipantPresenceStatus(
stateful: Object | Function, id: string) {
if (!id) {
return undefined;
}
const participantById = getParticipantById(stateful, id);
if (!participantById) {
return undefined;
}
return participantById.presence;
}
/**
* Selectors for getting all known participants with fake participants filtered
* out.
*
2017-10-13 19:31:05 +00:00
* @param {(Function|Object|Participant[])} stateful - The redux state
* features/base/participants, the (whole) redux state, or redux's
2017-10-13 19:31:05 +00:00
* {@code getState} function to be used to retrieve the state
* features/base/participants.
* @returns {Participant[]}
*/
2017-10-13 19:31:05 +00:00
export function getParticipants(stateful: Object | Function) {
2018-06-22 18:59:54 +00:00
return _getAllParticipants(stateful).filter(p => !p.isFakeParticipant);
}
/**
* Returns the participant which has its pinned state set to truthy.
*
2017-10-13 19:31:05 +00:00
* @param {(Function|Object|Participant[])} stateful - The redux state
* features/base/participants, the (whole) redux state, or redux's
2017-10-13 19:31:05 +00:00
* {@code getState} function to be used to retrieve the state
* features/base/participants.
* @returns {(Participant|undefined)}
*/
2017-10-13 19:31:05 +00:00
export function getPinnedParticipant(stateful: Object | Function) {
return _getAllParticipants(stateful).find(p => p.pinned);
}
/**
* Returns array of participants from Redux state.
*
2017-10-13 19:31:05 +00:00
* @param {(Function|Object|Participant[])} stateful - The redux state
* features/base/participants, the (whole) redux state, or redux's
2017-10-13 19:31:05 +00:00
* {@code getState} function to be used to retrieve the state
* features/base/participants.
* @private
* @returns {Participant[]}
*/
2017-10-13 19:31:05 +00:00
function _getAllParticipants(stateful) {
2017-09-01 21:25:48 +00:00
return (
2017-10-13 19:31:05 +00:00
Array.isArray(stateful)
? stateful
: toState(stateful)['features/base/participants'] || []);
}
/**
* Returns true if all of the meeting participants are moderators.
*
* @param {Object|Function} stateful -Object or function that can be resolved
* to the Redux state.
* @returns {boolean}
*/
export function isEveryoneModerator(stateful: Object | Function) {
const participants = _getAllParticipants(stateful);
for (const participant of participants) {
if (participant.role !== PARTICIPANT_ROLE.MODERATOR) {
return false;
}
}
return true;
}
/**
* Returns true if the current local participant is a moderator in the
* conference.
*
* @param {Object|Function} stateful - Object or function that can be resolved
* to the Redux state.
* @param {?boolean} ignoreToken - When true we ignore the token check.
* @returns {boolean}
*/
export function isLocalParticipantModerator(
stateful: Object | Function,
ignoreToken: ?boolean = false) {
const state = toState(stateful);
const localParticipant = getLocalParticipant(state);
if (!localParticipant) {
return false;
}
return (
localParticipant.role === PARTICIPANT_ROLE.MODERATOR
&& (ignoreToken
|| !state['features/base/config'].enableUserRolesBasedOnToken
|| !state['features/base/jwt'].isGuest));
}
/**
* Returns true if the video of the participant should be rendered.
*
* @param {Object|Function} stateful - Object or function that can be resolved
* to the Redux state.
* @param {string} id - The ID of the participant.
* @returns {boolean}
*/
export function shouldRenderParticipantVideo(
stateful: Object | Function, id: string) {
const state = toState(stateful);
const participant = getParticipantById(state, id);
if (!participant) {
return false;
}
const audioOnly = state['features/base/conference'].audioOnly;
const connectionStatus = participant.connectionStatus
|| JitsiParticipantConnectionStatus.ACTIVE;
const videoTrack = getTrackByMediaTypeAndParticipant(
state['features/base/tracks'],
MEDIA_TYPE.VIDEO,
id);
// Is the video to be rendered?
// FIXME It's currently impossible to have true as the value of
// waitForVideoStarted because videoTrack's state videoStarted will be
// updated only after videoTrack is rendered.
// XXX Note that, unlike on web, we don't render video when the
// connection status is interrupted, this is because the renderer
// doesn't retain the last frame forever, so we would end up with a
// black screen.
const waitForVideoStarted = false;
return !audioOnly
&& (connectionStatus
=== JitsiParticipantConnectionStatus.ACTIVE)
&& shouldRenderVideoTrack(videoTrack, waitForVideoStarted);
}
2019-06-26 14:08:23 +00:00
/**
* Resolves the first loadable avatar URL for a participant.
*
* @param {Object} participant - The participant to resolve avatars for.
* @returns {?string}
*/
async function _getFirstLoadableAvatarUrl(participant) {
for (let i = 0; i < AVATAR_CHECKER_FUNCTIONS.length; i++) {
const url = AVATAR_CHECKER_FUNCTIONS[i](participant);
if (url) {
if (AVATAR_CHECKED_URLS.has(url)) {
if (AVATAR_CHECKED_URLS.get(url)) {
return url;
}
} else {
try {
const finalUrl = await preloadImage(url);
AVATAR_CHECKED_URLS.set(finalUrl, true);
return finalUrl;
} catch (e) {
AVATAR_CHECKED_URLS.set(url, false);
}
}
}
}
return undefined;
}