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

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