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

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