Separate handling config and profile with precedence (#2784)

This commit is contained in:
Zoltan Bettenbuk 2018-04-11 19:02:31 +02:00 committed by virtuacoplenny
parent 76c56339c4
commit 78ff0f7864
4 changed files with 78 additions and 55 deletions

View File

@ -124,10 +124,8 @@ function _appNavigateToMandatoryLocation(
});
}
const profile = getState()['features/base/profile'];
return promise.then(() =>
dispatch(setConfig(_mergeConfigWithProfile(config, profile))));
dispatch(setConfig(config)));
}
}
@ -290,23 +288,3 @@ function _loadConfig({ contextRoot, host, protocol, room }) {
throw error;
});
}
/**
* Merges the downloaded config with the current profile values. The profile
* values are named the same way as the config values in the config.js so
* a clean merge is possible.
*
* @param {Object|undefined} config - The downloaded config.
* @param {Object} profile - The persisted profile.
* @returns {Object}
*/
function _mergeConfigWithProfile(config, profile) {
if (!config) {
return;
}
return {
...config,
...profile
};
}

View File

@ -7,8 +7,8 @@ import {
sendAnalytics
} from '../../analytics';
import { SET_ROOM, setAudioOnly } from '../conference';
import { parseURLParams } from '../config';
import JitsiMeetJS from '../lib-jitsi-meet';
import { getPropertyValue } from '../profile';
import { MiddlewareRegistry } from '../redux';
import { setTrackMuted, TRACK_ADDED } from '../tracks';
@ -60,34 +60,13 @@ function _setRoom({ dispatch, getState }, next, action) {
// Read the config.
const state = getState();
let urlParams;
let audioMuted;
let videoMuted;
if (room) {
// The Jitsi Meet client may override the Jitsi Meet deployment in the
// (location) URL on the subject of the following:
// - startAudioOnly
// - startWithAudioMuted
// - startWithVideoMuted
urlParams
= parseURLParams(state['features/base/connection'].locationURL);
audioMuted = urlParams['config.startWithAudioMuted'];
videoMuted = urlParams['config.startWithVideoMuted'];
}
// Of course, the Jitsi Meet deployment defines config.js which should be
// respected if the client did not override it.
const config = state['features/base/config'];
typeof audioMuted === 'undefined'
&& (audioMuted = config.startWithAudioMuted);
typeof videoMuted === 'undefined'
&& (videoMuted = config.startWithVideoMuted);
audioMuted = Boolean(audioMuted);
videoMuted = Boolean(videoMuted);
const audioMuted = Boolean(getPropertyValue(state, 'startWithAudioMuted', {
ignoreUrlParams: !room
}));
const videoMuted = Boolean(getPropertyValue(state, 'startWithVideoMuted', {
ignoreUrlParams: !room
}));
sendAnalytics(createStartMutedConfigurationEvent(
'local', audioMuted, videoMuted));
@ -113,10 +92,7 @@ function _setRoom({ dispatch, getState }, next, action) {
let audioOnly;
if (JitsiMeetJS.mediaDevices.supportsVideo()) {
audioOnly = urlParams && urlParams['config.startAudioOnly'];
typeof audioOnly === 'undefined'
&& (audioOnly = config.startAudioOnly);
audioOnly = Boolean(audioOnly);
audioOnly = Boolean(getPropertyValue(state, 'startAudioOnly'));
} else {
// Always default to being audio only if the current environment
// does not support sending or receiving video.

View File

@ -0,0 +1,68 @@
// @flow
import { parseURLParams } from '../config';
import { toState } from '../redux';
/**
* Returns the effective value of a property by applying a precedence
* between values in URL, config and profile.
*
* @param {Object|Function} stateful - The redux state object or function
* to retreive the state.
* @param {string} propertyName - The name of the property we need.
* @param {{
* ignoreJWT: boolean,
* ignoreUrlParams: boolean,
* ignoreProfile: boolean,
* ignoreConfig: boolean
* }} precedence - A structure of booleans to set which property sources
* should be ignored.
* @returns {any}
*/
export function getPropertyValue(
stateful: Object | Function,
propertyName: string,
precedence: Object = {
ignoreJWT: false,
ignoreUrlParams: false,
ignoreProfile: false,
ignoreConfig: false
}
) {
const state = toState(stateful);
const jwt = state['features/base/jwt'];
const urlParams
= parseURLParams(state['features/base/connection'].locationURL);
const profile = state['features/base/profile'];
const config = state['features/base/config'];
const urlParamName = `config.${propertyName}`;
// Precedence: jwt -> urlParams -> profile -> config
if (
!precedence.ignoreJWT
&& typeof jwt[propertyName] !== 'undefined'
) {
return jwt[propertyName];
}
if (
!precedence.ignoreUrlParams
&& typeof urlParams[urlParamName] !== 'undefined'
) {
return urlParams[urlParamName];
}
if (
!precedence.ignoreProfile
&& typeof profile[propertyName] !== 'undefined'
) {
return profile[propertyName];
}
if (!precedence.ignoreConfig) {
return config[propertyName];
}
return undefined;
}

View File

@ -1,4 +1,5 @@
export * from './actions';
export * from './functions';
import './middleware';
import './reducer';