Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

functions.js 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // @flow
  2. import { getAvatarURL as _getAvatarURL } from 'js-utils/avatar';
  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. // The deployment is allowed to choose the avatar service which is to
  40. // generate the random avatars.
  41. const avatarService
  42. = typeof interfaceConfig === 'object'
  43. && interfaceConfig.RANDOM_AVATAR_URL_PREFIX
  44. ? {
  45. urlPrefix: interfaceConfig.RANDOM_AVATAR_URL_PREFIX,
  46. urlSuffix: interfaceConfig.RANDOM_AVATAR_URL_SUFFIX }
  47. : undefined;
  48. // eslint-disable-next-line object-property-newline
  49. return _getAvatarURL({ avatarID, email, id }, avatarService);
  50. }
  51. /**
  52. * Returns the avatarURL for the participant associated with the passed in
  53. * participant ID.
  54. *
  55. * @param {(Function|Object|Participant[])} stateful - The redux state
  56. * features/base/participants, the (whole) redux state, or redux's
  57. * {@code getState} function to be used to retrieve the state
  58. * features/base/participants.
  59. * @param {string} id - The ID of the participant to retrieve.
  60. * @param {boolean} isLocal - An optional parameter indicating whether or not
  61. * the partcipant id is for the local user. If true, a different logic flow is
  62. * used find the local user, ignoring the id value as it can change through the
  63. * beginning and end of a call.
  64. * @returns {(string|undefined)}
  65. */
  66. export function getAvatarURLByParticipantId(
  67. stateful: Object | Function,
  68. id: string = LOCAL_PARTICIPANT_DEFAULT_ID) {
  69. const participant = getParticipantById(stateful, id);
  70. return participant && getAvatarURL(participant);
  71. }
  72. /**
  73. * Returns local participant from Redux state.
  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. * @returns {(Participant|undefined)}
  80. */
  81. export function getLocalParticipant(stateful: Object | Function) {
  82. const participants = _getAllParticipants(stateful);
  83. return participants.find(p => p.local);
  84. }
  85. /**
  86. * Returns participant by ID from Redux state.
  87. *
  88. * @param {(Function|Object|Participant[])} stateful - The redux state
  89. * features/base/participants, the (whole) redux state, or redux's
  90. * {@code getState} function to be used to retrieve the state
  91. * features/base/participants.
  92. * @param {string} id - The ID of the participant to retrieve.
  93. * @private
  94. * @returns {(Participant|undefined)}
  95. */
  96. export function getParticipantById(stateful: Object | Function, id: string) {
  97. const participants = _getAllParticipants(stateful);
  98. return participants.find(p => p.id === id);
  99. }
  100. /**
  101. * Returns a count of the known participants in the passed in redux state,
  102. * excluding any fake participants.
  103. *
  104. * @param {(Function|Object|Participant[])} stateful - The redux state
  105. * features/base/participants, the (whole) redux state, or redux's
  106. * {@code getState} function to be used to retrieve the state
  107. * features/base/participants.
  108. * @returns {number}
  109. */
  110. export function getParticipantCount(stateful: Object | Function) {
  111. return getParticipants(stateful).length;
  112. }
  113. /**
  114. * Returns participant's display name.
  115. *
  116. * FIXME: Remove the hardcoded strings once interfaceConfig is stored in redux
  117. * and merge with a similarly named method in {@code conference.js}.
  118. *
  119. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  120. * {@code getState} function to be used to retrieve the state.
  121. * @param {string} id - The ID of the participant's display name to retrieve.
  122. * @private
  123. * @returns {string}
  124. */
  125. export function getParticipantDisplayName(
  126. stateful: Object | Function,
  127. id: string) {
  128. const participant = getParticipantById(stateful, id);
  129. if (participant) {
  130. if (participant.name) {
  131. return participant.name;
  132. }
  133. if (participant.local) {
  134. return typeof interfaceConfig === 'object'
  135. ? interfaceConfig.DEFAULT_LOCAL_DISPLAY_NAME
  136. : 'me';
  137. }
  138. }
  139. return typeof interfaceConfig === 'object'
  140. ? interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME
  141. : 'Fellow Jitster';
  142. }
  143. /**
  144. * Returns the presence status of a participant associated with the passed id.
  145. *
  146. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  147. * {@code getState} function to be used to retrieve the state.
  148. * @param {string} id - The id of the participant.
  149. * @returns {string} - The presence status.
  150. */
  151. export function getParticipantPresenceStatus(
  152. stateful: Object | Function, id: string) {
  153. if (!id) {
  154. return undefined;
  155. }
  156. const participantById = getParticipantById(stateful, id);
  157. if (!participantById) {
  158. return undefined;
  159. }
  160. return participantById.presence;
  161. }
  162. /**
  163. * Selectors for getting all known participants with fake participants filtered
  164. * out.
  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. * @returns {Participant[]}
  171. */
  172. export function getParticipants(stateful: Object | Function) {
  173. return _getAllParticipants(stateful).filter(p => !p.isFakeParticipant);
  174. }
  175. /**
  176. * Returns the participant which has its pinned state set to truthy.
  177. *
  178. * @param {(Function|Object|Participant[])} stateful - The redux state
  179. * features/base/participants, the (whole) redux state, or redux's
  180. * {@code getState} function to be used to retrieve the state
  181. * features/base/participants.
  182. * @returns {(Participant|undefined)}
  183. */
  184. export function getPinnedParticipant(stateful: Object | Function) {
  185. return _getAllParticipants(stateful).find(p => p.pinned);
  186. }
  187. /**
  188. * Returns array of participants from Redux state.
  189. *
  190. * @param {(Function|Object|Participant[])} stateful - The redux state
  191. * features/base/participants, the (whole) redux state, or redux's
  192. * {@code getState} function to be used to retrieve the state
  193. * features/base/participants.
  194. * @private
  195. * @returns {Participant[]}
  196. */
  197. function _getAllParticipants(stateful) {
  198. return (
  199. Array.isArray(stateful)
  200. ? stateful
  201. : toState(stateful)['features/base/participants'] || []);
  202. }
  203. /**
  204. * Returns true if the current local participant is a moderator in the
  205. * conference.
  206. *
  207. * @param {Object|Function} stateful - Object or function that can be resolved
  208. * to the Redux state.
  209. * @returns {boolean}
  210. */
  211. export function isLocalParticipantModerator(stateful: Object | Function) {
  212. const state = toState(stateful);
  213. const localParticipant = getLocalParticipant(state);
  214. if (!localParticipant) {
  215. return false;
  216. }
  217. return (
  218. localParticipant.role === PARTICIPANT_ROLE.MODERATOR
  219. && (!state['features/base/config'].enableUserRolesBasedOnToken
  220. || !state['features/base/jwt'].isGuest));
  221. }