您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.js 9.0KB

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