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
|
|
|
declare var MD5: Object;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* @returns {string} The URL of the image for the avatar of the specified
|
|
|
|
* participant.
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export function getAvatarURL(participant) {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
const { avatarID, avatarURL, email, id } = participant;
|
|
|
|
|
|
|
|
// 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;
|
2017-02-28 23:12:02 +00:00
|
|
|
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 {
|
|
|
|
// Otherwise, use a default (of course).
|
|
|
|
urlPrefix = 'https://api.adorable.io/avatars/200/';
|
|
|
|
urlSuffix = '.png';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return urlPrefix + MD5.hexdigest(key.trim().toLowerCase()) + urlSuffix;
|
|
|
|
}
|
2017-02-27 21:42:28 +00:00
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* Returns local participant from Redux state.
|
|
|
|
*
|
2017-05-31 05:32:13 +00:00
|
|
|
* @param {(Function|Object|Participant[])} stateOrGetState - The redux state
|
|
|
|
* features/base/participants, the (whole) redux state, or redux's
|
|
|
|
* {@code getState} function to be used to retrieve the
|
|
|
|
* features/base/participants state.
|
2016-10-05 14:36:59 +00:00
|
|
|
* @returns {(Participant|undefined)}
|
|
|
|
*/
|
2017-05-31 05:32:13 +00:00
|
|
|
export function getLocalParticipant(stateOrGetState) {
|
|
|
|
const participants = _getParticipants(stateOrGetState);
|
2016-10-05 14:36:59 +00:00
|
|
|
|
|
|
|
return participants.find(p => p.local);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns participant by ID from Redux state.
|
|
|
|
*
|
2017-05-31 05:32:13 +00:00
|
|
|
* @param {(Function|Object|Participant[])} stateOrGetState - The redux state
|
|
|
|
* features/base/participants, the (whole) redux state, or redux's
|
|
|
|
* {@code getState} function to be used to retrieve the
|
|
|
|
* features/base/participants state.
|
2016-10-05 14:36:59 +00:00
|
|
|
* @param {string} id - The ID of the participant to retrieve.
|
|
|
|
* @private
|
|
|
|
* @returns {(Participant|undefined)}
|
|
|
|
*/
|
2017-05-31 05:32:13 +00:00
|
|
|
export function getParticipantById(stateOrGetState, id) {
|
|
|
|
const participants = _getParticipants(stateOrGetState);
|
2016-10-05 14:36:59 +00:00
|
|
|
|
|
|
|
return participants.find(p => p.id === id);
|
|
|
|
}
|
|
|
|
|
2017-06-27 22:56:55 +00:00
|
|
|
/**
|
|
|
|
* Returns a count of the known participants in the passed in redux state,
|
|
|
|
* excluding any fake participants.
|
|
|
|
*
|
|
|
|
* @param {(Function|Object|Participant[])} stateOrGetState - The redux state
|
|
|
|
* features/base/participants, the (whole) redux state, or redux's
|
|
|
|
* {@code getState} function to be used to retrieve the
|
|
|
|
* features/base/participants state.
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
|
|
|
export function getParticipantCount(stateOrGetState) {
|
|
|
|
const participants = _getParticipants(stateOrGetState);
|
|
|
|
const realParticipants = participants.filter(p => !p.isBot);
|
|
|
|
|
|
|
|
return realParticipants.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the participant which has its pinned state set to truthy.
|
|
|
|
*
|
|
|
|
* @param {(Function|Object|Participant[])} stateOrGetState - The redux state
|
|
|
|
* features/base/participants, the (whole) redux state, or redux's
|
|
|
|
* {@code getState} function to be used to retrieve the
|
|
|
|
* features/base/participants state.
|
|
|
|
* @returns {(Participant|undefined)}
|
|
|
|
*/
|
|
|
|
export function getPinnedParticipant(stateOrGetState) {
|
|
|
|
const participants = _getParticipants(stateOrGetState);
|
|
|
|
|
|
|
|
return participants.find(p => p.pinned);
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* Returns array of participants from Redux state.
|
|
|
|
*
|
2017-05-31 05:32:13 +00:00
|
|
|
* @param {(Function|Object|Participant[])} stateOrGetState - The redux state
|
|
|
|
* features/base/participants, the (whole) redux state, or redux's
|
|
|
|
* {@code getState} function to be used to retrieve the
|
|
|
|
* features/base/participants state.
|
2016-10-05 14:36:59 +00:00
|
|
|
* @private
|
|
|
|
* @returns {Participant[]}
|
|
|
|
*/
|
2017-05-31 05:32:13 +00:00
|
|
|
function _getParticipants(stateOrGetState) {
|
|
|
|
if (Array.isArray(stateOrGetState)) {
|
|
|
|
return stateOrGetState;
|
|
|
|
}
|
|
|
|
|
|
|
|
const state
|
|
|
|
= typeof stateOrGetState === 'function'
|
|
|
|
? stateOrGetState()
|
|
|
|
: stateOrGetState;
|
2016-10-05 14:36:59 +00:00
|
|
|
|
2017-05-31 05:32:13 +00:00
|
|
|
return state['features/base/participants'] || [];
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|