From 5c4772427a9d9195ba1bce2597168baaf41ee1e2 Mon Sep 17 00:00:00 2001
From: Reinaldo Neto <47038980+reinaldonetof@users.noreply.github.com>
Date: Wed, 26 May 2021 18:10:20 -0300
Subject: [PATCH] [FIX] Add Existing Channel screen showing discussions and
 channels without permission (#3151)

* [Fix] the filter to show the existing channel

* [Refactor] the function that filter to isolate it

* Refactor how to wsearch properly

Co-authored-by: Diego Mello <diegolmello@gmail.com>
---
 app/views/AddExistingChannelView.js | 46 ++++++++++++++++-------------
 1 file changed, 25 insertions(+), 21 deletions(-)

diff --git a/app/views/AddExistingChannelView.js b/app/views/AddExistingChannelView.js
index a3acf9e2..03cfaa4f 100644
--- a/app/views/AddExistingChannelView.js
+++ b/app/views/AddExistingChannelView.js
@@ -17,9 +17,10 @@ import StatusBar from '../containers/StatusBar';
 import { themes } from '../constants/colors';
 import { withTheme } from '../theme';
 import SafeAreaView from '../containers/SafeAreaView';
+import Loading from '../containers/Loading';
 import { animateNextTransition } from '../utils/layoutAnimation';
 import { goRoom } from '../utils/goRoom';
-import Loading from '../containers/Loading';
+import debounce from '../utils/debounce';
 
 const QUERY_SIZE = 50;
 
@@ -34,7 +35,7 @@ class AddExistingChannelView extends React.Component {
 
 	constructor(props) {
 		super(props);
-		this.init();
+		this.query();
 		this.teamId = props.route?.params?.teamId;
 		this.state = {
 			search: [],
@@ -66,7 +67,7 @@ class AddExistingChannelView extends React.Component {
 		navigation.setOptions(options);
 	}
 
-	init = async() => {
+	query = async(stringToSearch = '') => {
 		try {
 			const { addTeamChannelPermission } = this.props;
 			const db = database.active;
@@ -75,39 +76,42 @@ class AddExistingChannelView extends React.Component {
 				.query(
 					Q.where('team_id', ''),
 					Q.where('t', Q.oneOf(['c', 'p'])),
+					Q.where('name', Q.like(`%${ stringToSearch }%`)),
 					Q.experimentalTake(QUERY_SIZE),
 					Q.experimentalSortBy('room_updated_at', Q.desc)
 				)
 				.fetch();
-			const filteredChannels = channels.filter(async(channel) => {
-				const permissions = await RocketChat.hasPermission([addTeamChannelPermission], channel.rid);
-				if (!permissions[0]) {
-					return;
-				}
-				return channel;
-			});
-			this.setState({ channels: filteredChannels });
+
+			const asyncFilter = async(channelsArray) => {
+				const results = await Promise.all(channelsArray.map(async(channel) => {
+					if (channel.prid) {
+						return false;
+					}
+					const permissions = await RocketChat.hasPermission([addTeamChannelPermission], channel.rid);
+					if (!permissions[0]) {
+						return false;
+					}
+					return true;
+				}));
+
+				return channelsArray.filter((_v, index) => results[index]);
+			};
+			const channelFiltered = await asyncFilter(channels);
+			this.setState({ channels: channelFiltered });
 		} catch (e) {
 			log(e);
 		}
 	}
 
-	onSearchChangeText(text) {
-		this.search(text);
-	}
+	onSearchChangeText = debounce((text) => {
+		this.query(text);
+	}, 300)
 
 	dismiss = () => {
 		const { navigation } = this.props;
 		return navigation.pop();
 	}
 
-	search = async(text) => {
-		const result = await RocketChat.search({ text, filterUsers: false });
-		this.setState({
-			search: result
-		});
-	}
-
 	submit = async() => {
 		const { selected } = this.state;
 		const { isMasterDetail } = this.props;