Skip to content

Commit

Permalink
Make apps handle the order logic
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  • Loading branch information
skjnldsv committed Aug 5, 2020
1 parent 71b62c4 commit d98f7c1
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 49 deletions.
2 changes: 1 addition & 1 deletion apps/comments/lib/Search/CommentsSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function getName(): string {
/**
* @inheritDoc
*/
public function getOrder(): int {
public function getOrder(string $from): int {
return 10;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/dav/lib/Search/ContactsSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function getName(): string {
/**
* @inheritDoc
*/
public function getOrder(): int {
public function getOrder(string $from): int {
return 7;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/dav/lib/Search/EventsSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function getName(): string {
/**
* @inheritDoc
*/
public function getOrder(): int {
public function getOrder(string $from): int {
return 10;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/dav/lib/Search/TasksSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getName(): string {
/**
* @inheritDoc
*/
public function getOrder(): int {
public function getOrder(string $from): int {
return 10;
}

Expand Down
2 changes: 1 addition & 1 deletion apps/files/lib/Search/FilesSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function getName(): string {
/**
* @inheritDoc
*/
public function getOrder(): int {
public function getOrder(string $from): int {
return 5;
}

Expand Down
6 changes: 5 additions & 1 deletion apps/settings/lib/Search/SectionSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ public function getName(): string {
/**
* @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;
}

Expand Down
8 changes: 6 additions & 2 deletions core/Controller/UnifiedSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ public function __construct(IRequest $request,
/**
* @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)
);
}

Expand Down
33 changes: 8 additions & 25 deletions core/src/services/UnifiedSearchService.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,22 @@ 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)
}
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
*
Expand Down
13 changes: 1 addition & 12 deletions core/src/views/UnifiedSearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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]
})
Expand Down
8 changes: 5 additions & 3 deletions lib/private/Search/SearchComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,19 @@ private function loadLazyProviders(): void {
* 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)
);
Expand Down
4 changes: 3 additions & 1 deletion lib/public/Search/IProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ public function getName(): string;
* 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
Expand Down

0 comments on commit d98f7c1

Please sign in to comment.