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 Oct 6, 2023
1 parent d061e05 commit 26d4498
Show file tree
Hide file tree
Showing 19 changed files with 865 additions and 101 deletions.
25 changes: 14 additions & 11 deletions core/Controller/UnifiedSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
*/
namespace OC\Core\Controller;

use OC\Search\InvalidFilter;
use OC\Search\SearchComposer;
use OC\Search\SearchQuery;
use OCA\Core\ResponseDefinitions;
use OCP\AppFramework\OCSController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
Expand Down Expand Up @@ -83,7 +84,6 @@ public function getProviders(string $from = ''): DataResponse {
* Search
*
* @param string $providerId ID of the provider
* @param string $term Term to search
* @param int|null $sortOrder Order of entries
* @param int|null $limit Maximum amount of entries
* @param int|string|null $cursor Offset for searching
Expand All @@ -95,22 +95,25 @@ 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 (trim($term) === "") {
return new DataResponse(null, Http::STATUS_BAD_REQUEST);
}
?int $sortOrder = null,
?int $limit = null,
$cursor = null,
string $from = ''): DataResponse {
[$route, $routeParameters] = $this->getRouteInformation($from);

try {
$filters = $this->composer->buildFilterList($providerId, $this->request->getParams());
} catch (InvalidFilter $e) {
// Unsupported filter, send empty answer
return new DataResponse([], Http::STATUS_OK);
}

return new DataResponse(
$this->composer->search(
$this->userSession->getUser(),
$providerId,
new SearchQuery(
$term,
$filters,
$sortOrder ?? ISearchQuery::SORT_DATE_DESC,
$limit ?? SearchQuery::LIMIT_DEFAULT,
$cursor,
Expand Down
11 changes: 1 addition & 10 deletions core/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -3966,15 +3966,6 @@
}
],
"parameters": [
{
"name": "term",
"in": "query",
"description": "Term to search",
"schema": {
"type": "string",
"default": ""
}
},
{
"name": "sortOrder",
"in": "query",
Expand Down Expand Up @@ -5162,4 +5153,4 @@
"description": "Controller about the endpoint /ocm-provider/"
}
]
}
}
48 changes: 48 additions & 0 deletions lib/private/Search/Filter/BoolFilter.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\Filter;

use OC\Search\SingleFilter;

class BoolFilter extends SingleFilter {
private bool $value;

protected function set(string $value): void {
$this->value = match ($value) {
'true', 'yes', 'y', '1' => true,
'false', 'no', 'n', '0', '' => false,
};
}

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

public static function type(): string {
return 'bool';
}
}
50 changes: 50 additions & 0 deletions lib/private/Search/Filter/DateTimeFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\SingleFilter;

class DateTimeFilter extends SingleFilter {
private DateTimeImmutable $value;

protected function set(string $value): void {
if (filter_var($value, FILTER_VALIDATE_INT)) {
$value = '@'.$value;
}

$this->value = new DateTimeImmutable($value);
}

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

public static function type(): string {
return 'datetime';
}
}
49 changes: 49 additions & 0 deletions lib/private/Search/Filter/FloatFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\InvalidFilter;
use OC\Search\SingleFilter;

class FloatFilter extends SingleFilter {
private float $value;

protected function set(string $value): void {
$this->value = filter_var($value, FILTER_VALIDATE_FLOAT);
if ($this->value === false) {
throw new InvalidFilter($value);
}
}

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

public static function type(): string {
return 'float';
}
}
49 changes: 49 additions & 0 deletions lib/private/Search/Filter/IntFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\InvalidFilter;
use OC\Search\SingleFilter;

class IntFilter extends SingleFilter {
private int $value;

protected function set(string $value): void {
$this->value = filter_var($value, FILTER_VALIDATE_INT);
if ($this->value === false) {
throw new InvalidFilter($value);
}
}

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

public static function type(): string {
return 'int';
}
}
52 changes: 52 additions & 0 deletions lib/private/Search/Filter/MultiStringFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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\InvalidFilter;
use OC\Search\MultiFilter;

class MultiStringFilter extends MultiFilter {
/**
* @var string[]
*/
private array $values;

protected function set(string ...$values): void {
$this->values = array_unique(array_filter($values));
if (empty($this->values)) {
throw new InvalidFilter($values);
}
}

public function get(): array {
return $this->values;
}

public static function type(): string {
return 'string';
}
}
49 changes: 49 additions & 0 deletions lib/private/Search/Filter/StringFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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\InvalidFilter;
use OC\Search\SingleFilter;

class StringFilter extends SingleFilter {
private string $value;

protected function set(string $value): void {
if ($value === '') {
throw new InvalidFilter($value);
}
$this->value = $value;
}

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

public static function type(): string {
return 'string';
}
}
Loading

0 comments on commit 26d4498

Please sign in to comment.