Skip to content

Commit

Permalink
Remove mm_config and mm_license global state from webapp, phase 2 (PR m…
Browse files Browse the repository at this point in the history
…attermost#2) (mattermost#863)

* MM-9635: expunge global mm_config from switch channel provider

* MM-9635: expunge global mm_config from the root components, except for globally exporting it

* MM-9635: fix incorrectly required customDescriptionText in email signup component

* MM-9635: expunge global mm_config from various action files

* MM-9635: deduplicate config/license code loading

This preserves semantics, except for allowing a `loadCurrentLocale` even if config hasn't
loaded, since this is safe.
  • Loading branch information
lieut-data committed Feb 28, 2018
1 parent 67ddf39 commit 78180eb
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 72 deletions.
9 changes: 7 additions & 2 deletions actions/diagnostics_actions.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import {getConfig} from 'mattermost-redux/selectors/entities/general';

import store from 'stores/redux_store.jsx';
import UserStore from 'stores/user_store.jsx';

const SUPPORTS_CLEAR_MARKS = isSupported([performance.clearMarks]);
Expand Down Expand Up @@ -118,5 +121,7 @@ function isSupported(checks) {
}

function isDevMode() {
return global.mm_config.EnableDeveloper === 'true';
}
const config = getConfig(store.getState());

return config.EnableDeveloper === 'true';
}
10 changes: 7 additions & 3 deletions actions/global_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import {getPostThread} from 'mattermost-redux/actions/posts';
import {removeUserFromTeam} from 'mattermost-redux/actions/teams';
import {Client4} from 'mattermost-redux/client';
import {getConfig, getLicense} from 'mattermost-redux/selectors/entities/general';

import {browserHistory} from 'utils/browser_history';
import {loadChannelsForCurrentUser} from 'actions/channel_actions.jsx';
Expand Down Expand Up @@ -392,7 +393,8 @@ export function loadCurrentLocale() {
}

