aboutsummaryrefslogtreecommitdiffstats
path: root/core/Controller/SearchController.php
blob: 1ca8dd5dae490e540d9df7d5232550508a052fb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OC\Core\Controller;

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\ISearch;
use OCP\Search\Result;
use Psr\Log\LoggerInterface;

class SearchController extends Controller {
	public function __construct(
		string $appName,
		IRequest $request,
		private ISearch $searcher,
		private LoggerInterface $logger,
	) {
		parent::__construct($appName, $request);
	}

	/**
	 * @NoAdminRequired
	 */
	#[FrontpageRoute(verb: 'GET', url: '/core/search')]
	public function search(string $query, array $inApps = [], int $page = 1, int $size = 30): JSONResponse {
		$results = $this->searcher->searchPaged($query, $inApps, $page, $size);

		$results = array_filter($results, function (Result $result) {
			if (json_encode($result, JSON_HEX_TAG) === false) {
				$this->logger->warning("Skipping search result due to invalid encoding: {type: " . $result->type . ", id: " . $result->id . "}");
				return false;
			} else {
				return true;
			}
		});

		return new JSONResponse($results);
	}
}