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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Comments\Search;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
use function array_map;
use function pathinfo;
class CommentsSearchProvider implements IProvider {
public function __construct(
private IUserManager $userManager,
private IL10N $l10n,
private IURLGenerator $urlGenerator,
private LegacyProvider $legacyProvider,
) {
}
public function getId(): string {
return 'comments';
}
public function getName(): string {
return $this->l10n->t('Comments');
}
public function getOrder(string $route, array $routeParameters): int {
if ($route === 'files.View.index') {
// Files first
return 0;
}
return 10;
}
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);
$isUser = $this->userManager->userExists($result->authorId);
$avatarUrl = $isUser
? $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $result->authorId, 'size' => 42])
: $this->urlGenerator->linkToRouteAbsolute('core.GuestAvatar.getAvatar', ['guestName' => $result->authorId, 'size' => 42]);
return new SearchResultEntry(
$avatarUrl,
$result->name,
$path,
$this->urlGenerator->linkToRouteAbsolute('files.view.index', [
'dir' => $pathInfo['dirname'],
'scrollto' => $pathInfo['basename'],
]),
'',
true
);
}, $this->legacyProvider->search($query->getTerm()))
);
}
}
|