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

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