您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.ts 12KB

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