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

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