diff --git a/webapp/channels/src/actions/websocket_actions.jsx b/webapp/channels/src/actions/websocket_actions.jsx index 79388b0a49..731b383350 100644 --- a/webapp/channels/src/actions/websocket_actions.jsx +++ b/webapp/channels/src/actions/websocket_actions.jsx @@ -234,7 +234,7 @@ export function reconnect() { // we can request for getPosts again when socket is connected dispatch(getPosts(currentChannelId)); } - dispatch(StatusActions.loadStatusesForChannelAndSidebar()); + StatusActions.loadStatusesForChannelAndSidebar(); const crtEnabled = isCollapsedThreadsEnabled(state); dispatch(TeamActions.getMyTeamUnreads(crtEnabled, true)); diff --git a/webapp/channels/src/packages/mattermost-redux/src/actions/users.ts b/webapp/channels/src/packages/mattermost-redux/src/actions/users.ts index 58f6fb9126..c0e1da102b 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/actions/users.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/actions/users.ts @@ -35,6 +35,7 @@ import {getServerVersion} from 'mattermost-redux/selectors/entities/general'; import {getCurrentUserId, getUsers} from 'mattermost-redux/selectors/entities/users'; import {isCollapsedThreadsEnabled} from 'mattermost-redux/selectors/entities/preferences'; +import {removeUserFromList} from 'mattermost-redux/utils/user_utils'; import {isMinimumServerVersion} from 'mattermost-redux/utils/helpers'; import {General} from 'mattermost-redux/constants'; @@ -214,10 +215,12 @@ export function getFilteredUsersStats(options: GetFilteredUsersStatsOpts = {}, u export function getProfiles(page = 0, perPage: number = General.PROFILE_CHUNK_SIZE, options: any = {}): ActionFunc { return async (dispatch: DispatchFunc, getState: GetStateFunc) => { + const {currentUserId} = getState().entities.users; let profiles: UserProfile[]; try { profiles = await Client4.getProfiles(page, perPage, options); + removeUserFromList(currentUserId, profiles); } catch (error) { forceLogoutIfNecessary(error, dispatch, getState); dispatch(logError(error)); @@ -277,10 +280,12 @@ export function getMissingProfilesByUsernames(usernames: string[]): ActionFunc { export function getProfilesByIds(userIds: string[], options?: any): ActionFunc { return async (dispatch: DispatchFunc, getState: GetStateFunc) => { + const {currentUserId} = getState().entities.users; let profiles: UserProfile[]; try { profiles = await Client4.getProfilesByIds(userIds, options); + removeUserFromList(currentUserId, profiles); } catch (error) { forceLogoutIfNecessary(error, dispatch, getState); dispatch(logError(error)); @@ -298,10 +303,12 @@ export function getProfilesByIds(userIds: string[], options?: any): ActionFunc { export function getProfilesByUsernames(usernames: string[]): ActionFunc { return async (dispatch: DispatchFunc, getState: GetStateFunc) => { + const {currentUserId} = getState().entities.users; let profiles; try { profiles = await Client4.getProfilesByUsernames(usernames); + removeUserFromList(currentUserId, profiles); } catch (error) { forceLogoutIfNecessary(error, dispatch, getState); dispatch(logError(error)); @@ -319,6 +326,7 @@ export function getProfilesByUsernames(usernames: string[]): ActionFunc { export function getProfilesInTeam(teamId: string, page: number, perPage: number = General.PROFILE_CHUNK_SIZE, sort = '', options: any = {}): ActionFunc { return async (dispatch: DispatchFunc, getState: GetStateFunc) => { + const {currentUserId} = getState().entities.users; let profiles; try { @@ -337,7 +345,7 @@ export function getProfilesInTeam(teamId: string, page: number, perPage: number }, { type: UserTypes.RECEIVED_PROFILES_LIST, - data: profiles, + data: removeUserFromList(currentUserId, [...profiles]), }, ])); @@ -407,6 +415,7 @@ export enum ProfilesInChannelSortBy { export function getProfilesInChannel(channelId: string, page: number, perPage: number = General.PROFILE_CHUNK_SIZE, sort = '', options: {active?: boolean} = {}): ActionFunc { return async (dispatch: DispatchFunc, getState: GetStateFunc) => { + const {currentUserId} = getState().entities.users; let profiles; try { @@ -425,7 +434,7 @@ export function getProfilesInChannel(channelId: string, page: number, perPage: n }, { type: UserTypes.RECEIVED_PROFILES_LIST, - data: profiles, + data: removeUserFromList(currentUserId, [...profiles]), }, ])); @@ -435,6 +444,7 @@ export function getProfilesInChannel(channelId: string, page: number, perPage: n export function getProfilesInGroupChannels(channelsIds: string[]): ActionFunc { return async (dispatch: DispatchFunc, getState: GetStateFunc) => { + const {currentUserId} = getState().entities.users; let channelProfiles; try { @@ -458,7 +468,7 @@ export function getProfilesInGroupChannels(channelsIds: string[]): ActionFunc { }, { type: UserTypes.RECEIVED_PROFILES_LIST, - data: profiles, + data: removeUserFromList(currentUserId, [...profiles]), }, ); } @@ -472,6 +482,7 @@ export function getProfilesInGroupChannels(channelsIds: string[]): ActionFunc { export function getProfilesNotInChannel(teamId: string, channelId: string, groupConstrained: boolean, page: number, perPage: number = General.PROFILE_CHUNK_SIZE): ActionFunc { return async (dispatch: DispatchFunc, getState: GetStateFunc) => { + const {currentUserId} = getState().entities.users; let profiles; try { @@ -492,7 +503,7 @@ export function getProfilesNotInChannel(teamId: string, channelId: string, group }, { type: UserTypes.RECEIVED_PROFILES_LIST, - data: profiles, + data: removeUserFromList(currentUserId, [...profiles]), }, ])); @@ -553,6 +564,7 @@ export function updateMyTermsOfServiceStatus(termsOfServiceId: string, accepted: export function getProfilesInGroup(groupId: string, page = 0, perPage: number = General.PROFILE_CHUNK_SIZE, sort = ''): ActionFunc { return async (dispatch: DispatchFunc, getState: GetStateFunc) => { + const {currentUserId} = getState().entities.users; let profiles; try { @@ -571,7 +583,7 @@ export function getProfilesInGroup(groupId: string, page = 0, perPage: number = }, { type: UserTypes.RECEIVED_PROFILES_LIST, - data: profiles, + data: removeUserFromList(currentUserId, [...profiles]), }, ])); @@ -581,6 +593,7 @@ export function getProfilesInGroup(groupId: string, page = 0, perPage: number = export function getProfilesNotInGroup(groupId: string, page = 0, perPage: number = General.PROFILE_CHUNK_SIZE): ActionFunc { return async (dispatch: DispatchFunc, getState: GetStateFunc) => { + const {currentUserId} = getState().entities.users; let profiles; try { @@ -599,7 +612,7 @@ export function getProfilesNotInGroup(groupId: string, page = 0, perPage: number }, { type: UserTypes.RECEIVED_PROFILES_LIST, - data: profiles, + data: removeUserFromList(currentUserId, [...profiles]), }, ])); @@ -831,6 +844,9 @@ export function autocompleteUsers(term: string, teamId = '', channelId = '', opt }): ActionFunc { return async (dispatch: DispatchFunc, getState: GetStateFunc) => { dispatch({type: UserTypes.AUTOCOMPLETE_USERS_REQUEST, data: null}); + + const {currentUserId} = getState().entities.users; + let data; try { data = await Client4.autocompleteUsers(term, teamId, channelId, options); @@ -845,6 +861,7 @@ export function autocompleteUsers(term: string, teamId = '', channelId = '', opt if (data.out_of_channel) { users = [...users, ...data.out_of_channel]; } + removeUserFromList(currentUserId, users); const actions: AnyAction[] = [{ type: UserTypes.RECEIVED_PROFILES_LIST, data: users, @@ -887,6 +904,8 @@ export function autocompleteUsers(term: string, teamId = '', channelId = '', opt export function searchProfiles(term: string, options: any = {}): ActionFunc { return async (dispatch: DispatchFunc, getState: GetStateFunc) => { + const {currentUserId} = getState().entities.users; + let profiles; try { profiles = await Client4.searchUsers(term, options); @@ -896,7 +915,7 @@ export function searchProfiles(term: string, options: any = {}): ActionFunc { return {error}; } - const actions: AnyAction[] = [{type: UserTypes.RECEIVED_PROFILES_LIST, data: profiles}]; + const actions: AnyAction[] = [{type: UserTypes.RECEIVED_PROFILES_LIST, data: removeUserFromList(currentUserId, [...profiles])}]; if (options.in_channel_id) { actions.push({ diff --git a/webapp/channels/src/packages/mattermost-redux/src/reducers/entities/users.test.ts b/webapp/channels/src/packages/mattermost-redux/src/reducers/entities/users.test.ts index 66e32e6dec..296258177a 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/reducers/entities/users.test.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/reducers/entities/users.test.ts @@ -4,9 +4,6 @@ import {UserTypes, ChannelTypes} from 'mattermost-redux/action_types'; import {GenericAction} from 'mattermost-redux/types/actions'; import reducer from 'mattermost-redux/reducers/entities/users'; - -import {TestHelper} from 'utils/test_helper'; - type ReducerState = ReturnType; describe('Reducers.users', () => { @@ -676,35 +673,4 @@ describe('Reducers.users', () => { expect(newState.profilesNotInGroup).toEqual(expectedState.profilesNotInGroup); }); }); - describe('profiles', () => { - it('UserTypes.RECEIVED_PROFILES_LIST, should merge existing users with new ones', () => { - const firstUser = TestHelper.getUserMock({id: 'first_user_id'}); - const secondUser = TestHelper.getUserMock({id: 'seocnd_user_id'}); - const thirdUser = TestHelper.getUserMock({id: 'third_user_id'}); - const partialUpdatedFirstUser = { - ...firstUser, - update_at: 123456789, - }; - Reflect.deleteProperty(partialUpdatedFirstUser, 'email'); - Reflect.deleteProperty(partialUpdatedFirstUser, 'notify_props'); - const state = { - profiles: { - first_user_id: firstUser, - second_user_id: secondUser, - }, - }; - const action = { - type: UserTypes.RECEIVED_PROFILES_LIST, - data: [ - partialUpdatedFirstUser, - thirdUser, - ], - }; - const {profiles: newProfiles} = reducer(state as unknown as ReducerState, action); - - expect(newProfiles.first_user_id).toEqual({...firstUser, ...partialUpdatedFirstUser}); - expect(newProfiles.second_user_id).toEqual(secondUser); - expect(newProfiles.third_user_id).toEqual(thirdUser); - }); - }); }); diff --git a/webapp/channels/src/packages/mattermost-redux/src/reducers/entities/users.ts b/webapp/channels/src/packages/mattermost-redux/src/reducers/entities/users.ts index 746f35e09e..834e13ae0c 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/reducers/entities/users.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/reducers/entities/users.ts @@ -198,18 +198,15 @@ function profiles(state: IDMappedObjects = {}, action: GenericActio const users: UserProfile[] = action.data; return users.reduce((nextState, user) => { - const oldUser = nextState[user.id] || {}; + const oldUser = nextState[user.id]; - if (isEqual(user, oldUser)) { + if (oldUser && isEqual(user, oldUser)) { return nextState; } return { ...nextState, - [user.id]: { - ...oldUser, - ...user, - }, + [user.id]: user, }; }, state); }