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.any.ts 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* eslint-disable lines-around-comment */
  2. import _ from 'lodash';
  3. import { AnyAction } from 'redux';
  4. import { IStore } from '../../app/types';
  5. import { SET_LOCATION_URL } from '../connection/actionTypes';
  6. import { participantUpdated } from '../participants/actions';
  7. import { getLocalParticipant } from '../participants/functions';
  8. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  9. import { parseURLParams } from '../util/parseURLParams';
  10. import { SETTINGS_UPDATED } from './actionTypes';
  11. import { updateSettings } from './actions';
  12. /**
  13. * The middleware of the feature base/settings. Distributes changes to the state
  14. * of base/settings to the states of other features computed from the state of
  15. * base/settings.
  16. *
  17. * @param {Store} store - The redux store.
  18. * @returns {Function}
  19. */
  20. MiddlewareRegistry.register(store => next => action => {
  21. const result = next(action);
  22. switch (action.type) {
  23. case SETTINGS_UPDATED:
  24. _updateLocalParticipant(store, action);
  25. break;
  26. case SET_LOCATION_URL:
  27. _updateLocalParticipantFromUrl(store);
  28. break;
  29. }
  30. return result;
  31. });
  32. /**
  33. * Maps the settings field names to participant names where they don't match.
  34. * Currently there is only one such field, but may be extended in the future.
  35. *
  36. * @private
  37. * @param {string} settingsField - The name of the settings field to map.
  38. * @returns {string}
  39. */
  40. function _mapSettingsFieldToParticipant(settingsField: string) {
  41. switch (settingsField) {
  42. case 'displayName':
  43. return 'name';
  44. }
  45. return settingsField;
  46. }
  47. /**
  48. * Updates the local participant according to settings changes.
  49. *
  50. * @param {Store} store - The redux store.
  51. * @param {Object} action - The dispatched action.
  52. * @private
  53. * @returns {void}
  54. */
  55. function _updateLocalParticipant({ dispatch, getState }: IStore, action: AnyAction) {
  56. const { settings } = action;
  57. const localParticipant = getLocalParticipant(getState());
  58. const newLocalParticipant = {
  59. ...localParticipant
  60. };
  61. for (const key in settings) {
  62. if (settings.hasOwnProperty(key)) {
  63. newLocalParticipant[_mapSettingsFieldToParticipant(key) as keyof typeof newLocalParticipant]
  64. = settings[key];
  65. }
  66. }
  67. dispatch(participantUpdated({
  68. ...newLocalParticipant,
  69. id: newLocalParticipant.id ?? ''
  70. }));
  71. }
  72. /**
  73. * Returns the userInfo set in the URL.
  74. *
  75. * @param {Store} store - The redux store.
  76. * @private
  77. * @returns {void}
  78. */
  79. function _updateLocalParticipantFromUrl({ dispatch, getState }: IStore) {
  80. const urlParams
  81. = parseURLParams(getState()['features/base/connection'].locationURL ?? '');
  82. const urlEmail = urlParams['userInfo.email'];
  83. const urlDisplayName = urlParams['userInfo.displayName'];
  84. if (!urlEmail && !urlDisplayName) {
  85. return;
  86. }
  87. const localParticipant = getLocalParticipant(getState());
  88. if (localParticipant) {
  89. const displayName = _.escape(urlDisplayName);
  90. const email = _.escape(urlEmail);
  91. dispatch(participantUpdated({
  92. ...localParticipant,
  93. email,
  94. name: displayName
  95. }));
  96. dispatch(updateSettings({
  97. displayName,
  98. email
  99. }));
  100. }
  101. }