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 'js-utils';
  3. import _ from 'lodash';
  4. import CONFIG_WHITELIST from './configWhitelist';
  5. import { _CONFIG_STORE_PREFIX } from './constants';
  6. import INTERFACE_CONFIG_WHITELIST from './interfaceConfigWhitelist';
  7. import { parseURLParams } from '../util';
  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. * @private
  101. * @returns {Object} - The result object only with the keys
  102. * that are whitelisted.
  103. */
  104. function _getWhitelistedJSON(configName, configJSON) {
  105. if (configName === 'interfaceConfig') {
  106. return _.pick(configJSON, INTERFACE_CONFIG_WHITELIST);
  107. } else if (configName === 'config') {
  108. return _.pick(configJSON, CONFIG_WHITELIST);
  109. }
  110. return configJSON;
  111. }
  112. /**
  113. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  114. * previously downloaded from a specific {@code baseURL} and stored with
  115. * {@link storeConfig}.
  116. *
  117. * @param {string} baseURL - The base URL from which the config.js was
  118. * previously downloaded and stored with {@code storeConfig}.
  119. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  120. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  121. * otherwise, {@code undefined}.
  122. */
  123. export function restoreConfig(baseURL: string): ?Object {
  124. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  125. const config = jitsiLocalStorage.getItem(key);
  126. if (config) {
  127. try {
  128. return JSON.parse(config) || undefined;
  129. } catch (e) {
  130. // Somehow incorrect data ended up in the storage. Clean it up.
  131. jitsiLocalStorage.removeItem(key);
  132. }
  133. }
  134. return undefined;
  135. }
  136. /* eslint-disable max-params */
  137. /**
  138. * Inspects the hash part of the location URI and overrides values specified
  139. * there in the corresponding config objects given as the arguments. The syntax
  140. * is: {@code https://server.com/room#config.debug=true
  141. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  142. *
  143. * In the hash part each parameter will be parsed to JSON and then the root
  144. * object will be matched with the corresponding config object given as the
  145. * argument to this function.
  146. *
  147. * @param {Object} config - This is the general config.
  148. * @param {Object} interfaceConfig - This is the interface config.
  149. * @param {Object} loggingConfig - The logging config.
  150. * @param {URI} location - The new location to which the app is navigating to.
  151. * @returns {void}
  152. */
  153. export function setConfigFromURLParams(
  154. config: ?Object,
  155. interfaceConfig: ?Object,
  156. loggingConfig: ?Object,
  157. location: Object) {
  158. const params = parseURLParams(location);
  159. const json = {};
  160. // At this point we have:
  161. // params = {
  162. // "config.disableAudioLevels": false,
  163. // "config.channelLastN": -1,
  164. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  165. // }
  166. // We want to have:
  167. // json = {
  168. // config: {
  169. // "disableAudioLevels": false,
  170. // "channelLastN": -1
  171. // },
  172. // interfaceConfig: {
  173. // "APP_NAME": "Jitsi Meet"
  174. // }
  175. // }
  176. config && (json.config = {});
  177. interfaceConfig && (json.interfaceConfig = {});
  178. loggingConfig && (json.loggingConfig = {});
  179. for (const param of Object.keys(params)) {
  180. let base = json;
  181. const names = param.split('.');
  182. const last = names.pop();
  183. for (const name of names) {
  184. base = base[name] = base[name] || {};
  185. }
  186. base[last] = params[param];
  187. }
  188. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  189. }
  190. /* eslint-enable max-params */