Avatar.js code cleanup

This commit is contained in:
isymchych 2016-01-22 17:04:34 +02:00
parent a0355ea080
commit 6df1fcef40
1 changed files with 39 additions and 40 deletions

View File

@ -1,7 +1,8 @@
/* global Strophe, APP, MD5, config, interfaceConfig */
var users = {};
/* global MD5, config, interfaceConfig */
var Avatar = {
let users = {};
export default {
/**
* Sets the user's avatar in the settings menu(if local user), contact list
@ -16,48 +17,46 @@ var Avatar = {
}
users[id] = email;
}
var avatarUrl = this.getAvatarUrl(id);
},
/**
* Returns the URL of the image for the avatar of a particular user,
+ identified by its jid
* @param jid
* identified by its id.
* @param {string} userId user id
*/
getAvatarUrl: function (jid) {
getAvatarUrl: function (userId) {
if (config.disableThirdPartyRequests) {
return 'images/avatar2.png';
} else {
if (!jid) {
console.error("Get avatar - jid is undefined");
}
if (!userId) {
console.error("Get avatar - id is undefined");
return null;
}
var id = users[jid];
let avatarId = users[userId];
// If the ID looks like an email, we'll use gravatar.
// Otherwise, it's a random avatar, and we'll use the configured
// URL.
var random = !id || id.indexOf('@') < 0;
let random = !avatarId || avatarId.indexOf('@') < 0;
if (!id) {
if (!avatarId) {
console.warn(
"No avatar stored yet for " + jid + " - using JID as ID");
id = jid;
`No avatar stored yet for ${userId} - using ID as avatar ID`);
avatarId = userId;
}
id = MD5.hexdigest(id.trim().toLowerCase());
avatarId = MD5.hexdigest(avatarId.trim().toLowerCase());
// Default to using gravatar.
var urlPref = 'https://www.gravatar.com/avatar/';
var urlSuf = "?d=wavatar&size=100";
let urlPref = 'https://www.gravatar.com/avatar/';
let urlSuf = "?d=wavatar&size=100";
if (random && interfaceConfig.RANDOM_AVATAR_URL_PREFIX) {
urlPref = interfaceConfig.RANDOM_AVATAR_URL_PREFIX;
urlSuf = interfaceConfig.RANDOM_AVATAR_URL_SUFFIX;
}
return urlPref + id + urlSuf;
}
return urlPref + avatarId + urlSuf;
}
};
module.exports = Avatar;