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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // @flow
  2. import { isNameReadOnly } from '../base/config';
  3. import { SERVER_URL_CHANGE_ENABLED, getFeatureFlag } from '../base/flags';
  4. import { i18next, DEFAULT_LANGUAGE, LANGUAGES } from '../base/i18n';
  5. import { createLocalTrack } from '../base/lib-jitsi-meet/functions';
  6. import {
  7. getLocalParticipant,
  8. isLocalParticipantModerator
  9. } from '../base/participants';
  10. import { toState } from '../base/redux';
  11. import { getHideSelfView } from '../base/settings';
  12. import { parseStandardURIString } from '../base/util';
  13. import { isFollowMeActive } from '../follow-me';
  14. import { isReactionsEnabled } from '../reactions/functions.any';
  15. import { SS_DEFAULT_FRAME_RATE, SS_SUPPORTED_FRAMERATES } from './constants';
  16. declare var interfaceConfig: Object;
  17. /**
  18. * Used for web. Indicates if the setting section is enabled.
  19. *
  20. * @param {string} settingName - The name of the setting section as defined in
  21. * interface_config.js and SettingsMenu.js.
  22. * @returns {boolean} True to indicate that the given setting section
  23. * is enabled, false otherwise.
  24. */
  25. export function isSettingEnabled(settingName: string) {
  26. return interfaceConfig.SETTINGS_SECTIONS.includes(settingName);
  27. }
  28. /**
  29. * Returns true if user is allowed to change Server URL.
  30. *
  31. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  32. * {@code getState} function to be used to retrieve the state.
  33. * @returns {boolean} True to indicate that user can change Server URL, false otherwise.
  34. */
  35. export function isServerURLChangeEnabled(stateful: Object | Function) {
  36. const state = toState(stateful);
  37. const flag = getFeatureFlag(state, SERVER_URL_CHANGE_ENABLED, true);
  38. return flag;
  39. }
  40. /**
  41. * Normalizes a URL entered by the user.
  42. * FIXME: Consider adding this to base/util/uri.
  43. *
  44. * @param {string} url - The URL to validate.
  45. * @returns {string|null} - The normalized URL, or null if the URL is invalid.
  46. */
  47. export function normalizeUserInputURL(url: string) {
  48. /* eslint-disable no-param-reassign */
  49. if (url) {
  50. url = url.replace(/\s/g, '').toLowerCase();
  51. const urlRegExp = new RegExp('^(\\w+://)?(.+)$');
  52. const urlComponents = urlRegExp.exec(url);
  53. if (urlComponents && (!urlComponents[1]
  54. || !urlComponents[1].startsWith('http'))) {
  55. url = `https://${urlComponents[2]}`;
  56. }
  57. const parsedURI = parseStandardURIString(url);
  58. if (!parsedURI.host) {
  59. return null;
  60. }
  61. return parsedURI.toString();
  62. }
  63. return url;
  64. /* eslint-enable no-param-reassign */
  65. }
  66. /**
  67. * Returns the notification types and their user selected configuration.
  68. *
  69. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  70. * {@code getState} function to be used to retrieve the state.
  71. * @returns {Object} - The section of notifications to be configured.
  72. */
  73. export function getNotificationsMap(stateful: Object | Function) {
  74. const state = toState(stateful);
  75. const { notifications } = state['features/base/config'];
  76. const { userSelectedNotifications } = state['features/base/settings'];
  77. return Object.keys(userSelectedNotifications)
  78. .filter(key => !notifications || notifications.includes(key))
  79. .reduce((notificationsMap, key) => {
  80. return {
  81. ...notificationsMap,
  82. [key]: userSelectedNotifications[key]
  83. };
  84. }, {});
  85. }
  86. /**
  87. * Returns the properties for the "More" tab from settings dialog from Redux
  88. * state.
  89. *
  90. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  91. * {@code getState} function to be used to retrieve the state.
  92. * @returns {Object} - The properties for the "More" tab from settings dialog.
  93. */
  94. export function getMoreTabProps(stateful: Object | Function) {
  95. const state = toState(stateful);
  96. const framerate = state['features/screen-share'].captureFrameRate ?? SS_DEFAULT_FRAME_RATE;
  97. const language = i18next.language || DEFAULT_LANGUAGE;
  98. const configuredTabs = interfaceConfig.SETTINGS_SECTIONS || [];
  99. const enabledNotifications = getNotificationsMap(stateful);
  100. // when self view is controlled by the config we hide the settings
  101. const { disableSelfView, disableSelfViewSettings } = state['features/base/config'];
  102. return {
  103. currentFramerate: framerate,
  104. currentLanguage: language,
  105. desktopShareFramerates: SS_SUPPORTED_FRAMERATES,
  106. disableHideSelfView: disableSelfViewSettings || disableSelfView,
  107. hideSelfView: getHideSelfView(state),
  108. languages: LANGUAGES,
  109. showLanguageSettings: configuredTabs.includes('language'),
  110. enabledNotifications,
  111. showNotificationsSettings: Object.keys(enabledNotifications).length > 0,
  112. showPrejoinPage: !state['features/base/settings'].userSelectedSkipPrejoin,
  113. showPrejoinSettings: state['features/base/config'].prejoinConfig?.enabled,
  114. maxStageParticipants: state['features/filmstrip'].maxStageParticipants
  115. };
  116. }
  117. /**
  118. * Returns the properties for the "More" tab from settings dialog from Redux
  119. * state.
  120. *
  121. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  122. * {@code getState} function to be used to retrieve the state.
  123. * @returns {Object} - The properties for the "More" tab from settings dialog.
  124. */
  125. export function getModeratorTabProps(stateful: Object | Function) {
  126. const state = toState(stateful);
  127. const {
  128. conference,
  129. followMeEnabled,
  130. startAudioMutedPolicy,
  131. startVideoMutedPolicy,
  132. startReactionsMuted
  133. } = state['features/base/conference'];
  134. const { disableReactionsModeration } = state['features/base/config'];
  135. const followMeActive = isFollowMeActive(state);
  136. const showModeratorSettings = shouldShowModeratorSettings(state);
  137. // The settings sections to display.
  138. return {
  139. showModeratorSettings: Boolean(conference && showModeratorSettings),
  140. disableReactionsModeration: Boolean(disableReactionsModeration),
  141. followMeActive: Boolean(conference && followMeActive),
  142. followMeEnabled: Boolean(conference && followMeEnabled),
  143. startReactionsMuted: Boolean(conference && startReactionsMuted),
  144. startAudioMuted: Boolean(conference && startAudioMutedPolicy),
  145. startVideoMuted: Boolean(conference && startVideoMutedPolicy)
  146. };
  147. }
  148. /**
  149. * Returns true if moderator tab in settings should be visible/accessible.
  150. *
  151. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  152. * {@code getState} function to be used to retrieve the state.
  153. * @returns {boolean} True to indicate that moderator tab should be visible, false otherwise.
  154. */
  155. export function shouldShowModeratorSettings(stateful: Object | Function) {
  156. const state = toState(stateful);
  157. return Boolean(
  158. isSettingEnabled('moderator')
  159. && isLocalParticipantModerator(state));
  160. }
  161. /**
  162. * Returns the properties for the "Profile" tab from settings dialog from Redux
  163. * state.
  164. *
  165. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  166. * {@code getState} function to be used to retrieve the state.
  167. * @returns {Object} - The properties for the "Profile" tab from settings
  168. * dialog.
  169. */
  170. export function getProfileTabProps(stateful: Object | Function) {
  171. const state = toState(stateful);
  172. const {
  173. authEnabled,
  174. authLogin,
  175. conference
  176. } = state['features/base/conference'];
  177. const { hideEmailInSettings } = state['features/base/config'];
  178. const localParticipant = getLocalParticipant(state);
  179. return {
  180. authEnabled: Boolean(conference && authEnabled),
  181. authLogin,
  182. displayName: localParticipant.name,
  183. email: localParticipant.email,
  184. readOnlyName: isNameReadOnly(state),
  185. hideEmailInSettings
  186. };
  187. }
  188. /**
  189. * Returns the properties for the "Sounds" tab from settings dialog from Redux
  190. * state.
  191. *
  192. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  193. * {@code getState} function to be used to retrieve the state.
  194. * @returns {Object} - The properties for the "Sounds" tab from settings
  195. * dialog.
  196. */
  197. export function getSoundsTabProps(stateful: Object | Function) {
  198. const state = toState(stateful);
  199. const {
  200. soundsIncomingMessage,
  201. soundsParticipantJoined,
  202. soundsParticipantLeft,
  203. soundsTalkWhileMuted,
  204. soundsReactions
  205. } = state['features/base/settings'];
  206. const enableReactions = isReactionsEnabled(state);
  207. const moderatorMutedSoundsReactions = state['features/base/conference'].startReactionsMuted ?? false;
  208. return {
  209. soundsIncomingMessage,
  210. soundsParticipantJoined,
  211. soundsParticipantLeft,
  212. soundsTalkWhileMuted,
  213. soundsReactions,
  214. enableReactions,
  215. moderatorMutedSoundsReactions
  216. };
  217. }
  218. /**
  219. * Returns a promise which resolves with a list of objects containing
  220. * all the video jitsiTracks and appropriate errors for the given device ids.
  221. *
  222. * @param {string[]} ids - The list of the camera ids for which to create tracks.
  223. * @param {number} [timeout] - A timeout for the createLocalTrack function call.
  224. *
  225. * @returns {Promise<Object[]>}
  226. */
  227. export function createLocalVideoTracks(ids: string[], timeout: ?number) {
  228. return Promise.all(ids.map(deviceId => createLocalTrack('video', deviceId, timeout)
  229. .then(jitsiTrack => {
  230. return {
  231. jitsiTrack,
  232. deviceId
  233. };
  234. })
  235. .catch(() => {
  236. return {
  237. jitsiTrack: null,
  238. deviceId,
  239. error: 'deviceSelection.previewUnavailable'
  240. };
  241. })));
  242. }
  243. /**
  244. * Returns a promise which resolves with a list of objects containing
  245. * the audio track and the corresponding audio device information.
  246. *
  247. * @param {Object[]} devices - A list of microphone devices.
  248. * @param {number} [timeout] - A timeout for the createLocalTrack function call.
  249. * @returns {Promise<{
  250. * deviceId: string,
  251. * hasError: boolean,
  252. * jitsiTrack: Object,
  253. * label: string
  254. * }[]>}
  255. */
  256. export function createLocalAudioTracks(devices: Object[], timeout: ?number) {
  257. return Promise.all(
  258. devices.map(async ({ deviceId, label }) => {
  259. let jitsiTrack = null;
  260. let hasError = false;
  261. try {
  262. jitsiTrack = await createLocalTrack('audio', deviceId, timeout);
  263. } catch (err) {
  264. hasError = true;
  265. }
  266. return {
  267. deviceId,
  268. hasError,
  269. jitsiTrack,
  270. label
  271. };
  272. }));
  273. }
  274. /**
  275. * Returns the visibility state of the audio settings.
  276. *
  277. * @param {Object} state - The state of the application.
  278. * @returns {boolean}
  279. */
  280. export function getAudioSettingsVisibility(state: Object) {
  281. return state['features/settings'].audioSettingsVisible;
  282. }
  283. /**
  284. * Returns the visibility state of the video settings.
  285. *
  286. * @param {Object} state - The state of the application.
  287. * @returns {boolean}
  288. */
  289. export function getVideoSettingsVisibility(state: Object) {
  290. return state['features/settings'].videoSettingsVisible;
  291. }