您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.js 5.6KB

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