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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. * Selector for determining if the display name is read only.
  139. *
  140. * @param {Object} state - The state of the app.
  141. * @returns {boolean}
  142. */
  143. export function isNameReadOnly(state: Object): boolean {
  144. return state['features/base/config'].disableProfile
  145. || state['features/base/config'].readOnlyName;
  146. }
  147. /**
  148. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  149. * previously downloaded from a specific {@code baseURL} and stored with
  150. * {@link storeConfig}.
  151. *
  152. * @param {string} baseURL - The base URL from which the config.js was
  153. * previously downloaded and stored with {@code storeConfig}.
  154. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  155. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  156. * otherwise, {@code undefined}.
  157. */
  158. export function restoreConfig(baseURL: string): ?Object {
  159. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  160. const config = jitsiLocalStorage.getItem(key);
  161. if (config) {
  162. try {
  163. return Bourne.parse(config) || undefined;
  164. } catch (e) {
  165. // Somehow incorrect data ended up in the storage. Clean it up.
  166. jitsiLocalStorage.removeItem(key);
  167. }
  168. }
  169. return undefined;
  170. }
  171. /* eslint-disable max-params */
  172. /**
  173. * Inspects the hash part of the location URI and overrides values specified
  174. * there in the corresponding config objects given as the arguments. The syntax
  175. * is: {@code https://server.com/room#config.debug=true
  176. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  177. *
  178. * In the hash part each parameter will be parsed to JSON and then the root
  179. * object will be matched with the corresponding config object given as the
  180. * argument to this function.
  181. *
  182. * @param {Object} config - This is the general config.
  183. * @param {Object} interfaceConfig - This is the interface config.
  184. * @param {Object} loggingConfig - The logging config.
  185. * @param {URI} location - The new location to which the app is navigating to.
  186. * @returns {void}
  187. */
  188. export function setConfigFromURLParams(
  189. config: ?Object,
  190. interfaceConfig: ?Object,
  191. loggingConfig: ?Object,
  192. location: Object) {
  193. const params = parseURLParams(location);
  194. const json = {};
  195. // At this point we have:
  196. // params = {
  197. // "config.disableAudioLevels": false,
  198. // "config.channelLastN": -1,
  199. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  200. // }
  201. // We want to have:
  202. // json = {
  203. // config: {
  204. // "disableAudioLevels": false,
  205. // "channelLastN": -1
  206. // },
  207. // interfaceConfig: {
  208. // "APP_NAME": "Jitsi Meet"
  209. // }
  210. // }
  211. config && (json.config = {});
  212. interfaceConfig && (json.interfaceConfig = {});
  213. loggingConfig && (json.loggingConfig = {});
  214. for (const param of Object.keys(params)) {
  215. let base = json;
  216. const names = param.split('.');
  217. const last = names.pop();
  218. for (const name of names) {
  219. base = base[name] = base[name] || {};
  220. }
  221. base[last] = params[param];
  222. }
  223. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  224. }
  225. /* eslint-enable max-params */