export function loadDefaultLocale() {
let locale = global.window.mm_config.DefaultClientLocale;
const config = getConfig(getState());
let locale = config.DefaultClientLocale;

if (!I18n.getLanguageInfo(locale)) {
locale = 'en';
Expand All @@ -406,14 +408,16 @@ export function emitLocalUserTypingEvent(channelId, parentId) {
const t = Date.now();
const membersInChannel = ChannelStore.getStats(channelId).member_count;

if (global.mm_license.IsLicensed === 'true' && global.mm_config.ExperimentalTownSquareIsReadOnly === 'true') {
const license = getLicense(getState());
const config = getConfig(getState());
if (license.IsLicensed === 'true' && config.ExperimentalTownSquareIsReadOnly === 'true') {
const channel = ChannelStore.getChannelById(channelId);
if (channel && ChannelStore.isDefault(channel)) {
return;
}
}

if (((t - lastTimeTypingSent) > global.window.mm_config.TimeBetweenUserTypingUpdatesMilliseconds) && membersInChannel < global.window.mm_config.MaxNotificationsPerChannel && global.window.mm_config.EnableUserTypingMessages === 'true') {
if (((t - lastTimeTypingSent) > config.TimeBetweenUserTypingUpdatesMilliseconds) && membersInChannel < config.MaxNotificationsPerChannel && config.EnableUserTypingMessages === 'true') {
WebSocketClient.userTyping(channelId, parentId);
lastTimeTypingSent = t;
}
Expand Down
6 changes: 5 additions & 1 deletion actions/notification_actions.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import {getConfig} from 'mattermost-redux/selectors/entities/general';

import ChannelStore from 'stores/channel_store.jsx';
import NotificationStore from 'stores/notification_store.jsx';
import UserStore from 'stores/user_store.jsx';
import Constants, {NotificationLevels, UserStatuses} from 'utils/constants.jsx';
import {isSystemMessage} from 'utils/post_utils.jsx';
import {isMacApp, isMobileApp, isWindowsApp} from 'utils/user_agent.jsx';
import * as Utils from 'utils/utils.jsx';
import store from 'stores/redux_store.jsx';

export function sendDesktopNotification(post, msgProps) {
if ((UserStore.getCurrentId() === post.user_id && post.props.from_webhook !== 'true')) {
Expand Down Expand Up @@ -44,8 +47,9 @@ export function sendDesktopNotification(post, msgProps) {
return;
}

const config = getConfig(store.getState());
let username = Utils.localizeMessage('channel_loader.someone', 'Someone');
if (post.props.override_username && global.window.mm_config.EnablePostUsernameOverride === 'true') {
if (post.props.override_username && config.EnablePostUsernameOverride === 'true') {
username = post.props.override_username;
} else if (msgProps.sender_name) {
username = msgProps.sender_name;
Expand Down
53 changes: 30 additions & 23 deletions actions/user_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {Client4} from 'mattermost-redux/client';
import {Preferences as PreferencesRedux} from 'mattermost-redux/constants';
import {getBool} from 'mattermost-redux/selectors/entities/preferences';
import * as Selectors from 'mattermost-redux/selectors/entities/users';
import {getConfig} from 'mattermost-redux/selectors/entities/general';

import {browserHistory} from 'utils/browser_history';
import {getChannelMembersForUserIds} from 'actions/channel_actions.jsx';
Expand All @@ -28,41 +29,45 @@ const getState = store.getState;

export async function loadMe() {
await UserActions.loadMe()(dispatch, getState);

if (window.mm_config) {
loadCurrentLocale();
}
loadCurrentLocale();
}

export async function loadMeAndConfig(callback) {
const {data: config} = await getClientConfig()(store.dispatch, store.getState);

global.window.mm_config = config;

if (global.window && global.window.analytics) {
global.window.analytics.identify(global.window.mm_config.DiagnosticId, {}, {
context: {
ip: '0.0.0.0',
},
page: {
path: '',
referrer: '',
search: '',
title: '',
url: '',
},
anonymousId: '00000000000000000000000000',
});
const promises = [];

if (document.cookie.indexOf('MMUSERID=') > -1) {
if (global.window && global.window.analytics) {
global.window.analytics.identify(config.DiagnosticId, {}, {
context: {
ip: '0.0.0.0',
},
page: {
path: '',
referrer: '',
search: '',
title: '',
url: '',
},
anonymousId: '00000000000000000000000000',
});
}

promises.push(loadMe());
}

Promise.all([
loadMe(),
promises.push(
getLicenseConfig()(store.dispatch, store.getState).then(
({data: license}) => {
global.window.mm_license = license;
}
),
]).then(callback);
)
);

Promise.all(promises).then(callback);
}

export async function switchFromLdapToEmail(email, password, token, ldapPassword, success, error) {
Expand Down Expand Up @@ -490,7 +495,9 @@ export async function deactivateMfa(success, error) {
}

export async function checkMfa(loginId, success, error) {
if (global.window.mm_config.EnableMultifactorAuthentication !== 'true') {
const config = getConfig(getState());

if (config.EnableMultifactorAuthentication !== 'true') {
success(false);
return;
}
Expand Down
12 changes: 7 additions & 5 deletions actions/websocket_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {getMe} from 'mattermost-redux/actions/users';
import {Client4} from 'mattermost-redux/client';
import {getCurrentUser} from 'mattermost-redux/selectors/entities/users';
import {getMyTeams} from 'mattermost-redux/selectors/entities/teams';
import {getConfig} from 'mattermost-redux/selectors/entities/general';

import {browserHistory} from 'utils/browser_history';
import {loadChannelsForCurrentUser} from 'actions/channel_actions.jsx';
Expand Down Expand Up @@ -45,8 +46,11 @@ export function initialize() {
return;
}

const config = getConfig(getState());
let connUrl = '';
if (global.window.mm_config.WebsocketURL === '') {
if (config.WebsocketURL) {
connUrl = config.WebsocketURL;
} else {
connUrl = getSiteURL();

// replace the protocol with a websocket one
Expand All @@ -59,13 +63,11 @@ export function initialize() {
// append a port number if one isn't already specified
if (!(/:\d+$/).test(connUrl)) {
if (connUrl.startsWith('wss:')) {
connUrl += ':' + global.window.mm_config.WebsocketSecurePort;
connUrl += ':' + config.WebsocketSecurePort;
} else {
connUrl += ':' + global.window.mm_config.WebsocketPort;
connUrl += ':' + config.WebsocketPort;
}
}
} else {
connUrl = global.window.mm_config.WebsocketURL;
}

connUrl += Client4.getUrlVersion() + '/websocket';
Expand Down
18 changes: 18 additions & 0 deletions components/root/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import {connect} from 'react-redux';
import {getConfig} from 'mattermost-redux/selectors/entities/general';

import Root from './root.jsx';

function mapStateToProps(state) {
const config = getConfig(state);

return {
diagnosticsEnabled: config.DiagnosticsEnabled === 'true',
noAccounts: config.NoAccounts === 'true',
};
}

export default connect(mapStateToProps)(Root);
47 changes: 13 additions & 34 deletions components/root.jsx → components/root/root.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React from 'react';
import {IntlProvider} from 'react-intl';
import FastClick from 'fastclick';
import {Route, Switch, Redirect} from 'react-router-dom';
import {getClientConfig, getLicenseConfig, setUrl} from 'mattermost-redux/actions/general';
import {setUrl} from 'mattermost-redux/actions/general';
import {setSystemEmojis} from 'mattermost-redux/actions/emojis';
import {getConfig} from 'mattermost-redux/selectors/entities/general';
import {Client4} from 'mattermost-redux/client';
Expand Down Expand Up @@ -83,11 +83,14 @@ const LoggedInRoute = ({component: Component, ...rest}) => (
);

export default class Root extends React.Component {
static propTypes = {
diagnosticsEnabled: PropTypes.bool,
noAccounts: PropTypes.bool,
children: PropTypes.object,
}

constructor(props) {
super(props);
this.localizationChanged = this.localizationChanged.bind(this);
this.redirectIfNecessary = this.redirectIfNecessary.bind(this);
this.onConfigLoaded = this.onConfigLoaded.bind(this);

// Redux
setUrl(window.location.origin);
Expand Down Expand Up @@ -141,11 +144,11 @@ export default class Root extends React.Component {
};
}

onConfigLoaded() {
onConfigLoaded = () => {
const segmentKey = Constants.DIAGNOSTICS_SEGMENT_KEY;

/*eslint-disable */
if (segmentKey != null && segmentKey !== '' && window.mm_config.DiagnosticsEnabled === 'true') {
if (segmentKey != null && segmentKey !== '' && this.props.diagnosticsEnabled) {
!function(){var analytics=global.window.analytics=global.window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","group","track","ready","alias","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http:https://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="3.0.1";
analytics.load(segmentKey);

Expand Down Expand Up @@ -200,16 +203,16 @@ export default class Root extends React.Component {
}
}

localizationChanged() {
localizationChanged = () => {
const locale = LocalizationStore.getLocale();

Client4.setAcceptLanguage(locale);
this.setState({locale, translations: LocalizationStore.getTranslations()});
}

redirectIfNecessary(props) {
redirectIfNecessary = (props) => {
if (props.location.pathname === '/') {
if (global.mm_config.NoAccounts === 'true') {
if (this.props.noAccounts) {
this.props.history.push('/signup_user_complete');
} else if (UserStore.getCurrentUser()) {
GlobalActions.redirectUserToDefaultTeam();
Expand All @@ -222,24 +225,7 @@ export default class Root extends React.Component {
}

componentDidMount() {
// Load config
if (document.cookie.indexOf('MMUSERID=') > -1) {
loadMeAndConfig(this.onConfigLoaded);
} else {
getClientConfig()(store.dispatch, store.getState).then(
({data: config}) => {
global.window.mm_config = config;

getLicenseConfig()(store.dispatch, store.getState).then(
({data: license}) => {
global.window.mm_license = license;
this.onConfigLoaded();
}
);
}
);
}

loadMeAndConfig(this.onConfigLoaded);
trackLoadTime();
}

Expand Down Expand Up @@ -342,10 +328,3 @@ export default class Root extends React.Component {
);
}
}

Root.defaultProps = {
};

Root.propTypes = {
children: PropTypes.object,
};
2 changes: 1 addition & 1 deletion components/signup/signup_email/signup_email.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class SignupEmail extends React.Component {
privacyPolicyLink: PropTypes.string,
customBrand: PropTypes.bool.isRequired,
enableCustomBrand: PropTypes.bool.isRequired,
customDescriptionText: PropTypes.string.isRequired,
customDescriptionText: PropTypes.string,
};
}

Expand Down
3 changes: 2 additions & 1 deletion i18n/i18n.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import ptLocaleData from 'react-intl/locale-data/pt';
import trLocaleData from 'react-intl/locale-data/tr';
import ruLocaleData from 'react-intl/locale-data/ru';
import zhLocaleData from 'react-intl/locale-data/zh';
import {getConfig} from 'mattermost-redux/selectors/entities/general';

import store from 'stores/redux_store.jsx';

Expand Down Expand Up @@ -140,7 +141,7 @@ export function getAllLanguages() {
}

export function getLanguages() {
const config = store.getState().entities.general.config;
const config = getConfig(store.getState());
if (!config.AvailableLocales) {
return getAllLanguages();
}
Expand Down
7 changes: 5 additions & 2 deletions root.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import {Router, Route} from 'react-router-dom';
import {getConfig} from 'mattermost-redux/selectors/entities/general';

// Import our styles
import 'bootstrap-colorpicker/dist/css/bootstrap-colorpicker.css';
Expand All @@ -14,7 +15,7 @@ import 'katex/dist/katex.min.css';
import {browserHistory} from 'utils/browser_history';
import {makeAsyncComponent} from 'components/async_load';
import store from 'stores/redux_store.jsx';
import loadRoot from 'bundle-loader?lazy!components/root.jsx';
import loadRoot from 'bundle-loader?lazy!components/root';

const Root = makeAsyncComponent(loadRoot);

Expand All @@ -31,7 +32,9 @@ function preRenderSetup(callwhendone) {
req.setRequestHeader('Content-Type', 'application/json');
req.send(JSON.stringify(l));

if (window.mm_config && window.mm_config.EnableDeveloper === 'true') {
const state = store.getState();
const config = getConfig(state);
if (config.EnableDeveloper === 'true') {
window.ErrorStore.storeLastError({type: 'developer', message: 'DEVELOPER MODE: A JavaScript error has occurred. Please use the JavaScript console to capture and report the error (row: ' + line + ' col: ' + column + ').'});
window.ErrorStore.emitChange();
}
Expand Down

0 comments on commit 78180eb

Please sign in to comment.