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

reducer.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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: jibri uses old settings.js local storage values to set its display
  103. // name and email. Provide another way for jibri to set these values, update
  104. // jibri, and remove the old settings.js values.
  105. const savedDisplayName = window.localStorage.getItem('displayname');
  106. const savedEmail = window.localStorage.getItem('email');
  107. let avatarID = _.escape(window.localStorage.getItem('avatarId'));
  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
  114. = savedDisplayName === null ? undefined : _.escape(savedDisplayName);
  115. const email = savedEmail === null ? undefined : _.escape(savedEmail);
  116. if (!avatarID) {
  117. // if there is no avatar id, we generate a unique one and use it forever
  118. avatarID = randomHexString(32);
  119. }
  120. settings = assignIfDefined({
  121. avatarID,
  122. displayName,
  123. email
  124. }, settings);
  125. if (!browser.isReactNative()) {
  126. // Browser only
  127. const localFlipX
  128. = JSON.parse(window.localStorage.getItem('localFlipX') || 'true');
  129. const cameraDeviceId
  130. = window.localStorage.getItem('cameraDeviceId') || '';
  131. const micDeviceId = window.localStorage.getItem('micDeviceId') || '';
  132. // Currently audio output device change is supported only in Chrome and
  133. // default output always has 'default' device ID
  134. const audioOutputDeviceId
  135. = window.localStorage.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. }