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

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