[MM-52546] webapp/channels : Update current user and status on WebSocket reconnect (#23071)

* update user status on reconnect

* small type improvement

* profiles reducer: merge old profiles with new ones

- remove current user exclusion from profiles received
- merge old profiles with new ones

* profiles reducer unit test

profiles reducer should merge existing users with new ones

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
LeonardJouve
2023-05-15 22:29:45 +02:00
коммит произвёл GitHub
родитель 4cbf6e93d2
Коммит 6d3354266a
4 изменённых файлов: 41 добавлений и 6 удалений

Просмотреть файл

@@ -233,7 +233,7 @@ export function reconnect() {
// we can request for getPosts again when socket is connected
dispatch(getPosts(currentChannelId));
}
StatusActions.loadStatusesForChannelAndSidebar();
dispatch(StatusActions.loadStatusesForChannelAndSidebar());
const crtEnabled = isCollapsedThreadsEnabled(state);
dispatch(TeamActions.getMyTeamUnreads(crtEnabled, true));

Просмотреть файл

@@ -280,12 +280,10 @@ 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));

Просмотреть файл

@@ -4,6 +4,9 @@
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<typeof reducer>;
describe('Reducers.users', () => {
@@ -673,4 +676,35 @@ 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);
});
});
});

Просмотреть файл

@@ -198,15 +198,18 @@ function profiles(state: IDMappedObjects<UserProfile> = {}, action: GenericActio
const users: UserProfile[] = action.data;
return users.reduce((nextState, user) => {
const oldUser = nextState[user.id];
const oldUser = nextState[user.id] || {};
if (oldUser && isEqual(user, oldUser)) {
if (isEqual(user, oldUser)) {
return nextState;
}
return {
...nextState,
[user.id]: user,
[user.id]: {
...oldUser,
...user,
},
};
}, state);
}