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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 { getBreakoutRoomsConfig, isInBreakoutRoom } from '../breakout-rooms/functions';
  14. import { isStageFilmstripEnabled } from '../filmstrip/functions';
  15. import { isFollowMeActive } from '../follow-me';
  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 inBreakoutRoom = isInBreakoutRoom(state);
  162. const { hideModeratorSettingsTab } = getBreakoutRoomsConfig(state);
  163. const hasModeratorRights = Boolean(
  164. isSettingEnabled('moderator')
  165. && isLocalParticipantModerator(state)
  166. );
  167. return inBreakoutRoom
  168. ? hasModeratorRights && !hideModeratorSettingsTab
  169. : hasModeratorRights;
  170. }
  171. /**
  172. * Returns the properties for the "Profile" tab from settings dialog from Redux
  173. * state.
  174. *
  175. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  176. * {@code getState} function to be used to retrieve the state.
  177. * @returns {Object} - The properties for the "Profile" tab from settings
  178. * dialog.
  179. */
  180. export function getProfileTabProps(stateful: Object | Function) {
  181. const state = toState(stateful);
  182. const {
  183. authEnabled,
  184. authLogin,
  185. conference
  186. } = state['features/base/conference'];
  187. const { hideEmailInSettings } = state['features/base/config'];
  188. const localParticipant = getLocalParticipant(state);
  189. return {
  190. authEnabled: Boolean(conference && authEnabled),
  191. authLogin,
  192. displayName: localParticipant.name,
  193. email: localParticipant.email,
  194. readOnlyName: isNameReadOnly(state),
  195. hideEmailInSettings
  196. };
  197. }
  198. /**
  199. * Returns the properties for the "Sounds" tab from settings dialog from Redux
  200. * state.
  201. *
  202. * @param {(Function|Object)} stateful -The (whole) redux state, or redux's
  203. * {@code getState} function to be used to retrieve the state.
  204. * @returns {Object} - The properties for the "Sounds" tab from settings
  205. * dialog.
  206. */
  207. export function getSoundsTabProps(stateful: Object | Function) {
  208. const state = toState(stateful);
  209. const {
  210. soundsIncomingMessage,
  211. soundsParticipantJoined,
  212. soundsParticipantLeft,
  213. soundsTalkWhileMuted,
  214. soundsReactions
  215. } = state['features/base/settings'];
  216. const enableReactions = isReactionsEnabled(state);
  217. const moderatorMutedSoundsReactions = state['features/base/conference'].startReactionsMuted ?? false;
  218. return {
  219. soundsIncomingMessage,
  220. soundsParticipantJoined,
  221. soundsParticipantLeft,
  222. soundsTalkWhileMuted,
  223. soundsReactions,
  224. enableReactions,
  225. moderatorMutedSoundsReactions
  226. };
  227. }
  228. /**
  229. * Returns a promise which resolves with a list of objects containing
  230. * all the video jitsiTracks and appropriate errors for the given device ids.
  231. *
  232. * @param {string[]} ids - The list of the camera ids for which to create tracks.
  233. * @param {number} [timeout] - A timeout for the createLocalTrack function call.
  234. *
  235. * @returns {Promise<Object[]>}
  236. */
  237. export function createLocalVideoTracks(ids: string[], timeout: ?number) {
  238. return Promise.all(ids.map(deviceId => createLocalTrack('video', deviceId, timeout)
  239. .then(jitsiTrack => {
  240. return {
  241. jitsiTrack,
  242. deviceId
  243. };
  244. })
  245. .catch(() => {
  246. return {
  247. jitsiTrack: null,
  248. deviceId,
  249. error: 'deviceSelection.previewUnavailable'
  250. };
  251. })));
  252. }
  253. /**
  254. * Returns a promise which resolves with a list of objects containing
  255. * the audio track and the corresponding audio device information.
  256. *
  257. * @param {Object[]} devices - A list of microphone devices.
  258. * @param {number} [timeout] - A timeout for the createLocalTrack function call.
  259. * @returns {Promise<{
  260. * deviceId: string,
  261. * hasError: boolean,
  262. * jitsiTrack: Object,
  263. * label: string
  264. * }[]>}
  265. */
  266. export function createLocalAudioTracks(devices: Object[], timeout: ?number) {
  267. return Promise.all(
  268. devices.map(async ({ deviceId, label }) => {
  269. let jitsiTrack = null;
  270. let hasError = false;
  271. try {
  272. jitsiTrack = await createLocalTrack('audio', deviceId, timeout);
  273. } catch (err) {
  274. hasError = true;
  275. }
  276. return {
  277. deviceId,
  278. hasError,
  279. jitsiTrack,
  280. label
  281. };
  282. }));
  283. }
  284. /**
  285. * Returns the visibility state of the audio settings.
  286. *
  287. * @param {Object} state - The state of the application.
  288. * @returns {boolean}
  289. */
  290. export function getAudioSettingsVisibility(state: Object) {
  291. return state['features/settings'].audioSettingsVisible;
  292. }
  293. /**
  294. * Returns the visibility state of the video settings.
  295. *
  296. * @param {Object} state - The state of the application.
  297. * @returns {boolean}
  298. */
  299. export function getVideoSettingsVisibility(state: Object) {
  300. return state['features/settings'].videoSettingsVisible;
  301. }