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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 _ from 'lodash';
  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']?.disableRemoveRaisedHandOnFocus || false;
  83. }
  84. /**
  85. * Selector used to get the endpoint used for fetching the recording.
  86. *
  87. * @param {Object} state - The global state.
  88. * @returns {string}
  89. */
  90. export function getRecordingSharingUrl(state: IReduxState) {
  91. return state['features/base/config'].recordingSharingUrl;
  92. }
  93. /**
  94. * Overrides JSON properties in {@code config} and
  95. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  96. * Overrides only the whitelisted keys.
  97. *
  98. * @param {Object} config - The config Object in which we'll be overriding
  99. * properties.
  100. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  101. * be overriding properties.
  102. * @param {Object} json - Object containing configuration properties.
  103. * Destination object is selected based on root property name:
  104. * {
  105. * config: {
  106. * // config.js properties here
  107. * },
  108. * interfaceConfig: {
  109. * // interface_config.js properties here
  110. * }
  111. * }.
  112. * @returns {void}
  113. */
  114. export function overrideConfigJSON(config: IConfig, interfaceConfig: any, json: any) {
  115. for (const configName of Object.keys(json)) {
  116. let configObj;
  117. if (configName === 'config') {
  118. configObj = config;
  119. } else if (configName === 'interfaceConfig') {
  120. configObj = interfaceConfig;
  121. }
  122. if (configObj) {
  123. const configJSON
  124. = getWhitelistedJSON(configName as 'interfaceConfig' | 'config', json[configName]);
  125. if (!_.isEmpty(configJSON)) {
  126. logger.info(
  127. `Extending ${configName} with: ${
  128. JSON.stringify(configJSON)}`);
  129. // eslint-disable-next-line arrow-body-style
  130. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  131. // XXX We don't want to merge the arrays, we want to
  132. // overwrite them.
  133. return Array.isArray(oldValue) ? newValue : undefined;
  134. });
  135. }
  136. }
  137. }
  138. }
  139. /* eslint-enable max-params, no-shadow */
  140. /**
  141. * Apply whitelist filtering for configs with whitelists.
  142. * Only extracts overridden values for keys we allow to be overridden.
  143. *
  144. * @param {string} configName - The config name, one of config or interfaceConfig.
  145. * @param {Object} configJSON - The object with keys and values to override.
  146. * @returns {Object} - The result object only with the keys
  147. * that are whitelisted.
  148. */
  149. export function getWhitelistedJSON(configName: 'interfaceConfig' | 'config', configJSON: any): Object {
  150. if (configName === 'interfaceConfig') {
  151. return _.pick(configJSON, INTERFACE_CONFIG_WHITELIST);
  152. } else if (configName === 'config') {
  153. return _.pick(configJSON, CONFIG_WHITELIST);
  154. }
  155. return configJSON;
  156. }
  157. /**
  158. * Selector for determining if the display name is read only.
  159. *
  160. * @param {Object} state - The state of the app.
  161. * @returns {boolean}
  162. */
  163. export function isNameReadOnly(state: IReduxState): boolean {
  164. return Boolean(state['features/base/config'].disableProfile
  165. || state['features/base/config'].readOnlyName);
  166. }
  167. /**
  168. * Selector for determining if the participant is the next one in the queue to speak.
  169. *
  170. * @param {Object} state - The state of the app.
  171. * @returns {boolean}
  172. */
  173. export function isNextToSpeak(state: IReduxState): boolean {
  174. const raisedHandsQueue = state['features/base/participants'].raisedHandsQueue || [];
  175. const participantId = getLocalParticipant(state)?.id;
  176. return participantId === raisedHandsQueue[0]?.id;
  177. }
  178. /**
  179. * Selector for determining if the next to speak participant in the queue has been notified.
  180. *
  181. * @param {Object} state - The state of the app.
  182. * @returns {boolean}
  183. */
  184. export function hasBeenNotified(state: IReduxState): boolean {
  185. const raisedHandsQueue = state['features/base/participants'].raisedHandsQueue;
  186. return Boolean(raisedHandsQueue[0]?.hasBeenNotified);
  187. }
  188. /**
  189. * Selector for determining if the display name is visible.
  190. *
  191. * @param {Object} state - The state of the app.
  192. * @returns {boolean}
  193. */
  194. export function isDisplayNameVisible(state: IReduxState): boolean {
  195. return !state['features/base/config'].hideDisplayName;
  196. }
  197. /**
  198. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  199. * previously downloaded from a specific {@code baseURL} and stored with
  200. * {@link storeConfig}.
  201. *
  202. * @param {string} baseURL - The base URL from which the config.js was
  203. * previously downloaded and stored with {@code storeConfig}.
  204. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  205. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  206. * otherwise, {@code undefined}.
  207. */
  208. export function restoreConfig(baseURL: string) {
  209. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  210. const config = jitsiLocalStorage.getItem(key);
  211. if (config) {
  212. try {
  213. return safeJsonParse(config) || undefined;
  214. } catch (e) {
  215. // Somehow incorrect data ended up in the storage. Clean it up.
  216. jitsiLocalStorage.removeItem(key);
  217. }
  218. }
  219. return undefined;
  220. }
  221. /**
  222. * Inspects the hash part of the location URI and overrides values specified
  223. * there in the corresponding config objects given as the arguments. The syntax
  224. * is: {@code https://server.com/room#config.debug=true
  225. * &interfaceConfig.showButton=false}.
  226. *
  227. * In the hash part each parameter will be parsed to JSON and then the root
  228. * object will be matched with the corresponding config object given as the
  229. * argument to this function.
  230. *
  231. * @param {Object} config - This is the general config.
  232. * @param {Object} interfaceConfig - This is the interface config.
  233. * @param {URI} location - The new location to which the app is navigating to.
  234. * @returns {void}
  235. */
  236. export function setConfigFromURLParams(
  237. config: IConfig, interfaceConfig: any, location: string | URL) {
  238. const params = parseURLParams(location);
  239. const json: any = {};
  240. // At this point we have:
  241. // params = {
  242. // "config.disableAudioLevels": false,
  243. // "config.channelLastN": -1,
  244. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  245. // }
  246. // We want to have:
  247. // json = {
  248. // config: {
  249. // "disableAudioLevels": false,
  250. // "channelLastN": -1
  251. // },
  252. // interfaceConfig: {
  253. // "APP_NAME": "Jitsi Meet"
  254. // }
  255. // }
  256. config && (json.config = {});
  257. interfaceConfig && (json.interfaceConfig = {});
  258. for (const param of Object.keys(params)) {
  259. let base = json;
  260. const names = param.split('.');
  261. const last = names.pop() ?? '';
  262. for (const name of names) {
  263. base = base[name] = base[name] || {};
  264. }
  265. base[last] = params[param];
  266. }
  267. overrideConfigJSON(config, interfaceConfig, json);
  268. }
  269. /* eslint-enable max-params */
  270. /**
  271. * Returns the dial out url.
  272. *
  273. * @param {Object} state - The state of the app.
  274. * @returns {string}
  275. */
  276. export function getDialOutStatusUrl(state: IReduxState) {
  277. return state['features/base/config'].guestDialOutStatusUrl;
  278. }
  279. /**
  280. * Returns the dial out status url.
  281. *
  282. * @param {Object} state - The state of the app.
  283. * @returns {string}
  284. */
  285. export function getDialOutUrl(state: IReduxState) {
  286. return state['features/base/config'].guestDialOutUrl;
  287. }
  288. /**
  289. * Selector to return the security UI config.
  290. *
  291. * @param {IReduxState} state - State object.
  292. * @returns {Object}
  293. */
  294. export function getSecurityUiConfig(state: IReduxState) {
  295. return state['features/base/config']?.securityUi || {};
  296. }
  297. /**
  298. * Returns the terms, privacy and help centre URL's.
  299. *
  300. * @param {IReduxState} state - The state of the application.
  301. * @returns {{
  302. * privacy: string,
  303. * helpCentre: string,
  304. * terms: string
  305. * }}
  306. */
  307. export function getLegalUrls(state: IReduxState) {
  308. const helpCentreURL = state['features/base/config']?.helpCentreURL;
  309. const configLegalUrls = state['features/base/config']?.legalUrls;
  310. return {
  311. privacy: configLegalUrls?.privacy || DEFAULT_PRIVACY_URL,
  312. helpCentre: helpCentreURL || configLegalUrls?.helpCentre || DEFAULT_HELP_CENTRE_URL,
  313. terms: configLegalUrls?.terms || DEFAULT_TERMS_URL
  314. };
  315. }