Skip to content

Commit e739898

Browse files
authored
Merge pull request #55400 from nextcloud/carl/remove-legacy-search-provider
refactor: Remove legacy search provider
2 parents b984e32 + aa1f531 commit e739898

File tree

11 files changed

+77
-489
lines changed

11 files changed

+77
-489
lines changed

apps/comments/composer/composer/autoload_classmap.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,4 @@
2323
'OCA\\Comments\\Notification\\Listener' => $baseDir . '/../lib/Notification/Listener.php',
2424
'OCA\\Comments\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
2525
'OCA\\Comments\\Search\\CommentsSearchProvider' => $baseDir . '/../lib/Search/CommentsSearchProvider.php',
26-
'OCA\\Comments\\Search\\LegacyProvider' => $baseDir . '/../lib/Search/LegacyProvider.php',
27-
'OCA\\Comments\\Search\\Result' => $baseDir . '/../lib/Search/Result.php',
2826
);

apps/comments/composer/composer/autoload_static.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ class ComposerStaticInitComments
3838
'OCA\\Comments\\Notification\\Listener' => __DIR__ . '/..' . '/../lib/Notification/Listener.php',
3939
'OCA\\Comments\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
4040
'OCA\\Comments\\Search\\CommentsSearchProvider' => __DIR__ . '/..' . '/../lib/Search/CommentsSearchProvider.php',
41-
'OCA\\Comments\\Search\\LegacyProvider' => __DIR__ . '/..' . '/../lib/Search/LegacyProvider.php',
42-
'OCA\\Comments\\Search\\Result' => __DIR__ . '/..' . '/../lib/Search/Result.php',
4341
);
4442

4543
public static function getInitializer(ClassLoader $loader)

apps/comments/lib/Search/CommentsSearchProvider.php

Lines changed: 77 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
*/
99
namespace OCA\Comments\Search;
1010

11+
use OCP\Comments\IComment;
12+
use OCP\Comments\ICommentsManager;
13+
use OCP\Files\Folder;
14+
use OCP\Files\InvalidPathException;
15+
use OCP\Files\IRootFolder;
16+
use OCP\Files\Node;
17+
use OCP\Files\NotFoundException;
1118
use OCP\IL10N;
1219
use OCP\IURLGenerator;
1320
use OCP\IUser;
@@ -16,14 +23,14 @@
1623
use OCP\Search\ISearchQuery;
1724
use OCP\Search\SearchResult;
1825
use OCP\Search\SearchResultEntry;
19-
use function array_map;
2026

2127
class CommentsSearchProvider implements IProvider {
2228
public function __construct(
2329
private IUserManager $userManager,
2430
private IL10N $l10n,
2531
private IURLGenerator $urlGenerator,
26-
private LegacyProvider $legacyProvider,
32+
private ICommentsManager $commentsManager,
33+
private IRootFolder $rootFolder,
2734
) {
2835
}
2936

@@ -44,30 +51,76 @@ public function getOrder(string $route, array $routeParameters): int {
4451
}
4552

4653
public function search(IUser $user, ISearchQuery $query): SearchResult {
54+
$userFolder = $this->rootFolder->getUserFolder($user->getUID());
55+
56+
if ($userFolder === null) {
57+
return SearchResult::complete($this->l10n->t('Comments'), []);
58+
}
59+
60+
$result = [];
61+
$numComments = 50;
62+
$offset = 0;
63+
64+
while (count($result) < $numComments) {
65+
$comments = $this->commentsManager->search($query->getTerm(), 'files', '', 'comment', $offset, $numComments);
66+
67+
foreach ($comments as $comment) {
68+
if ($comment->getActorType() !== 'users') {
69+
continue;
70+
}
71+
72+
$displayName = $this->commentsManager->resolveDisplayName('user', $comment->getActorId());
73+
74+
try {
75+
$file = $this->getFileForComment($userFolder, $comment);
76+
77+
$isUser = $this->userManager->userExists($comment->getActorId());
78+
$avatarUrl = $isUser
79+
? $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $comment->getActorId(), 'size' => 42])
80+
: $this->urlGenerator->linkToRouteAbsolute('core.GuestAvatar.getAvatar', ['guestName' => $comment->getActorId(), 'size' => 42]);
81+
$link = $this->urlGenerator->linkToRoute(
82+
'files.View.showFile',
83+
['fileid' => $file->getId()]
84+
);
85+
86+
$result[] = new SearchResultEntry(
87+
$avatarUrl,
88+
$displayName,
89+
$file->getPath(),
90+
$link,
91+
'',
92+
true
93+
);
94+
} catch (NotFoundException|InvalidPathException $e) {
95+
continue;
96+
}
97+
}
98+
99+
if (count($comments) < $numComments) {
100+
// Didn't find more comments when we tried to get, so there are no more comments.
101+
break;
102+
}
103+
104+
$offset += $numComments;
105+
$numComments = 50 - count($result);
106+
}
107+
108+
47109
return SearchResult::complete(
48110
$this->l10n->t('Comments'),
49-
array_map(function (Result $result) {
50-
$path = $result->path;
51-
$isUser = $this->userManager->userExists($result->authorId);
52-
$avatarUrl = $isUser
53-
? $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $result->authorId, 'size' => 42])
54-
: $this->urlGenerator->linkToRouteAbsolute('core.GuestAvatar.getAvatar', ['guestName' => $result->authorId, 'size' => 42]);
55-
$link = $this->urlGenerator->linkToRoute(
56-
'files.View.showFile',
57-
['fileid' => $result->fileId]
58-
);
59-
$searchResultEntry = new SearchResultEntry(
60-
$avatarUrl,
61-
$result->name,
62-
$path,
63-
$link,
64-
'',
65-
true
66-
);
67-
$searchResultEntry->addAttribute('fileId', (string)$result->fileId);
68-
$searchResultEntry->addAttribute('path', $path);
69-
return $searchResultEntry;
70-
}, $this->legacyProvider->search($query->getTerm()))
111+
$result,
71112
);
72113
}
114+
115+
/**
116+
* @throws NotFoundException
117+
*/
118+
protected function getFileForComment(Folder $userFolder, IComment $comment): Node {
119+
$nodes = $userFolder->getById((int)$comment->getObjectId());
120+
if (empty($nodes)) {
121+
throw new NotFoundException('File not found');
122+
}
123+
124+
return array_shift($nodes);
125+
}
73126
}

apps/comments/lib/Search/LegacyProvider.php

Lines changed: 0 additions & 99 deletions
This file was deleted.

apps/comments/lib/Search/Result.php

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)