diff --git a/webapp/channels/src/actions/websocket_actions.jsx b/webapp/channels/src/actions/websocket_actions.jsx index 9d0a568542..566540c3c4 100644 --- a/webapp/channels/src/actions/websocket_actions.jsx +++ b/webapp/channels/src/actions/websocket_actions.jsx @@ -35,6 +35,7 @@ import {loadRolesIfNeeded} from 'mattermost-redux/actions/roles'; import {isCollapsedThreadsEnabled} from 'mattermost-redux/selectors/entities/preferences'; import {getNewestThreadInTeam, getThread, getThreads} from 'mattermost-redux/selectors/entities/threads'; +import {getGroup} from 'mattermost-redux/selectors/entities/groups'; import { getThread as fetchThread, getCountsAndThreadsSince, @@ -65,6 +66,7 @@ import { checkForModifiedUsers, getUser as loadUser, } from 'mattermost-redux/actions/users'; +import {getGroup as fetchGroup} from 'mattermost-redux/actions/groups'; import {removeNotVisibleUsers} from 'mattermost-redux/actions/websocket'; import {setGlobalItem} from 'actions/storage'; import {setGlobalDraft, transformServerDraft} from 'actions/views/drafts'; @@ -1355,20 +1357,32 @@ function handleGroupUpdatedEvent(msg) { ); } -function handleGroupAddedMemberEvent(msg) { - return (doDispatch, doGetState) => { +export function handleGroupAddedMemberEvent(msg) { + return async (doDispatch, doGetState) => { const state = doGetState(); const currentUserId = getCurrentUserId(state); - const data = JSON.parse(msg.data.group_member); + const groupInfo = JSON.parse(msg.data.group_member); - if (currentUserId === data.user_id) { - dispatch( - { - type: GroupTypes.ADD_MY_GROUP, - data, - id: data.group_id, - }, - ); + if (currentUserId === groupInfo.user_id) { + const group = getGroup(state, groupInfo.group_id); + if (group) { + dispatch( + { + type: GroupTypes.ADD_MY_GROUP, + id: groupInfo.group_id, + }, + ); + } else { + const {error} = await doDispatch(fetchGroup(groupInfo.group_id, true)); + if (!error) { + dispatch( + { + type: GroupTypes.ADD_MY_GROUP, + id: groupInfo.group_id, + }, + ); + } + } } }; } diff --git a/webapp/channels/src/actions/websocket_actions.test.jsx b/webapp/channels/src/actions/websocket_actions.test.jsx index f97cc74eed..36c25a0a16 100644 --- a/webapp/channels/src/actions/websocket_actions.test.jsx +++ b/webapp/channels/src/actions/websocket_actions.test.jsx @@ -6,6 +6,7 @@ import { getThreadsForPosts, receivedNewPost, } from 'mattermost-redux/actions/posts'; +import {getGroup} from 'mattermost-redux/actions/groups'; import {ChannelTypes, UserTypes, CloudTypes} from 'mattermost-redux/action_types'; import {getUser} from 'mattermost-redux/actions/users'; @@ -37,6 +38,7 @@ import { handleAppsPluginEnabled, handleAppsPluginDisabled, handleCloudSubscriptionChanged, + handleGroupAddedMemberEvent, } from './websocket_actions'; jest.mock('mattermost-redux/actions/posts', () => ({ @@ -45,6 +47,11 @@ jest.mock('mattermost-redux/actions/posts', () => ({ getProfilesAndStatusesForPosts: jest.fn(), })); +jest.mock('mattermost-redux/actions/groups', () => ({ + ...jest.requireActual('mattermost-redux/actions/groups'), + getGroup: jest.fn(() => ({type: 'RECEIVED_GROUP'})), +})); + jest.mock('mattermost-redux/actions/users', () => ({ getMissingProfilesByIds: jest.fn(() => ({type: 'GET_MISSING_PROFILES_BY_IDS'})), getStatusesByIds: jest.fn(() => ({type: 'GET_STATUSES_BY_IDS'})), @@ -105,6 +112,20 @@ let mockState = { PluginsEnabled: 'true', }, }, + groups: { + syncables: {}, + groups: { + 'group-1': { + id: 'group-1', + name: 'group1', + display_name: 'Group 1', + member_count: 1, + allow_reference: true, + }, + }, + stats: {}, + myGroups: {}, + }, channels: { currentChannelId: 'otherChannel', channels: { @@ -202,6 +223,42 @@ describe('handlePostEditEvent', () => { }); }); +describe('handleGroupAddedMemberEvent', () => { + test('add to group in state', async () => { + const testStore = configureStore(mockState); + const msg = { + data: { + group_member: '{"group_id":"group-1","user_id":"currentUserId","create_at":1691178673417,"delete_at":0}', + }, + broadcast: { + user_id: 'currentUserId', + }, + }; + + testStore.dispatch(handleGroupAddedMemberEvent(msg)); + expect(store.dispatch).toHaveBeenCalledWith({ + type: 'ADD_MY_GROUP', + id: 'group-1', + }); + }); + + test('add to group not in state', async () => { + const testStore = configureStore(mockState); + const msg = { + data: { + group_member: '{"group_id":"group-2","user_id":"currentUserId","create_at":1691178673417,"delete_at":0}', + }, + broadcast: { + user_id: 'currentUserId', + }, + }; + + testStore.dispatch(handleGroupAddedMemberEvent(msg)); + expect(getGroup).toHaveBeenCalled(); + expect(testStore.getActions()).toEqual([{type: 'RECEIVED_GROUP'}]); + }); +}); + describe('handlePostUnreadEvent', () => { test('post marked as unred', async () => { const msgData = {last_viewed_at: 123, msg_count: 40, mention_count: 1};