Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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