You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Avatar.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* global MD5, config, interfaceConfig */
  2. let users = {};
  3. export default {
  4. /**
  5. * Sets the user's avatar in the settings menu(if local user), contact list
  6. * and thumbnail
  7. * @param id id of the user
  8. * @param email email or nickname to be used as a hash
  9. */
  10. setUserAvatar: function (id, email) {
  11. if (email) {
  12. if (users[id] === email) {
  13. return;
  14. }
  15. users[id] = email;
  16. }
  17. },
  18. /**
  19. * Returns the URL of the image for the avatar of a particular user,
  20. * identified by its id.
  21. * @param {string} userId user id
  22. */
  23. getAvatarUrl: function (userId) {
  24. if (config.disableThirdPartyRequests) {
  25. return 'images/avatar2.png';
  26. }
  27. if (!userId) {
  28. console.error("Get avatar - id is undefined");
  29. return null;
  30. }
  31. let avatarId = users[userId];
  32. // If the ID looks like an email, we'll use gravatar.
  33. // Otherwise, it's a random avatar, and we'll use the configured
  34. // URL.
  35. let random = !avatarId || avatarId.indexOf('@') < 0;
  36. if (!avatarId) {
  37. console.warn(
  38. `No avatar stored yet for ${userId} - using ID as avatar ID`);
  39. console.log("USER ID ", userId);
  40. avatarId = userId;
  41. }
  42. avatarId = MD5.hexdigest(avatarId.trim().toLowerCase());
  43. let urlPref = null;
  44. let urlSuf = null;
  45. if (!random) {
  46. urlPref = 'https://www.gravatar.com/avatar/';
  47. urlSuf = "?d=wavatar&size=200";
  48. }
  49. else if (random && interfaceConfig.RANDOM_AVATAR_URL_PREFIX) {
  50. urlPref = interfaceConfig.RANDOM_AVATAR_URL_PREFIX;
  51. urlSuf = interfaceConfig.RANDOM_AVATAR_URL_SUFFIX;
  52. }
  53. else {
  54. urlPref = 'https://robohash.org/';
  55. urlSuf = ".png?size=200x200";
  56. }
  57. return urlPref + avatarId + urlSuf;
  58. }
  59. };