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.7KB

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