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 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* global MD5, config, interfaceConfig */
  2. let users = {};
  3. export default {
  4. /**
  5. * Sets prop in users object.
  6. * @param id {string} user id
  7. * @param prop {string} name of the prop
  8. * @param val {string} value to be set
  9. */
  10. _setUserProp: function (id, prop, val) {
  11. if(!val || (users[id] && users[id][prop] === val))
  12. return;
  13. if(!users[id])
  14. users[id] = {};
  15. users[id][prop] = val;
  16. },
  17. /**
  18. * Sets the user's avatar in the settings menu(if local user), contact list
  19. * and thumbnail
  20. * @param id id of the user
  21. * @param email email or nickname to be used as a hash
  22. */
  23. setUserEmail: function (id, email) {
  24. this._setUserProp(id, "email", email);
  25. },
  26. /**
  27. * Sets the user's avatar in the settings menu(if local user), contact list
  28. * and thumbnail
  29. * @param id id of the user
  30. * @param url the url for the avatar
  31. */
  32. setUserAvatarUrl: function (id, url) {
  33. this._setUserProp(id, "url", url);
  34. },
  35. /**
  36. * Sets the user's avatar id.
  37. * @param id id of the user
  38. * @param avatarId an id to be used for the avatar
  39. */
  40. setUserAvatarID: function (id, avatarId) {
  41. this._setUserProp(id, "avatarId", avatarId);
  42. },
  43. /**
  44. * Returns the URL of the image for the avatar of a particular user,
  45. * identified by its id.
  46. * @param {string} userId user id
  47. */
  48. getAvatarUrl: function (userId) {
  49. if (config.disableThirdPartyRequests) {
  50. return 'images/avatar2.png';
  51. }
  52. if (!userId) {
  53. console.error("Get avatar - id is undefined");
  54. return null;
  55. }
  56. let avatarId = null;
  57. const user = users[userId];
  58. // The priority is url, email and lowest is avatarId
  59. if(user) {
  60. if(user.url)
  61. return user.url;
  62. if (user.email)
  63. avatarId = user.email;
  64. else {
  65. avatarId = user.avatarId;
  66. }
  67. }
  68. // If the ID looks like an email, we'll use gravatar.
  69. // Otherwise, it's a random avatar, and we'll use the configured
  70. // URL.
  71. let random = !avatarId || avatarId.indexOf('@') < 0;
  72. if (!avatarId) {
  73. console.warn(
  74. `No avatar stored yet for ${userId} - using ID as avatar ID`);
  75. avatarId = userId;
  76. }
  77. avatarId = MD5.hexdigest(avatarId.trim().toLowerCase());
  78. let urlPref = null;
  79. let urlSuf = null;
  80. if (!random) {
  81. urlPref = 'https://www.gravatar.com/avatar/';
  82. urlSuf = "?d=wavatar&size=200";
  83. }
  84. else if (random && interfaceConfig.RANDOM_AVATAR_URL_PREFIX) {
  85. urlPref = interfaceConfig.RANDOM_AVATAR_URL_PREFIX;
  86. urlSuf = interfaceConfig.RANDOM_AVATAR_URL_SUFFIX;
  87. }
  88. else {
  89. urlPref = 'https://robohash.org/';
  90. urlSuf = ".png?size=200x200";
  91. }
  92. return urlPref + avatarId + urlSuf;
  93. }
  94. };