123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- declare(strict_types=1);
- namespace OCA\Comments\Search;
- use OCP\Comments\IComment;
- use OCP\Files\Folder;
- use OCP\Files\Node;
- use OCP\Files\NotFoundException;
- use OCP\IUser;
- use OCP\Search\Provider;
- use OCP\Comments\ICommentsManager;
- use function count;
- class LegacyProvider extends Provider {
-
- public function search($query): array {
- $cm = \OC::$server->get(ICommentsManager::class);
- $us = \OC::$server->getUserSession();
- $user = $us->getUser();
- if (!$user instanceof IUser) {
- return [];
- }
- $uf = \OC::$server->getUserFolder($user->getUID());
- if ($uf === null) {
- return [];
- }
- $result = [];
- $numComments = 50;
- $offset = 0;
- while (count($result) < $numComments) {
-
- $comments = $cm->search($query, 'files', '', 'comment', $offset, $numComments);
- foreach ($comments as $comment) {
- if ($comment->getActorType() !== 'users') {
- continue;
- }
- $displayName = $cm->resolveDisplayName('user', $comment->getActorId());
- try {
- $file = $this->getFileForComment($uf, $comment);
- $result[] = new Result($query,
- $comment,
- $displayName,
- $file->getPath()
- );
- } catch (NotFoundException $e) {
- continue;
- }
- }
- if (count($comments) < $numComments) {
-
- return $result;
- }
- $offset += $numComments;
- $numComments = 50 - count($result);
- }
- return $result;
- }
-
- 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);
- }
- }
|