From c0a0065472c199166d32b052dd328b0212f5b693 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Thu, 15 Jun 2017 10:34:20 -0400 Subject: Move user actions over to use redux and v4 (#6649) --- webapp/actions/admin_actions.jsx | 69 ++++----- webapp/actions/user_actions.jsx | 296 +++++++++++++++++---------------------- 2 files changed, 159 insertions(+), 206 deletions(-) (limited to 'webapp/actions') diff --git a/webapp/actions/admin_actions.jsx b/webapp/actions/admin_actions.jsx index 04d0d65bb..307f0c4a7 100644 --- a/webapp/actions/admin_actions.jsx +++ b/webapp/actions/admin_actions.jsx @@ -2,14 +2,15 @@ // See License.txt for license information. import Client from 'client/web_client.jsx'; -import {browserHistory} from 'react-router/es6'; + +import {clientLogout} from 'actions/global_actions.jsx'; import store from 'stores/redux_store.jsx'; const dispatch = store.dispatch; const getState = store.getState; -import {updateUserMfa, updateUserPassword} from 'mattermost-redux/actions/users'; import * as AdminActions from 'mattermost-redux/actions/admin'; +import * as UserActions from 'mattermost-redux/actions/users'; export function saveConfig(config, success, error) { AdminActions.updateConfig(config)(dispatch, getState).then( @@ -39,7 +40,7 @@ export function reloadConfig(success, error) { } export function adminResetMfa(userId, success, error) { - updateUserMfa(userId, false)(dispatch, getState).then( + UserActions.updateUserMfa(userId, false)(dispatch, getState).then( (data) => { if (data && success) { success(data); @@ -117,7 +118,7 @@ export function recycleDatabaseConnection(success, error) { } export function adminResetPassword(userId, password, success, error) { - updateUserPassword(userId, '', password)(dispatch, getState).then( + UserActions.updateUserPassword(userId, '', password)(dispatch, getState).then( (data) => { if (data && success) { success(data); @@ -193,60 +194,44 @@ export function allowOAuth2(params, success, error) { } export function emailToLdap(loginId, password, token, ldapId, ldapPassword, success, error) { - Client.emailToLdap( - loginId, - password, - token, - ldapId, - ldapPassword, + UserActions.switchEmailToLdap(loginId, password, ldapId, ldapPassword, token)(dispatch, getState).then( (data) => { - if (success) { + if (data && success) { success(data); - } - }, - (err) => { - if (error) { - error(err); + } else if (data == null && error) { + const serverError = getState().requests.users.switchLogin.error; + error({id: serverError.server_error_id, ...serverError}); } } ); } export function emailToOAuth(loginId, password, token, newType, success, error) { - Client.emailToOAuth( - loginId, - password, - token, - newType, + UserActions.switchEmailToOAuth(newType, loginId, password, token)(dispatch, getState).then( (data) => { - if (success) { + if (data && success) { success(data); - } - }, - (err) => { - if (error) { - error(err); + } else if (data == null && error) { + const serverError = getState().requests.users.switchLogin.error; + error({id: serverError.server_error_id, ...serverError}); } } ); } -export function oauthToEmail(email, password, success, error) { - Client.oauthToEmail( - email, - password, +export function oauthToEmail(currentService, email, password, success, error) { + UserActions.switchOAuthToEmail(currentService, email, password)(dispatch, getState).then( (data) => { - if (data.follow_link) { - browserHistory.push(data.follow_link); - } - - if (success) { - success(data); - } - }, - (err) => { - if (error) { - error(err); + if (data) { + if (data.follow_link) { + clientLogout(data.follow_link); + } + if (success) { + success(data); + } + } else if (data == null && error) { + const serverError = getState().requests.users.switchLogin.error; + error({id: serverError.server_error_id, ...serverError}); } } ); diff --git a/webapp/actions/user_actions.jsx b/webapp/actions/user_actions.jsx index 63c716d5d..cd8b05562 100644 --- a/webapp/actions/user_actions.jsx +++ b/webapp/actions/user_actions.jsx @@ -1,20 +1,17 @@ // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. -import AppDispatcher from 'dispatcher/app_dispatcher.jsx'; - import PreferenceStore from 'stores/preference_store.jsx'; import TeamStore from 'stores/team_store.jsx'; import UserStore from 'stores/user_store.jsx'; import ChannelStore from 'stores/channel_store.jsx'; import {getChannelMembersForUserIds} from 'actions/channel_actions.jsx'; -import {loadCurrentLocale} from 'actions/global_actions.jsx'; +import {loadCurrentLocale, clientLogout} from 'actions/global_actions.jsx'; import {loadStatusesForProfilesList, loadStatusesForProfilesMap} from 'actions/status_actions.jsx'; import {getDirectChannelName, getUserIdFromChannelName} from 'utils/utils.jsx'; -import * as AsyncClient from 'utils/async_client.jsx'; import Client from 'client/web_client.jsx'; import {Constants, ActionTypes, Preferences} from 'utils/constants.jsx'; @@ -28,34 +25,17 @@ const getState = store.getState; import * as Selectors from 'mattermost-redux/selectors/entities/users'; import {getBool} from 'mattermost-redux/selectors/entities/preferences'; -import { - getProfiles, - getProfilesInChannel, - getProfilesInTeam, - getProfilesWithoutTeam, - getProfilesByIds, - getMe, - searchProfiles, - autocompleteUsers as autocompleteRedux, - updateMe, - updateUserMfa, - checkMfa as checkMfaRedux, - updateUserPassword, - createUser, - login, - loadMe as loadMeRedux, - updateUserRoles as updateUserRolesRedux, - getStatus, - setStatus -} from 'mattermost-redux/actions/users'; +import * as UserActions from 'mattermost-redux/actions/users'; import {getClientConfig, getLicenseConfig} from 'mattermost-redux/actions/general'; -import {getTeamMembersByIds, getMyTeamMembers} from 'mattermost-redux/actions/teams'; +import {getTeamMembersByIds, getMyTeamMembers, getMyTeamUnreads} from 'mattermost-redux/actions/teams'; import {getChannelAndMyMember} from 'mattermost-redux/actions/channels'; +import {savePreferences, deletePreferences} from 'mattermost-redux/actions/preferences'; + import {Preferences as PreferencesRedux} from 'mattermost-redux/constants'; export function loadMe(callback) { - loadMeRedux()(dispatch, getState).then( + UserActions.loadMe()(dispatch, getState).then( () => { loadCurrentLocale(); @@ -102,27 +82,26 @@ export function loadMeAndConfig(callback) { }); } -export function switchFromLdapToEmail(email, password, token, ldapPassword, onSuccess, onError) { - Client.ldapToEmail( - email, - password, - token, - ldapPassword, +export function switchFromLdapToEmail(email, password, token, ldapPassword, success, error) { + UserActions.switchLdapToEmail(ldapPassword, email, password, token)(dispatch, getState).then( (data) => { - if (data.follow_link) { - window.location.href = data.follow_link; - } - - if (onSuccess) { - onSuccess(data); + if (data) { + if (data.follow_link) { + clientLogout(data.follow_link); + } + if (success) { + success(data); + } + } else if (data == null && error) { + const serverError = getState().requests.users.switchLogin.error; + error({id: serverError.server_error_id, ...serverError}); } - }, - onError + } ); } export function loadProfilesAndTeamMembers(page, perPage, teamId = TeamStore.getCurrentId(), success) { - getProfilesInTeam(teamId, page, perPage)(dispatch, getState).then( + UserActions.getProfilesInTeam(teamId, page, perPage)(dispatch, getState).then( (data) => { loadTeamMembersForProfilesList(data, teamId, success); loadStatusesForProfilesList(data); @@ -131,7 +110,7 @@ export function loadProfilesAndTeamMembers(page, perPage, teamId = TeamStore.get } export function loadProfilesAndTeamMembersAndChannelMembers(page, perPage, teamId = TeamStore.getCurrentId(), channelId = ChannelStore.getCurrentId(), success, error) { - getProfilesInChannel(channelId, page, perPage)(dispatch, getState).then( + UserActions.getProfilesInChannel(channelId, page, perPage)(dispatch, getState).then( (data) => { loadTeamMembersForProfilesList( data, @@ -167,7 +146,7 @@ export function loadTeamMembersForProfilesList(profiles, teamId = TeamStore.getC } export function loadProfilesWithoutTeam(page, perPage, success) { - getProfilesWithoutTeam(page, perPage)(dispatch, getState).then( + UserActions.getProfilesWithoutTeam(page, perPage)(dispatch, getState).then( (data) => { loadStatusesForProfilesMap(data); @@ -272,7 +251,8 @@ export function loadNewDMIfNeeded(channelId) { const pref = PreferenceStore.getBool(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, userId, false); if (pref === false) { PreferenceStore.setPreference(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, userId, 'true'); - AsyncClient.savePreference(Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, userId, 'true'); + const currentUserId = UserStore.getCurrentId(); + savePreferences(currentUserId, [{user_id: currentUserId, category: Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, name: userId, value: 'true'}])(dispatch, getState); loadProfilesForDM(); } } @@ -296,7 +276,8 @@ export function loadNewGMIfNeeded(channelId) { const pref = PreferenceStore.getBool(Preferences.CATEGORY_GROUP_CHANNEL_SHOW, channelId, false); if (pref === false) { PreferenceStore.setPreference(Preferences.CATEGORY_GROUP_CHANNEL_SHOW, channelId, 'true'); - AsyncClient.savePreference(Preferences.CATEGORY_GROUP_CHANNEL_SHOW, channelId, 'true'); + const currentUserId = UserStore.getCurrentId(); + savePreferences(currentUserId, [{user_id: currentUserId, category: Preferences.CATEGORY_GROUP_CHANNEL_SHOW, name: channelId, value: 'true'}])(dispatch, getState); loadProfilesForGM(); } } @@ -348,7 +329,7 @@ export function loadProfilesForGM() { }); } - getProfilesInChannel(channel.id, 0, Constants.MAX_USERS_IN_GM)(dispatch, getState).then( + UserActions.getProfilesInChannel(channel.id, 0, Constants.MAX_USERS_IN_GM)(dispatch, getState).then( (data) => { populateChannelWithProfiles(channel.id, data); } @@ -356,7 +337,8 @@ export function loadProfilesForGM() { } if (newPreferences.length > 0) { - AsyncClient.savePreferences(newPreferences); + const currentUserId = UserStore.getCurrentId(); + savePreferences(currentUserId, newPreferences)(dispatch, getState); } } @@ -396,11 +378,12 @@ export function loadProfilesForDM() { } if (newPreferences.length > 0) { - AsyncClient.savePreferences(newPreferences); + const currentUserId = UserStore.getCurrentId(); + savePreferences(currentUserId, newPreferences)(dispatch, getState); } if (profilesToLoad.length > 0) { - getProfilesByIds(profilesToLoad)(dispatch, getState).then( + UserActions.getProfilesByIds(profilesToLoad)(dispatch, getState).then( () => { populateDMChannelsWithProfiles(profileIds); }, @@ -411,15 +394,20 @@ export function loadProfilesForDM() { } export function saveTheme(teamId, theme, onSuccess, onError) { - AsyncClient.savePreference( - Preferences.CATEGORY_THEME, - teamId, - JSON.stringify(theme), - () => { - onThemeSaved(teamId, theme, onSuccess); - }, - (err) => { - onError(err); + const currentUserId = UserStore.getCurrentId(); + savePreferences(currentUserId, [{ + user_id: currentUserId, + category: Preferences.CATEGORY_THEME, + name: teamId, + value: JSON.stringify(theme) + }])(dispatch, getState).then( + (data) => { + if (data && onSuccess) { + onThemeSaved(teamId, theme, onSuccess); + } else if (data == null && onError) { + const serverError = getState().requests.users.savePreferences.error; + onError({id: serverError.server_error_id, ...serverError}); + } } ); } @@ -449,20 +437,15 @@ function onThemeSaved(teamId, theme, onSuccess) { if (toDelete.length > 0) { // we're saving a new global theme so delete any team-specific ones - AsyncClient.deletePreferences(toDelete); - - // delete them locally before we hear from the server so that the UI flow is smoother - AppDispatcher.handleServerAction({ - type: ActionTypes.DELETED_PREFERENCES, - preferences: toDelete - }); + const currentUserId = UserStore.getCurrentId(); + deletePreferences(currentUserId, toDelete)(dispatch, getState); } onSuccess(); } export function searchUsers(term, teamId = TeamStore.getCurrentId(), options = {}, success) { - searchProfiles(term, {team_id: teamId, ...options})(dispatch, getState).then( + UserActions.searchProfiles(term, {team_id: teamId, ...options})(dispatch, getState).then( (data) => { loadStatusesForProfilesList(data); @@ -474,7 +457,7 @@ export function searchUsers(term, teamId = TeamStore.getCurrentId(), options = { } export function searchUsersNotInTeam(term, teamId = TeamStore.getCurrentId(), options = {}, success) { - searchProfiles(term, {not_in_team_id: teamId, ...options})(dispatch, getState).then( + UserActions.searchProfiles(term, {not_in_team_id: teamId, ...options})(dispatch, getState).then( (data) => { loadStatusesForProfilesList(data); @@ -488,7 +471,7 @@ export function searchUsersNotInTeam(term, teamId = TeamStore.getCurrentId(), op export function autocompleteUsersInChannel(username, channelId, success) { const channel = ChannelStore.get(channelId); const teamId = channel ? channel.team_id : TeamStore.getCurrentId(); - autocompleteRedux(username, teamId, channelId)(dispatch, getState).then( + UserActions.autocompleteRedux(username, teamId, channelId)(dispatch, getState).then( (data) => { if (success) { success(data); @@ -498,7 +481,7 @@ export function autocompleteUsersInChannel(username, channelId, success) { } export function autocompleteUsersInTeam(username, success) { - autocompleteRedux(username, TeamStore.getCurrentId())(dispatch, getState).then( + UserActions.autocompleteRedux(username, TeamStore.getCurrentId())(dispatch, getState).then( (data) => { if (success) { success(data); @@ -508,7 +491,7 @@ export function autocompleteUsersInTeam(username, success) { } export function autocompleteUsers(username, success) { - autocompleteRedux(username)(dispatch, getState).then( + UserActions.autocompleteRedux(username)(dispatch, getState).then( (data) => { if (success) { success(data); @@ -518,7 +501,7 @@ export function autocompleteUsers(username, success) { } export function updateUser(user, type, success, error) { - updateMe(user)(dispatch, getState).then( + UserActions.updateMe(user)(dispatch, getState).then( (data) => { if (data && success) { success(data); @@ -531,24 +514,20 @@ export function updateUser(user, type, success, error) { } export function generateMfaSecret(success, error) { - Client.generateMfaSecret( + UserActions.generateMfaSecret()(dispatch, getState).then( (data) => { - if (success) { + if (data && success) { success(data); - } - }, - (err) => { - AsyncClient.dispatchError(err, 'generateMfaSecret'); - - if (error) { - error(err); + } else if (data == null && error) { + const serverError = getState().requests.users.generateMfaSecret.error; + error({id: serverError.server_error_id, ...serverError}); } } ); } export function updateUserNotifyProps(props, success, error) { - updateMe({notify_props: props})(dispatch, getState).then( + UserActions.updateMe({notify_props: props})(dispatch, getState).then( (data) => { if (data && success) { success(data); @@ -561,7 +540,7 @@ export function updateUserNotifyProps(props, success, error) { } export function updateUserRoles(userId, newRoles, success, error) { - updateUserRolesRedux(userId, newRoles)(dispatch, getState).then( + UserActions.updateUserRolesRedux(userId, newRoles)(dispatch, getState).then( (data) => { if (data && success) { success(data); @@ -574,7 +553,7 @@ export function updateUserRoles(userId, newRoles, success, error) { } export function activateMfa(code, success, error) { - updateUserMfa(UserStore.getCurrentId(), true, code)(dispatch, getState).then( + UserActions.updateUserMfa(UserStore.getCurrentId(), true, code)(dispatch, getState).then( (data) => { if (data && success) { success(data); @@ -587,7 +566,7 @@ export function activateMfa(code, success, error) { } export function deactivateMfa(success, error) { - updateUserMfa(UserStore.getCurrentId(), false)(dispatch, getState).then( + UserActions.updateUserMfa(UserStore.getCurrentId(), false)(dispatch, getState).then( (data) => { if (data && success) { success(data); @@ -605,7 +584,7 @@ export function checkMfa(loginId, success, error) { return; } - checkMfaRedux(loginId)(dispatch, getState).then( + UserActions.checkMfaRedux(loginId)(dispatch, getState).then( (data) => { if (data != null && success) { success(data); @@ -618,20 +597,20 @@ export function checkMfa(loginId, success, error) { } export function updateActive(userId, active, success, error) { - Client.updateActive(userId, active, + UserActions.updateUserActive(userId, active)(dispatch, getState).then( (data) => { - UserStore.saveProfile(data); - - if (success) { + if (data && success) { success(data); + } else if (data == null && error) { + const serverError = getState().requests.users.updateUser.error; + error({id: serverError.server_error_id, ...serverError}); } - }, - error + } ); } export function updatePassword(userId, currentPassword, newPassword, success, error) { - updateUserPassword(userId, currentPassword, newPassword)(dispatch, getState).then( + UserActions.updateUserPassword(userId, currentPassword, newPassword)(dispatch, getState).then( (data) => { if (data && success) { success(data); @@ -644,76 +623,68 @@ export function updatePassword(userId, currentPassword, newPassword, success, er } export function verifyEmail(token, success, error) { - Client.verifyEmail( - token, + UserActions.verifyUserEmail(token)(dispatch, getState).then( (data) => { - if (success) { + if (data && success) { success(data); - } - }, - (err) => { - if (error) { - error(err); + } else if (data == null && error) { + const serverError = getState().requests.users.verifyEmail.error; + error({id: serverError.server_error_id, ...serverError}); } } ); } export function resetPassword(token, password, success, error) { - Client.resetPassword( - token, - password, - () => { - browserHistory.push('/login?extra=' + ActionTypes.PASSWORD_CHANGE); - - if (success) { - success(); - } - }, - (err) => { - if (error) { - error(err); + UserActions.resetUserPassword(token, password)(dispatch, getState).then( + (data) => { + if (data) { + browserHistory.push('/login?extra=' + ActionTypes.PASSWORD_CHANGE); + if (success) { + success(data); + } + } else if (data == null && error) { + const serverError = getState().requests.users.passwordReset.error; + error({id: serverError.server_error_id, ...serverError}); } } ); } export function resendVerification(email, success, error) { - Client.resendVerification( - email, - () => { - if (success) { - success(); - } - }, - (err) => { - if (error) { - error(err); + UserActions.sendVerificationEmail(email)(dispatch, getState).then( + (data) => { + if (data && success) { + success(data); + } else if (data == null && error) { + const serverError = getState().requests.users.verifyEmail.error; + error({id: serverError.server_error_id, ...serverError}); } } ); } export function loginById(userId, password, mfaToken, success, error) { - Client.loginById( - userId, - password, - mfaToken, - () => { - if (success) { + UserActions.loginById(userId, password, mfaToken)(dispatch, getState).then( + (ok) => { + if (ok && success) { success(); - } - }, - (err) => { - if (error) { - error(err); + } else if (!ok && error) { + const serverError = getState().requests.users.login.error; + if (serverError.server_error_id === 'api.context.mfa_required.app_error') { + if (success) { + success(); + } + return; + } + error({id: serverError.server_error_id, ...serverError}); } } ); } export function createUserWithInvite(user, data, emailHash, inviteId, success, error) { - createUser(user, data, emailHash, inviteId)(dispatch, getState).then( + UserActions.createUser(user, data, emailHash, inviteId)(dispatch, getState).then( (resp) => { if (resp && success) { success(resp); @@ -726,7 +697,7 @@ export function createUserWithInvite(user, data, emailHash, inviteId, success, e } export function webLogin(loginId, password, token, success, error) { - login(loginId, password, token)(dispatch, getState).then( + UserActions.login(loginId, password, token)(dispatch, getState).then( (ok) => { if (ok && success) { success(); @@ -745,18 +716,19 @@ export function webLogin(loginId, password, token, success, error) { } export function webLoginByLdap(loginId, password, token, success, error) { - Client.webLoginByLdap( - loginId, - password, - token, - (data) => { - if (success) { - success(data); - } - }, - (err) => { - if (error) { - error(err); + UserActions.login(loginId, password, token, true)(dispatch, getState).then( + (ok) => { + if (ok && success) { + success(); + } else if (!ok && error) { + const serverError = getState().requests.users.login.error; + if (serverError.server_error_id === 'api.context.mfa_required.app_error') { + if (success) { + success(); + } + return; + } + error({id: serverError.server_error_id, ...serverError}); } } ); @@ -792,24 +764,20 @@ export function deauthorizeOAuthApp(appId, success, error) { } export function uploadProfileImage(userPicture, success, error) { - Client.uploadProfileImage( - userPicture, - () => { - getMe()(dispatch, getState); - if (success) { - success(); - } - }, - (err) => { - if (error) { - error(err); + UserActions.uploadProfileImage(Selectors.getCurrentUserId(getState()), userPicture)(dispatch, getState).then( + (data) => { + if (data && success) { + success(data); + } else if (data == null && error) { + const serverError = getState().requests.users.updateUser.error; + error({id: serverError.server_error_id, ...serverError}); } } ); } export function loadProfiles(page, perPage, success) { - getProfiles(page, perPage)(dispatch, getState).then( + UserActions.getProfiles(page, perPage)(dispatch, getState).then( (data) => { if (success) { success(data); @@ -825,13 +793,13 @@ export function getMissingProfiles(ids) { return; } - getProfilesByIds(missingIds)(dispatch, getState); + UserActions.getProfilesByIds(missingIds)(dispatch, getState); } export function loadMyTeamMembers() { getMyTeamMembers()(dispatch, getState).then( () => { - AsyncClient.getMyTeamsUnread(); + getMyTeamUnreads()(dispatch, getState); } ); } @@ -839,7 +807,7 @@ export function loadMyTeamMembers() { export function autoResetStatus() { return async (doDispatch, doGetState) => { const {currentUserId} = getState().entities.users; - const userStatus = await getStatus(currentUserId)(doDispatch, doGetState); + const userStatus = await UserActions.getStatus(currentUserId)(doDispatch, doGetState); if (!userStatus.manual) { return userStatus; @@ -848,7 +816,7 @@ export function autoResetStatus() { const autoReset = getBool(getState(), PreferencesRedux.CATEGORY_AUTO_RESET_MANUAL_STATUS, currentUserId, false); if (autoReset) { - setStatus({user_id: currentUserId, status: 'online'})(doDispatch, doGetState); + UserActions.setStatus({user_id: currentUserId, status: 'online'})(doDispatch, doGetState); return userStatus; } -- cgit v1.2.3-1-g7c22