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

reducer.js 5.3KB

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