2016-01-22 15:04:34 +00:00
|
|
|
/* global MD5, config, interfaceConfig */
|
2015-01-07 14:54:03 +00:00
|
|
|
|
2016-01-22 15:04:34 +00:00
|
|
|
let users = {};
|
|
|
|
|
|
|
|
export default {
|
2016-06-13 21:11:44 +00:00
|
|
|
/**
|
|
|
|
* Sets prop in users object.
|
|
|
|
* @param id {string} user id
|
|
|
|
* @param prop {string} name of the prop
|
|
|
|
* @param val {string} value to be set
|
|
|
|
*/
|
|
|
|
_setUserProp: function (id, prop, val) {
|
|
|
|
if(!val || (users[id] && users[id][prop] === val))
|
|
|
|
return;
|
|
|
|
if(!users[id])
|
|
|
|
users[id] = {};
|
|
|
|
users[id][prop] = val;
|
|
|
|
},
|
2015-01-07 14:54:03 +00:00
|
|
|
|
2014-10-31 11:47:12 +00:00
|
|
|
/**
|
|
|
|
* Sets the user's avatar in the settings menu(if local user), contact list
|
|
|
|
* and thumbnail
|
2015-12-02 15:24:57 +00:00
|
|
|
* @param id id of the user
|
|
|
|
* @param email email or nickname to be used as a hash
|
2014-10-31 11:47:12 +00:00
|
|
|
*/
|
2016-06-13 21:11:44 +00:00
|
|
|
setUserEmail: function (id, email) {
|
|
|
|
this._setUserProp(id, "email", email);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the user's avatar in the settings menu(if local user), contact list
|
|
|
|
* and thumbnail
|
|
|
|
* @param id id of the user
|
|
|
|
* @param url the url for the avatar
|
|
|
|
*/
|
|
|
|
setUserAvatarUrl: function (id, url) {
|
|
|
|
this._setUserProp(id, "url", url);
|
2015-01-07 14:54:03 +00:00
|
|
|
},
|
2016-01-22 15:04:34 +00:00
|
|
|
|
2015-07-20 17:32:04 +00:00
|
|
|
/**
|
2016-01-19 23:10:44 +00:00
|
|
|
* Returns the URL of the image for the avatar of a particular user,
|
2016-01-22 15:04:34 +00:00
|
|
|
* identified by its id.
|
|
|
|
* @param {string} userId user id
|
2015-07-20 17:32:04 +00:00
|
|
|
*/
|
2016-01-22 15:04:34 +00:00
|
|
|
getAvatarUrl: function (userId) {
|
2016-01-19 23:10:44 +00:00
|
|
|
if (config.disableThirdPartyRequests) {
|
|
|
|
return 'images/avatar2.png';
|
2016-01-22 15:04:34 +00:00
|
|
|
}
|
2016-01-05 23:22:29 +00:00
|
|
|
|
2016-01-22 15:04:34 +00:00
|
|
|
if (!userId) {
|
|
|
|
console.error("Get avatar - id is undefined");
|
|
|
|
return null;
|
|
|
|
}
|
2016-01-19 23:10:44 +00:00
|
|
|
|
2016-06-13 21:11:44 +00:00
|
|
|
let avatarId = null;
|
|
|
|
const user = users[userId];
|
|
|
|
|
|
|
|
if(user) {
|
|
|
|
if(user.url)
|
|
|
|
return users[userId].url;
|
|
|
|
|
|
|
|
avatarId = users[userId].email;
|
|
|
|
}
|
2016-01-05 23:22:29 +00:00
|
|
|
|
2016-01-22 15:04:34 +00:00
|
|
|
// If the ID looks like an email, we'll use gravatar.
|
|
|
|
// Otherwise, it's a random avatar, and we'll use the configured
|
|
|
|
// URL.
|
|
|
|
let random = !avatarId || avatarId.indexOf('@') < 0;
|
2016-01-05 23:22:29 +00:00
|
|
|
|
2016-01-22 15:04:34 +00:00
|
|
|
if (!avatarId) {
|
|
|
|
console.warn(
|
|
|
|
`No avatar stored yet for ${userId} - using ID as avatar ID`);
|
|
|
|
avatarId = userId;
|
|
|
|
}
|
|
|
|
avatarId = MD5.hexdigest(avatarId.trim().toLowerCase());
|
|
|
|
|
2016-01-05 23:22:29 +00:00
|
|
|
|
2016-03-15 20:28:57 +00:00
|
|
|
let urlPref = null;
|
|
|
|
let urlSuf = null;
|
|
|
|
if (!random) {
|
|
|
|
urlPref = 'https://www.gravatar.com/avatar/';
|
|
|
|
urlSuf = "?d=wavatar&size=200";
|
|
|
|
}
|
|
|
|
else if (random && interfaceConfig.RANDOM_AVATAR_URL_PREFIX) {
|
2016-01-22 15:04:34 +00:00
|
|
|
urlPref = interfaceConfig.RANDOM_AVATAR_URL_PREFIX;
|
|
|
|
urlSuf = interfaceConfig.RANDOM_AVATAR_URL_SUFFIX;
|
2015-12-07 20:41:35 +00:00
|
|
|
}
|
2016-03-15 20:28:57 +00:00
|
|
|
else {
|
|
|
|
urlPref = 'https://robohash.org/';
|
|
|
|
urlSuf = ".png?size=200x200";
|
|
|
|
}
|
2016-01-22 15:04:34 +00:00
|
|
|
|
|
|
|
return urlPref + avatarId + urlSuf;
|
2014-10-31 11:47:12 +00:00
|
|
|
}
|
2015-01-07 14:54:03 +00:00
|
|
|
};
|