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 7.3KB

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