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

functions.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // @flow
  2. import { SERVER_URL_CHANGE_ENABLED, getFeatureFlag } from '../base/flags';
  3. import { i18next, DEFAULT_LANGUAGE, LANGUAGES } from '../base/i18n';
  4. import { createLocalTrack } from '../base/lib-jitsi-meet/functions';
  5. import {
  6. getLocalParticipant,
  7. isLocalParticipantModerator
  8. } from '../base/participants';
  9. import { toState } from '../base/redux';
  10. import { parseStandardURIString } from '../base/util';
  11. import { isFollowMeActive } from '../follow-me';
  12. import { SS_DEFAULT_FRAME_RATE, SS_SUPPORTED_FRAMERATES } from './constants';
  13. declare var interfaceConfig: Object;
  14. /**
  15. * Used for web. Indicates if the setting section is enabled.
  16. *
  17. * @param {string} settingName - The name of the setting section as defined in
  18. * interface_config.js and SettingsMenu.js.
  19. * @returns {boolean} True to indicate that the given setting section
  20. * is enabled, false otherwise.
  21. */
  22. export function isSettingEnabled(settingName: string) {
  23. return interfaceConfig.SETTINGS_SECTIONS.includes(settingName);
  24. }
  25. /**
  26. * Returns true if user is allowed to change Server URL.
  27. *
  28. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  29. * {@code getState} function to be used to retrieve the state.
  30. * @returns {boolean} True to indicate that user can change Server URL, false otherwise.
  31. */
  32. export function isServerURLChangeEnabled(stateful: Object | Function) {
  33. const state = toState(stateful);
  34. const flag = getFeatureFlag(state, SERVER_URL_CHANGE_ENABLED, true);
  35. return flag;
  36. }
  37. /**
  38. * Normalizes a URL entered by the user.
  39. * FIXME: Consider adding this to base/util/uri.
  40. *
  41. * @param {string} url - The URL to validate.
  42. * @returns {string|null} - The normalized URL, or null if the URL is invalid.
  43. */
  44. export function normalizeUserInputURL(url: string) {
  45. /* eslint-disable no-param-reassign */
  46. if (url) {
  47. url = url.replace(/\s/g, '').toLowerCase();
  48. const urlRegExp = new RegExp('^(\\w+://)?(.+)$');
  49. const urlComponents = urlRegExp.exec(url);
  50. if (urlComponents && (!urlComponents[1]
  51. || !urlComponents[1].startsWith('http'))) {
  52. url = `https://${urlComponents[2]}`;
  53. }
  54. const parsedURI = parseStandardURIString(url);
  55. if (!parsedURI.host) {
  56. return null;
  57. }
  58. return parsedURI.toString();
  59. }
  60. return url;
  61. /* eslint-enable no-param-reassign */
  62. }
  63. /**
  64. * Used for web. Returns whether or not only Device Selection is configured to
  65. * display as a setting.
  66. *
  67. * @returns {boolean}
  68. */
  69. export function shouldShowOnlyDeviceSelection() {
  70. return interfaceConfig.SETTINGS_SECTIONS.length === 1
  71. && isSettingEnabled('devices');
  72. }
  73. /**
  74. * Returns the properties for the "More" tab from settings dialog from Redux
  75. * state.
  76. *
  77. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  78. * {@code getState} function to be used to retrieve the state.
  79. * @returns {Object} - The properties for the "More" tab from settings dialog.
  80. */
  81. export function getMoreTabProps(stateful: Object | Function) {
  82. const state = toState(stateful);
  83. const framerate = state['features/screen-share'].captureFrameRate ?? SS_DEFAULT_FRAME_RATE;
  84. const language = i18next.language || DEFAULT_LANGUAGE;
  85. const {
  86. conference,
  87. followMeEnabled,
  88. startAudioMutedPolicy,
  89. startVideoMutedPolicy
  90. } = state['features/base/conference'];
  91. const followMeActive = isFollowMeActive(state);
  92. const configuredTabs = interfaceConfig.SETTINGS_SECTIONS || [];
  93. // The settings sections to display.
  94. const showModeratorSettings = Boolean(
  95. conference
  96. && configuredTabs.includes('moderator')
  97. && isLocalParticipantModerator(state));
  98. return {
  99. currentFramerate: framerate,
  100. currentLanguage: language,
  101. desktopShareFramerates: SS_SUPPORTED_FRAMERATES,
  102. followMeActive: Boolean(conference && followMeActive),
  103. followMeEnabled: Boolean(conference && followMeEnabled),
  104. languages: LANGUAGES,
  105. showLanguageSettings: configuredTabs.includes('language'),
  106. showModeratorSettings,
  107. showPrejoinSettings: state['features/base/config'].prejoinPageEnabled,
  108. showPrejoinPage: !state['features/base/settings'].userSelectedSkipPrejoin,
  109. startAudioMuted: Boolean(conference && startAudioMutedPolicy),
  110. startVideoMuted: Boolean(conference && startVideoMutedPolicy)
  111. };
  112. }
  113. /**
  114. * Returns the properties for the "Profile" tab from settings dialog from Redux
  115. * state.
  116. *
  117. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  118. * {@code getState} function to be used to retrieve the state.
  119. * @returns {Object} - The properties for the "Profile" tab from settings
  120. * dialog.
  121. */
  122. export function getProfileTabProps(stateful: Object | Function) {
  123. const state = toState(stateful);
  124. const {
  125. authEnabled,
  126. authLogin,
  127. conference
  128. } = state['features/base/conference'];
  129. const localParticipant = getLocalParticipant(state);
  130. return {
  131. authEnabled: Boolean(conference && authEnabled),
  132. authLogin,
  133. displayName: localParticipant.name,
  134. email: localParticipant.email
  135. };
  136. }
  137. /**
  138. * Returns a promise which resolves with a list of objects containing
  139. * all the video jitsiTracks and appropriate errors for the given device ids.
  140. *
  141. * @param {string[]} ids - The list of the camera ids for which to create tracks.
  142. * @param {number} [timeout] - A timeout for the createLocalTrack function call.
  143. *
  144. * @returns {Promise<Object[]>}
  145. */
  146. export function createLocalVideoTracks(ids: string[], timeout: ?number) {
  147. return Promise.all(ids.map(deviceId => createLocalTrack('video', deviceId, timeout)
  148. .then(jitsiTrack => {
  149. return {
  150. jitsiTrack,
  151. deviceId
  152. };
  153. })
  154. .catch(() => {
  155. return {
  156. jitsiTrack: null,
  157. deviceId,
  158. error: 'deviceSelection.previewUnavailable'
  159. };
  160. })));
  161. }
  162. /**
  163. * Returns a promise which resolves with a list of objects containing
  164. * the audio track and the corresponding audio device information.
  165. *
  166. * @param {Object[]} devices - A list of microphone devices.
  167. * @param {number} [timeout] - A timeout for the createLocalTrack function call.
  168. * @returns {Promise<{
  169. * deviceId: string,
  170. * hasError: boolean,
  171. * jitsiTrack: Object,
  172. * label: string
  173. * }[]>}
  174. */
  175. export function createLocalAudioTracks(devices: Object[], timeout: ?number) {
  176. return Promise.all(
  177. devices.map(async ({ deviceId, label }) => {
  178. let jitsiTrack = null;
  179. let hasError = false;
  180. try {
  181. jitsiTrack = await createLocalTrack('audio', deviceId, timeout);
  182. } catch (err) {
  183. hasError = true;
  184. }
  185. return {
  186. deviceId,
  187. hasError,
  188. jitsiTrack,
  189. label
  190. };
  191. }));
  192. }
  193. /**
  194. * Returns the visibility state of the audio settings.
  195. *
  196. * @param {Object} state - The state of the application.
  197. * @returns {boolean}
  198. */
  199. export function getAudioSettingsVisibility(state: Object) {
  200. return state['features/settings'].audioSettingsVisible;
  201. }
  202. /**
  203. * Returns the visibility state of the video settings.
  204. *
  205. * @param {Object} state - The state of the application.
  206. * @returns {boolean}
  207. */
  208. export function getVideoSettingsVisibility(state: Object) {
  209. return state['features/settings'].videoSettingsVisible;
  210. }