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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* global MD5 */
  2. declare var config: Object;
  3. declare var interfaceConfig: Object;
  4. /**
  5. * Returns local participant from Redux state.
  6. *
  7. * @param {(Function|Participant[])} participantsOrGetState - Either the
  8. * features/base/participants Redux state or Redux's getState function to be
  9. * used to retrieve the features/base/participants state.
  10. * @returns {(Participant|undefined)}
  11. */
  12. export function getLocalParticipant(participantsOrGetState) {
  13. const participants = _getParticipants(participantsOrGetState);
  14. return participants.find(p => p.local);
  15. }
  16. /**
  17. * Returns participant by ID from Redux state.
  18. *
  19. * @param {(Function|Participant[])} participantsOrGetState - Either the
  20. * features/base/participants Redux state or Redux's getState function to be
  21. * used to retrieve the features/base/participants state.
  22. * @param {string} id - The ID of the participant to retrieve.
  23. * @private
  24. * @returns {(Participant|undefined)}
  25. */
  26. export function getParticipantById(participantsOrGetState, id) {
  27. const participants = _getParticipants(participantsOrGetState);
  28. return participants.find(p => p.id === id);
  29. }
  30. /**
  31. * Returns array of participants from Redux state.
  32. *
  33. * @param {(Function|Participant[])} participantsOrGetState - Either the
  34. * features/base/participants Redux state or Redux's getState function to be
  35. * used to retrieve the features/base/participants state.
  36. * @private
  37. * @returns {Participant[]}
  38. */
  39. function _getParticipants(participantsOrGetState) {
  40. const participants
  41. = typeof participantsOrGetState === 'function'
  42. ? participantsOrGetState()['features/base/participants']
  43. : participantsOrGetState;
  44. return participants || [];
  45. }
  46. /**
  47. * Returns the URL of the image for the avatar of a particular participant
  48. * identified by their id and/or e-mail address.
  49. *
  50. * @param {string} [participantId] - Participant's id.
  51. * @param {Object} [options] - The optional arguments.
  52. * @param {string} [options.avatarId] - Participant's avatar id.
  53. * @param {string} [options.avatarUrl] - Participant's avatar url.
  54. * @param {string} [options.email] - Participant's email.
  55. * @returns {string} The URL of the image for the avatar of the participant
  56. * identified by the specified participantId and/or email.
  57. *
  58. * @public
  59. */
  60. export function getAvatarURL(participantId, options = {}) {
  61. // If disableThirdPartyRequests is enabled we shouldn't use third party
  62. // avatar services, we are returning one of our images.
  63. if (typeof config === 'object' && config.disableThirdPartyRequests) {
  64. return 'images/avatar2.png';
  65. }
  66. const { avatarId, avatarUrl, email } = options;
  67. // If we have avatarUrl we don't need to generate new one.
  68. if (avatarUrl) {
  69. return avatarUrl;
  70. }
  71. let avatarKey = null;
  72. if (email) {
  73. avatarKey = email;
  74. } else {
  75. avatarKey = avatarId;
  76. }
  77. // If the ID looks like an email, we'll use gravatar.
  78. // Otherwise, it's a random avatar, and we'll use the configured
  79. // URL.
  80. const isEmail = avatarKey && avatarKey.indexOf('@') > 0;
  81. if (!avatarKey) {
  82. avatarKey = participantId;
  83. }
  84. avatarKey = MD5.hexdigest(avatarKey.trim().toLowerCase());
  85. let urlPref = null;
  86. let urlSuf = null;
  87. // gravatar doesn't support random avatars that's why we need to use other
  88. // services for the use case when the email is undefined.
  89. if (isEmail) {
  90. urlPref = 'https://www.gravatar.com/avatar/';
  91. urlSuf = '?d=wavatar&size=200';
  92. } else if (typeof interfaceConfig === 'object'
  93. && interfaceConfig.RANDOM_AVATAR_URL_PREFIX) { // custom avatar service
  94. urlPref = interfaceConfig.RANDOM_AVATAR_URL_PREFIX;
  95. urlSuf = interfaceConfig.RANDOM_AVATAR_URL_SUFFIX;
  96. } else { // default avatar service
  97. urlPref = 'https://api.adorable.io/avatars/200/';
  98. urlSuf = '.png';
  99. }
  100. return urlPref + avatarKey + urlSuf;
  101. }