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.

middleware.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // @flow
  2. import { jitsiLocalStorage } from '@jitsi/js-utils';
  3. import { APP_WILL_MOUNT } from '../app';
  4. import { getFeatureFlag } from '../flags/functions';
  5. import { addKnownDomains } from '../known-domains';
  6. import { MiddlewareRegistry } from '../redux';
  7. import { parseURIString } from '../util';
  8. import { SET_CONFIG } from './actionTypes';
  9. import { updateConfig } from './actions';
  10. import { _CONFIG_STORE_PREFIX } from './constants';
  11. /**
  12. * The middleware of the feature {@code base/config}.
  13. *
  14. * @param {Store} store - The redux store.
  15. * @private
  16. * @returns {Function}
  17. */
  18. MiddlewareRegistry.register(store => next => action => {
  19. switch (action.type) {
  20. case APP_WILL_MOUNT:
  21. return _appWillMount(store, next, action);
  22. case SET_CONFIG:
  23. return _setConfig(store, next, action);
  24. }
  25. return next(action);
  26. });
  27. /**
  28. * Notifies the feature {@code base/config} that the {@link APP_WILL_MOUNT}
  29. * redux action is being {@code dispatch}ed in a specific redux store.
  30. *
  31. * @param {Store} store - The redux store in which the specified {@code action}
  32. * is being dispatched.
  33. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  34. * specified {@code action} in the specified {@code store}.
  35. * @param {Action} action - The redux action which is being {@code dispatch}ed
  36. * in the specified {@code store}.
  37. * @private
  38. * @returns {*} The return value of {@code next(action)}.
  39. */
  40. function _appWillMount(store, next, action) {
  41. const result = next(action);
  42. // It's an opportune time to transfer the feature base/config's knowledge
  43. // about "known domains" (which is local to the feature) to the feature
  44. // base/known-domains (which is global to the app).
  45. //
  46. // XXX Since the feature base/config predates the feature calendar-sync and,
  47. // consequently, the feature known-domains, it's possible for the feature
  48. // base/config to know of domains which the feature known-domains is yet to
  49. // discover.
  50. const prefix = `${_CONFIG_STORE_PREFIX}/`;
  51. const knownDomains = [];
  52. for (let i = 0; /* localStorage.key(i) */; ++i) {
  53. const key = jitsiLocalStorage.key(i);
  54. if (key) {
  55. let baseURL;
  56. if (key.startsWith(prefix)
  57. && (baseURL = key.substring(prefix.length))) {
  58. const uri = parseURIString(baseURL);
  59. let host;
  60. uri && (host = uri.host) && knownDomains.push(host);
  61. }
  62. } else {
  63. break;
  64. }
  65. }
  66. knownDomains.length && store.dispatch(addKnownDomains(knownDomains));
  67. return result;
  68. }
  69. /**
  70. * Notifies the feature {@code base/config} that the {@link SET_CONFIG} redux
  71. * action is being {@code dispatch}ed in a specific redux store.
  72. *
  73. * @param {Store} store - The redux store in which the specified {@code action}
  74. * is being dispatched.
  75. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  76. * specified {@code action} in the specified {@code store}.
  77. * @param {Action} action - The redux action which is being {@code dispatch}ed
  78. * in the specified {@code store}.
  79. * @private
  80. * @returns {*} The return value of {@code next(action)}.
  81. */
  82. function _setConfig({ dispatch, getState }, next, action) {
  83. // The reducer is doing some alterations to the config passed in the action,
  84. // so make sure it's the final state by waiting for the action to be
  85. // reduced.
  86. const result = next(action);
  87. const state = getState();
  88. // Update the config with user defined settings.
  89. const settings = state['features/base/settings'];
  90. const config = {};
  91. if (typeof settings.disableP2P !== 'undefined') {
  92. config.p2p = { enabled: !settings.disableP2P };
  93. }
  94. const resolutionFlag = getFeatureFlag(state, 'resolution');
  95. if (typeof resolutionFlag !== 'undefined') {
  96. config.resolution = resolutionFlag;
  97. }
  98. dispatch(updateConfig(config));
  99. // FIXME On Web we rely on the global 'config' variable which gets altered
  100. // multiple times, before it makes it to the reducer. At some point it may
  101. // not be the global variable which is being modified anymore due to
  102. // different merge methods being used along the way. The global variable
  103. // must be synchronized with the final state resolved by the reducer.
  104. if (typeof window.config !== 'undefined') {
  105. window.config = state['features/base/config'];
  106. }
  107. return result;
  108. }