Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

functions.js 11KB

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