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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 wich to create tracks.
  138. *
  139. * @returns {Promise<Object[]>}
  140. */
  141. export function createLocalVideoTracks(ids: string[]) {
  142. return Promise.all(ids.map(deviceId => createLocalTrack('video', deviceId)
  143. .then(jitsiTrack => {
  144. return {
  145. jitsiTrack,
  146. deviceId
  147. };
  148. })
  149. .catch(() => {
  150. return {
  151. jitsiTrack: null,
  152. deviceId,
  153. error: 'deviceSelection.previewUnavailable'
  154. };
  155. })));
  156. }
  157. /**
  158. * Returns a promise which resolves with a list of objects containing
  159. * the audio track and the corresponding audio device information.
  160. *
  161. * @param {Object[]} devices - A list of microphone devices.
  162. * @returns {Promise<{
  163. * deviceId: string,
  164. * hasError: boolean,
  165. * jitsiTrack: Object,
  166. * label: string
  167. * }[]>}
  168. */
  169. export function createLocalAudioTracks(devices: Object[]) {
  170. return Promise.all(
  171. devices.map(async ({ deviceId, label }) => {
  172. let jitsiTrack = null;
  173. let hasError = false;
  174. try {
  175. jitsiTrack = await createLocalTrack('audio', deviceId);
  176. } catch (err) {
  177. hasError = true;
  178. }
  179. return {
  180. deviceId,
  181. hasError,
  182. jitsiTrack,
  183. label
  184. };
  185. }));
  186. }
  187. /**
  188. * Returns the visibility state of the audio settings.
  189. *
  190. * @param {Object} state - The state of the application.
  191. * @returns {boolean}
  192. */
  193. export function getAudioSettingsVisibility(state: Object) {
  194. return state['features/settings'].audioSettingsVisible;
  195. }
  196. /**
  197. * Returns the visibility state of the video settings.
  198. *
  199. * @param {Object} state - The state of the application.
  200. * @returns {boolean}
  201. */
  202. export function getVideoSettingsVisibility(state: Object) {
  203. return state['features/settings'].videoSettingsVisible;
  204. }