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.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // @flow
  2. import { jitsiLocalStorage } from '@jitsi/js-utils';
  3. import _ from 'lodash';
  4. import { parseURLParams } from '../util';
  5. import CONFIG_WHITELIST from './configWhitelist';
  6. import { _CONFIG_STORE_PREFIX } from './constants';
  7. import INTERFACE_CONFIG_WHITELIST from './interfaceConfigWhitelist';
  8. import logger from './logger';
  9. // XXX The function getRoomName is split out of
  10. // functions.js because it is bundled in both app.bundle and
  11. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  12. // want all functions to be bundled in do_external_connect.
  13. export { default as getRoomName } from './getRoomName';
  14. /**
  15. * Create a "fake" configuration object for the given base URL. This is used in case the config
  16. * couldn't be loaded in the welcome page, so at least we have something to try with.
  17. *
  18. * @param {string} baseURL - URL of the deployment for which we want the fake config.
  19. * @returns {Object}
  20. */
  21. export function createFakeConfig(baseURL: string) {
  22. const url = new URL(baseURL);
  23. return {
  24. hosts: {
  25. domain: url.hostname,
  26. muc: `conference.${url.hostname}`
  27. },
  28. bosh: `${baseURL}http-bind`,
  29. clientNode: 'https://jitsi.org/jitsi-meet',
  30. p2p: {
  31. enabled: true
  32. }
  33. };
  34. }
  35. /* eslint-disable max-params, no-shadow */
  36. /**
  37. * Overrides JSON properties in {@code config} and
  38. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  39. * Overrides only the whitelisted keys.
  40. *
  41. * @param {Object} config - The config Object in which we'll be overriding
  42. * properties.
  43. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  44. * be overriding properties.
  45. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  46. * overriding properties.
  47. * @param {Object} json - Object containing configuration properties.
  48. * Destination object is selected based on root property name:
  49. * {
  50. * config: {
  51. * // config.js properties here
  52. * },
  53. * interfaceConfig: {
  54. * // interface_config.js properties here
  55. * },
  56. * loggingConfig: {
  57. * // logging_config.js properties here
  58. * }
  59. * }.
  60. * @returns {void}
  61. */
  62. export function overrideConfigJSON(
  63. config: ?Object, interfaceConfig: ?Object, loggingConfig: ?Object,
  64. json: Object) {
  65. for (const configName of Object.keys(json)) {
  66. let configObj;
  67. if (configName === 'config') {
  68. configObj = config;
  69. } else if (configName === 'interfaceConfig') {
  70. configObj = interfaceConfig;
  71. } else if (configName === 'loggingConfig') {
  72. configObj = loggingConfig;
  73. }
  74. if (configObj) {
  75. const configJSON
  76. = getWhitelistedJSON(configName, json[configName]);
  77. if (!_.isEmpty(configJSON)) {
  78. logger.info(
  79. `Extending ${configName} with: ${
  80. JSON.stringify(configJSON)}`);
  81. // eslint-disable-next-line arrow-body-style
  82. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  83. // XXX We don't want to merge the arrays, we want to
  84. // overwrite them.
  85. return Array.isArray(oldValue) ? newValue : undefined;
  86. });
  87. }
  88. }
  89. }
  90. }
  91. /* eslint-enable max-params, no-shadow */
  92. /**
  93. * Apply whitelist filtering for configs with whitelists, skips this for others
  94. * configs (loggingConfig).
  95. * Only extracts overridden values for keys we allow to be overridden.
  96. *
  97. * @param {string} configName - The config name, one of config,
  98. * interfaceConfig, loggingConfig.
  99. * @param {Object} configJSON - The object with keys and values to override.
  100. * @returns {Object} - The result object only with the keys
  101. * that are whitelisted.
  102. */
  103. export function getWhitelistedJSON(configName: string, configJSON: Object): Object {
  104. if (configName === 'interfaceConfig') {
  105. return _.pick(configJSON, INTERFACE_CONFIG_WHITELIST);
  106. } else if (configName === 'config') {
  107. return _.pick(configJSON, CONFIG_WHITELIST);
  108. }
  109. return configJSON;
  110. }
  111. /**
  112. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  113. * previously downloaded from a specific {@code baseURL} and stored with
  114. * {@link storeConfig}.
  115. *
  116. * @param {string} baseURL - The base URL from which the config.js was
  117. * previously downloaded and stored with {@code storeConfig}.
  118. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  119. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  120. * otherwise, {@code undefined}.
  121. */
  122. export function restoreConfig(baseURL: string): ?Object {
  123. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  124. const config = jitsiLocalStorage.getItem(key);
  125. if (config) {
  126. try {
  127. return JSON.parse(config) || undefined;
  128. } catch (e) {
  129. // Somehow incorrect data ended up in the storage. Clean it up.
  130. jitsiLocalStorage.removeItem(key);
  131. }
  132. }
  133. return undefined;
  134. }
  135. /* eslint-disable max-params */
  136. /**
  137. * Inspects the hash part of the location URI and overrides values specified
  138. * there in the corresponding config objects given as the arguments. The syntax
  139. * is: {@code https://server.com/room#config.debug=true
  140. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  141. *
  142. * In the hash part each parameter will be parsed to JSON and then the root
  143. * object will be matched with the corresponding config object given as the
  144. * argument to this function.
  145. *
  146. * @param {Object} config - This is the general config.
  147. * @param {Object} interfaceConfig - This is the interface config.
  148. * @param {Object} loggingConfig - The logging config.
  149. * @param {URI} location - The new location to which the app is navigating to.
  150. * @returns {void}
  151. */
  152. export function setConfigFromURLParams(
  153. config: ?Object,
  154. interfaceConfig: ?Object,
  155. loggingConfig: ?Object,
  156. location: Object) {
  157. const params = parseURLParams(location);
  158. const json = {};
  159. // At this point we have:
  160. // params = {
  161. // "config.disableAudioLevels": false,
  162. // "config.channelLastN": -1,
  163. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  164. // }
  165. // We want to have:
  166. // json = {
  167. // config: {
  168. // "disableAudioLevels": false,
  169. // "channelLastN": -1
  170. // },
  171. // interfaceConfig: {
  172. // "APP_NAME": "Jitsi Meet"
  173. // }
  174. // }
  175. config && (json.config = {});
  176. interfaceConfig && (json.interfaceConfig = {});
  177. loggingConfig && (json.loggingConfig = {});
  178. for (const param of Object.keys(params)) {
  179. let base = json;
  180. const names = param.split('.');
  181. const last = names.pop();
  182. for (const name of names) {
  183. base = base[name] = base[name] || {};
  184. }
  185. base[last] = params[param];
  186. }
  187. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  188. }
  189. /* eslint-enable max-params */