123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- /* global MD5 */
-
- declare var config: Object;
- declare var interfaceConfig: Object;
-
- /**
- * Returns local participant from Redux state.
- *
- * @param {(Function|Participant[])} participantsOrGetState - Either the
- * features/base/participants Redux state or Redux's getState function to be
- * used to retrieve the features/base/participants state.
- * @returns {(Participant|undefined)}
- */
- export function getLocalParticipant(participantsOrGetState) {
- const participants = _getParticipants(participantsOrGetState);
-
- return participants.find(p => p.local);
- }
-
- /**
- * Returns participant by ID from Redux state.
- *
- * @param {(Function|Participant[])} participantsOrGetState - Either the
- * features/base/participants Redux state or Redux's getState function to be
- * used to retrieve the features/base/participants state.
- * @param {string} id - The ID of the participant to retrieve.
- * @private
- * @returns {(Participant|undefined)}
- */
- export function getParticipantById(participantsOrGetState, id) {
- const participants = _getParticipants(participantsOrGetState);
-
- return participants.find(p => p.id === id);
- }
-
- /**
- * Returns array of participants from Redux state.
- *
- * @param {(Function|Participant[])} participantsOrGetState - Either the
- * features/base/participants Redux state or Redux's getState function to be
- * used to retrieve the features/base/participants state.
- * @private
- * @returns {Participant[]}
- */
- function _getParticipants(participantsOrGetState) {
- const participants
- = typeof participantsOrGetState === 'function'
- ? participantsOrGetState()['features/base/participants']
- : participantsOrGetState;
-
- return participants || [];
- }
-
- /**
- * Returns the URL of the image for the avatar of a particular participant
- * identified by their id and/or e-mail address.
- *
- * @param {string} [participantId] - Participant's id.
- * @param {Object} [options] - The optional arguments.
- * @param {string} [options.avatarId] - Participant's avatar id.
- * @param {string} [options.avatarUrl] - Participant's avatar url.
- * @param {string} [options.email] - Participant's email.
- * @returns {string} The URL of the image for the avatar of the participant
- * identified by the specified participantId and/or email.
- *
- * @public
- */
- export function getAvatarURL(participantId, options = {}) {
- // If disableThirdPartyRequests is enabled we shouldn't use third party
- // avatar services, we are returning one of our images.
- if (typeof config === 'object' && config.disableThirdPartyRequests) {
- return 'images/avatar2.png';
- }
-
- const { avatarId, avatarUrl, email } = options;
-
- // If we have avatarUrl we don't need to generate new one.
- if (avatarUrl) {
- return avatarUrl;
- }
-
- let avatarKey = null;
-
- if (email) {
- avatarKey = email;
- } else {
- avatarKey = avatarId;
- }
-
- // If the ID looks like an email, we'll use gravatar.
- // Otherwise, it's a random avatar, and we'll use the configured
- // URL.
- const isEmail = avatarKey && avatarKey.indexOf('@') > 0;
-
- if (!avatarKey) {
- avatarKey = participantId;
- }
-
- avatarKey = MD5.hexdigest(avatarKey.trim().toLowerCase());
-
- let urlPref = null;
- let urlSuf = null;
-
- // gravatar doesn't support random avatars that's why we need to use other
- // services for the use case when the email is undefined.
- if (isEmail) {
- urlPref = 'https://www.gravatar.com/avatar/';
- urlSuf = '?d=wavatar&size=200';
- } else if (typeof interfaceConfig === 'object'
- && interfaceConfig.RANDOM_AVATAR_URL_PREFIX) { // custom avatar service
- urlPref = interfaceConfig.RANDOM_AVATAR_URL_PREFIX;
- urlSuf = interfaceConfig.RANDOM_AVATAR_URL_SUFFIX;
- } else { // default avatar service
- urlPref = 'https://api.adorable.io/avatars/200/';
- urlSuf = '.png';
- }
-
- return urlPref + avatarKey + urlSuf;
- }
|