diff --git a/webapp/channels/src/components/suggestion/switch_channel_provider.test.jsx b/webapp/channels/src/components/suggestion/switch_channel_provider.test.jsx index a4cfa9553a..a9a7cbb4d2 100644 --- a/webapp/channels/src/components/suggestion/switch_channel_provider.test.jsx +++ b/webapp/channels/src/components/suggestion/switch_channel_provider.test.jsx @@ -43,7 +43,9 @@ jest.mock('mattermost-redux/actions/channels', () => ({ name: 'other_user', display_name: 'other_user', delete_at: 0, - }], + team_id: 'currentTeamId', + }, + ], })), })); @@ -119,6 +121,7 @@ describe('components/SwitchChannelProvider', () => { id: 'currentTeamId', display_name: 'test', type: 'O', + delete_at: 0, }, }, }, @@ -827,6 +830,59 @@ describe('components/SwitchChannelProvider', () => { expect(results.terms).toEqual(expectedOrder); }); + it('should filter out channels belonging to archived teams', async () => { + const modifiedState = { + ...defaultState, + entities: { + ...defaultState.entities, + channels: { + ...defaultState.entities.channels, + myMembers: { + channel_1: {}, + channel_2: {}, + }, + channels: { + channel_1: { + id: 'channel_1', + type: 'O', + name: 'channel_1', + display_name: 'channel 1', + delete_at: 0, + team_id: 'currentTeamId', + }, + channel_2: { + id: 'channel_2', + type: 'O', + name: 'channel_2', + display_name: 'channel 2', + delete_at: 0, + team_id: 'archivedTeam', + }, + }, + }, + }, + }; + + getState.mockClear(); + + const switchProvider = new SwitchChannelProvider(); + const store = mockStore(modifiedState); + + getState.mockImplementation(store.getState); + const searchText = 'chan'; + const resultsCallback = jest.fn(); + + switchProvider.startNewRequest(); + await switchProvider.fetchUsersAndChannels(searchText, resultsCallback); + const channelsFromActiveTeams = [ + 'channel_1', + ]; + + expect(resultsCallback).toBeCalledWith(expect.objectContaining({ + terms: channelsFromActiveTeams, + })); + }); + it('Should show threads as the first item in the list if search term matches', async () => { const modifiedState = { ...defaultState, diff --git a/webapp/channels/src/components/suggestion/switch_channel_provider.tsx b/webapp/channels/src/components/suggestion/switch_channel_provider.tsx index d8aa290ae6..d0b9badaa4 100644 --- a/webapp/channels/src/components/suggestion/switch_channel_provider.tsx +++ b/webapp/channels/src/components/suggestion/switch_channel_provider.tsx @@ -31,6 +31,7 @@ import { import {getMyPreferences, isGroupChannelManuallyVisible, isCollapsedThreadsEnabled} from 'mattermost-redux/selectors/entities/preferences'; import {getConfig} from 'mattermost-redux/selectors/entities/general'; import { + getActiveTeamsList, getCurrentTeamId, getMyTeams, getTeam, @@ -441,7 +442,8 @@ export default class SwitchChannelProvider extends Provider { } // Dispatch suggestions for local data (filter out deleted and archived channels from local store data) - const channels = getChannelsInAllTeams(getState()).concat(getDirectAndGroupChannels(getState())).filter((c) => c.delete_at === 0); + let channels = getChannelsInAllTeams(getState()).concat(getDirectAndGroupChannels(getState())).filter((c) => c.delete_at === 0); + channels = this.removeChannelsFromArchivedTeams(channels); const users = searchProfilesMatchingWithTerm(getState(), channelPrefix, false); const formattedData = this.formatList(channelPrefix, [ThreadsChannel, ...channels], users, true, true); if (formattedData) { @@ -494,10 +496,13 @@ export default class SwitchChannelProvider extends Provider { const currentUserId = getCurrentUserId(state); // filter out deleted and archived channels from local store data - const localChannelData = getChannelsInAllTeams(state).concat(getDirectAndGroupChannels(state)).filter((c) => c.delete_at === 0) || []; + let localChannelData = getChannelsInAllTeams(state).concat(getDirectAndGroupChannels(state)).filter((c) => c.delete_at === 0) || []; + localChannelData = this.removeChannelsFromArchivedTeams(localChannelData); const localUserData = searchProfilesMatchingWithTerm(state, channelPrefix, false); const localFormattedData = this.formatList(channelPrefix, [ThreadsChannel, ...localChannelData], localUserData); - const remoteChannelData = channelsFromServer.concat(getGroupChannels(state)) || []; + let remoteChannelData = channelsFromServer.concat(getGroupChannels(state)) || []; + remoteChannelData = this.removeChannelsFromArchivedTeams(remoteChannelData); + const remoteUserData = usersFromServer.users || []; const remoteFormattedData = this.formatList(channelPrefix, remoteChannelData, remoteUserData, false); @@ -689,9 +694,22 @@ export default class SwitchChannelProvider extends Provider { }; } + removeChannelsFromArchivedTeams(channels: Channel[]) { + const state = getState(); + const activeTeams = getActiveTeamsList(state).map((team: Team) => team.id); + const newChannels = channels.filter((channel: Channel) => { + if (!channel.team_id) { + return true; + } + return activeTeams.includes(channel.team_id); + }); + return newChannels; + } + fetchAndFormatRecentlyViewedChannels(resultsCallback: ResultsCallback) { const state = getState(); - const recentChannels = getChannelsInAllTeams(state).concat(getDirectAndGroupChannels(state)); + let recentChannels = getChannelsInAllTeams(state).concat(getDirectAndGroupChannels(state)); + recentChannels = this.removeChannelsFromArchivedTeams(recentChannels); const wrappedRecentChannels = this.wrapChannels(recentChannels, Constants.MENTION_RECENT_CHANNELS); const unreadChannels = getSortedAllTeamsUnreadChannels(state); const myMembers = getMyChannelMemberships(state);