From db898694a5ee48e0661f9ca048aa30f951a33809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Fri, 7 Sep 2018 11:51:36 +0200 Subject: [PATCH] MM-11725: Filter archived channels by membership on search autocomplete (#2066) * MM-11725: Filter archived channels by membership on search autocomplete * Fixing PR suggestions --- .../channel_mention/channel_mention.js | 9 ++++++--- .../autocomplete/channel_mention/index.js | 4 ++++ app/selectors/autocomplete.js | 17 +++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/components/autocomplete/channel_mention/channel_mention.js b/app/components/autocomplete/channel_mention/channel_mention.js index 6555f62f8..c510f7074 100644 --- a/app/components/autocomplete/channel_mention/channel_mention.js +++ b/app/components/autocomplete/channel_mention/channel_mention.js @@ -24,11 +24,13 @@ export default class ChannelMention extends PureComponent { listHeight: PropTypes.number, matchTerm: PropTypes.string, myChannels: PropTypes.array, + myMembers: PropTypes.object, otherChannels: PropTypes.array, onChangeText: PropTypes.func.isRequired, onResultCountChange: PropTypes.func.isRequired, privateChannels: PropTypes.array, publicChannels: PropTypes.array, + deletedPublicChannels: PropTypes.instanceOf(Set), requestStatus: PropTypes.string.isRequired, theme: PropTypes.object.isRequired, value: PropTypes.string, @@ -48,7 +50,7 @@ export default class ChannelMention extends PureComponent { } componentWillReceiveProps(nextProps) { - const {isSearch, matchTerm, myChannels, otherChannels, privateChannels, publicChannels, requestStatus} = nextProps; + const {isSearch, matchTerm, myChannels, otherChannels, privateChannels, publicChannels, requestStatus, myMembers, deletedPublicChannels} = nextProps; if ((matchTerm !== this.props.matchTerm && matchTerm === null) || this.state.mentionComplete) { // if the term changes but is null or the mention has been completed we render this component as null @@ -74,7 +76,8 @@ export default class ChannelMention extends PureComponent { if (requestStatus !== RequestStatus.STARTED && (myChannels !== this.props.myChannels || otherChannels !== this.props.otherChannels || - privateChannels !== this.props.privateChannels || publicChannels !== this.props.publicChannels)) { + privateChannels !== this.props.privateChannels || publicChannels !== this.props.publicChannels || + myMembers !== this.props.myMembers || deletedPublicChannels !== this.props.deletedPublicChannels)) { // if the request is complete and the term is not null we show the autocomplete const sections = []; if (isSearch) { @@ -82,7 +85,7 @@ export default class ChannelMention extends PureComponent { sections.push({ id: 'suggestion.search.public', defaultMessage: 'Public Channels', - data: publicChannels, + data: publicChannels.filter((cId) => !deletedPublicChannels.has(cId) || myMembers[cId]), key: 'publicChannels', }); } diff --git a/app/components/autocomplete/channel_mention/index.js b/app/components/autocomplete/channel_mention/index.js index c83f2d6ce..57cea375c 100644 --- a/app/components/autocomplete/channel_mention/index.js +++ b/app/components/autocomplete/channel_mention/index.js @@ -5,6 +5,7 @@ import {bindActionCreators} from 'redux'; import {connect} from 'react-redux'; import {searchChannels} from 'mattermost-redux/actions/channels'; +import {getMyChannelMemberships} from 'mattermost-redux/selectors/entities/channels'; import {getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams'; import { @@ -12,6 +13,7 @@ import { filterOtherChannels, filterPublicChannels, filterPrivateChannels, + getDeletedPublicChannelsIds, getMatchTermForChannelMention, } from 'app/selectors/autocomplete'; import {getTheme} from 'mattermost-redux/selectors/entities/preferences'; @@ -38,8 +40,10 @@ function mapStateToProps(state, ownProps) { return { myChannels, + myMembers: getMyChannelMemberships(state), otherChannels, publicChannels, + deletedPublicChannels: getDeletedPublicChannelsIds(state), privateChannels, currentTeamId: getCurrentTeamId(state), matchTerm, diff --git a/app/selectors/autocomplete.js b/app/selectors/autocomplete.js index 8bc92d477..0bfeb74e8 100644 --- a/app/selectors/autocomplete.js +++ b/app/selectors/autocomplete.js @@ -242,3 +242,20 @@ export const makeGetMatchTermForDateMention = () => { return lastMatchTerm; }; }; + +export const getDeletedPublicChannelsIds = createSelector( + getMyChannels, + getOtherChannels, + (myChannels, otherChannels) => { + const channels = myChannels.filter((c) => { + return (c.type === General.OPEN_CHANNEL); + }).concat(otherChannels); + + return new Set(channels.reduce((acc, c) => { + if (c.delete_at !== 0) { + acc.push(c.id); + } + return acc; + }, [])); + } +);