2018-04-18 12:29:49 +03:00
|
|
|
<?php
|
2020-06-22 13:19:34 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-04-18 12:29:49 +03:00
|
|
|
/**
|
2020-06-22 13:19:34 +03:00
|
|
|
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
|
2018-04-18 12:29:49 +03:00
|
|
|
*
|
2020-06-22 13:19:34 +03:00
|
|
|
* @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
*
|
2018-04-18 12:29:49 +03:00
|
|
|
* @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
|
2020-06-22 13:19:34 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-04-18 12:29:49 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Comments\Search;
|
|
|
|
|
2020-06-22 13:19:34 +03:00
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IURLGenerator;
|
2018-04-18 12:29:49 +03:00
|
|
|
use OCP\IUser;
|
2020-08-03 13:54:37 +03:00
|
|
|
use OCP\IUserManager;
|
2020-06-22 13:19:34 +03:00
|
|
|
use OCP\Search\IProvider;
|
|
|
|
use OCP\Search\ISearchQuery;
|
|
|
|
use OCP\Search\SearchResult;
|
2020-08-04 11:00:27 +03:00
|
|
|
use OCP\Search\SearchResultEntry;
|
2020-06-22 13:19:34 +03:00
|
|
|
use function array_map;
|
|
|
|
use function pathinfo;
|
2018-04-18 12:29:49 +03:00
|
|
|
|
2020-08-04 11:00:27 +03:00
|
|
|
class CommentsSearchProvider implements IProvider {
|
2018-04-18 12:29:49 +03:00
|
|
|
|
2020-08-03 13:54:37 +03:00
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
2020-06-22 13:19:34 +03:00
|
|
|
/** @var IL10N */
|
|
|
|
private $l10n;
|
2018-04-23 15:58:43 +03:00
|
|
|
|
2020-06-22 13:19:34 +03:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
2018-04-23 15:58:43 +03:00
|
|
|
|
2020-06-22 13:19:34 +03:00
|
|
|
/** @var LegacyProvider */
|
|
|
|
private $legacyProvider;
|
2018-04-23 15:58:43 +03:00
|
|
|
|
2020-08-03 13:54:37 +03:00
|
|
|
public function __construct(IUserManager $userManager,
|
|
|
|
IL10N $l10n,
|
2020-06-22 13:19:34 +03:00
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
LegacyProvider $legacyProvider) {
|
2020-08-03 13:54:37 +03:00
|
|
|
$this->userManager = $userManager;
|
2020-06-22 13:19:34 +03:00
|
|
|
$this->l10n = $l10n;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->legacyProvider = $legacyProvider;
|
2018-04-18 12:29:49 +03:00
|
|
|
}
|
|
|
|
|
2020-08-04 11:00:27 +03:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2020-06-22 13:19:34 +03:00
|
|
|
public function getId(): string {
|
|
|
|
return 'comments';
|
|
|
|
}
|
2018-04-18 12:29:49 +03:00
|
|
|
|
2020-08-04 11:00:27 +03:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2020-08-03 13:54:37 +03:00
|
|
|
public function getName(): string {
|
|
|
|
return $this->l10n->t('Comments');
|
|
|
|
}
|
|
|
|
|
2020-08-04 11:00:27 +03:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2020-08-05 10:49:18 +03:00
|
|
|
public function getOrder(string $route, array $routeParameters): int {
|
|
|
|
if ($route === 'files.View.index') {
|
|
|
|
// Files first
|
|
|
|
return 0;
|
|
|
|
}
|
2020-08-04 11:00:27 +03:00
|
|
|
return 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2020-06-22 13:19:34 +03:00
|
|
|
public function search(IUser $user, ISearchQuery $query): SearchResult {
|
|
|
|
return SearchResult::complete(
|
|
|
|
$this->l10n->t('Comments'),
|
|
|
|
array_map(function (Result $result) {
|
|
|
|
$path = $result->path;
|
|
|
|
$pathInfo = pathinfo($path);
|
2020-08-03 13:54:37 +03:00
|
|
|
$isUser = $this->userManager->userExists($result->authorId);
|
|
|
|
$avatarUrl = $isUser
|
|
|
|
? $this->urlGenerator->linkToRoute('core.avatar.getAvatar', ['userId' => $result->authorId, 'size' => 42])
|
|
|
|
: $this->urlGenerator->linkToRoute('core.GuestAvatar.getAvatar', ['guestName' => $result->authorId, 'size' => 42]);
|
2020-08-04 11:00:27 +03:00
|
|
|
return new SearchResultEntry(
|
2020-08-03 13:54:37 +03:00
|
|
|
$avatarUrl,
|
2020-06-22 13:19:34 +03:00
|
|
|
$result->name,
|
|
|
|
$path,
|
2020-08-03 13:54:37 +03:00
|
|
|
$this->urlGenerator->linkToRoute('files.view.index',[
|
|
|
|
'dir' => $pathInfo['dirname'],
|
|
|
|
'scrollto' => $pathInfo['basename'],
|
|
|
|
]),
|
|
|
|
'',
|
|
|
|
true
|
2020-06-22 13:19:34 +03:00
|
|
|
);
|
|
|
|
}, $this->legacyProvider->search($query->getTerm()))
|
|
|
|
);
|
2018-04-18 12:29:49 +03:00
|
|
|
}
|
|
|
|
}
|