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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. };
  115. }
  116. /**
  117. * Returns the properties for the "More" tab from settings dialog from Redux
  118. * state.
  119. *
  120. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  121. * {@code getState} function to be used to retrieve the state.
  122. * @returns {Object} - The properties for the "More" tab from settings dialog.
  123. */
  124. export function getModeratorTabProps(stateful: Object | Function) {
  125. const state = toState(stateful);
  126. const {
  127. conference,
  128. followMeEnabled,
  129. startAudioMutedPolicy,
  130. startVideoMutedPolicy,
  131. startReactionsMuted
  132. } = state['features/base/conference'];
  133. const { disableReactionsModeration } = state['features/base/config'];
  134. const followMeActive = isFollowMeActive(state);
  135. const showModeratorSettings = shouldShowModeratorSettings(state);
  136. // The settings sections to display.
  137. return {
  138. showModeratorSettings: Boolean(conference && showModeratorSettings),
  139. disableReactionsModeration: Boolean(disableReactionsModeration),
  140. followMeActive: Boolean(conference && followMeActive),
  141. followMeEnabled: Boolean(conference && followMeEnabled),
  142. startReactionsMuted: Boolean(conference && startReactionsMuted),
  143. startAudioMuted: Boolean(conference && startAudioMutedPolicy),
  144. startVideoMuted: Boolean(conference && startVideoMutedPolicy)
  145. };
  146. }
  147. /**
  148. * Returns true if moderator tab in settings should be visible/accessible.
  149. *
  150. * @param {(Function|Object)} stateful - The (whole) redux state, or redux's
  151. * {@code getState} function to be used to retrieve the state.
  152. * @returns {boolean} True to indicate that moderator tab should be visible, false otherwise.
  153. */
  154. export function shouldShowModeratorSettings(stateful: Object | Function) {
  155. const state = toState(stateful);
  156. return Boolean(
  157. isSettingEnabled('moderator')
  158. && isLocalParticipantModerator(state));
  159. }
  160. /**
  161. * Returns the properties for the "Profile" tab from settings dialog from Redux
  162. * state.
  163. *
  164. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  165. * {@code getState} function to be used to retrieve the state.
  166. * @returns {Object} - The properties for the "Profile" tab from settings
  167. * dialog.
  168. */
  169. export function getProfileTabProps(stateful: Object | Function) {
  170. const state = toState(stateful);
  171. const {
  172. authEnabled,
  173. authLogin,
  174. conference
  175. } = state['features/base/conference'];
  176. const { hideEmailInSettings } = state['features/base/config'];
  177. const localParticipant = getLocalParticipant(state);
  178. return {
  179. authEnabled: Boolean(conference && authEnabled),
  180. authLogin,
  181. displayName: localParticipant.name,
  182. email: localParticipant.email,
  183. readOnlyName: isNameReadOnly(state),
  184. hideEmailInSettings
  185. };
  186. }
  187. /**
  188. * Returns the properties for the "Sounds" tab from settings dialog from Redux
  189. * state.
  190. *
  191. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  192. * {@code getState} function to be used to retrieve the state.
  193. * @returns {Object} - The properties for the "Sounds" tab from settings
  194. * dialog.
  195. */
  196. export function getSoundsTabProps(stateful: Object | Function) {
  197. const state = toState(stateful);
  198. const {
  199. soundsIncomingMessage,
  200. soundsParticipantJoined,
  201. soundsParticipantLeft,
  202. soundsTalkWhileMuted,
  203. soundsReactions
  204. } = state['features/base/settings'];
  205. const enableReactions = isReactionsEnabled(state);
  206. const moderatorMutedSoundsReactions = state['features/base/conference'].startReactionsMuted ?? false;
  207. return {
  208. soundsIncomingMessage,
  209. soundsParticipantJoined,
  210. soundsParticipantLeft,
  211. soundsTalkWhileMuted,
  212. soundsReactions,
  213. enableReactions,
  214. moderatorMutedSoundsReactions
  215. };
  216. }
  217. /**
  218. * Returns a promise which resolves with a list of objects containing
  219. * all the video jitsiTracks and appropriate errors for the given device ids.
  220. *
  221. * @param {string[]} ids - The list of the camera ids for which to create tracks.
  222. * @param {number} [timeout] - A timeout for the createLocalTrack function call.
  223. *
  224. * @returns {Promise<Object[]>}
  225. */
  226. export function createLocalVideoTracks(ids: string[], timeout: ?number) {
  227. return Promise.all(ids.map(deviceId => createLocalTrack('video', deviceId, timeout)
  228. .then(jitsiTrack => {
  229. return {
  230. jitsiTrack,
  231. deviceId
  232. };
  233. })
  234. .catch(() => {
  235. return {
  236. jitsiTrack: null,
  237. deviceId,
  238. error: 'deviceSelection.previewUnavailable'
  239. };
  240. })));
  241. }
  242. /**
  243. * Returns a promise which resolves with a list of objects containing
  244. * the audio track and the corresponding audio device information.
  245. *
  246. * @param {Object[]} devices - A list of microphone devices.
  247. * @param {number} [timeout] - A timeout for the createLocalTrack function call.
  248. * @returns {Promise<{
  249. * deviceId: string,
  250. * hasError: boolean,
  251. * jitsiTrack: Object,
  252. * label: string
  253. * }[]>}
  254. */
  255. export function createLocalAudioTracks(devices: Object[], timeout: ?number) {
  256. return Promise.all(
  257. devices.map(async ({ deviceId, label }) => {
  258. let jitsiTrack = null;
  259. let hasError = false;
  260. try {
  261. jitsiTrack = await createLocalTrack('audio', deviceId, timeout);
  262. } catch (err) {
  263. hasError = true;
  264. }
  265. return {
  266. deviceId,
  267. hasError,
  268. jitsiTrack,
  269. label
  270. };
  271. }));
  272. }
  273. /**
  274. * Returns the visibility state of the audio settings.
  275. *
  276. * @param {Object} state - The state of the application.
  277. * @returns {boolean}
  278. */
  279. export function getAudioSettingsVisibility(state: Object) {
  280. return state['features/settings'].audioSettingsVisible;
  281. }
  282. /**
  283. * Returns the visibility state of the video settings.
  284. *
  285. * @param {Object} state - The state of the application.
  286. * @returns {boolean}
  287. */
  288. export function getVideoSettingsVisibility(state: Object) {
  289. return state['features/settings'].videoSettingsVisible;
  290. }