Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

functions.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // @flow
  2. import md5 from 'js-md5';
  3. import { toState } from '../redux';
  4. import { DEFAULT_AVATAR_RELATIVE_PATH } from './constants';
  5. declare var config: Object;
  6. declare var interfaceConfig: 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.hex(key.trim().toLowerCase()) + urlSuffix;
  66. }
  67. /**
  68. * Returns local participant from Redux state.
  69. *
  70. * @param {(Function|Object|Participant[])} stateful - The redux state
  71. * features/base/participants, the (whole) redux state, or redux's
  72. * {@code getState} function to be used to retrieve the state
  73. * features/base/participants.
  74. * @returns {(Participant|undefined)}
  75. */
  76. export function getLocalParticipant(stateful: Object | Function) {
  77. const participants = _getAllParticipants(stateful);
  78. return participants.find(p => p.local);
  79. }
  80. /**
  81. * Returns participant by ID from Redux state.
  82. *
  83. * @param {(Function|Object|Participant[])} stateful - The redux state
  84. * features/base/participants, the (whole) redux state, or redux's
  85. * {@code getState} function to be used to retrieve the state
  86. * features/base/participants.
  87. * @param {string} id - The ID of the participant to retrieve.
  88. * @private
  89. * @returns {(Participant|undefined)}
  90. */
  91. export function getParticipantById(
  92. stateful: Object | Function,
  93. id: string) {
  94. const participants = _getAllParticipants(stateful);
  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[])} stateful - The redux state
  102. * features/base/participants, the (whole) redux state, or redux's
  103. * {@code getState} function to be used to retrieve the state
  104. * features/base/participants.
  105. * @returns {number}
  106. */
  107. export function getParticipantCount(stateful: Object | Function) {
  108. return getParticipants(stateful).length;
  109. }
  110. /**
  111. * Returns participant's display name.
  112. * FIXME: remove the hardcoded strings once interfaceConfig is stored in redux
  113. * and merge with a similarly named method in conference.js.
  114. *
  115. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  116. * {@code getState} function to be used to retrieve the state.
  117. * @param {string} id - The ID of the participant's display name to retrieve.
  118. * @private
  119. * @returns {string}
  120. */
  121. export function getParticipantDisplayName(
  122. stateful: Object | Function, id: string) {
  123. const participant = getParticipantById(stateful, id);
  124. if (participant) {
  125. if (participant.name) {
  126. return participant.name;
  127. }
  128. if (participant.local) {
  129. return typeof interfaceConfig === 'object'
  130. ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME
  131. : 'me';
  132. }
  133. }
  134. return typeof interfaceConfig === 'object'
  135. ? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME
  136. : 'Fellow Jitster';
  137. }
  138. /**
  139. * Selectors for getting all known participants with fake participants filtered
  140. * out.
  141. *
  142. * @param {(Function|Object|Participant[])} stateful - The redux state
  143. * features/base/participants, the (whole) redux state, or redux's
  144. * {@code getState} function to be used to retrieve the state
  145. * features/base/participants.
  146. * @returns {Participant[]}
  147. */
  148. export function getParticipants(stateful: Object | Function) {
  149. return _getAllParticipants(stateful).filter(p => !p.isBot);
  150. }
  151. /**
  152. * Returns the participant which has its pinned state set to truthy.
  153. *
  154. * @param {(Function|Object|Participant[])} stateful - The redux state
  155. * features/base/participants, the (whole) redux state, or redux's
  156. * {@code getState} function to be used to retrieve the state
  157. * features/base/participants.
  158. * @returns {(Participant|undefined)}
  159. */
  160. export function getPinnedParticipant(stateful: Object | Function) {
  161. return _getAllParticipants(stateful).find(p => p.pinned);
  162. }
  163. /**
  164. * Returns array of participants from Redux state.
  165. *
  166. * @param {(Function|Object|Participant[])} stateful - The redux state
  167. * features/base/participants, the (whole) redux state, or redux's
  168. * {@code getState} function to be used to retrieve the state
  169. * features/base/participants.
  170. * @private
  171. * @returns {Participant[]}
  172. */
  173. function _getAllParticipants(stateful) {
  174. return (
  175. Array.isArray(stateful)
  176. ? stateful
  177. : toState(stateful)['features/base/participants'] || []);
  178. }