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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Adorable Avatars service used at the end of this file is released under the
  3. * terms of the MIT License.
  4. *
  5. * Copyright (c) 2014 Adorable IO LLC
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. /* global MD5, config, interfaceConfig, APP */
  24. let users = {};
  25. export default {
  26. /**
  27. * Sets prop in users object.
  28. * @param id {string} user id
  29. * @param prop {string} name of the prop
  30. * @param val {string} value to be set
  31. */
  32. _setUserProp: function (id, prop, val) {
  33. // FIXME: Fixes the issue with not be able to return avatar for the
  34. // local user when the conference has been left. Maybe there is beter
  35. // way to solve it.
  36. if(APP.conference.isLocalId(id)) {
  37. id = "local";
  38. }
  39. if(!val || (users[id] && users[id][prop] === val))
  40. return;
  41. if(!users[id])
  42. users[id] = {};
  43. users[id][prop] = val;
  44. },
  45. /**
  46. * Sets the user's avatar in the settings menu(if local user), contact list
  47. * and thumbnail
  48. * @param id id of the user
  49. * @param email email or nickname to be used as a hash
  50. */
  51. setUserEmail: function (id, email) {
  52. this._setUserProp(id, "email", email);
  53. },
  54. /**
  55. * Sets the user's avatar in the settings menu(if local user), contact list
  56. * and thumbnail
  57. * @param id id of the user
  58. * @param url the url for the avatar
  59. */
  60. setUserAvatarUrl: function (id, url) {
  61. this._setUserProp(id, "url", url);
  62. },
  63. /**
  64. * Sets the user's avatar id.
  65. * @param id id of the user
  66. * @param avatarId an id to be used for the avatar
  67. */
  68. setUserAvatarID: function (id, avatarId) {
  69. this._setUserProp(id, "avatarId", avatarId);
  70. },
  71. /**
  72. * Returns the URL of the image for the avatar of a particular user,
  73. * identified by its id.
  74. * @param {string} userId user id
  75. */
  76. getAvatarUrl: function (userId) {
  77. if (config.disableThirdPartyRequests) {
  78. return 'images/avatar2.png';
  79. }
  80. if (!userId || APP.conference.isLocalId(userId)) {
  81. userId = "local";
  82. }
  83. let avatarId = null;
  84. const user = users[userId];
  85. // The priority is url, email and lowest is avatarId
  86. if(user) {
  87. if(user.url)
  88. return user.url;
  89. if (user.email)
  90. avatarId = user.email;
  91. else {
  92. avatarId = user.avatarId;
  93. }
  94. }
  95. // If the ID looks like an email, we'll use gravatar.
  96. // Otherwise, it's a random avatar, and we'll use the configured
  97. // URL.
  98. let random = !avatarId || avatarId.indexOf('@') < 0;
  99. if (!avatarId) {
  100. console.warn(
  101. `No avatar stored yet for ${userId} - using ID as avatar ID`);
  102. avatarId = userId;
  103. }
  104. avatarId = MD5.hexdigest(avatarId.trim().toLowerCase());
  105. let urlPref = null;
  106. let urlSuf = null;
  107. if (!random) {
  108. urlPref = 'https://www.gravatar.com/avatar/';
  109. urlSuf = "?d=wavatar&size=200";
  110. }
  111. else if (random && interfaceConfig.RANDOM_AVATAR_URL_PREFIX) {
  112. urlPref = interfaceConfig.RANDOM_AVATAR_URL_PREFIX;
  113. urlSuf = interfaceConfig.RANDOM_AVATAR_URL_SUFFIX;
  114. }
  115. else {
  116. urlPref = 'https://api.adorable.io/avatars/200/';
  117. urlSuf = ".png";
  118. }
  119. return urlPref + avatarId + urlSuf;
  120. }
  121. };