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

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