您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.any.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // @flow
  2. import Bourne from '@hapi/bourne';
  3. import { jitsiLocalStorage } from '@jitsi/js-utils';
  4. import _ from 'lodash';
  5. import { parseURLParams } from '../util';
  6. import CONFIG_WHITELIST from './configWhitelist';
  7. import { _CONFIG_STORE_PREFIX } from './constants';
  8. import INTERFACE_CONFIG_WHITELIST from './interfaceConfigWhitelist';
  9. import logger from './logger';
  10. // XXX The function getRoomName is split out of
  11. // functions.js because it is bundled in both app.bundle and
  12. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  13. // want all functions to be bundled in do_external_connect.
  14. export { default as getRoomName } from './getRoomName';
  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. * @returns {Object} - The result object only with the keys
  102. * that are whitelisted.
  103. */
  104. export function getWhitelistedJSON(configName: string, configJSON: Object): Object {
  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 Bourne.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 */