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

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