Skip to content

Commit

Permalink
Remove flux store usages from utils (mattermost#1952)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilander authored and JayaKrishnaNamburu committed Dec 3, 2018
1 parent c6b4c00 commit 68f3c58
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 34 deletions.
5 changes: 2 additions & 3 deletions components/logged_in/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {autoUpdateTimezone} from 'mattermost-redux/actions/timezone';
import {getLicense, getConfig} from 'mattermost-redux/selectors/entities/general';

import {shouldShowTermsOfService} from 'mattermost-redux/selectors/entities/users';
import {getCurrentUser, shouldShowTermsOfService} from 'mattermost-redux/selectors/entities/users';

import {checkIfMFARequired} from 'utils/route';

Expand All @@ -18,7 +17,7 @@ function mapStateToProps(state, ownProps) {
const showTermsOfService = shouldShowTermsOfService(state);

return {
mfaRequired: checkIfMFARequired(license, config, ownProps.match.url),
mfaRequired: checkIfMFARequired(getCurrentUser(state), license, config, ownProps.match.url),
enableTimezone: config.ExperimentalTimezone === 'true',
showTermsOfService,
};
Expand Down
5 changes: 3 additions & 2 deletions components/needs_team/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import NeedsTeam from './needs_team.jsx';
function mapStateToProps(state, ownProps) {
const license = getLicense(state);
const config = getConfig(state);
const currentUser = getCurrentUser(state);

return {
theme: getTheme(state),
mfaRequired: checkIfMFARequired(license, config, ownProps.match.url),
currentUser: getCurrentUser(state),
mfaRequired: checkIfMFARequired(currentUser, license, config, ownProps.match.url),
currentUser,
currentTeamId: getCurrentTeamId(state),
teamsList: getMyTeams(state),
currentChannelId: getCurrentChannelId(state),
Expand Down
47 changes: 47 additions & 0 deletions tests/utils/route.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import assert from 'assert';

import {checkIfMFARequired} from 'utils/route';

describe('Utils.Route', function() {
describe('checkIfMFARequired', () => {
test('mfa is enforced', () => {
const user = {mfa_active: false, auth_service: ''};
const config = {EnableMultifactorAuthentication: 'true', EnforceMultifactorAuthentication: 'true'};
const license = {MFA: 'true'};

assert.ok(checkIfMFARequired(user, license, config, ''));
assert.ok(!checkIfMFARequired(user, license, config, '/mfa/setup'));
assert.ok(!checkIfMFARequired(user, license, config, '/mfa/confirm'));

user.auth_service = 'email';
assert.ok(checkIfMFARequired(user, license, config, ''));

user.auth_service = 'ldap';
assert.ok(checkIfMFARequired(user, license, config, ''));

user.auth_service = 'saml';
assert.ok(!checkIfMFARequired(user, license, config, ''));

user.auth_service = '';
user.mfa_active = true;
assert.ok(!checkIfMFARequired(user, license, config, ''));
});

test('mfa is not enforced or enabled', () => {
const user = {mfa_active: false, auth_service: ''};
const config = {EnableMultifactorAuthentication: 'true', EnforceMultifactorAuthentication: 'false'};
const license = {MFA: 'true'};
assert.ok(!checkIfMFARequired(user, license, config, ''));

config.EnforceMultifactorAuthentication = 'true';
config.EnableMultifactorAuthentication = 'false';
assert.ok(!checkIfMFARequired(user, license, config, ''));

license.MFA = 'false';
config.EnableMultifactorAuthentication = 'true';
assert.ok(!checkIfMFARequired(user, license, config, ''));
});
});
});
4 changes: 2 additions & 2 deletions utils/post_utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import {Client4} from 'mattermost-redux/client';
import {getLicense, getConfig} from 'mattermost-redux/selectors/entities/general';
import {haveIChannelPermission} from 'mattermost-redux/selectors/entities/roles';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';
import {getChannel} from 'mattermost-redux/selectors/entities/channels';
import {Permissions} from 'mattermost-redux/constants';

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

import Constants from 'utils/constants.jsx';
Expand All @@ -29,7 +29,7 @@ export function isFromWebhook(post) {
}

export function isPostOwner(post) {
return UserStore.getCurrentId() === post.user_id;
return getCurrentUserId(store.getState()) === post.user_id;
}

export function isComment(post) {
Expand Down
4 changes: 1 addition & 3 deletions utils/route.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import UserStore from 'stores/user_store.jsx';
import {ErrorPageTypes} from 'utils/constants.jsx';

export function importComponentSuccess(callback) {
Expand All @@ -27,12 +26,11 @@ const mfaAuthServices = [
'ldap',
];

export function checkIfMFARequired(license, config, path) {
export function checkIfMFARequired(user, license, config, path) {
if (license.MFA === 'true' &&
config.EnableMultifactorAuthentication === 'true' &&
config.EnforceMultifactorAuthentication === 'true' &&
mfaPaths.indexOf(path) === -1) {
const user = UserStore.getCurrentUser();
if (user && !user.mfa_active &&
mfaAuthServices.indexOf(user.auth_service) !== -1) {
return true;
Expand Down
59 changes: 35 additions & 24 deletions utils/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ import {FormattedMessage} from 'react-intl';

import {Client4} from 'mattermost-redux/client';
import {Posts} from 'mattermost-redux/constants';
import {getTeammateNameDisplaySetting} from 'mattermost-redux/selectors/entities/preferences';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';
import {getChannel} from 'mattermost-redux/selectors/entities/channels';
import {getTeammateNameDisplaySetting, getBool, getInt} from 'mattermost-redux/selectors/entities/preferences';
import {getCurrentUserId, getUser} from 'mattermost-redux/selectors/entities/users';
import {
blendColors,
changeOpacity,
} from 'mattermost-redux/utils/theme_utils';
import {displayUsername} from 'mattermost-redux/utils/user_utils';
import {getCurrentTeamId, getCurrentRelativeTeamUrl, getTeam} from 'mattermost-redux/selectors/entities/teams';

import {browserHistory} from 'utils/browser_history';
import {searchForTerm} from 'actions/post_actions';
import UserStore from 'stores/user_store.jsx';
import ChannelStore from 'stores/channel_store.jsx';
import PreferenceStore from 'stores/preference_store.jsx';
import TeamStore from 'stores/team_store.jsx';
import Constants, {FileTypes, UserStatuses} from 'utils/constants.jsx';
import * as UserAgent from 'utils/user_agent.jsx';
import bing from 'images/bing.mp3';
Expand Down Expand Up @@ -122,21 +120,32 @@ export function isSystemAdmin(roles) {
return false;
}

export function getTeamRelativeUrl(team) {
if (!team) {
return '';
}

return '/' + team.name;
}

export function notifyMe(title, body, channel, teamId, silent) {
showNotification({title,
body,
requireInteraction: false,
silent,
onClick: () => {
const state = store.getState();
window.focus();
if (channel && (channel.type === Constants.DM_CHANNEL || channel.type === Constants.GM_CHANNEL)) {
browserHistory.push(TeamStore.getCurrentTeamRelativeUrl() + '/channels/' + channel.name);
browserHistory.push(getCurrentRelativeTeamUrl(state) + '/channels/' + channel.name);
} else if (channel) {
browserHistory.push(TeamStore.getTeamRelativeUrl(teamId) + '/channels/' + channel.name);
const team = getTeam(state, teamId);
browserHistory.push(getTeamRelativeUrl(team) + '/channels/' + channel.name);
} else if (teamId) {
browserHistory.push(TeamStore.getTeamRelativeUrl(teamId) + `/channels/${Constants.DEFAULT_CHANNEL}`);
const team = getTeam(state, teamId);
browserHistory.push(getTeamRelativeUrl(team) + `/channels/${Constants.DEFAULT_CHANNEL}`);
} else {
browserHistory.push(TeamStore.getCurrentTeamRelativeUrl() + `/channels/${Constants.DEFAULT_CHANNEL}`);
browserHistory.push(getCurrentRelativeTeamUrl(state) + `/channels/${Constants.DEFAULT_CHANNEL}`);
}
},
}).catch(() => {
Expand Down Expand Up @@ -192,7 +201,7 @@ export function displayTime(ticks, utc) {
minutes = '0' + minutes;
}

const useMilitaryTime = PreferenceStore.getBool(Constants.Preferences.CATEGORY_DISPLAY_SETTINGS, 'use_military_time');
const useMilitaryTime = getBool(store.getState(), Constants.Preferences.CATEGORY_DISPLAY_SETTINGS, 'use_military_time');
if (!useMilitaryTime) {
ampm = ' AM';
if (hours >= 12) {
Expand Down Expand Up @@ -1100,27 +1109,28 @@ export function isMobile() {
}

export function getDirectTeammate(channelId) {
const state = store.getState();
let teammate = {};

const channel = ChannelStore.get(channelId);
const channel = getChannel(state, channelId);
if (!channel) {
return teammate;
}

const userIds = channel.name.split('__');
const curUserId = UserStore.getCurrentId();
const curUserId = getCurrentUserId(state);

if (userIds.length !== 2 || userIds.indexOf(curUserId) === -1) {
return teammate;
}

if (userIds[0] === userIds[1]) {
return UserStore.getProfile(userIds[0]);
return getUser(state, userIds[0]);
}

for (var idx in userIds) {
if (userIds[idx] !== curUserId) {
teammate = UserStore.getProfile(userIds[idx]);
teammate = getUser(state, userIds[idx]);
break;
}
}
Expand Down Expand Up @@ -1198,7 +1208,7 @@ export function getDisplayName(user) {
* Gets the display name of the user with the specified id, respecting the TeammateNameDisplay configuration setting
*/
export function getDisplayNameByUserId(userId) {
return getDisplayNameByUser(UserStore.getProfile(userId));
return getDisplayNameByUser(getUser(store.getState(), userId));
}

/**
Expand Down Expand Up @@ -1247,7 +1257,7 @@ export function sortUsersByStatusAndDisplayName(users, statusesByUserId) {
* Gets the entire name, including username, full name, and nickname, of the user with the specified id
*/
export function displayEntireName(userId) {
return displayEntireNameForUser(UserStore.getProfile(userId));
return displayEntireNameForUser(getUser(store.getState(), userId));
}

/**
Expand Down Expand Up @@ -1292,7 +1302,7 @@ export function displayEntireNameForUser(user) {

export function imageURLForUser(userIdOrObject) {
if (typeof userIdOrObject == 'string') {
const profile = UserStore.getProfile(userIdOrObject);
const profile = getUser(store.getState(), userIdOrObject);
if (profile) {
return imageURLForUser(profile);
}
Expand All @@ -1304,7 +1314,7 @@ export function imageURLForUser(userIdOrObject) {
// in contrast to Client4.getTeamIconUrl, for ui logic this function returns null if last_team_icon_update is unset
export function imageURLForTeam(teamIdOrObject) {
if (typeof teamIdOrObject == 'string') {
const team = TeamStore.get(teamIdOrObject);
const team = getTeam(store.getState(), teamIdOrObject);
if (team) {
return imageURLForTeam(team);
}
Expand Down Expand Up @@ -1382,7 +1392,7 @@ export function getUserIdFromChannelId(channelId, currentUserId = getCurrentUser
}

export function importSlack(file, success, error) {
Client4.importTeam(TeamStore.getCurrent().id, file, 'slack').then(success).catch(error);
Client4.importTeam(getCurrentTeamId(store.getState()), file, 'slack').then(success).catch(error);
}

export function windowWidth() {
Expand All @@ -1394,7 +1404,7 @@ export function windowHeight() {
}

export function isFeatureEnabled(feature) {
return PreferenceStore.getBool(Constants.Preferences.CATEGORY_ADVANCED_SETTINGS, Constants.FeatureTogglePrefix + feature.label);
return getBool(store.getState(), Constants.Preferences.CATEGORY_ADVANCED_SETTINGS, Constants.FeatureTogglePrefix + feature.label);
}

export function fillArray(value, length) {
Expand Down Expand Up @@ -1544,7 +1554,7 @@ export function handleFormattedTextClick(e) {
}
} else if (channelMentionAttribute) {
e.preventDefault();
browserHistory.push('/' + TeamStore.getCurrent().name + '/channels/' + channelMentionAttribute.value);
browserHistory.push(getCurrentRelativeTeamUrl(store.getState()) + '/channels/' + channelMentionAttribute.value);
}
}

Expand Down Expand Up @@ -1591,17 +1601,18 @@ export function getEmailInterval(enableEmailBatching, isEmailEnabled) {
const validValuesWithoutEmailBatching = [INTERVAL_IMMEDIATE];

let emailInterval;
const state = store.getState();

if (enableEmailBatching) {
// when email batching is enabled, the default interval is 15 minutes
emailInterval = PreferenceStore.getInt(CATEGORY_NOTIFICATIONS, EMAIL_INTERVAL, INTERVAL_FIFTEEN_MINUTES);
emailInterval = getInt(state, CATEGORY_NOTIFICATIONS, EMAIL_INTERVAL, INTERVAL_FIFTEEN_MINUTES);

if (validValuesWithEmailBatching.indexOf(emailInterval) === -1) {
emailInterval = INTERVAL_FIFTEEN_MINUTES;
}
} else {
// otherwise, the default interval is immediately
emailInterval = PreferenceStore.getInt(CATEGORY_NOTIFICATIONS, EMAIL_INTERVAL, INTERVAL_IMMEDIATE);
emailInterval = getInt(state, CATEGORY_NOTIFICATIONS, EMAIL_INTERVAL, INTERVAL_IMMEDIATE);

if (validValuesWithoutEmailBatching.indexOf(emailInterval) === -1) {
emailInterval = INTERVAL_IMMEDIATE;
Expand Down

0 comments on commit 68f3c58

Please sign in to comment.