Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

functions.js 11KB

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