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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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|Object|Participant[])} stateOrGetState - The redux state
  65. * features/base/participants, the (whole) redux state, or redux's
  66. * {@code getState} function to be used to retrieve the
  67. * features/base/participants state.
  68. * @returns {(Participant|undefined)}
  69. */
  70. export function getLocalParticipant(stateOrGetState) {
  71. const participants = _getParticipants(stateOrGetState);
  72. return participants.find(p => p.local);
  73. }
  74. /**
  75. * Returns participant by ID from Redux state.
  76. *
  77. * @param {(Function|Object|Participant[])} stateOrGetState - The redux state
  78. * features/base/participants, the (whole) redux state, or redux's
  79. * {@code getState} function to be used to retrieve the
  80. * features/base/participants state.
  81. * @param {string} id - The ID of the participant to retrieve.
  82. * @private
  83. * @returns {(Participant|undefined)}
  84. */
  85. export function getParticipantById(stateOrGetState, id) {
  86. const participants = _getParticipants(stateOrGetState);
  87. return participants.find(p => p.id === id);
  88. }
  89. /**
  90. * Returns a count of the known participants in the passed in redux state,
  91. * excluding any fake participants.
  92. *
  93. * @param {(Function|Object|Participant[])} stateOrGetState - The redux state
  94. * features/base/participants, the (whole) redux state, or redux's
  95. * {@code getState} function to be used to retrieve the
  96. * features/base/participants state.
  97. * @returns {number}
  98. */
  99. export function getParticipantCount(stateOrGetState) {
  100. const participants = _getParticipants(stateOrGetState);
  101. const realParticipants = participants.filter(p => !p.isBot);
  102. return realParticipants.length;
  103. }
  104. /**
  105. * Returns the participant which has its pinned state set to truthy.
  106. *
  107. * @param {(Function|Object|Participant[])} stateOrGetState - The redux state
  108. * features/base/participants, the (whole) redux state, or redux's
  109. * {@code getState} function to be used to retrieve the
  110. * features/base/participants state.
  111. * @returns {(Participant|undefined)}
  112. */
  113. export function getPinnedParticipant(stateOrGetState) {
  114. const participants = _getParticipants(stateOrGetState);
  115. return participants.find(p => p.pinned);
  116. }
  117. /**
  118. * Returns array of participants from Redux state.
  119. *
  120. * @param {(Function|Object|Participant[])} stateOrGetState - The redux state
  121. * features/base/participants, the (whole) redux state, or redux's
  122. * {@code getState} function to be used to retrieve the
  123. * features/base/participants state.
  124. * @private
  125. * @returns {Participant[]}
  126. */
  127. function _getParticipants(stateOrGetState) {
  128. if (Array.isArray(stateOrGetState)) {
  129. return stateOrGetState;
  130. }
  131. const state
  132. = typeof stateOrGetState === 'function'
  133. ? stateOrGetState()
  134. : stateOrGetState;
  135. return state['features/base/participants'] || [];
  136. }