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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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(stateful: Object | Function, id: string) {
  117. const participants = _getAllParticipants(stateful);
  118. return participants.find(p => p.id === id);
  119. }
  120. /**
  121. * Returns a count of the known participants in the passed in redux state,
  122. * excluding any fake participants.
  123. *
  124. * @param {(Function|Object|Participant[])} stateful - The redux state
  125. * features/base/participants, the (whole) redux state, or redux's
  126. * {@code getState} function to be used to retrieve the state
  127. * features/base/participants.
  128. * @returns {number}
  129. */
  130. export function getParticipantCount(stateful: Object | Function) {
  131. return getParticipants(stateful).length;
  132. }
  133. /**
  134. * Returns participant's display name.
  135. * FIXME: remove the hardcoded strings once interfaceConfig is stored in redux
  136. * and merge with a similarly named method in conference.js.
  137. *
  138. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  139. * {@code getState} function to be used to retrieve the state.
  140. * @param {string} id - The ID of the participant's display name to retrieve.
  141. * @private
  142. * @returns {string}
  143. */
  144. export function getParticipantDisplayName(
  145. stateful: Object | Function,
  146. id: string) {
  147. const participant = getParticipantById(stateful, id);
  148. if (participant) {
  149. if (participant.name) {
  150. return participant.name;
  151. }
  152. if (participant.local) {
  153. return typeof interfaceConfig === 'object'
  154. ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME
  155. : 'me';
  156. }
  157. }
  158. return typeof interfaceConfig === 'object'
  159. ? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME
  160. : 'Fellow Jitster';
  161. }
  162. /**
  163. * Returns the presence status of a participant associated with the passed id.
  164. *
  165. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  166. * {@code getState} function to be used to retrieve the state.
  167. * @param {string} id - The id of the participant.
  168. * @returns {string} - The presence status.
  169. */
  170. export function getParticipantPresenceStatus(
  171. stateful: Object | Function, id: string) {
  172. if (!id) {
  173. return undefined;
  174. }
  175. const participantById = getParticipantById(stateful, id);
  176. if (!participantById) {
  177. return undefined;
  178. }
  179. return participantById.presence;
  180. }
  181. /**
  182. * Selectors for getting all known participants with fake participants filtered
  183. * out.
  184. *
  185. * @param {(Function|Object|Participant[])} stateful - The redux state
  186. * features/base/participants, the (whole) redux state, or redux's
  187. * {@code getState} function to be used to retrieve the state
  188. * features/base/participants.
  189. * @returns {Participant[]}
  190. */
  191. export function getParticipants(stateful: Object | Function) {
  192. return _getAllParticipants(stateful).filter(p => !p.isBot);
  193. }
  194. /**
  195. * Returns the participant which has its pinned state set to truthy.
  196. *
  197. * @param {(Function|Object|Participant[])} stateful - The redux state
  198. * features/base/participants, the (whole) redux state, or redux's
  199. * {@code getState} function to be used to retrieve the state
  200. * features/base/participants.
  201. * @returns {(Participant|undefined)}
  202. */
  203. export function getPinnedParticipant(stateful: Object | Function) {
  204. return _getAllParticipants(stateful).find(p => p.pinned);
  205. }
  206. /**
  207. * Returns array of participants from Redux state.
  208. *
  209. * @param {(Function|Object|Participant[])} stateful - The redux state
  210. * features/base/participants, the (whole) redux state, or redux's
  211. * {@code getState} function to be used to retrieve the state
  212. * features/base/participants.
  213. * @private
  214. * @returns {Participant[]}
  215. */
  216. function _getAllParticipants(stateful) {
  217. return (
  218. Array.isArray(stateful)
  219. ? stateful
  220. : toState(stateful)['features/base/participants'] || []);
  221. }
  222. /**
  223. * Returns true if the current local participant is a moderator in the
  224. * conference.
  225. *
  226. * @param {Object|Function} stateful - Object or function that can be resolved
  227. * to the Redux state.
  228. * @returns {boolean}
  229. */
  230. export function isLocalParticipantModerator(stateful: Object | Function) {
  231. const state = toState(stateful);
  232. const localParticipant = getLocalParticipant(state);
  233. if (!localParticipant) {
  234. return false;
  235. }
  236. return (
  237. localParticipant.role === PARTICIPANT_ROLE.MODERATOR
  238. && (!state['features/base/config'].enableUserRolesBasedOnToken
  239. || !state['features/base/jwt'].isGuest));
  240. }