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

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