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

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