Skip to content

Commit

Permalink
Hand in the route and the parameters of the request
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
Signed-off-by: npmbuildbot[bot] <npmbuildbot[bot]@users.noreply.github.com>
  • Loading branch information
nickvergessen authored and npmbuildbot[bot] committed Aug 5, 2020
1 parent d98f7c1 commit ea8f68b
Show file tree
Hide file tree
Showing 26 changed files with 167 additions and 56 deletions.
6 changes: 5 additions & 1 deletion apps/comments/lib/Search/CommentsSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ public function getName(): string {
/**
* @inheritDoc
*/
public function getOrder(string $from): int {
public function getOrder(string $route, array $routeParameters): int {
if ($route === 'files.View.index') {
// Files first
return 0;
}
return 10;
}

Expand Down
7 changes: 5 additions & 2 deletions apps/dav/lib/Search/ContactsSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ public function getName(): string {
/**
* @inheritDoc
*/
public function getOrder(string $from): int {
return 7;
public function getOrder(string $route, array $routeParameters): int {
if ($route === 'contacts.Page.index') {
return -1;
}
return 20;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion apps/dav/lib/Search/EventsSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ public function getName(): string {
/**
* @inheritDoc
*/
public function getOrder(string $from): int {
public function getOrder(string $route, array $routeParameters): int {
if ($route === 'calendar.View.index') {
return -1;
}
return 10;
}

Expand Down
5 changes: 4 additions & 1 deletion apps/dav/lib/Search/TasksSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public function getName(): string {
/**
* @inheritDoc
*/
public function getOrder(string $from): int {
public function getOrder(string $route, array $routeParameters): int {
if ($route === 'tasks.Page.index') {
return -1;
}
return 10;
}

Expand Down
6 changes: 5 additions & 1 deletion apps/files/lib/Search/FilesSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ public function getName(): string {
/**
* @inheritDoc
*/
public function getOrder(string $from): int {
public function getOrder(string $route, array $routeParameters): int {
if ($route === 'files.View.index') {
// Before comments
return -5;
}
return 5;
}

Expand Down
5 changes: 2 additions & 3 deletions apps/settings/lib/Search/SectionSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ public function getName(): string {
/**
* @inheritDoc
*/
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) {
public function getOrder(string $route, array $routeParameters): int {
if ($route === 'settings.PersonalSettings.index' || $route === 'settings.AdminSettings.index') {
return -1;
}
return 20;
Expand Down
62 changes: 55 additions & 7 deletions core/Controller/UnifiedSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\IUserSession;
use OCP\Route\IRouter;
use OCP\Search\ISearchQuery;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;

class UnifiedSearchController extends Controller {

Expand All @@ -42,26 +44,33 @@ class UnifiedSearchController extends Controller {
/** @var IUserSession */
private $userSession;

/** @var IRouter */
private $router;

public function __construct(IRequest $request,
IUserSession $userSession,
SearchComposer $composer) {
SearchComposer $composer,
IRouter $router) {
parent::__construct('core', $request);

$this->composer = $composer;
$this->userSession = $userSession;
$this->router = $router;
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*
*
* @param string $from the url the user is currently at
*
*
* @return JSONResponse
*/
public function getProviders(string $from): JSONResponse {
public function getProviders(string $from = ''): JSONResponse {
[$route, $parameters] = $this->getRouteInformation($from);

return new JSONResponse(
$this->composer->getProviders($from)
$this->composer->getProviders($route, $parameters)
);
}

Expand All @@ -74,17 +83,20 @@ public function getProviders(string $from): JSONResponse {
* @param int|null $sortOrder
* @param int|null $limit
* @param int|string|null $cursor
* @param string $from
*
* @return JSONResponse
*/
public function search(string $providerId,
string $term = '',
?int $sortOrder = null,
?int $limit = null,
$cursor = null): JSONResponse {
$cursor = null,
string $from = ''): JSONResponse {
if (empty(trim($term))) {
return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
}
[$route, $routeParameters] = $this->getRouteInformation($from);

return new JSONResponse(
$this->composer->search(
Expand All @@ -94,9 +106,45 @@ public function search(string $providerId,
$term,
$sortOrder ?? ISearchQuery::SORT_DATE_DESC,
$limit ?? SearchQuery::LIMIT_DEFAULT,
$cursor
$cursor,
$route,
$routeParameters
)
)
);
}

protected function getRouteInformation(string $url): array {
$routeStr = '';
$parameters = [];

if ($url !== '') {
$urlParts = parse_url($url);

try {
$parameters = $this->router->findMatchingRoute($urlParts['path']);

// contacts.PageController.index => contacts.Page.index
$route = $parameters['caller'];
if (substr($route[1], -10) === 'Controller') {
$route[1] = substr($route[1], 0, -10);
}
$routeStr = implode('.', $route);

// cleanup
unset($parameters['_route'], $parameters['action'], $parameters['caller']);
} catch (ResourceNotFoundException $exception) {
}

if (isset($urlParts['query'])) {
parse_str($urlParts['query'], $queryParameters);
$parameters = array_merge($parameters, $queryParameters);
}
}

return [
$routeStr,
$parameters,
];
}
}
2 changes: 1 addition & 1 deletion core/js/dist/install.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/install.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/login.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/login.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/main.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/maintenance.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/maintenance.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/recommendedapps.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/recommendedapps.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/unified-search.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/unified-search.js.map

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions core/src/services/UnifiedSearchService.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import { loadState } from '@nextcloud/initial-state'
import axios from '@nextcloud/axios'

export const defaultLimit = loadState('unified-search', 'limit-default')
export const activeApp = loadState('core', 'active-app')

export const minSearchLength = 2
/**
* Get the list of available search providers
*
Expand Down
29 changes: 12 additions & 17 deletions core/src/views/UnifiedSearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

<!-- Grouped search results -->
<template v-else>
<ul v-for="(list, type, typesIndex) in orderedResults"
<ul v-for="({list, type}, typesIndex) in orderedResults"
:key="type"
class="unified-search__results"
:class="`unified-search__results-${type}`"
Expand Down Expand Up @@ -97,7 +97,7 @@
</template>

<script>
import { getTypes, search, defaultLimit, activeApp } from '../services/UnifiedSearchService'
import { minSearchLength, getTypes, search, defaultLimit } from '../services/UnifiedSearchService'
import EmptyContent from '@nextcloud/vue/dist/Components/EmptyContent'
import Magnify from 'vue-material-design-icons/Magnify'
import debounce from 'debounce'
Expand All @@ -106,8 +106,6 @@ import HeaderMenu from '../components/HeaderMenu'
import SearchResult from '../components/UnifiedSearch/SearchResult'
import SearchResultPlaceholder from '../components/UnifiedSearch/SearchResultPlaceholder'
const minSearchLength = 2
export default {
name: 'UnifiedSearch',
Expand All @@ -132,7 +130,6 @@ export default {
query: '',
focused: null,
activeApp,
defaultLimit,
minSearchLength,
Expand Down Expand Up @@ -163,18 +160,16 @@ export default {
},
/**
* Order results by putting the active app first
* Return ordered results
* @returns {Object}
*/
orderedResults() {
const ordered = {}
Object.keys(this.results)
.sort((a, b) => this.typesMap[a].order - this.typesMap[b].order)
.forEach(type => {
ordered[type] = this.results[type]
})
return ordered
return Object.values(this.typesIDs)
.filter(type => type in this.results)
.map(type => ({
type,
list: this.results[type],
}))
},
/**
Expand Down Expand Up @@ -390,10 +385,10 @@ export default {
* @returns {Array}
*/
limitIfAny(list, type) {
if (!this.limits[type]) {
return list
if (type in this.limits) {
return list.slice(0, this.limits[type])
}
return list.slice(0, this.limits[type])
return list
},
getResultsList() {
Expand Down
17 changes: 15 additions & 2 deletions lib/private/Route/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ public function create($name,
*
* @param string $url The url to find
* @throws \Exception
* @return void
* @return array
*/
public function match($url) {
public function findMatchingRoute(string $url): array {
if (substr($url, 0, 6) === '/apps/') {
// empty string / 'apps' / $app / rest of the route
list(, , $app,) = explode('/', $url, 4);
Expand Down Expand Up @@ -287,6 +287,19 @@ public function match($url) {
}
}

return $parameters;
}

/**
* Find and execute the route matching $url
*
* @param string $url The url to find
* @throws \Exception
* @return void
*/
public function match($url) {
$parameters = $this->findMatchingRoute($url);

\OC::$server->getEventLogger()->start('run_route', 'Run route');
if (isset($parameters['caller'])) {
$caller = $parameters['caller'];
Expand Down
9 changes: 5 additions & 4 deletions lib/private/Search/SearchComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,20 @@ 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
* @param string $route the route the user is currently at
* @param array $routeParameters the parameters of the route the user is currently at
*
* @return array
*/
public function getProviders(string $from): array {
public function getProviders(string $route, array $routeParameters): array {
$this->loadLazyProviders();

$providers = array_values(
array_map(function (IProvider $provider) use ($from) {
array_map(function (IProvider $provider) use ($route, $routeParameters) {
return [
'id' => $provider->getId(),
'name' => $provider->getName(),
'order' => $provider->getOrder($from)
'order' => $provider->getOrder($route, $routeParameters),
];
}, $this->providers)
);
Expand Down
28 changes: 27 additions & 1 deletion lib/private/Search/SearchQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,32 @@ class SearchQuery implements ISearchQuery {
/** @var int|string|null */
private $cursor;

/** @var string */
private $route;

/** @var array */
private $routeParameters;

/**
* @param string $term
* @param int $sortOrder
* @param int $limit
* @param int|string|null $cursor
* @param string $route
* @param array $routeParameters
*/
public function __construct(string $term,
int $sortOrder = ISearchQuery::SORT_DATE_DESC,
int $limit = self::LIMIT_DEFAULT,
$cursor = null) {
$cursor = null,
string $route = '',
array $routeParameters = []) {
$this->term = $term;
$this->sortOrder = $sortOrder;
$this->limit = $limit;
$this->cursor = $cursor;
$this->route = $route;
$this->routeParameters = $routeParameters;
}

/**
Expand Down Expand Up @@ -85,4 +97,18 @@ public function getLimit(): int {
public function getCursor() {
return $this->cursor;
}

/**
* @inheritDoc
*/
public function getRoute(): string {
return $this->route;
}

/**
* @inheritDoc
*/
public function getRouteParameters(): array {
return $this->routeParameters;
}
}
Loading

0 comments on commit ea8f68b

Please sign in to comment.