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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // @flow
  2. import _ from 'lodash';
  3. import CONFIG_WHITELIST from './configWhitelist';
  4. import { _CONFIG_STORE_PREFIX } from './constants';
  5. import INTERFACE_CONFIG_WHITELIST from './interfaceConfigWhitelist';
  6. import parseURLParams from './parseURLParams';
  7. import logger from './logger';
  8. declare var $: Object;
  9. // XXX The functions getRoomName and parseURLParams are split out of
  10. // functions.js because they are bundled in both app.bundle and
  11. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  12. // want all functions to be bundled in do_external_connect.
  13. export { default as getRoomName } from './getRoomName';
  14. export { parseURLParams };
  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. * Promise wrapper on obtain config method. When HttpConfigFetch will be moved
  38. * to React app it's better to use load config instead.
  39. *
  40. * @param {string} location - URL of the domain from which the config is to be
  41. * obtained.
  42. * @param {string} room - Room name.
  43. * @private
  44. * @returns {Promise<void>}
  45. */
  46. export function obtainConfig(location: string, room: string): Promise<void> {
  47. return new Promise((resolve, reject) =>
  48. _obtainConfig(location, room, (success, error) => {
  49. success ? resolve() : reject(error);
  50. })
  51. );
  52. }
  53. /**
  54. * Sends HTTP POST request to specified {@code endpoint}. In request the name
  55. * of the room is included in JSON format:
  56. * {
  57. * "rooomName": "someroom12345"
  58. * }.
  59. *
  60. * @param {string} endpoint - The name of HTTP endpoint to which to send
  61. * the HTTP POST request.
  62. * @param {string} roomName - The name of the conference room for which config
  63. * is requested.
  64. * @param {Function} complete - The callback to invoke upon success or failure.
  65. * @returns {void}
  66. */
  67. function _obtainConfig(endpoint: string, roomName: string, complete: Function) {
  68. logger.info(`Send config request to ${endpoint} for room: ${roomName}`);
  69. $.ajax(
  70. endpoint,
  71. {
  72. contentType: 'application/json',
  73. data: JSON.stringify({ roomName }),
  74. dataType: 'json',
  75. method: 'POST',
  76. error(jqXHR, textStatus, errorThrown) {
  77. logger.error('Get config error: ', jqXHR, errorThrown);
  78. complete(false, `Get config response status: ${textStatus}`);
  79. },
  80. success(data) {
  81. const { config, interfaceConfig, loggingConfig } = window;
  82. try {
  83. overrideConfigJSON(
  84. config, interfaceConfig, loggingConfig,
  85. data);
  86. complete(true);
  87. } catch (e) {
  88. logger.error('Parse config error: ', e);
  89. complete(false, e);
  90. }
  91. }
  92. }
  93. );
  94. }
  95. /* eslint-disable max-params, no-shadow */
  96. /**
  97. * Overrides JSON properties in {@code config} and
  98. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  99. * Overrides only the whitelisted keys.
  100. *
  101. * @param {Object} config - The config Object in which we'll be overriding
  102. * properties.
  103. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  104. * be overriding properties.
  105. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  106. * overriding properties.
  107. * @param {Object} json - Object containing configuration properties.
  108. * Destination object is selected based on root property name:
  109. * {
  110. * config: {
  111. * // config.js properties here
  112. * },
  113. * interfaceConfig: {
  114. * // interface_config.js properties here
  115. * },
  116. * loggingConfig: {
  117. * // logging_config.js properties here
  118. * }
  119. * }.
  120. * @returns {void}
  121. */
  122. export function overrideConfigJSON(
  123. config: ?Object, interfaceConfig: ?Object, loggingConfig: ?Object,
  124. json: Object) {
  125. for (const configName of Object.keys(json)) {
  126. let configObj;
  127. if (configName === 'config') {
  128. configObj = config;
  129. } else if (configName === 'interfaceConfig') {
  130. configObj = interfaceConfig;
  131. } else if (configName === 'loggingConfig') {
  132. configObj = loggingConfig;
  133. }
  134. if (configObj) {
  135. const configJSON
  136. = _getWhitelistedJSON(configName, json[configName]);
  137. if (!_.isEmpty(configJSON)) {
  138. logger.info(
  139. `Extending ${configName} with: ${
  140. JSON.stringify(configJSON)}`);
  141. // eslint-disable-next-line arrow-body-style
  142. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  143. // XXX We don't want to merge the arrays, we want to
  144. // overwrite them.
  145. return Array.isArray(oldValue) ? newValue : undefined;
  146. });
  147. }
  148. }
  149. }
  150. }
  151. /* eslint-enable max-params, no-shadow */
  152. /**
  153. * Apply whitelist filtering for configs with whitelists, skips this for others
  154. * configs (loggingConfig).
  155. * Only extracts overridden values for keys we allow to be overridden.
  156. *
  157. * @param {string} configName - The config name, one of config,
  158. * interfaceConfig, loggingConfig.
  159. * @param {Object} configJSON - The object with keys and values to override.
  160. * @private
  161. * @returns {Object} - The result object only with the keys
  162. * that are whitelisted.
  163. */
  164. function _getWhitelistedJSON(configName, configJSON) {
  165. if (configName === 'interfaceConfig') {
  166. return _.pick(configJSON, INTERFACE_CONFIG_WHITELIST);
  167. } else if (configName === 'config') {
  168. return _.pick(configJSON, CONFIG_WHITELIST);
  169. }
  170. return configJSON;
  171. }
  172. /**
  173. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  174. * previously downloaded from a specific {@code baseURL} and stored with
  175. * {@link storeConfig}.
  176. *
  177. * @param {string} baseURL - The base URL from which the config.js was
  178. * previously downloaded and stored with {@code storeConfig}.
  179. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  180. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  181. * otherwise, {@code undefined}.
  182. */
  183. export function restoreConfig(baseURL: string): ?Object {
  184. let storage;
  185. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  186. try {
  187. // XXX Even reading the property localStorage of window may throw an
  188. // error (which is user agent-specific behavior).
  189. storage = window.localStorage;
  190. const config = storage.getItem(key);
  191. if (config) {
  192. return JSON.parse(config) || undefined;
  193. }
  194. } catch (e) {
  195. // Somehow incorrect data ended up in the storage. Clean it up.
  196. storage && storage.removeItem(key);
  197. }
  198. return undefined;
  199. }
  200. /* eslint-disable max-params */
  201. /**
  202. * Inspects the hash part of the location URI and overrides values specified
  203. * there in the corresponding config objects given as the arguments. The syntax
  204. * is: {@code https://server.com/room#config.debug=true
  205. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  206. *
  207. * In the hash part each parameter will be parsed to JSON and then the root
  208. * object will be matched with the corresponding config object given as the
  209. * argument to this function.
  210. *
  211. * @param {Object} config - This is the general config.
  212. * @param {Object} interfaceConfig - This is the interface config.
  213. * @param {Object} loggingConfig - The logging config.
  214. * @param {URI} location - The new location to which the app is navigating to.
  215. * @returns {void}
  216. */
  217. export function setConfigFromURLParams(
  218. config: ?Object,
  219. interfaceConfig: ?Object,
  220. loggingConfig: ?Object,
  221. location: Object) {
  222. const params = parseURLParams(location);
  223. const json = {};
  224. // At this point we have:
  225. // params = {
  226. // "config.disableAudioLevels": false,
  227. // "config.channelLastN": -1,
  228. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  229. // }
  230. // We want to have:
  231. // json = {
  232. // config: {
  233. // "disableAudioLevels": false,
  234. // "channelLastN": -1
  235. // },
  236. // interfaceConfig: {
  237. // "APP_NAME": "Jitsi Meet"
  238. // }
  239. // }
  240. config && (json.config = {});
  241. interfaceConfig && (json.interfaceConfig = {});
  242. loggingConfig && (json.loggingConfig = {});
  243. for (const param of Object.keys(params)) {
  244. let base = json;
  245. const names = param.split('.');
  246. const last = names.pop();
  247. for (const name of names) {
  248. base = base[name] = base[name] || {};
  249. }
  250. base[last] = params[param];
  251. }
  252. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  253. }
  254. /* eslint-enable max-params */