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

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