|
1 | 1 | <?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
2 | 5 | /** |
3 | | - * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com> |
| 6 | + * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> |
4 | 7 | * |
5 | | - * @author Joas Schilling <coding@schilljs.com> |
| 8 | + * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> |
6 | 9 | * |
7 | 10 | * @license GNU AGPL version 3 or any later version |
8 | 11 | * |
|
17 | 20 | * GNU Affero General Public License for more details. |
18 | 21 | * |
19 | 22 | * You should have received a copy of the GNU Affero General Public License |
20 | | - * along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | | - * |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
22 | 24 | */ |
23 | 25 |
|
24 | 26 | namespace OCA\Comments\Search; |
25 | 27 |
|
26 | | -use OCP\Comments\IComment; |
27 | | -use OCP\Files\Folder; |
28 | | -use OCP\Files\Node; |
29 | | -use OCP\Files\NotFoundException; |
| 28 | +use OCP\IL10N; |
| 29 | +use OCP\IURLGenerator; |
30 | 30 | use OCP\IUser; |
| 31 | +use OCP\Search\IProvider; |
| 32 | +use OCP\Search\ISearchQuery; |
| 33 | +use OCP\Search\SearchResult; |
| 34 | +use function array_map; |
| 35 | +use function pathinfo; |
31 | 36 |
|
32 | | -class Provider extends \OCP\Search\Provider { |
33 | | - |
34 | | - /** |
35 | | - * Search for $query |
36 | | - * |
37 | | - * @param string $query |
38 | | - * @return array An array of OCP\Search\Result's |
39 | | - * @since 7.0.0 |
40 | | - */ |
41 | | - public function search($query): array { |
42 | | - $cm = \OC::$server->getCommentsManager(); |
43 | | - $us = \OC::$server->getUserSession(); |
44 | | - |
45 | | - $user = $us->getUser(); |
46 | | - if (!$user instanceof IUser) { |
47 | | - return []; |
48 | | - } |
49 | | - $uf = \OC::$server->getUserFolder($user->getUID()); |
50 | | - |
51 | | - if ($uf === null) { |
52 | | - return []; |
53 | | - } |
| 37 | +class Provider implements IProvider { |
54 | 38 |
|
55 | | - $result = []; |
56 | | - $numComments = 50; |
57 | | - $offset = 0; |
| 39 | + /** @var IL10N */ |
| 40 | + private $l10n; |
58 | 41 |
|
59 | | - while (\count($result) < $numComments) { |
60 | | - /** @var IComment[] $comments */ |
61 | | - $comments = $cm->search($query, 'files', '', 'comment', $offset, $numComments); |
| 42 | + /** @var IURLGenerator */ |
| 43 | + private $urlGenerator; |
62 | 44 |
|
63 | | - foreach ($comments as $comment) { |
64 | | - if ($comment->getActorType() !== 'users') { |
65 | | - continue; |
66 | | - } |
| 45 | + /** @var LegacyProvider */ |
| 46 | + private $legacyProvider; |
67 | 47 |
|
68 | | - $displayName = $cm->resolveDisplayName('user', $comment->getActorId()); |
69 | | - |
70 | | - try { |
71 | | - $file = $this->getFileForComment($uf, $comment); |
72 | | - $result[] = new Result($query, |
73 | | - $comment, |
74 | | - $displayName, |
75 | | - $file->getPath() |
76 | | - ); |
77 | | - } catch (NotFoundException $e) { |
78 | | - continue; |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - if (\count($comments) < $numComments) { |
83 | | - // Didn't find more comments when we tried to get, so there are no more comments. |
84 | | - return $result; |
85 | | - } |
86 | | - |
87 | | - $offset += $numComments; |
88 | | - $numComments = 50 - \count($result); |
89 | | - } |
90 | | - |
91 | | - return $result; |
| 48 | + public function __construct(IL10N $l10n, |
| 49 | + IURLGenerator $urlGenerator, |
| 50 | + LegacyProvider $legacyProvider) { |
| 51 | + $this->l10n = $l10n; |
| 52 | + $this->urlGenerator = $urlGenerator; |
| 53 | + $this->legacyProvider = $legacyProvider; |
92 | 54 | } |
93 | 55 |
|
94 | | - /** |
95 | | - * @param Folder $userFolder |
96 | | - * @param IComment $comment |
97 | | - * @return Node |
98 | | - * @throws NotFoundException |
99 | | - */ |
100 | | - protected function getFileForComment(Folder $userFolder, IComment $comment): Node { |
101 | | - $nodes = $userFolder->getById((int) $comment->getObjectId()); |
102 | | - if (empty($nodes)) { |
103 | | - throw new NotFoundException('File not found'); |
104 | | - } |
| 56 | + public function getId(): string { |
| 57 | + return 'comments'; |
| 58 | + } |
105 | 59 |
|
106 | | - return array_shift($nodes); |
| 60 | + public function search(IUser $user, ISearchQuery $query): SearchResult { |
| 61 | + return SearchResult::complete( |
| 62 | + $this->l10n->t('Comments'), |
| 63 | + array_map(function (Result $result) { |
| 64 | + $path = $result->path; |
| 65 | + $pathInfo = pathinfo($path); |
| 66 | + return new CommentsSearchResultEntry( |
| 67 | + $this->urlGenerator->linkToRoute('core.Preview.getPreviewByFileId', ['x' => 32, 'y' => 32, 'fileId' => $result->id]), |
| 68 | + $result->name, |
| 69 | + $path, |
| 70 | + $this->urlGenerator->linkToRoute( |
| 71 | + 'files.view.index', |
| 72 | + [ |
| 73 | + 'dir' => $pathInfo['dirname'], |
| 74 | + 'scrollto' => $pathInfo['basename'], |
| 75 | + ] |
| 76 | + ) |
| 77 | + ); |
| 78 | + }, $this->legacyProvider->search($query->getTerm())) |
| 79 | + ); |
107 | 80 | } |
108 | 81 | } |
0 commit comments