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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // @flow
  2. import _ from 'lodash';
  3. import { APP_WILL_MOUNT } from '../app';
  4. import { setAudioOnly } from '../audio-only';
  5. import { parseURLParams } from '../util';
  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, handleCrashReportingChange } 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. _maybeCrashReportingChange(action);
  30. break;
  31. case SET_LOCATION_URL:
  32. _updateLocalParticipantFromUrl(store);
  33. break;
  34. }
  35. return result;
  36. });
  37. /**
  38. * Initializes the audio device handler based on the `disableCallIntegration` setting.
  39. *
  40. * @param {Store} store - The redux store.
  41. * @private
  42. * @returns {void}
  43. */
  44. function _initializeCallIntegration({ getState }) {
  45. const { disableCallIntegration } = getState()['features/base/settings'];
  46. if (typeof disableCallIntegration === 'boolean') {
  47. handleCallIntegrationChange(disableCallIntegration);
  48. }
  49. }
  50. /**
  51. * Maps the settings field names to participant names where they don't match.
  52. * Currently there is only one such field, but may be extended in the future.
  53. *
  54. * @private
  55. * @param {string} settingsField - The name of the settings field to map.
  56. * @returns {string}
  57. */
  58. function _mapSettingsFieldToParticipant(settingsField) {
  59. switch (settingsField) {
  60. case 'displayName':
  61. return 'name';
  62. }
  63. return settingsField;
  64. }
  65. /**
  66. * Handles a change in the `disableCallIntegration` setting.
  67. *
  68. * @param {Object} action - The redux action.
  69. * @private
  70. * @returns {void}
  71. */
  72. function _maybeHandleCallIntegrationChange({ settings: { disableCallIntegration } }) {
  73. if (typeof disableCallIntegration === 'boolean') {
  74. handleCallIntegrationChange(disableCallIntegration);
  75. }
  76. }
  77. /**
  78. * Handles a change in the `disableCrashReporting` setting.
  79. *
  80. * @param {Object} action - The redux action.
  81. * @private
  82. * @returns {void}
  83. */
  84. function _maybeCrashReportingChange({ settings: { disableCrashReporting } }) {
  85. if (typeof disableCrashReporting === 'boolean') {
  86. handleCrashReportingChange(disableCrashReporting);
  87. }
  88. }
  89. /**
  90. * Updates {@code startAudioOnly} flag if it's updated in the settings.
  91. *
  92. * @param {Store} store - The redux store.
  93. * @param {Object} action - The redux action.
  94. * @private
  95. * @returns {void}
  96. */
  97. function _maybeSetAudioOnly(
  98. { dispatch },
  99. { settings: { startAudioOnly } }) {
  100. if (typeof startAudioOnly === 'boolean') {
  101. dispatch(setAudioOnly(startAudioOnly, true));
  102. }
  103. }
  104. /**
  105. * Updates the local participant according to settings changes.
  106. *
  107. * @param {Store} store - The redux store.
  108. * @param {Object} action - The dispatched action.
  109. * @private
  110. * @returns {void}
  111. */
  112. function _updateLocalParticipant({ dispatch, getState }, action) {
  113. const { settings } = action;
  114. const localParticipant = getLocalParticipant(getState());
  115. const newLocalParticipant = {
  116. ...localParticipant
  117. };
  118. for (const key in settings) {
  119. if (settings.hasOwnProperty(key)) {
  120. newLocalParticipant[_mapSettingsFieldToParticipant(key)]
  121. = settings[key];
  122. }
  123. }
  124. dispatch(participantUpdated(newLocalParticipant));
  125. }
  126. /**
  127. * Returns the userInfo set in the URL.
  128. *
  129. * @param {Store} store - The redux store.
  130. * @private
  131. * @returns {void}
  132. */
  133. function _updateLocalParticipantFromUrl({ dispatch, getState }) {
  134. const urlParams
  135. = parseURLParams(getState()['features/base/connection'].locationURL);
  136. const urlEmail = urlParams['userInfo.email'];
  137. const urlDisplayName = urlParams['userInfo.displayName'];
  138. if (!urlEmail && !urlDisplayName) {
  139. return;
  140. }
  141. const localParticipant = getLocalParticipant(getState());
  142. if (localParticipant) {
  143. dispatch(participantUpdated({
  144. ...localParticipant,
  145. email: _.escape(urlEmail),
  146. name: _.escape(urlDisplayName)
  147. }));
  148. }
  149. }