Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

functions.any.js 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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, FEATURE_FLAGS } 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 sourceNameSignaling feature flag.
  46. *
  47. * @param {Object} state - The global state.
  48. * @returns {boolean}
  49. */
  50. export function getSourceNameSignalingFeatureFlag(state: Object) {
  51. return getFeatureFlag(state, FEATURE_FLAGS.SOURCE_NAME_SIGNALING);
  52. }
  53. /**
  54. * Selector used to get a feature flag.
  55. *
  56. * @param {Object} state - The global state.
  57. * @param {string} featureFlag - The name of the feature flag.
  58. * @returns {boolean}
  59. */
  60. export function getFeatureFlag(state: Object, featureFlag: string) {
  61. const featureFlags = state['features/base/config']?.flags || {};
  62. return Boolean(featureFlags[featureFlag]);
  63. }
  64. /**
  65. * Selector used to get the disableRemoveRaisedHandOnFocus.
  66. *
  67. * @param {Object} state - The global state.
  68. * @returns {boolean}
  69. */
  70. export function getDisableRemoveRaisedHandOnFocus(state: Object) {
  71. return state['features/base/config']?.disableRemoveRaisedHandOnFocus || false;
  72. }
  73. /**
  74. * Selector used to get the endpoint used for fetching the recording.
  75. *
  76. * @param {Object} state - The global state.
  77. * @returns {string}
  78. */
  79. export function getRecordingSharingUrl(state: Object) {
  80. return state['features/base/config'].recordingSharingUrl;
  81. }
  82. /**
  83. * Overrides JSON properties in {@code config} and
  84. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  85. * Overrides only the whitelisted keys.
  86. *
  87. * @param {Object} config - The config Object in which we'll be overriding
  88. * properties.
  89. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  90. * be overriding properties.
  91. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  92. * overriding properties.
  93. * @param {Object} json - Object containing configuration properties.
  94. * Destination object is selected based on root property name:
  95. * {
  96. * config: {
  97. * // config.js properties here
  98. * },
  99. * interfaceConfig: {
  100. * // interface_config.js properties here
  101. * },
  102. * loggingConfig: {
  103. * // logging_config.js properties here
  104. * }
  105. * }.
  106. * @returns {void}
  107. */
  108. export function overrideConfigJSON(
  109. config: ?Object, interfaceConfig: ?Object, loggingConfig: ?Object,
  110. json: Object) {
  111. for (const configName of Object.keys(json)) {
  112. let configObj;
  113. if (configName === 'config') {
  114. configObj = config;
  115. } else if (configName === 'interfaceConfig') {
  116. configObj = interfaceConfig;
  117. } else if (configName === 'loggingConfig') {
  118. configObj = loggingConfig;
  119. }
  120. if (configObj) {
  121. const configJSON
  122. = getWhitelistedJSON(configName, json[configName]);
  123. if (!_.isEmpty(configJSON)) {
  124. logger.info(
  125. `Extending ${configName} with: ${
  126. JSON.stringify(configJSON)}`);
  127. // eslint-disable-next-line arrow-body-style
  128. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  129. // XXX We don't want to merge the arrays, we want to
  130. // overwrite them.
  131. return Array.isArray(oldValue) ? newValue : undefined;
  132. });
  133. }
  134. }
  135. }
  136. }
  137. /* eslint-enable max-params, no-shadow */
  138. /**
  139. * Apply whitelist filtering for configs with whitelists, skips this for others
  140. * configs (loggingConfig).
  141. * Only extracts overridden values for keys we allow to be overridden.
  142. *
  143. * @param {string} configName - The config name, one of config,
  144. * interfaceConfig, loggingConfig.
  145. * @param {Object} configJSON - The object with keys and values to override.
  146. * @returns {Object} - The result object only with the keys
  147. * that are whitelisted.
  148. */
  149. export function getWhitelistedJSON(configName: string, configJSON: Object): Object {
  150. if (configName === 'interfaceConfig') {
  151. return _.pick(configJSON, INTERFACE_CONFIG_WHITELIST);
  152. } else if (configName === 'config') {
  153. return _.pick(configJSON, CONFIG_WHITELIST);
  154. }
  155. return configJSON;
  156. }
  157. /**
  158. * Selector for determining if the display name is read only.
  159. *
  160. * @param {Object} state - The state of the app.
  161. * @returns {boolean}
  162. */
  163. export function isNameReadOnly(state: Object): boolean {
  164. return state['features/base/config'].disableProfile
  165. || state['features/base/config'].readOnlyName;
  166. }
  167. /**
  168. * Selector for determining if the display name is visible.
  169. *
  170. * @param {Object} state - The state of the app.
  171. * @returns {boolean}
  172. */
  173. export function isDisplayNameVisible(state: Object): boolean {
  174. return !state['features/base/config'].hideDisplayName;
  175. }
  176. /**
  177. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  178. * previously downloaded from a specific {@code baseURL} and stored with
  179. * {@link storeConfig}.
  180. *
  181. * @param {string} baseURL - The base URL from which the config.js was
  182. * previously downloaded and stored with {@code storeConfig}.
  183. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  184. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  185. * otherwise, {@code undefined}.
  186. */
  187. export function restoreConfig(baseURL: string): ?Object {
  188. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  189. const config = jitsiLocalStorage.getItem(key);
  190. if (config) {
  191. try {
  192. return Bourne.parse(config) || undefined;
  193. } catch (e) {
  194. // Somehow incorrect data ended up in the storage. Clean it up.
  195. jitsiLocalStorage.removeItem(key);
  196. }
  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 */