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

middleware.ts 6.6KB

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