您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.any.ts 3.2KB

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