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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. import logger from './logger';
  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. disableCallIntegration: undefined,
  22. disableP2P: undefined,
  23. displayName: undefined,
  24. email: undefined,
  25. localFlipX: true,
  26. micDeviceId: undefined,
  27. serverURL: undefined,
  28. startAudioOnly: false,
  29. startWithAudioMuted: false,
  30. startWithVideoMuted: false,
  31. userSelectedAudioOutputDeviceId: undefined,
  32. userSelectedCameraDeviceId: undefined,
  33. userSelectedMicDeviceId: undefined,
  34. userSelectedAudioOutputDeviceLabel: undefined,
  35. userSelectedCameraDeviceLabel: undefined,
  36. userSelectedMicDeviceLabel: 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
  77. = window.localStorage.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 = window.localStorage.getItem('displayname');
  108. const savedEmail = window.localStorage.getItem('email');
  109. let avatarID = _.escape(window.localStorage.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
  130. = JSON.parse(window.localStorage.getItem('localFlipX') || 'true');
  131. const cameraDeviceId
  132. = window.localStorage.getItem('cameraDeviceId') || '';
  133. const micDeviceId = window.localStorage.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
  137. = window.localStorage.getItem('audioOutputDeviceId') || 'default';
  138. settings = assignIfDefined({
  139. audioOutputDeviceId,
  140. cameraDeviceId,
  141. localFlipX,
  142. micDeviceId
  143. }, settings);
  144. }
  145. // Things we stored in profile earlier
  146. const legacyProfile = _getLegacyProfile();
  147. settings = assignIfDefined(legacyProfile, settings);
  148. return settings;
  149. }