@@ -25,102 +25,102 @@ |
||
| 25 | 25 | use OCP\Search\SearchResultEntry; |
| 26 | 26 | |
| 27 | 27 | class CommentsSearchProvider implements IProvider { |
| 28 | - public function __construct( |
|
| 29 | - private IUserManager $userManager, |
|
| 30 | - private IL10N $l10n, |
|
| 31 | - private IURLGenerator $urlGenerator, |
|
| 32 | - private ICommentsManager $commentsManager, |
|
| 33 | - private IRootFolder $rootFolder, |
|
| 34 | - ) { |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public function getId(): string { |
|
| 38 | - return 'comments'; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - public function getName(): string { |
|
| 42 | - return $this->l10n->t('Comments'); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - public function getOrder(string $route, array $routeParameters): int { |
|
| 46 | - if ($route === 'files.View.index') { |
|
| 47 | - // Files first |
|
| 48 | - return 0; |
|
| 49 | - } |
|
| 50 | - return 10; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - 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 | - |
|
| 109 | - return SearchResult::complete( |
|
| 110 | - $this->l10n->t('Comments'), |
|
| 111 | - $result, |
|
| 112 | - ); |
|
| 113 | - } |
|
| 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 | - } |
|
| 28 | + public function __construct( |
|
| 29 | + private IUserManager $userManager, |
|
| 30 | + private IL10N $l10n, |
|
| 31 | + private IURLGenerator $urlGenerator, |
|
| 32 | + private ICommentsManager $commentsManager, |
|
| 33 | + private IRootFolder $rootFolder, |
|
| 34 | + ) { |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function getId(): string { |
|
| 38 | + return 'comments'; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + public function getName(): string { |
|
| 42 | + return $this->l10n->t('Comments'); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + public function getOrder(string $route, array $routeParameters): int { |
|
| 46 | + if ($route === 'files.View.index') { |
|
| 47 | + // Files first |
|
| 48 | + return 0; |
|
| 49 | + } |
|
| 50 | + return 10; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + 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 | + |
|
| 109 | + return SearchResult::complete( |
|
| 110 | + $this->l10n->t('Comments'), |
|
| 111 | + $result, |
|
| 112 | + ); |
|
| 113 | + } |
|
| 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 | + } |
|
| 126 | 126 | } |