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

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