-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathCommentsSearchProvider.php
More file actions
130 lines (108 loc) · 3.17 KB
/
Copy pathCommentsSearchProvider.php
File metadata and controls
130 lines (108 loc) · 3.17 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?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\Comments\IComment;
use OCP\Comments\ICommentsManager;
use OCP\Files\Folder;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\Files\NotFoundException;
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;
class CommentsSearchProvider implements IProvider {
public function __construct(
private IUserManager $userManager,
private IL10N $l10n,
private IURLGenerator $urlGenerator,
private ICommentsManager $commentsManager,
private IRootFolder $rootFolder,
) {
}
#[\Override]
public function getId(): string {
return 'comments';
}
#[\Override]
public function getName(): string {
return $this->l10n->t('Comments');
}
#[\Override]
public function getOrder(string $route, array $routeParameters): int {
if ($route === 'files.View.index') {
// Files first
return 0;
}
return 10;
}
#[\Override]
public function search(IUser $user, ISearchQuery $query): SearchResult {
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
if ($userFolder === null) {
return SearchResult::complete($this->l10n->t('Comments'), []);
}
$result = [];
$numComments = 50;
$offset = 0;
while (count($result) < $numComments) {
$comments = $this->commentsManager->search($query->getTerm(), 'files', '', 'comment', $offset, $numComments);
foreach ($comments as $comment) {
if ($comment->getActorType() !== 'users') {
continue;
}
$displayName = $this->commentsManager->resolveDisplayName('user', $comment->getActorId());
try {
$file = $this->getFileForComment($userFolder, $comment);
$isUser = $this->userManager->userExists($comment->getActorId());
$avatarUrl = $isUser
? $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $comment->getActorId(), 'size' => 42])
: $this->urlGenerator->linkToRouteAbsolute('core.GuestAvatar.getAvatar', ['guestName' => $comment->getActorId(), 'size' => 42]);
$link = $this->urlGenerator->linkToRoute(
'files.View.showFile',
['fileid' => $file->getId()]
);
$result[] = new SearchResultEntry(
$avatarUrl,
$displayName,
$file->getPath(),
$link,
'',
true
);
} catch (NotFoundException|InvalidPathException $e) {
continue;
}
}
if (count($comments) < $numComments) {
// Didn't find more comments when we tried to get, so there are no more comments.
break;
}
$offset += $numComments;
$numComments = 50 - count($result);
}
return SearchResult::complete(
$this->l10n->t('Comments'),
$result,
);
}
/**
* @throws NotFoundException
*/
protected function getFileForComment(Folder $userFolder, IComment $comment): Node {
$node = $userFolder->getFirstNodeById((int)$comment->getObjectId());
if ($node === null) {
throw new NotFoundException('File not found');
}
return $node;
}
}