Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

functions.js 8.6KB

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