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

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