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

169 lines
5.4 KiB
JavaScript
Raw Normal View History

2017-10-13 19:31:05 +00:00
// @flow
import md5 from 'js-md5';
2017-09-01 21:25:48 +00:00
import { toState } from '../redux';
2017-08-16 21:28:39 +00:00
import { DEFAULT_AVATAR_RELATIVE_PATH } from './constants';
2017-02-27 21:42:28 +00:00
declare var config: Object;
declare var interfaceConfig: Object;
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.
*/
2017-09-01 21:25:48 +00:00
export function getAvatarURL({ avatarID, avatarURL, email, id }: {
avatarID: string,
avatarURL: string,
email: string,
id: string
}) {
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;
}
let key = email || avatarID;
let urlPrefix;
let urlSuffix;
// If the ID looks like an e-mail address, we'll use Gravatar because it
// supports e-mail addresses.
if (key && key.indexOf('@') > 0) {
urlPrefix = 'https://www.gravatar.com/avatar/';
urlSuffix = '?d=wavatar&size=200';
} else {
// Otherwise, we do not have much a choice but a random avatar (fetched
// from a configured avatar service).
if (!key) {
key = id;
if (!key) {
return undefined;
}
2017-03-07 16:50:17 +00:00
}
// The deployment is allowed to choose the avatar service which is to
// generate the random avatars.
urlPrefix
= typeof interfaceConfig === 'object'
&& interfaceConfig.RANDOM_AVATAR_URL_PREFIX;
if (urlPrefix) {
urlSuffix = interfaceConfig.RANDOM_AVATAR_URL_SUFFIX;
} else {
2017-08-28 08:17:47 +00:00
// Otherwise, use a default (meeples, of course).
urlPrefix = 'https://abotars.jitsi.net/meeple/';
urlSuffix = '';
2017-03-07 16:50:17 +00:00
}
}
return urlPrefix + md5.hex(key.trim().toLowerCase()) + urlSuffix;
2017-03-07 16:50:17 +00:00
}
2017-02-27 21:42:28 +00:00
/**
* 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);
}
/**
* 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)}
*/
2017-09-01 21:25:48 +00:00
export function getParticipantById(
2017-10-13 19:31:05 +00:00
stateful: Object | Function,
2017-09-01 21:25:48 +00:00
id: string) {
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;
}
/**
* 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) {
return _getAllParticipants(stateful).filter(p => !p.isBot);
}
/**
* 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'] || []);
}