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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { DEFAULT_AVATAR_RELATIVE_PATH } from './constants';
  2. declare var config: Object;
  3. declare var interfaceConfig: Object;
  4. declare var MD5: Object;
  5. /**
  6. * Returns the URL of the image for the avatar of a specific participant.
  7. *
  8. * @param {Participant} [participant] - The participant to return the avatar URL
  9. * of.
  10. * @param {string} [participant.avatarID] - Participant's avatar ID.
  11. * @param {string} [participant.avatarURL] - Participant's avatar URL.
  12. * @param {string} [participant.email] - Participant's e-mail address.
  13. * @param {string} [participant.id] - Participant's ID.
  14. * @returns {string} The URL of the image for the avatar of the specified
  15. * participant.
  16. *
  17. * @public
  18. */
  19. export function getAvatarURL(participant) {
  20. // If disableThirdPartyRequests disables third-party avatar services, we are
  21. // restricted to a stock image of ours.
  22. if (typeof config === 'object' && config.disableThirdPartyRequests) {
  23. return DEFAULT_AVATAR_RELATIVE_PATH;
  24. }
  25. const { avatarID, avatarURL, email, id } = participant;
  26. // If an avatarURL is specified, then obviously there's nothing to generate.
  27. if (avatarURL) {
  28. return avatarURL;
  29. }
  30. let key = email || avatarID;
  31. let urlPrefix;
  32. let urlSuffix;
  33. // If the ID looks like an e-mail address, we'll use Gravatar because it
  34. // supports e-mail addresses.
  35. if (key && key.indexOf('@') > 0) {
  36. urlPrefix = 'https://www.gravatar.com/avatar/';
  37. urlSuffix = '?d=wavatar&size=200';
  38. } else {
  39. // Otherwise, we do not have much a choice but a random avatar (fetched
  40. // from a configured avatar service).
  41. if (!key) {
  42. key = id;
  43. if (!key) {
  44. return undefined;
  45. }
  46. }
  47. // The deployment is allowed to choose the avatar service which is to
  48. // generate the random avatars.
  49. urlPrefix
  50. = typeof interfaceConfig === 'object'
  51. && interfaceConfig.RANDOM_AVATAR_URL_PREFIX;
  52. if (urlPrefix) {
  53. urlSuffix = interfaceConfig.RANDOM_AVATAR_URL_SUFFIX;
  54. } else {
  55. // Otherwise, use a default (meeples, of course).
  56. urlPrefix = 'https://abotars.jitsi.net/meeple/';
  57. urlSuffix = '';
  58. }
  59. }
  60. return urlPrefix + MD5.hexdigest(key.trim().toLowerCase()) + urlSuffix;
  61. }
  62. /**
  63. * Returns local participant from Redux state.
  64. *
  65. * @param {(Function|Object|Participant[])} stateOrGetState - The redux state
  66. * features/base/participants, the (whole) redux state, or redux's
  67. * {@code getState} function to be used to retrieve the
  68. * features/base/participants state.
  69. * @returns {(Participant|undefined)}
  70. */
  71. export function getLocalParticipant(stateOrGetState) {
  72. const participants = _getParticipants(stateOrGetState);
  73. return participants.find(p => p.local);
  74. }
  75. /**
  76. * Returns participant by ID from Redux state.
  77. *
  78. * @param {(Function|Object|Participant[])} stateOrGetState - The redux state
  79. * features/base/participants, the (whole) redux state, or redux's
  80. * {@code getState} function to be used to retrieve the
  81. * features/base/participants state.
  82. * @param {string} id - The ID of the participant to retrieve.
  83. * @private
  84. * @returns {(Participant|undefined)}
  85. */
  86. export function getParticipantById(stateOrGetState, id) {
  87. const participants = _getParticipants(stateOrGetState);
  88. return participants.find(p => p.id === id);
  89. }
  90. /**
  91. * Returns a count of the known participants in the passed in redux state,
  92. * excluding any fake participants.
  93. *
  94. * @param {(Function|Object|Participant[])} stateOrGetState - The redux state
  95. * features/base/participants, the (whole) redux state, or redux's
  96. * {@code getState} function to be used to retrieve the
  97. * features/base/participants state.
  98. * @returns {number}
  99. */
  100. export function getParticipantCount(stateOrGetState) {
  101. const participants = _getParticipants(stateOrGetState);
  102. const realParticipants = participants.filter(p => !p.isBot);
  103. return realParticipants.length;
  104. }
  105. /**
  106. * Returns the participant which has its pinned state set to truthy.
  107. *
  108. * @param {(Function|Object|Participant[])} stateOrGetState - The redux state
  109. * features/base/participants, the (whole) redux state, or redux's
  110. * {@code getState} function to be used to retrieve the
  111. * features/base/participants state.
  112. * @returns {(Participant|undefined)}
  113. */
  114. export function getPinnedParticipant(stateOrGetState) {
  115. const participants = _getParticipants(stateOrGetState);
  116. return participants.find(p => p.pinned);
  117. }
  118. /**
  119. * Returns array of participants from Redux state.
  120. *
  121. * @param {(Function|Object|Participant[])} stateOrGetState - The redux state
  122. * features/base/participants, the (whole) redux state, or redux's
  123. * {@code getState} function to be used to retrieve the
  124. * features/base/participants state.
  125. * @private
  126. * @returns {Participant[]}
  127. */
  128. function _getParticipants(stateOrGetState) {
  129. if (Array.isArray(stateOrGetState)) {
  130. return stateOrGetState;
  131. }
  132. const state
  133. = typeof stateOrGetState === 'function'
  134. ? stateOrGetState()
  135. : stateOrGetState;
  136. return state['features/base/participants'] || [];
  137. }