您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Avatar.js 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * Returns the URL of the image for the avatar of a particular user,
  37. * identified by its id.
  38. * @param {string} userId user id
  39. */
  40. getAvatarUrl: function (userId) {
  41. if (config.disableThirdPartyRequests) {
  42. return 'images/avatar2.png';
  43. }
  44. if (!userId) {
  45. console.error("Get avatar - id is undefined");
  46. return null;
  47. }
  48. let avatarId = null;
  49. const user = users[userId];
  50. if(user) {
  51. if(user.url)
  52. return users[userId].url;
  53. avatarId = users[userId].email;
  54. }
  55. // If the ID looks like an email, we'll use gravatar.
  56. // Otherwise, it's a random avatar, and we'll use the configured
  57. // URL.
  58. let random = !avatarId || avatarId.indexOf('@') < 0;
  59. if (!avatarId) {
  60. console.warn(
  61. `No avatar stored yet for ${userId} - using ID as avatar ID`);
  62. avatarId = userId;
  63. }
  64. avatarId = MD5.hexdigest(avatarId.trim().toLowerCase());
  65. let urlPref = null;
  66. let urlSuf = null;
  67. if (!random) {
  68. urlPref = 'https://www.gravatar.com/avatar/';
  69. urlSuf = "?d=wavatar&size=200";
  70. }
  71. else if (random && interfaceConfig.RANDOM_AVATAR_URL_PREFIX) {
  72. urlPref = interfaceConfig.RANDOM_AVATAR_URL_PREFIX;
  73. urlSuf = interfaceConfig.RANDOM_AVATAR_URL_SUFFIX;
  74. }
  75. else {
  76. urlPref = 'https://robohash.org/';
  77. urlSuf = ".png?size=200x200";
  78. }
  79. return urlPref + avatarId + urlSuf;
  80. }
  81. };