Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.ts 6.5KB

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