Skip to content

Commit

Permalink
feat(search): Allow multiple search terms in UnifiedController
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
  • Loading branch information
Altahrim committed Sep 28, 2023
1 parent cdd0790 commit e7f6acd
Show file tree
Hide file tree
Showing 13 changed files with 584 additions and 61 deletions.
9 changes: 4 additions & 5 deletions core/Controller/UnifiedSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*/
namespace OC\Core\Controller;

use OCP\IUserManager;
use OC\Search\SearchComposer;
use OC\Search\SearchQuery;
use OCA\Core\ResponseDefinitions;
Expand All @@ -39,6 +40,7 @@
use OCP\IUserSession;
use OCP\Route\IRouter;
use OCP\Search\ISearchQuery;
use OC\Search\Filters;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;

/**
Expand All @@ -52,6 +54,7 @@ public function __construct(
private SearchComposer $composer,
private IRouter $router,
private IURLGenerator $urlGenerator,
private IUserManager $userManager,
) {
parent::__construct('core', $request);
}
Expand Down Expand Up @@ -95,22 +98,18 @@ public function getProviders(string $from = ''): DataResponse {
* 400: Searching is not possible
*/
public function search(string $providerId,
string $term = '',
?int $sortOrder = null,
?int $limit = null,
$cursor = null,
string $from = ''): DataResponse {
if (empty(trim($term))) {
return new DataResponse(null, Http::STATUS_BAD_REQUEST);
}
[$route, $routeParameters] = $this->getRouteInformation($from);

return new DataResponse(
$this->composer->search(
$this->userSession->getUser(),
$providerId,
new SearchQuery(
$term,
new Filters($this->request, $this->userManager),
$sortOrder ?? ISearchQuery::SORT_DATE_DESC,
$limit ?? SearchQuery::LIMIT_DEFAULT,
$cursor,
Expand Down
48 changes: 48 additions & 0 deletions lib/private/Search/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/**
* @copyright 2023 Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @author Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Search;

use Throwable;

abstract class Filter {
public readonly mixed $value;

public function __construct(
public readonly string $name,
string $value
) {
try {
$this->value = $this->parse($value);
} catch (InvalidFilter $e) {
throw $e;
} catch (Throwable $e) {
throw new InvalidFilter($name, $value, $e);
}
}

abstract protected function parse(string $value): mixed;
}
42 changes: 42 additions & 0 deletions lib/private/Search/Filter/TypeBool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/**
* @copyright 2023 Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @author Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Search\Filter;

use OC\Search\Filter;

class TypeBool extends Filter {
protected function parse(string $value): bool {
return match ($value) {
'true', 'yes', 'y', '1' => true,
'false', 'no', 'n', '0', '' => false,
};
}

public function get(): bool {
return $this->value;
}
}
44 changes: 44 additions & 0 deletions lib/private/Search/Filter/TypeDateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

/**
* @copyright 2023 Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @author Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Search\Filter;

use DateTimeImmutable;
use OC\Search\Filter;

class TypeDateTime extends Filter {
protected function parse(string $value): DateTimeImmutable {
if (filter_var($value, FILTER_VALIDATE_INT)) {
$value = '@'.$value;
}

return new DateTimeImmutable($value);
}

public function get(): DateTimeImmutable {
return $this->value;
}
}
45 changes: 45 additions & 0 deletions lib/private/Search/Filter/TypeFloat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/**
* @copyright 2023 Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @author Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Search\Filter;

use OC\Search\Filter;
use OC\Search\InvalidFilter;

class TypeFloat extends Filter {
protected function parse(string $value): float {
$value = filter_var($value, FILTER_VALIDATE_FLOAT);
if ($value === false) {
throw new InvalidFilter($this->name, $value);
}

return $value;
}

public function get(): float {
return $this->value;
}
}
45 changes: 45 additions & 0 deletions lib/private/Search/Filter/TypeInt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/**
* @copyright 2023 Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @author Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Search\Filter;

use OC\Search\Filter;
use OC\Search\InvalidFilter;

class TypeInt extends Filter {
protected function parse(string $value): int {
$value = filter_var($value, FILTER_VALIDATE_INT);
if ($value === false) {
throw new InvalidFilter($this->name, $value);
}

return $value;
}

public function get(): int {
return $this->value;
}
}
39 changes: 39 additions & 0 deletions lib/private/Search/Filter/TypeJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/**
* @copyright 2023 Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @author Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Search\Filter;

use OC\Search\Filter;

class TypeJson extends Filter {
protected function parse(string $value): mixed {
return json_decode(json: $value, associative: true, flags: JSON_THROW_ON_ERROR);
}

public function get(): mixed {
return $this->value;
}
}
43 changes: 43 additions & 0 deletions lib/private/Search/Filter/TypeString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* @copyright 2023 Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @author Benjamin Gaussorgues <benjamin.gaussorgues@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OC\Search\Filter;

use OC\Search\Filter;
use OC\Search\InvalidFilter;

class TypeString extends Filter {
protected function parse(string $value): string {
if ($value === '') {
throw new InvalidFilter($this->name, $value);
}
return $value;
}

public function get(): string {
return $this->value;
}
}
Loading

0 comments on commit e7f6acd

Please sign in to comment.