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.

reducer.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // @flow
  2. import { randomHexString } from 'js-utils/random';
  3. import _ from 'lodash';
  4. import { APP_WILL_MOUNT } from '../app';
  5. import { browser } from '../lib-jitsi-meet';
  6. import { ReducerRegistry } from '../redux';
  7. import { PersistenceRegistry } from '../storage';
  8. import { assignIfDefined } from '../util';
  9. import { SETTINGS_UPDATED } from './actionTypes';
  10. const logger = require('jitsi-meet-logger').getLogger(__filename);
  11. /**
  12. * The default/initial redux state of the feature {@code base/settings}.
  13. *
  14. * @type Object
  15. */
  16. const DEFAULT_STATE = {
  17. audioOutputDeviceId: undefined,
  18. avatarID: undefined,
  19. avatarURL: undefined,
  20. cameraDeviceId: undefined,
  21. displayName: undefined,
  22. email: undefined,
  23. localFlipX: true,
  24. micDeviceId: undefined,
  25. serverURL: undefined,
  26. startAudioOnly: false,
  27. startWithAudioMuted: false,
  28. startWithVideoMuted: false,
  29. userSelectedAudioOutputDeviceId: undefined,
  30. userSelectedCameraDeviceId: undefined,
  31. userSelectedMicDeviceId: undefined
  32. };
  33. const STORE_NAME = 'features/base/settings';
  34. /**
  35. * Sets up the persistence of the feature {@code base/settings}.
  36. */
  37. const filterSubtree = {};
  38. // start with the default state
  39. Object.keys(DEFAULT_STATE).forEach(key => {
  40. filterSubtree[key] = true;
  41. });
  42. // we want to filter these props, to not be stored as they represent
  43. // what is currently opened/used as devices
  44. filterSubtree.audioOutputDeviceId = false;
  45. filterSubtree.cameraDeviceId = false;
  46. filterSubtree.micDeviceId = false;
  47. PersistenceRegistry.register(STORE_NAME, filterSubtree);
  48. ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
  49. switch (action.type) {
  50. case APP_WILL_MOUNT:
  51. return _initSettings(state);
  52. case SETTINGS_UPDATED:
  53. return {
  54. ...state,
  55. ...action.settings
  56. };
  57. }
  58. return state;
  59. });
  60. /**
  61. * Retrieves the legacy profile values regardless of it's being in pre or
  62. * post-flattening format.
  63. *
  64. * FIXME: Let's remove this after a predefined time (e.g. By July 2018) to avoid
  65. * garbage in the source.
  66. *
  67. * @private
  68. * @returns {Object}
  69. */
  70. function _getLegacyProfile() {
  71. let persistedProfile
  72. = window.localStorage.getItem('features/base/profile');
  73. if (persistedProfile) {
  74. try {
  75. persistedProfile = JSON.parse(persistedProfile);
  76. if (persistedProfile && typeof persistedProfile === 'object') {
  77. const preFlattenedProfile = persistedProfile.profile;
  78. return preFlattenedProfile || persistedProfile;
  79. }
  80. } catch (e) {
  81. logger.warn('Error parsing persisted legacy profile', e);
  82. }
  83. }
  84. return {};
  85. }
  86. /**
  87. * Inits the settings object based on what information we have available.
  88. * Info taken into consideration:
  89. * - Old Settings.js style data
  90. * - Things that we stored in profile earlier but belong here.
  91. *
  92. * @private
  93. * @param {Object} featureState - The current state of the feature.
  94. * @returns {Object}
  95. */
  96. function _initSettings(featureState) {
  97. let settings = featureState;
  98. // Old Settings.js values
  99. // FIXME: Let's remove this after a predefined time (e.g. by July 2018) to
  100. // avoid garbage in the source.
  101. const displayName = _.escape(window.localStorage.getItem('displayname'));
  102. const email = _.escape(window.localStorage.getItem('email'));
  103. let avatarID = _.escape(window.localStorage.getItem('avatarId'));
  104. if (!avatarID) {
  105. // if there is no avatar id, we generate a unique one and use it forever
  106. avatarID = randomHexString(32);
  107. }
  108. settings = assignIfDefined({
  109. avatarID,
  110. displayName,
  111. email
  112. }, settings);
  113. if (!browser.isReactNative()) {
  114. // Browser only
  115. const localFlipX
  116. = JSON.parse(window.localStorage.getItem('localFlipX') || 'true');
  117. const cameraDeviceId
  118. = window.localStorage.getItem('cameraDeviceId') || '';
  119. const micDeviceId = window.localStorage.getItem('micDeviceId') || '';
  120. // Currently audio output device change is supported only in Chrome and
  121. // default output always has 'default' device ID
  122. const audioOutputDeviceId
  123. = window.localStorage.getItem('audioOutputDeviceId') || 'default';
  124. settings = assignIfDefined({
  125. audioOutputDeviceId,
  126. cameraDeviceId,
  127. localFlipX,
  128. micDeviceId
  129. }, settings);
  130. }
  131. // Things we stored in profile earlier
  132. const legacyProfile = _getLegacyProfile();
  133. settings = assignIfDefined(legacyProfile, settings);
  134. return settings;
  135. }