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

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