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.4KB

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