Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions apps/comments/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,4 @@
'OCA\\Comments\\Notification\\Listener' => $baseDir . '/../lib/Notification/Listener.php',
'OCA\\Comments\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
'OCA\\Comments\\Search\\CommentsSearchProvider' => $baseDir . '/../lib/Search/CommentsSearchProvider.php',
'OCA\\Comments\\Search\\LegacyProvider' => $baseDir . '/../lib/Search/LegacyProvider.php',
'OCA\\Comments\\Search\\Result' => $baseDir . '/../lib/Search/Result.php',
);
2 changes: 0 additions & 2 deletions apps/comments/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ class ComposerStaticInitComments
'OCA\\Comments\\Notification\\Listener' => __DIR__ . '/..' . '/../lib/Notification/Listener.php',
'OCA\\Comments\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
'OCA\\Comments\\Search\\CommentsSearchProvider' => __DIR__ . '/..' . '/../lib/Search/CommentsSearchProvider.php',
'OCA\\Comments\\Search\\LegacyProvider' => __DIR__ . '/..' . '/../lib/Search/LegacyProvider.php',
'OCA\\Comments\\Search\\Result' => __DIR__ . '/..' . '/../lib/Search/Result.php',
);

public static function getInitializer(ClassLoader $loader)
Expand Down
101 changes: 77 additions & 24 deletions apps/comments/lib/Search/CommentsSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
*/
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;
Expand All @@ -16,14 +23,14 @@
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
use function array_map;

class CommentsSearchProvider implements IProvider {
public function __construct(
private IUserManager $userManager,
private IL10N $l10n,
private IURLGenerator $urlGenerator,
private LegacyProvider $legacyProvider,
private ICommentsManager $commentsManager,
private IRootFolder $rootFolder,
) {
}

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

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'),
array_map(function (Result $result) {
$path = $result->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]);
$link = $this->urlGenerator->linkToRoute(
'files.View.showFile',
['fileid' => $result->fileId]
);
$searchResultEntry = new SearchResultEntry(
$avatarUrl,
$result->name,
$path,
$link,
'',
true
);
$searchResultEntry->addAttribute('fileId', (string)$result->fileId);
$searchResultEntry->addAttribute('path', $path);
return $searchResultEntry;
}, $this->legacyProvider->search($query->getTerm()))
$result,
);
}

/**
* @throws NotFoundException
*/
protected function getFileForComment(Folder $userFolder, IComment $comment): Node {
$nodes = $userFolder->getById((int)$comment->getObjectId());
if (empty($nodes)) {
throw new NotFoundException('File not found');
}

return array_shift($nodes);
}
}
99 changes: 0 additions & 99 deletions apps/comments/lib/Search/LegacyProvider.php

This file was deleted.

109 changes: 0 additions & 109 deletions apps/comments/lib/Search/Result.php

This file was deleted.

Loading
Loading