您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.any.ts 9.7KB

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