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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // @flow
  2. import Bourne from '@hapi/bourne';
  3. import { jitsiLocalStorage } from '@jitsi/js-utils';
  4. import _ from 'lodash';
  5. import { browser } from '../lib-jitsi-meet';
  6. import { parseURLParams } from '../util';
  7. import CONFIG_WHITELIST from './configWhitelist';
  8. import { _CONFIG_STORE_PREFIX, FEATURE_FLAGS } from './constants';
  9. import INTERFACE_CONFIG_WHITELIST from './interfaceConfigWhitelist';
  10. import logger from './logger';
  11. // XXX The function getRoomName is split out of
  12. // functions.any.js because it is bundled in both app.bundle and
  13. // do_external_connect, webpack 1 does not support tree shaking, and we don't
  14. // want all functions to be bundled in do_external_connect.
  15. export { default as getRoomName } from './getRoomName';
  16. /**
  17. * Create a "fake" configuration object for the given base URL. This is used in case the config
  18. * couldn't be loaded in the welcome page, so at least we have something to try with.
  19. *
  20. * @param {string} baseURL - URL of the deployment for which we want the fake config.
  21. * @returns {Object}
  22. */
  23. export function createFakeConfig(baseURL: string) {
  24. const url = new URL(baseURL);
  25. return {
  26. hosts: {
  27. domain: url.hostname,
  28. muc: `conference.${url.hostname}`
  29. },
  30. bosh: `${baseURL}http-bind`,
  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 for determining if receiving multiple stream support is enabled.
  47. *
  48. * @param {Object} state - The global state.
  49. * @returns {boolean}
  50. */
  51. export function getMultipleVideoSupportFeatureFlag(state: Object) {
  52. return getFeatureFlag(state, FEATURE_FLAGS.MULTIPLE_VIDEO_STREAMS_SUPPORT)
  53. && getSourceNameSignalingFeatureFlag(state);
  54. }
  55. /**
  56. * Selector for determining if sending multiple stream support is enabled.
  57. *
  58. * @param {Object} state - The global state.
  59. * @returns {boolean}
  60. */
  61. export function getMultipleVideoSendingSupportFeatureFlag(state: Object) {
  62. return getMultipleVideoSupportFeatureFlag(state) && isUnifiedPlanEnabled(state);
  63. }
  64. /**
  65. * Selector used to get the sourceNameSignaling feature flag.
  66. *
  67. * @param {Object} state - The global state.
  68. * @returns {boolean}
  69. */
  70. export function getSourceNameSignalingFeatureFlag(state: Object) {
  71. return getFeatureFlag(state, FEATURE_FLAGS.SOURCE_NAME_SIGNALING);
  72. }
  73. /**
  74. * Selector used to get a feature flag.
  75. *
  76. * @param {Object} state - The global state.
  77. * @param {string} featureFlag - The name of the feature flag.
  78. * @returns {boolean}
  79. */
  80. export function getFeatureFlag(state: Object, featureFlag: string) {
  81. const featureFlags = state['features/base/config']?.flags || {};
  82. return Boolean(featureFlags[featureFlag]);
  83. }
  84. /**
  85. * Selector used to get the disableRemoveRaisedHandOnFocus.
  86. *
  87. * @param {Object} state - The global state.
  88. * @returns {boolean}
  89. */
  90. export function getDisableRemoveRaisedHandOnFocus(state: Object) {
  91. return state['features/base/config']?.disableRemoveRaisedHandOnFocus || false;
  92. }
  93. /**
  94. * Selector used to get the endpoint used for fetching the recording.
  95. *
  96. * @param {Object} state - The global state.
  97. * @returns {string}
  98. */
  99. export function getRecordingSharingUrl(state: Object) {
  100. return state['features/base/config'].recordingSharingUrl;
  101. }
  102. /**
  103. * Overrides JSON properties in {@code config} and
  104. * {@code interfaceConfig} Objects with the values from {@code newConfig}.
  105. * Overrides only the whitelisted keys.
  106. *
  107. * @param {Object} config - The config Object in which we'll be overriding
  108. * properties.
  109. * @param {Object} interfaceConfig - The interfaceConfig Object in which we'll
  110. * be overriding properties.
  111. * @param {Object} loggingConfig - The loggingConfig Object in which we'll be
  112. * overriding properties.
  113. * @param {Object} json - Object containing configuration properties.
  114. * Destination object is selected based on root property name:
  115. * {
  116. * config: {
  117. * // config.js properties here
  118. * },
  119. * interfaceConfig: {
  120. * // interface_config.js properties here
  121. * },
  122. * loggingConfig: {
  123. * // logging_config.js properties here
  124. * }
  125. * }.
  126. * @returns {void}
  127. */
  128. export function overrideConfigJSON(
  129. config: ?Object, interfaceConfig: ?Object, loggingConfig: ?Object,
  130. json: Object) {
  131. for (const configName of Object.keys(json)) {
  132. let configObj;
  133. if (configName === 'config') {
  134. configObj = config;
  135. } else if (configName === 'interfaceConfig') {
  136. configObj = interfaceConfig;
  137. } else if (configName === 'loggingConfig') {
  138. configObj = loggingConfig;
  139. }
  140. if (configObj) {
  141. const configJSON
  142. = getWhitelistedJSON(configName, json[configName]);
  143. if (!_.isEmpty(configJSON)) {
  144. logger.info(
  145. `Extending ${configName} with: ${
  146. JSON.stringify(configJSON)}`);
  147. // eslint-disable-next-line arrow-body-style
  148. _.mergeWith(configObj, configJSON, (oldValue, newValue) => {
  149. // XXX We don't want to merge the arrays, we want to
  150. // overwrite them.
  151. return Array.isArray(oldValue) ? newValue : undefined;
  152. });
  153. }
  154. }
  155. }
  156. }
  157. /* eslint-enable max-params, no-shadow */
  158. /**
  159. * Apply whitelist filtering for configs with whitelists, skips this for others
  160. * configs (loggingConfig).
  161. * Only extracts overridden values for keys we allow to be overridden.
  162. *
  163. * @param {string} configName - The config name, one of config,
  164. * interfaceConfig, loggingConfig.
  165. * @param {Object} configJSON - The object with keys and values to override.
  166. * @returns {Object} - The result object only with the keys
  167. * that are whitelisted.
  168. */
  169. export function getWhitelistedJSON(configName: string, configJSON: Object): Object {
  170. if (configName === 'interfaceConfig') {
  171. return _.pick(configJSON, INTERFACE_CONFIG_WHITELIST);
  172. } else if (configName === 'config') {
  173. return _.pick(configJSON, CONFIG_WHITELIST);
  174. }
  175. return configJSON;
  176. }
  177. /**
  178. * Selector for determining if the display name is read only.
  179. *
  180. * @param {Object} state - The state of the app.
  181. * @returns {boolean}
  182. */
  183. export function isNameReadOnly(state: Object): boolean {
  184. return state['features/base/config'].disableProfile
  185. || state['features/base/config'].readOnlyName;
  186. }
  187. /**
  188. * Selector for determining if the display name is visible.
  189. *
  190. * @param {Object} state - The state of the app.
  191. * @returns {boolean}
  192. */
  193. export function isDisplayNameVisible(state: Object): boolean {
  194. return !state['features/base/config'].hideDisplayName;
  195. }
  196. /**
  197. * Selector for determining if Unified plan support is enabled.
  198. *
  199. * @param {Object} state - The state of the app.
  200. * @returns {boolean}
  201. */
  202. export function isUnifiedPlanEnabled(state: Object): boolean {
  203. const { enableUnifiedOnChrome = true } = state['features/base/config'];
  204. return browser.supportsUnifiedPlan()
  205. && (!browser.isChromiumBased() || (browser.isChromiumBased() && enableUnifiedOnChrome));
  206. }
  207. /**
  208. * Restores a Jitsi Meet config.js from {@code localStorage} if it was
  209. * previously downloaded from a specific {@code baseURL} and stored with
  210. * {@link storeConfig}.
  211. *
  212. * @param {string} baseURL - The base URL from which the config.js was
  213. * previously downloaded and stored with {@code storeConfig}.
  214. * @returns {?Object} The Jitsi Meet config.js which was previously downloaded
  215. * from {@code baseURL} and stored with {@code storeConfig} if it was restored;
  216. * otherwise, {@code undefined}.
  217. */
  218. export function restoreConfig(baseURL: string): ?Object {
  219. const key = `${_CONFIG_STORE_PREFIX}/${baseURL}`;
  220. const config = jitsiLocalStorage.getItem(key);
  221. if (config) {
  222. try {
  223. return Bourne.parse(config) || undefined;
  224. } catch (e) {
  225. // Somehow incorrect data ended up in the storage. Clean it up.
  226. jitsiLocalStorage.removeItem(key);
  227. }
  228. }
  229. return undefined;
  230. }
  231. /* eslint-disable max-params */
  232. /**
  233. * Inspects the hash part of the location URI and overrides values specified
  234. * there in the corresponding config objects given as the arguments. The syntax
  235. * is: {@code https://server.com/room#config.debug=true
  236. * &interfaceConfig.showButton=false&loggingConfig.something=1}.
  237. *
  238. * In the hash part each parameter will be parsed to JSON and then the root
  239. * object will be matched with the corresponding config object given as the
  240. * argument to this function.
  241. *
  242. * @param {Object} config - This is the general config.
  243. * @param {Object} interfaceConfig - This is the interface config.
  244. * @param {Object} loggingConfig - The logging config.
  245. * @param {URI} location - The new location to which the app is navigating to.
  246. * @returns {void}
  247. */
  248. export function setConfigFromURLParams(
  249. config: ?Object,
  250. interfaceConfig: ?Object,
  251. loggingConfig: ?Object,
  252. location: Object) {
  253. const params = parseURLParams(location);
  254. const json = {};
  255. // At this point we have:
  256. // params = {
  257. // "config.disableAudioLevels": false,
  258. // "config.channelLastN": -1,
  259. // "interfaceConfig.APP_NAME": "Jitsi Meet"
  260. // }
  261. // We want to have:
  262. // json = {
  263. // config: {
  264. // "disableAudioLevels": false,
  265. // "channelLastN": -1
  266. // },
  267. // interfaceConfig: {
  268. // "APP_NAME": "Jitsi Meet"
  269. // }
  270. // }
  271. config && (json.config = {});
  272. interfaceConfig && (json.interfaceConfig = {});
  273. loggingConfig && (json.loggingConfig = {});
  274. for (const param of Object.keys(params)) {
  275. let base = json;
  276. const names = param.split('.');
  277. const last = names.pop();
  278. for (const name of names) {
  279. base = base[name] = base[name] || {};
  280. }
  281. base[last] = params[param];
  282. }
  283. overrideConfigJSON(config, interfaceConfig, loggingConfig, json);
  284. }
  285. /* eslint-enable max-params */