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 5.4KB

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