Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

functions.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. declare var config: Object;
  2. declare var interfaceConfig: Object;
  3. declare var MD5: Object;
  4. /**
  5. * Returns the URL of the image for the avatar of a specific participant.
  6. *
  7. * @param {Participant} [participant] - The participant to return the avatar URL
  8. * of.
  9. * @param {string} [participant.avatarID] - Participant's avatar ID.
  10. * @param {string} [participant.avatarURL] - Participant's avatar URL.
  11. * @param {string} [participant.email] - Participant's e-mail address.
  12. * @param {string} [participant.id] - Participant's ID.
  13. * @returns {string} The URL of the image for the avatar of the specified
  14. * participant.
  15. *
  16. * @public
  17. */
  18. export function getAvatarURL(participant) {
  19. // If disableThirdPartyRequests disables third-party avatar services, we are
  20. // restricted to a stock image of ours.
  21. if (typeof config === 'object' && config.disableThirdPartyRequests) {
  22. return 'images/avatar2.png';
  23. }
  24. const { avatarID, avatarURL, email, id } = participant;
  25. // If an avatarURL is specified, then obviously there's nothing to generate.
  26. if (avatarURL) {
  27. return avatarURL;
  28. }
  29. let key = email || avatarID;
  30. let urlPrefix;
  31. let urlSuffix;
  32. // If the ID looks like an e-mail address, we'll use Gravatar because it
  33. // supports e-mail addresses.
  34. if (key && key.indexOf('@') > 0) {
  35. urlPrefix = 'https://www.gravatar.com/avatar/';
  36. urlSuffix = '?d=wavatar&size=200';
  37. } else {
  38. // Otherwise, we do not have much a choice but a random avatar (fetched
  39. // from a configured avatar service).
  40. if (!key) {
  41. key = id;
  42. }
  43. // The deployment is allowed to choose the avatar service which is to
  44. // generate the random avatars.
  45. urlPrefix
  46. = typeof interfaceConfig === 'object'
  47. && interfaceConfig.RANDOM_AVATAR_URL_PREFIX;
  48. if (urlPrefix) {
  49. urlSuffix = interfaceConfig.RANDOM_AVATAR_URL_SUFFIX;
  50. } else {
  51. // Otherwise, use a default (of course).
  52. urlPrefix = 'https://api.adorable.io/avatars/200/';
  53. urlSuffix = '.png';
  54. }
  55. }
  56. return urlPrefix + MD5.hexdigest(key.trim().toLowerCase()) + urlSuffix;
  57. }
  58. /**
  59. * Returns local participant from Redux state.
  60. *
  61. * @param {(Function|Participant[])} participantsOrGetState - Either the
  62. * features/base/participants Redux state or Redux's getState function to be
  63. * used to retrieve the features/base/participants state.
  64. * @returns {(Participant|undefined)}
  65. */
  66. export function getLocalParticipant(participantsOrGetState) {
  67. const participants = _getParticipants(participantsOrGetState);
  68. return participants.find(p => p.local);
  69. }
  70. /**
  71. * Returns participant by ID from Redux state.
  72. *
  73. * @param {(Function|Participant[])} participantsOrGetState - Either the
  74. * features/base/participants Redux state or Redux's getState function to be
  75. * used to retrieve the features/base/participants state.
  76. * @param {string} id - The ID of the participant to retrieve.
  77. * @private
  78. * @returns {(Participant|undefined)}
  79. */
  80. export function getParticipantById(participantsOrGetState, id) {
  81. const participants = _getParticipants(participantsOrGetState);
  82. return participants.find(p => p.id === id);
  83. }
  84. /**
  85. * Returns array of participants from Redux state.
  86. *
  87. * @param {(Function|Participant[])} participantsOrGetState - Either the
  88. * features/base/participants Redux state or Redux's getState function to be
  89. * used to retrieve the features/base/participants state.
  90. * @private
  91. * @returns {Participant[]}
  92. */
  93. function _getParticipants(participantsOrGetState) {
  94. const participants
  95. = typeof participantsOrGetState === 'function'
  96. ? participantsOrGetState()['features/base/participants']
  97. : participantsOrGetState;
  98. return participants || [];
  99. }