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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // @flow
  2. import _ from 'lodash';
  3. import { APP_WILL_MOUNT } from '../../app';
  4. import JitsiMeetJS, { browser } from '../lib-jitsi-meet';
  5. import { ReducerRegistry } from '../redux';
  6. import { PersistenceRegistry } from '../storage';
  7. import { assignIfDefined, randomHexString } from '../util';
  8. import { SETTINGS_UPDATED } from './actionTypes';
  9. const logger = require('jitsi-meet-logger').getLogger(__filename);
  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. avatarID: undefined,
  18. avatarURL: undefined,
  19. cameraDeviceId: undefined,
  20. displayName: undefined,
  21. email: undefined,
  22. localFlipX: true,
  23. micDeviceId: undefined,
  24. serverURL: undefined,
  25. startAudioOnly: false,
  26. startWithAudioMuted: false,
  27. startWithVideoMuted: false
  28. };
  29. const STORE_NAME = 'features/base/settings';
  30. /**
  31. * Sets up the persistence of the feature {@code base/settings}.
  32. */
  33. PersistenceRegistry.register(STORE_NAME);
  34. ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
  35. switch (action.type) {
  36. case APP_WILL_MOUNT:
  37. return _initSettings(state);
  38. case SETTINGS_UPDATED:
  39. return {
  40. ...state,
  41. ...action.settings
  42. };
  43. }
  44. return state;
  45. });
  46. /**
  47. * Retrieves the legacy profile values regardless of it's being in pre or
  48. * post-flattening format.
  49. *
  50. * FIXME: Let's remove this after a predefined time (e.g. by July 2018) to avoid
  51. * garbage in the source.
  52. *
  53. * @private
  54. * @returns {Object}
  55. */
  56. function _getLegacyProfile() {
  57. let persistedProfile
  58. = window.localStorage.getItem('features/base/profile');
  59. if (persistedProfile) {
  60. try {
  61. persistedProfile = JSON.parse(persistedProfile);
  62. if (persistedProfile && typeof persistedProfile === 'object') {
  63. const preFlattenedProfile = persistedProfile.profile;
  64. return preFlattenedProfile || persistedProfile;
  65. }
  66. } catch (e) {
  67. logger.warn('Error parsing persisted legacy profile', e);
  68. }
  69. }
  70. return {};
  71. }
  72. /**
  73. * Inits the settings object based on what information we have available.
  74. * Info taken into consideration:
  75. * - Old Settings.js style data
  76. * - Things that we stored in profile earlier but belong here.
  77. *
  78. * @private
  79. * @param {Object} featureState - The current state of the feature.
  80. * @returns {Object}
  81. */
  82. function _initSettings(featureState) {
  83. let settings = featureState;
  84. // Old Settings.js values
  85. // FIXME: Let's remove this after a predefined time (e.g. by July 2018) to
  86. // avoid garbage in the source.
  87. const displayName = _.escape(window.localStorage.getItem('displayname'));
  88. const email = _.escape(window.localStorage.getItem('email'));
  89. let avatarID = _.escape(window.localStorage.getItem('avatarId'));
  90. if (!avatarID) {
  91. // if there is no avatar id, we generate a unique one and use it forever
  92. avatarID = randomHexString(32);
  93. }
  94. settings = assignIfDefined({
  95. avatarID,
  96. displayName,
  97. email
  98. }, settings);
  99. if (!browser.isReactNative()) {
  100. // Browser only
  101. const localFlipX
  102. = JSON.parse(window.localStorage.getItem('localFlipX') || 'true');
  103. const cameraDeviceId
  104. = window.localStorage.getItem('cameraDeviceId') || '';
  105. const micDeviceId = window.localStorage.getItem('micDeviceId') || '';
  106. // Currently audio output device change is supported only in Chrome and
  107. // default output always has 'default' device ID
  108. const audioOutputDeviceId
  109. = window.localStorage.getItem('audioOutputDeviceId') || 'default';
  110. if (audioOutputDeviceId
  111. !== JitsiMeetJS.mediaDevices.getAudioOutputDevice()) {
  112. JitsiMeetJS.mediaDevices.setAudioOutputDevice(
  113. audioOutputDeviceId
  114. ).catch(ex => {
  115. logger.warn('Failed to set audio output device from local '
  116. + 'storage. Default audio output device will be used'
  117. + 'instead.', ex);
  118. });
  119. }
  120. settings = assignIfDefined({
  121. audioOutputDeviceId,
  122. cameraDeviceId,
  123. localFlipX,
  124. micDeviceId
  125. }, settings);
  126. }
  127. // Things we stored in profile earlier
  128. const legacyProfile = _getLegacyProfile();
  129. settings = assignIfDefined(legacyProfile, settings);
  130. return settings;
  131. }