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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // @flow
  2. import _ from 'lodash';
  3. import { APP_WILL_MOUNT } from '../app';
  4. import { setAudioOnly } from '../audio-only';
  5. import parseURLParams from '../config/parseURLParams'; // minimize imports to avoid circular imports
  6. import { SET_LOCATION_URL } from '../connection/actionTypes'; // minimize imports to avoid circular imports
  7. import { getLocalParticipant, participantUpdated } from '../participants';
  8. import { MiddlewareRegistry } from '../redux';
  9. import { SETTINGS_UPDATED } from './actionTypes';
  10. import { handleCallIntegrationChange } from './functions';
  11. /**
  12. * The middleware of the feature base/settings. Distributes changes to the state
  13. * of base/settings to the states of other features computed from the state of
  14. * base/settings.
  15. *
  16. * @param {Store} store - The redux store.
  17. * @returns {Function}
  18. */
  19. MiddlewareRegistry.register(store => next => action => {
  20. const result = next(action);
  21. switch (action.type) {
  22. case APP_WILL_MOUNT:
  23. _initializeCallIntegration(store);
  24. break;
  25. case SETTINGS_UPDATED:
  26. _maybeHandleCallIntegrationChange(action);
  27. _maybeSetAudioOnly(store, action);
  28. _updateLocalParticipant(store, action);
  29. break;
  30. case SET_LOCATION_URL:
  31. _updateLocalParticipantFromUrl(store);
  32. break;
  33. }
  34. return result;
  35. });
  36. /**
  37. * Initializes the audio device handler based on the `disableCallIntegration` setting.
  38. *
  39. * @param {Store} store - The redux store.
  40. * @private
  41. * @returns {void}
  42. */
  43. function _initializeCallIntegration({ getState }) {
  44. const { disableCallIntegration } = getState()['features/base/settings'];
  45. if (typeof disableCallIntegration === 'boolean') {
  46. handleCallIntegrationChange(disableCallIntegration);
  47. }
  48. }
  49. /**
  50. * Maps the settings field names to participant names where they don't match.
  51. * Currently there is only one such field, but may be extended in the future.
  52. *
  53. * @private
  54. * @param {string} settingsField - The name of the settings field to map.
  55. * @returns {string}
  56. */
  57. function _mapSettingsFieldToParticipant(settingsField) {
  58. switch (settingsField) {
  59. case 'displayName':
  60. return 'name';
  61. }
  62. return settingsField;
  63. }
  64. /**
  65. * Handles a change in the `disableCallIntegration` setting.
  66. *
  67. * @param {Object} action - The redux action.
  68. * @private
  69. * @returns {void}
  70. */
  71. function _maybeHandleCallIntegrationChange({ settings: { disableCallIntegration } }) {
  72. if (typeof disableCallIntegration === 'boolean') {
  73. handleCallIntegrationChange(disableCallIntegration);
  74. }
  75. }
  76. /**
  77. * Updates {@code startAudioOnly} flag if it's updated in the settings.
  78. *
  79. * @param {Store} store - The redux store.
  80. * @param {Object} action - The redux action.
  81. * @private
  82. * @returns {void}
  83. */
  84. function _maybeSetAudioOnly(
  85. { dispatch },
  86. { settings: { startAudioOnly } }) {
  87. if (typeof startAudioOnly === 'boolean') {
  88. dispatch(setAudioOnly(startAudioOnly, true));
  89. }
  90. }
  91. /**
  92. * Updates the local participant according to settings changes.
  93. *
  94. * @param {Store} store - The redux store.
  95. * @param {Object} action - The dispatched action.
  96. * @private
  97. * @returns {void}
  98. */
  99. function _updateLocalParticipant({ dispatch, getState }, action) {
  100. const { settings } = action;
  101. const localParticipant = getLocalParticipant(getState());
  102. const newLocalParticipant = {
  103. ...localParticipant
  104. };
  105. for (const key in settings) {
  106. if (settings.hasOwnProperty(key)) {
  107. newLocalParticipant[_mapSettingsFieldToParticipant(key)]
  108. = settings[key];
  109. }
  110. }
  111. dispatch(participantUpdated(newLocalParticipant));
  112. }
  113. /**
  114. * Returns the userInfo set in the URL.
  115. *
  116. * @param {Store} store - The redux store.
  117. * @private
  118. * @returns {void}
  119. */
  120. function _updateLocalParticipantFromUrl({ dispatch, getState }) {
  121. const urlParams
  122. = parseURLParams(getState()['features/base/connection'].locationURL);
  123. const urlEmail = urlParams['userInfo.email'];
  124. if (!urlEmail) {
  125. return;
  126. }
  127. const localParticipant = getLocalParticipant(getState());
  128. if (localParticipant) {
  129. dispatch(participantUpdated({
  130. ...localParticipant,
  131. email: _.escape(urlEmail)
  132. }));
  133. }
  134. }