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.any.ts 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // @ts-ignore
  2. import { jitsiLocalStorage } from '@jitsi/js-utils';
  3. // eslint-disable-next-line lines-around-comment
  4. // @ts-ignore
  5. import { safeJsonParse } from '@jitsi/js-utils/json';
  6. import { isEmpty, mergeWith, pick } from 'lodash-es';
  7. import { IReduxState } from '../../app/types';
  8. import { getLocalParticipant } from '../participants/functions';
  9. import { parseURLParams } from '../util/parseURLParams';
  10. import { IConfig } from './configType';
  11. import CONFIG_WHITELIST from './configWhitelist';
  12. import {
  13. DEFAULT_HELP_CENTRE_URL,
  14. DEFAULT_PRIVACY_URL,
  15. DEFAULT_TERMS_URL,
  16. FEATURE_FLAGS,
  17. _CONFIG_STORE_PREFIX
  18. } from './constants';
  19. import INTERFACE_CONFIG_WHITELIST from './interfaceConfigWhitelist';
  20. import logger from './logger';
  21. // XXX The function getRoomName is split out of
  22. // functions.any.js because it is bundled in both app.bundle and
  23. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  24. // want all functions to be bundled in do_external_connect.
  25. export { default as getRoomName } from './getRoomName';
  26. /**
  27. * Create a "fake" configuration object for the given base URL. This is used in case the config
  28. * couldn't be loaded in the welcome page, so at least we have something to try with.
  29. *
  30. * @param {string} baseURL - URL of the deployment for which we want the fake config.
  31. * @returns {Object}
  32. */
  33. export function createFakeConfig(baseURL: string) {
  34. const url = new URL(baseURL);
  35. return {
  36. hosts: {
  37. domain: url.hostname,
  38. muc: `conference.${url.hostname}`
  39. },
  40. bosh: `${baseURL}http-bind`,
  41. p2p: {
  42. enabled: true
  43. }
  44. };
  45. }
  46. /**
  47. * Selector used to get the meeting region.
  48. *
  49. * @param {Object} state - The global state.
  50. * @returns {string}
  51. */
  52. export function getMeetingRegion(state: IReduxState) {
  53. return state['features/base/config']?.deploymentInfo?.region || '';
  54. }
  55. /**
  56. * Selector used to get the SSRC-rewriting feature flag.
  57. *
  58. * @param {Object} state - The global state.
  59. * @returns {boolean}
  60. */
  61. export function getSsrcRewritingFeatureFlag(state: IReduxState) {
  62. return getFeatureFlag(state, FEATURE_FLAGS.SSRC_REWRITING) ?? true;
  63. }
  64. /**
  65. * Selector used to get a feature flag.
  66. *
  67. * @param {Object} state - The global state.
  68. * @param {string} featureFlag - The name of the feature flag.
  69. * @returns {boolean}
  70. */
  71. export function getFeatureFlag(state: IReduxState, featureFlag: string) {
  72. const featureFlags = state['features/base/config']?.flags || {};
  73. return featureFlags[featureFlag as keyof typeof featureFlags];
  74. }
  75. /**
  76. * Selector used to get the disableRemoveRaisedHandOnFocus.
  77. *
  78. * @param {Object} state - The global state.
  79. * @returns {boolean}
  80. */
  81. export function getDisableRemoveRaisedHandOnFocus(state: IReduxState) {
  82. return state['features/base/config']?.raisedHands?.disableRemoveRaisedHandOnFocus || false;
  83. }
  84. /**
  85. * Selector used to get the disableLowerHandByModerator.
  86. *
  87. * @param {Object} state - The global state.
  88. * @returns {boolean}
  89. */
  90. export function getDisableLowerHandByModerator(state: IReduxState) {
  91. return state['features/base/config']?.raisedHands?.disableLowerHandByModerator || false;
  92. }
  93. /**
  94. * Selector used to get the disableLowerHandNotification.
  95. *
  96. * @param {Object} state - The global state.
  97. * @returns {boolean}
  98. */
  99. export function getDisableLowerHandNotification(state: IReduxState) {
  100. return state['features/base/config']?.raisedHands?.disableLowerHandNotification || true;
  101. }
  102. /**
  103. * Selector used to get the disableNextSpeakerNotification.
  104. *
  105. * @param {Object} state - The global state.
  106. * @returns {boolean}
  107. */
  108. export function getDisableNextSpeakerNotification(state: IReduxState) {
  109. return state['features/base/config']?.raisedHands?.disableNextSpeakerNotification || false;
  110. }
  111. /**
  112. * Selector used to get the endpoint used for fetching the recording.
  113. *
  114. * @param {Object} state - The global state.
  115. * @returns {string}
  116. */
  117. export function getRecordingSharingUrl(state: IReduxState) {
  118. return state['features/base/config'].recordingSharingUrl;
  119. }
  120. /**
  121. * Overrides JSON properties in {@code config} and
  122. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  123. * Overrides only the whitelisted keys.
  124. *
  125. * @param {Object} config - The config Object in which we'll be overriding
  126. * properties.
  127. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  128. * be overriding properties.
  129. * @param {Object} json - Object containing configuration properties.
  130. * Destination object is selected based on root property name:
  131. * {
  132. * config: {
  133. * // config.js properties here
  134. * },
  135. * interfaceConfig: {
  136. * // interface_config.js properties here
  137. * }
  138. * }.
  139. * @returns {void}
  140. */
  141. export function overrideConfigJSON(config: IConfig, interfaceConfig: any, json: any) {
  142. for (const configName of Object.keys(json)) {
  143. let configObj;
  144. if (configName === 'config') {
  145. configObj = config;
  146. } else if (configName === 'interfaceConfig') {
  147. configObj = interfaceConfig;
  148. }
  149. if (configObj) {
  150. const configJSON
  151. = getWhitelistedJSON(configName as 'interfaceConfig' | 'config', json[configName]);
  152. if (!isEmpty(configJSON)) {
  153. logger.info(`Extending ${configName} with: ${JSON.stringify(configJSON)}`);
  154. // eslint-disable-next-line arrow-body-style
  155. mergeWith(configObj, configJSON, (oldValue, newValue) => {
  156. // XXX We don't want to merge the arrays, we want to
  157. // overwrite them.
  158. return Array.isArray(oldValue) ? newValue : undefined;
  159. });
  160. }
  161. }
  162. }
  163. }
  164. /* eslint-enable max-params, no-shadow */
  165. /**
  166. * Apply whitelist filtering for configs with whitelists.
  167. * Only extracts overridden values for keys we allow to be overridden.
  168. *
  169. * @param {string} configName - The config name, one of config or interfaceConfig.
  170. * @param {Object} configJSON - The object with keys and values to override.
  171. * @returns {Object} - The result object only with the keys
  172. * that are whitelisted.
  173. */
  174. export function getWhitelistedJSON(configName: 'interfaceConfig' | 'config', configJSON: any): Object {
  175. if (configName === 'interfaceConfig') {
  176. return pick(configJSON, INTERFACE_CONFIG_WHITELIST);
  177. } else if (configName === 'config') {
  178. return pick(configJSON, CONFIG_WHITELIST);
  179. }
  180. return configJSON;
  181. }
  182. /**
  183. * Selector for determining if the display name is read only.
  184. *
  185. * @param {Object} state - The state of the app.
  186. * @returns {boolean}
  187. */
  188. export function isNameReadOnly(state: IReduxState): boolean {
  189. return Boolean(state['features/base/config'].disableProfile
  190. || state['features/base/config'].readOnlyName);
  191. }
  192. /**
  193. * Selector for determining if the participant is the next one in the queue to speak.
  194. *
  195. * @param {Object} state - The state of the app.
  196. * @returns {boolean}
  197. */
  198. export function isNextToSpeak(state: IReduxState): boolean {
  199. const raisedHandsQueue = state['features/base/participants'].raisedHandsQueue || [];
  200. const participantId = getLocalParticipant(state)?.id;
  201. return participantId === raisedHandsQueue[0]?.id;
  202. }
  203. /**
  204. * Selector for determining if the next to speak participant in the queue has been notified.
  205. *
  206. * @param {Object} state - The state of the app.
  207. * @returns {boolean}
  208. */
  209. export function hasBeenNotified(state: IReduxState): boolean {
  210. const raisedHandsQueue = state['features/base/participants'].raisedHandsQueue;
  211. return Boolean(raisedHandsQueue[0]?.hasBeenNotified);
  212. }
  213. /**
  214. * Selector for determining if the display name is visible.
  215. *
  216. * @param {Object} state - The state of the app.
  217. * @returns {boolean}
  218. */
  219. export function isDisplayNameVisible(state: IReduxState): boolean {
  220. return !state['features/base/config'].hideDisplayName;
  221. }
  222. /**
  223. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  224. * previously downloaded from a specific {@code baseURL} and stored with
  225. * {@link storeConfig}.
  226. *
  227. * @param {string} baseURL - The base URL from which the config.js was
  228. * previously downloaded and stored with {@code storeConfig}.
  229. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  230. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  231. * otherwise, {@code undefined}.
  232. */
  233. export function restoreConfig(baseURL: string) {
  234. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  235. const config = jitsiLocalStorage.getItem(key);
  236. if (config) {
  237. try {
  238. return safeJsonParse(config) || undefined;
  239. } catch (e) {
  240. // Somehow incorrect data ended up in the storage. Clean it up.
  241. jitsiLocalStorage.removeItem(key);
  242. }
  243. }
  244. return undefined;
  245. }
  246. /**
  247. * Inspects the hash part of the location URI and overrides values specified
  248. * there in the corresponding config objects given as the arguments. The syntax
  249. * is: {@code https://server.com/room#config.debug=true
  250. * &interfaceConfig.showButton=false}.
  251. *
  252. * In the hash part each parameter will be parsed to JSON and then the root
  253. * object will be matched with the corresponding config object given as the
  254. * argument to this function.
  255. *
  256. * @param {Object} config - This is the general config.
  257. * @param {Object} interfaceConfig - This is the interface config.
  258. * @param {URI} location - The new location to which the app is navigating to.
  259. * @returns {void}
  260. */
  261. export function setConfigFromURLParams(
  262. config: IConfig, interfaceConfig: any, location: string | URL) {
  263. const params = parseURLParams(location);
  264. const json: any = {};
  265. // At this point we have:
  266. // params = {
  267. // "config.disableAudioLevels": false,
  268. // "config.channelLastN": -1,
  269. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  270. // }
  271. // We want to have:
  272. // json = {
  273. // config: {
  274. // "disableAudioLevels": false,
  275. // "channelLastN": -1
  276. // },
  277. // interfaceConfig: {
  278. // "APP_NAME": "Jitsi Meet"
  279. // }
  280. // }
  281. config && (json.config = {});
  282. interfaceConfig && (json.interfaceConfig = {});
  283. for (const param of Object.keys(params)) {
  284. let base = json;
  285. const names = param.split('.');
  286. const last = names.pop() ?? '';
  287. for (const name of names) {
  288. base = base[name] = base[name] || {};
  289. }
  290. base[last] = params[param];
  291. }
  292. overrideConfigJSON(config, interfaceConfig, json);
  293. }
  294. /* eslint-enable max-params */
  295. /**
  296. * Returns the dial out url.
  297. *
  298. * @param {Object} state - The state of the app.
  299. * @returns {string}
  300. */
  301. export function getDialOutStatusUrl(state: IReduxState) {
  302. return state['features/base/config'].guestDialOutStatusUrl;
  303. }
  304. /**
  305. * Returns the dial out status url.
  306. *
  307. * @param {Object} state - The state of the app.
  308. * @returns {string}
  309. */
  310. export function getDialOutUrl(state: IReduxState) {
  311. return state['features/base/config'].guestDialOutUrl;
  312. }
  313. /**
  314. * Selector to return the security UI config.
  315. *
  316. * @param {IReduxState} state - State object.
  317. * @returns {Object}
  318. */
  319. export function getSecurityUiConfig(state: IReduxState) {
  320. return state['features/base/config']?.securityUi || {};
  321. }
  322. /**
  323. * Returns the terms, privacy and help centre URL's.
  324. *
  325. * @param {IReduxState} state - The state of the application.
  326. * @returns {{
  327. * privacy: string,
  328. * helpCentre: string,
  329. * terms: string
  330. * }}
  331. */
  332. export function getLegalUrls(state: IReduxState) {
  333. const helpCentreURL = state['features/base/config']?.helpCentreURL;
  334. const configLegalUrls = state['features/base/config']?.legalUrls;
  335. return {
  336. privacy: configLegalUrls?.privacy || DEFAULT_PRIVACY_URL,
  337. helpCentre: helpCentreURL || configLegalUrls?.helpCentre || DEFAULT_HELP_CENTRE_URL,
  338. terms: configLegalUrls?.terms || DEFAULT_TERMS_URL
  339. };
  340. }
  341. /**
  342. * Utility function to debounce the execution of a callback function.
  343. *
  344. * @param {Function} callback - The callback to debounce.
  345. * @param {number} delay - The debounce delay in milliseconds.
  346. * @returns {Function} - A debounced function that delays the execution of the callback.
  347. */
  348. export function debounce(callback: (...args: any[]) => void, delay: number) {
  349. let timerId: any;
  350. return (...args: any[]) => {
  351. if (timerId) {
  352. clearTimeout(timerId);
  353. }
  354. timerId = setTimeout(() => callback(...args), delay);
  355. };
  356. }