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.2KB

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