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.

functions.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. if (!key) {
  43. return undefined;
  44. }
  45. }
  46. // The deployment is allowed to choose the avatar service which is to
  47. // generate the random avatars.
  48. urlPrefix
  49. = typeof interfaceConfig === 'object'
  50. && interfaceConfig.RANDOM_AVATAR_URL_PREFIX;
  51. if (urlPrefix) {
  52. urlSuffix = interfaceConfig.RANDOM_AVATAR_URL_SUFFIX;
  53. } else {
  54. // Otherwise, use a default (of course).
  55. urlPrefix = 'https://api.adorable.io/avatars/200/';
  56. urlSuffix = '.png';
  57. }
  58. }
  59. return urlPrefix + MD5.hexdigest(key.trim().toLowerCase()) + urlSuffix;
  60. }
  61. /**
  62. * Returns local participant from Redux state.
  63. *
  64. * @param {(Function|Participant[])} participantsOrGetState - Either the
  65. * features/base/participants Redux state or Redux's getState function to be
  66. * used to retrieve the features/base/participants state.
  67. * @returns {(Participant|undefined)}
  68. */
  69. export function getLocalParticipant(participantsOrGetState) {
  70. const participants = _getParticipants(participantsOrGetState);
  71. return participants.find(p => p.local);
  72. }
  73. /**
  74. * Returns participant by ID from Redux state.
  75. *
  76. * @param {(Function|Participant[])} participantsOrGetState - Either the
  77. * features/base/participants Redux state or Redux's getState function to be
  78. * used to retrieve the features/base/participants state.
  79. * @param {string} id - The ID of the participant to retrieve.
  80. * @private
  81. * @returns {(Participant|undefined)}
  82. */
  83. export function getParticipantById(participantsOrGetState, id) {
  84. const participants = _getParticipants(participantsOrGetState);
  85. return participants.find(p => p.id === id);
  86. }
  87. /**
  88. * Returns array of participants from Redux state.
  89. *
  90. * @param {(Function|Participant[])} participantsOrGetState - Either the
  91. * features/base/participants Redux state or Redux's getState function to be
  92. * used to retrieve the features/base/participants state.
  93. * @private
  94. * @returns {Participant[]}
  95. */
  96. function _getParticipants(participantsOrGetState) {
  97. const participants
  98. = typeof participantsOrGetState === 'function'
  99. ? participantsOrGetState()['features/base/participants']
  100. : participantsOrGetState;
  101. return participants || [];
  102. }