From d98f7c1bd83fc03fd297ebeac6279ffe17316950 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?= Date: Tue, 4 Aug 2020 18:15:09 +0200 Subject: [PATCH] Make apps handle the order logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- .../lib/Search/CommentsSearchProvider.php | 2 +- .../dav/lib/Search/ContactsSearchProvider.php | 2 +- apps/dav/lib/Search/EventsSearchProvider.php | 2 +- apps/dav/lib/Search/TasksSearchProvider.php | 2 +- apps/files/lib/Search/FilesSearchProvider.php | 2 +- apps/settings/lib/Search/SectionSearch.php | 6 +++- core/Controller/UnifiedSearchController.php | 8 +++-- core/src/services/UnifiedSearchService.js | 33 +++++-------------- core/src/views/UnifiedSearch.vue | 13 +------- lib/private/Search/SearchComposer.php | 8 +++-- lib/public/Search/IProvider.php | 4 ++- 11 files changed, 33 insertions(+), 49 deletions(-) diff --git a/apps/comments/lib/Search/CommentsSearchProvider.php b/apps/comments/lib/Search/CommentsSearchProvider.php index 3d503cf5c5..bab10a58ee 100644 --- a/apps/comments/lib/Search/CommentsSearchProvider.php +++ b/apps/comments/lib/Search/CommentsSearchProvider.php @@ -77,7 +77,7 @@ class CommentsSearchProvider implements IProvider { /** * @inheritDoc */ - public function getOrder(): int { + public function getOrder(string $from): int { return 10; } diff --git a/apps/dav/lib/Search/ContactsSearchProvider.php b/apps/dav/lib/Search/ContactsSearchProvider.php index d87f87d759..82fa44a7a2 100644 --- a/apps/dav/lib/Search/ContactsSearchProvider.php +++ b/apps/dav/lib/Search/ContactsSearchProvider.php @@ -96,7 +96,7 @@ class ContactsSearchProvider implements IProvider { /** * @inheritDoc */ - public function getOrder(): int { + public function getOrder(string $from): int { return 7; } diff --git a/apps/dav/lib/Search/EventsSearchProvider.php b/apps/dav/lib/Search/EventsSearchProvider.php index 6d264ae482..c9ce11a297 100644 --- a/apps/dav/lib/Search/EventsSearchProvider.php +++ b/apps/dav/lib/Search/EventsSearchProvider.php @@ -82,7 +82,7 @@ class EventsSearchProvider extends ACalendarSearchProvider { /** * @inheritDoc */ - public function getOrder(): int { + public function getOrder(string $from): int { return 10; } diff --git a/apps/dav/lib/Search/TasksSearchProvider.php b/apps/dav/lib/Search/TasksSearchProvider.php index 9a0a87eb5a..6c07cfd00f 100644 --- a/apps/dav/lib/Search/TasksSearchProvider.php +++ b/apps/dav/lib/Search/TasksSearchProvider.php @@ -74,7 +74,7 @@ class TasksSearchProvider extends ACalendarSearchProvider { /** * @inheritDoc */ - public function getOrder(): int { + public function getOrder(string $from): int { return 10; } diff --git a/apps/files/lib/Search/FilesSearchProvider.php b/apps/files/lib/Search/FilesSearchProvider.php index d0a4bf5662..ce6f705f72 100644 --- a/apps/files/lib/Search/FilesSearchProvider.php +++ b/apps/files/lib/Search/FilesSearchProvider.php @@ -77,7 +77,7 @@ class FilesSearchProvider implements IProvider { /** * @inheritDoc */ - public function getOrder(): int { + public function getOrder(string $from): int { return 5; } diff --git a/apps/settings/lib/Search/SectionSearch.php b/apps/settings/lib/Search/SectionSearch.php index 6e7b930046..d949d93fb6 100644 --- a/apps/settings/lib/Search/SectionSearch.php +++ b/apps/settings/lib/Search/SectionSearch.php @@ -75,7 +75,11 @@ class SectionSearch implements IProvider { /** * @inheritDoc */ - public function getOrder(): int { + public function getOrder(string $from): int { + if (strpos($from, $this->urlGenerator->linkToRoute('settings.PersonalSettings.index') === 0) + || strpos($from, $this->urlGenerator->linkToRoute('settings.AdminSettings.index')) === 0) { + return -1; + } return 20; } diff --git a/core/Controller/UnifiedSearchController.php b/core/Controller/UnifiedSearchController.php index ecc5192ff3..4aaa1b9b06 100644 --- a/core/Controller/UnifiedSearchController.php +++ b/core/Controller/UnifiedSearchController.php @@ -54,10 +54,14 @@ class UnifiedSearchController extends Controller { /** * @NoAdminRequired * @NoCSRFRequired + * + * @param string $from the url the user is currently at + * + * @return JSONResponse */ - public function getProviders(): JSONResponse { + public function getProviders(string $from): JSONResponse { return new JSONResponse( - $this->composer->getProviders() + $this->composer->getProviders($from) ); } diff --git a/core/src/services/UnifiedSearchService.js b/core/src/services/UnifiedSearchService.js index ff9a4aebe2..bc8cca19ce 100644 --- a/core/src/services/UnifiedSearchService.js +++ b/core/src/services/UnifiedSearchService.js @@ -33,9 +33,15 @@ export const activeApp = loadState('core', 'active-app') */ export async function getTypes() { try { - const { data } = await axios.get(generateUrl('/search/providers')) + const { data } = await axios.get(generateUrl('/search/providers'), { + params: { + // Sending which location we're currently at + from: window.location.pathname.replace('/index.php', '') + window.location.search, + }, + }) if (Array.isArray(data) && data.length > 0) { - return sortProviders(data) + // Providers are sorted by the api based on their order key + return data } } catch (error) { console.error(error) @@ -43,29 +49,6 @@ export async function getTypes() { return [] } -/** - * Sort the providers by the current active app - * - * @param {Array} providers the providers list - * @returns {Array} - */ -export function sortProviders(providers) { - providers.sort((a, b) => { - if (a.id.startsWith(activeApp) && b.id.startsWith(activeApp)) { - return a.order - b.order - } - - if (a.id.startsWith(activeApp)) { - return -1 - } - if (b.id.startsWith(activeApp)) { - return 1 - } - return 0 - }) - return providers -} - /** * Get the list of available search providers * diff --git a/core/src/views/UnifiedSearch.vue b/core/src/views/UnifiedSearch.vue index d7ad58ae20..4e32bdbedd 100644 --- a/core/src/views/UnifiedSearch.vue +++ b/core/src/views/UnifiedSearch.vue @@ -169,18 +169,7 @@ export default { orderedResults() { const ordered = {} Object.keys(this.results) - .sort((a, b) => { - if (a.startsWith(activeApp) && b.startsWith(activeApp)) { - return this.typesMap[a].order - this.typesMap[b].order - } - if (a.startsWith(activeApp)) { - return -1 - } - if (b.startsWith(activeApp)) { - return 1 - } - return 0 - }) + .sort((a, b) => this.typesMap[a].order - this.typesMap[b].order) .forEach(type => { ordered[type] = this.results[type] }) diff --git a/lib/private/Search/SearchComposer.php b/lib/private/Search/SearchComposer.php index 7ba6296c9d..f86626909c 100644 --- a/lib/private/Search/SearchComposer.php +++ b/lib/private/Search/SearchComposer.php @@ -109,17 +109,19 @@ class SearchComposer { * Get a list of all provider IDs & Names for the consecutive calls to `search` * Sort the list by the order property * + * @param string $from the url the user is currently at + * * @return array */ - public function getProviders(): array { + public function getProviders(string $from): array { $this->loadLazyProviders(); $providers = array_values( - array_map(function (IProvider $provider) { + array_map(function (IProvider $provider) use ($from) { return [ 'id' => $provider->getId(), 'name' => $provider->getName(), - 'order' => $provider->getOrder() + 'order' => $provider->getOrder($from) ]; }, $this->providers) ); diff --git a/lib/public/Search/IProvider.php b/lib/public/Search/IProvider.php index 66db62c682..5a6cfb3fd0 100644 --- a/lib/public/Search/IProvider.php +++ b/lib/public/Search/IProvider.php @@ -68,11 +68,13 @@ interface IProvider { * Get the search provider order * The lower the int, the higher it will be sorted (0 will be before 10) * + * @param string $from the url the user is currently at. (e.g. /apps/files/?dir=/&fileid=982) + * * @return int * * @since 20.0.0 */ - public function getOrder(): int; + public function getOrder(string $from): int; /** * Find matching search entries in an app