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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // @flow
  2. import md5 from 'js-md5';
  3. import { toState } from '../redux';
  4. import {
  5. DEFAULT_AVATAR_RELATIVE_PATH,
  6. LOCAL_PARTICIPANT_DEFAULT_ID
  7. } from './constants';
  8. declare var config: Object;
  9. declare var interfaceConfig: Object;
  10. /**
  11. * Returns the URL of the image for the avatar of a specific participant.
  12. *
  13. * @param {Participant} [participant] - The participant to return the avatar URL
  14. * of.
  15. * @param {string} [participant.avatarID] - Participant's avatar ID.
  16. * @param {string} [participant.avatarURL] - Participant's avatar URL.
  17. * @param {string} [participant.email] - Participant's e-mail address.
  18. * @param {string} [participant.id] - Participant's ID.
  19. * @public
  20. * @returns {string} The URL of the image for the avatar of the specified
  21. * participant.
  22. */
  23. export function getAvatarURL({ avatarID, avatarURL, email, id }: {
  24. avatarID: string,
  25. avatarURL: string,
  26. email: string,
  27. id: string
  28. }) {
  29. // If disableThirdPartyRequests disables third-party avatar services, we are
  30. // restricted to a stock image of ours.
  31. if (typeof config === 'object' && config.disableThirdPartyRequests) {
  32. return DEFAULT_AVATAR_RELATIVE_PATH;
  33. }
  34. // If an avatarURL is specified, then obviously there's nothing to generate.
  35. if (avatarURL) {
  36. return avatarURL;
  37. }
  38. let key = email || avatarID;
  39. let urlPrefix;
  40. let urlSuffix;
  41. // If the ID looks like an e-mail address, we'll use Gravatar because it
  42. // supports e-mail addresses.
  43. if (key && key.indexOf('@') > 0) {
  44. urlPrefix = 'https://www.gravatar.com/avatar/';
  45. urlSuffix = '?d=wavatar&size=200';
  46. } else {
  47. // Otherwise, we do not have much a choice but a random avatar (fetched
  48. // from a configured avatar service).
  49. if (!key) {
  50. key = id;
  51. if (!key) {
  52. return undefined;
  53. }
  54. }
  55. // The deployment is allowed to choose the avatar service which is to
  56. // generate the random avatars.
  57. urlPrefix
  58. = typeof interfaceConfig === 'object'
  59. && interfaceConfig.RANDOM_AVATAR_URL_PREFIX;
  60. if (urlPrefix) {
  61. urlSuffix = interfaceConfig.RANDOM_AVATAR_URL_SUFFIX;
  62. } else {
  63. // Otherwise, use a default (meeples, of course).
  64. urlPrefix = 'https://abotars.jitsi.net/meeple/';
  65. urlSuffix = '';
  66. }
  67. }
  68. return urlPrefix + md5.hex(key.trim().toLowerCase()) + urlSuffix;
  69. }
  70. /**
  71. * Returns the avatarURL for the participant associated with the passed in
  72. * participant ID.
  73. *
  74. * @param {(Function|Object|Participant[])} stateful - The redux state
  75. * features/base/participants, the (whole) redux state, or redux's
  76. * {@code getState} function to be used to retrieve the state
  77. * features/base/participants.
  78. * @param {string} id - The ID of the participant to retrieve.
  79. * @param {boolean} isLocal - An optional parameter indicating whether or not
  80. * the partcipant id is for the local user. If true, a different logic flow is
  81. * used find the local user, ignoring the id value as it can change through the
  82. * beginning and end of a call.
  83. * @returns {(string|undefined)}
  84. */
  85. export function getAvatarURLByParticipantId(
  86. stateful: Object | Function,
  87. id: string = LOCAL_PARTICIPANT_DEFAULT_ID) {
  88. const participant = getParticipantById(stateful, id);
  89. return participant && getAvatarURL(participant);
  90. }
  91. /**
  92. * Returns local participant from Redux state.
  93. *
  94. * @param {(Function|Object|Participant[])} stateful - The redux state
  95. * features/base/participants, the (whole) redux state, or redux's
  96. * {@code getState} function to be used to retrieve the state
  97. * features/base/participants.
  98. * @returns {(Participant|undefined)}
  99. */
  100. export function getLocalParticipant(stateful: Object | Function) {
  101. const participants = _getAllParticipants(stateful);
  102. return participants.find(p => p.local);
  103. }
  104. /**
  105. * Returns participant by ID from Redux state.
  106. *
  107. * @param {(Function|Object|Participant[])} stateful - The redux state
  108. * features/base/participants, the (whole) redux state, or redux's
  109. * {@code getState} function to be used to retrieve the state
  110. * features/base/participants.
  111. * @param {string} id - The ID of the participant to retrieve.
  112. * @private
  113. * @returns {(Participant|undefined)}
  114. */
  115. export function getParticipantById(
  116. stateful: Object | Function,
  117. id: string) {
  118. const participants = _getAllParticipants(stateful);
  119. return participants.find(p => p.id === id);
  120. }
  121. /**
  122. * Returns a count of the known participants in the passed in redux state,
  123. * excluding any fake participants.
  124. *
  125. * @param {(Function|Object|Participant[])} stateful - The redux state
  126. * features/base/participants, the (whole) redux state, or redux's
  127. * {@code getState} function to be used to retrieve the state
  128. * features/base/participants.
  129. * @returns {number}
  130. */
  131. export function getParticipantCount(stateful: Object | Function) {
  132. return getParticipants(stateful).length;
  133. }
  134. /**
  135. * Returns participant's display name.
  136. * FIXME: remove the hardcoded strings once interfaceConfig is stored in redux
  137. * and merge with a similarly named method in conference.js.
  138. *
  139. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  140. * {@code getState} function to be used to retrieve the state.
  141. * @param {string} id - The ID of the participant's display name to retrieve.
  142. * @private
  143. * @returns {string}
  144. */
  145. export function getParticipantDisplayName(
  146. stateful: Object | Function,
  147. id: string) {
  148. const participant = getParticipantById(stateful, id);
  149. if (participant) {
  150. if (participant.name) {
  151. return participant.name;
  152. }
  153. if (participant.local) {
  154. return typeof interfaceConfig === 'object'
  155. ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME
  156. : 'me';
  157. }
  158. }
  159. return typeof interfaceConfig === 'object'
  160. ? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME
  161. : 'Fellow Jitster';
  162. }
  163. /**
  164. * Selectors for getting all known participants with fake participants filtered
  165. * out.
  166. *
  167. * @param {(Function|Object|Participant[])} stateful - The redux state
  168. * features/base/participants, the (whole) redux state, or redux's
  169. * {@code getState} function to be used to retrieve the state
  170. * features/base/participants.
  171. * @returns {Participant[]}
  172. */
  173. export function getParticipants(stateful: Object | Function) {
  174. return _getAllParticipants(stateful).filter(p => !p.isBot);
  175. }
  176. /**
  177. * Returns the participant which has its pinned state set to truthy.
  178. *
  179. * @param {(Function|Object|Participant[])} stateful - The redux state
  180. * features/base/participants, the (whole) redux state, or redux's
  181. * {@code getState} function to be used to retrieve the state
  182. * features/base/participants.
  183. * @returns {(Participant|undefined)}
  184. */
  185. export function getPinnedParticipant(stateful: Object | Function) {
  186. return _getAllParticipants(stateful).find(p => p.pinned);
  187. }
  188. /**
  189. * Returns array of participants from Redux state.
  190. *
  191. * @param {(Function|Object|Participant[])} stateful - The redux state
  192. * features/base/participants, the (whole) redux state, or redux's
  193. * {@code getState} function to be used to retrieve the state
  194. * features/base/participants.
  195. * @private
  196. * @returns {Participant[]}
  197. */
  198. function _getAllParticipants(stateful) {
  199. return (
  200. Array.isArray(stateful)
  201. ? stateful
  202. : toState(stateful)['features/base/participants'] || []);
  203. }