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

middleware.ts 6.6KB

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