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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 the properties for the "Sounds" tab from settings dialog from Redux
  139. * state.
  140. *
  141. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  142. * {@code getState} function to be used to retrieve the state.
  143. * @returns {Object} - The properties for the "Sounds" tab from settings
  144. * dialog.
  145. */
  146. export function getSoundsTabProps(stateful: Object | Function) {
  147. const state = toState(stateful);
  148. const {
  149. soundsIncomingMessage,
  150. soundsParticipantJoined,
  151. soundsParticipantLeft,
  152. soundsTalkWhileMuted,
  153. soundsReactions
  154. } = state['features/base/settings'];
  155. const { enableReactions } = state['features/base/config'];
  156. return {
  157. soundsIncomingMessage,
  158. soundsParticipantJoined,
  159. soundsParticipantLeft,
  160. soundsTalkWhileMuted,
  161. soundsReactions,
  162. enableReactions
  163. };
  164. }
  165. /**
  166. * Returns a promise which resolves with a list of objects containing
  167. * all the video jitsiTracks and appropriate errors for the given device ids.
  168. *
  169. * @param {string[]} ids - The list of the camera ids for which to create tracks.
  170. * @param {number} [timeout] - A timeout for the createLocalTrack function call.
  171. *
  172. * @returns {Promise<Object[]>}
  173. */
  174. export function createLocalVideoTracks(ids: string[], timeout: ?number) {
  175. return Promise.all(ids.map(deviceId => createLocalTrack('video', deviceId, timeout)
  176. .then(jitsiTrack => {
  177. return {
  178. jitsiTrack,
  179. deviceId
  180. };
  181. })
  182. .catch(() => {
  183. return {
  184. jitsiTrack: null,
  185. deviceId,
  186. error: 'deviceSelection.previewUnavailable'
  187. };
  188. })));
  189. }
  190. /**
  191. * Returns a promise which resolves with a list of objects containing
  192. * the audio track and the corresponding audio device information.
  193. *
  194. * @param {Object[]} devices - A list of microphone devices.
  195. * @param {number} [timeout] - A timeout for the createLocalTrack function call.
  196. * @returns {Promise<{
  197. * deviceId: string,
  198. * hasError: boolean,
  199. * jitsiTrack: Object,
  200. * label: string
  201. * }[]>}
  202. */
  203. export function createLocalAudioTracks(devices: Object[], timeout: ?number) {
  204. return Promise.all(
  205. devices.map(async ({ deviceId, label }) => {
  206. let jitsiTrack = null;
  207. let hasError = false;
  208. try {
  209. jitsiTrack = await createLocalTrack('audio', deviceId, timeout);
  210. } catch (err) {
  211. hasError = true;
  212. }
  213. return {
  214. deviceId,
  215. hasError,
  216. jitsiTrack,
  217. label
  218. };
  219. }));
  220. }
  221. /**
  222. * Returns the visibility state of the audio settings.
  223. *
  224. * @param {Object} state - The state of the application.
  225. * @returns {boolean}
  226. */
  227. export function getAudioSettingsVisibility(state: Object) {
  228. return state['features/settings'].audioSettingsVisible;
  229. }
  230. /**
  231. * Returns the visibility state of the video settings.
  232. *
  233. * @param {Object} state - The state of the application.
  234. * @returns {boolean}
  235. */
  236. export function getVideoSettingsVisibility(state: Object) {
  237. return state['features/settings'].videoSettingsVisible;
  238. }