Skip to content

Commit

Permalink
MM-53609 - remove channels from archived teams during search (matterm…
Browse files Browse the repository at this point in the history
…ost#24053)

* MM-53609 - remove channels from archived teams during search

* filter channels after the channels from server are fetched

* filter only channels, not DM or GM

* add unit tests; prevent blinking when searching for terms
  • Loading branch information
pvev authored Jul 31, 2023
1 parent aa88f8b commit ef3aec4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ jest.mock('mattermost-redux/actions/channels', () => ({
name: 'other_user',
display_name: 'other_user',
delete_at: 0,
}],
team_id: 'currentTeamId',
},
],
})),
}));

Expand Down Expand Up @@ -119,6 +121,7 @@ describe('components/SwitchChannelProvider', () => {
id: 'currentTeamId',
display_name: 'test',
type: 'O',
delete_at: 0,
},
},
},
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<WrappedChannel>) {
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);
Expand Down

0 comments on commit ef3aec4

Please sign in to comment.