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.ts 9.8KB

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