From 55fd3854c35825fe41f39fdefef64c6b8b0ab17d Mon Sep 17 00:00:00 2001 From: AndyLnd Date: Tue, 7 Apr 2020 14:53:37 +0200 Subject: [PATCH 1/2] feat: Show members found on backend in start UI --- src/script/components/userList.ts | 7 ++-- .../view_model/list/StartUIViewModel.js | 36 ++++++++++++------- src/style/components/user-input.less | 1 - test/unit_tests/util/ArrayUtilSpec.js | 2 +- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/script/components/userList.ts b/src/script/components/userList.ts index ecc02b4d7f9..42860e0b20a 100644 --- a/src/script/components/userList.ts +++ b/src/script/components/userList.ts @@ -19,6 +19,8 @@ import ko from 'knockout'; +import {partition} from 'Util/ArrayUtil'; + import {ConversationRepository} from '../conversation/ConversationRepository'; import {Conversation} from '../entity/Conversation'; import {User} from '../entity/User'; @@ -175,7 +177,7 @@ ko.components.register('user-list', { }; // Filter all list items if a filter is provided - this.filteredUserEntities = ko.pureComputed(() => { + this.filteredUserEntities = ko.pureComputed(async () => { const connectedUsers = conversationRepository.connectedUsers(); let resultUsers: User[] = userEntities(); const normalizedQuery = SearchRepository.normalizeQuery(filter()); @@ -205,8 +207,7 @@ ko.components.register('user-list', { } // make sure the self user is the first one in the list - const selfUser = resultUsers.filter(user => user.isMe); - const otherUsers = resultUsers.filter(user => !user.isMe); + const [selfUser, otherUsers] = partition(resultUsers, user => user.isMe); return selfUser.concat(otherUsers); }); diff --git a/src/script/view_model/list/StartUIViewModel.js b/src/script/view_model/list/StartUIViewModel.js index 99163e97d2c..6904911a830 100644 --- a/src/script/view_model/list/StartUIViewModel.js +++ b/src/script/view_model/list/StartUIViewModel.js @@ -21,6 +21,7 @@ import {debounce} from 'underscore'; import {getLogger} from 'Util/Logger'; import {safeWindowOpen} from 'Util/SanitizationUtil'; +import {partition} from 'Util/ArrayUtil'; import {UserlistMode} from 'Components/userList'; @@ -335,7 +336,7 @@ class StartUIViewModel { this.services.removeAll(); } - _searchPeople(query) { + async _searchPeople(query) { const normalizedQuery = SearchRepository.normalizeQuery(query); if (normalizedQuery) { this.showMatches(false); @@ -343,17 +344,6 @@ class StartUIViewModel { // Contacts, groups and others const trimmedQuery = query.trim(); const isHandle = trimmedQuery.startsWith('@') && validateHandle(normalizedQuery); - if (!this.showOnlyConnectedUsers()) { - this.searchRepository - .search_by_name(normalizedQuery, isHandle) - .then(userEntities => { - const isCurrentQuery = normalizedQuery === SearchRepository.normalizeQuery(this.searchInput()); - if (isCurrentQuery) { - this.searchResults.others(userEntities); - } - }) - .catch(error => this.logger.error(`Error searching for contacts: ${error.message}`, error)); - } const allLocalUsers = this.isTeam() ? this.teamRepository.teamUsers() : this.userRepository.connected_users(); @@ -377,6 +367,28 @@ class StartUIViewModel { this.searchResults.contacts(filteredResults); this.searchResults.groups(this.conversationRepository.getGroupsByName(normalizedQuery, isHandle)); + + if (!this.showOnlyConnectedUsers()) { + try { + const userEntities = await this.searchRepository.search_by_name(normalizedQuery, isHandle); + + const isCurrentQuery = normalizedQuery === SearchRepository.normalizeQuery(this.searchInput()); + if (isCurrentQuery) { + if (this.selfUser().inTeam()) { + const selfTeamId = this.selfUser().teamId; + const [contacts, others] = partition(userEntities, user => user.teamId === selfTeamId); + const knownContactIds = this.searchResults.contacts().map(({id}) => id); + const newContacts = contacts.filter(({id}) => !knownContactIds.includes(id)); + this.searchResults.contacts.push(...newContacts); + this.searchResults.others(others); + } else { + this.searchResults.others(userEntities); + } + } + } catch (error) { + this.logger.error(`Error searching for contacts: ${error.message}`, error); + } + } } } diff --git a/src/style/components/user-input.less b/src/style/components/user-input.less index 78cd1c2540c..ad50dc43840 100644 --- a/src/style/components/user-input.less +++ b/src/style/components/user-input.less @@ -62,7 +62,6 @@ user-input { display: flex; max-height: 84px; flex-wrap: wrap; - margin-right: -17px; overflow-y: auto; } diff --git a/test/unit_tests/util/ArrayUtilSpec.js b/test/unit_tests/util/ArrayUtilSpec.js index 76e45bbea63..5d481b0a8ac 100644 --- a/test/unit_tests/util/ArrayUtilSpec.js +++ b/test/unit_tests/util/ArrayUtilSpec.js @@ -149,7 +149,7 @@ describe('ArrayUtil', () => { }); describe('partition', () => { - it('returns a correctly partioned array', () => { + it('returns a correctly partitioned array', () => { const array = [3, 6, 5, 1, 2, 4]; const isOdd = input => input % 2 === 1; const isLessThanTen = input => input < 10; From 1aa68d53e5a2282ef83cfd99fd7dcd1e8a2a8f80 Mon Sep 17 00:00:00 2001 From: AndyLnd Date: Tue, 7 Apr 2020 15:07:30 +0200 Subject: [PATCH 2/2] remove unnecessary async --- src/script/components/userList.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/script/components/userList.ts b/src/script/components/userList.ts index 42860e0b20a..ea279fc5f4f 100644 --- a/src/script/components/userList.ts +++ b/src/script/components/userList.ts @@ -177,7 +177,7 @@ ko.components.register('user-list', { }; // Filter all list items if a filter is provided - this.filteredUserEntities = ko.pureComputed(async () => { + this.filteredUserEntities = ko.pureComputed(() => { const connectedUsers = conversationRepository.connectedUsers(); let resultUsers: User[] = userEntities(); const normalizedQuery = SearchRepository.normalizeQuery(filter());