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.5KB

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