Skip to content

Commit 3df8676

Browse files
Migrate comments to unified search API
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
1 parent 7e1878e commit 3df8676

File tree

7 files changed

+222
-77
lines changed

7 files changed

+222
-77
lines changed

apps/comments/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
'OCA\\Comments\\Listener\\LoadSidebarScripts' => $baseDir . '/../lib/Listener/LoadSidebarScripts.php',
2222
'OCA\\Comments\\Notification\\Listener' => $baseDir . '/../lib/Notification/Listener.php',
2323
'OCA\\Comments\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php',
24+
'OCA\\Comments\\Search\\CommentsSearchResultEntry' => $baseDir . '/../lib/Search/CommentsSearchResultEntry.php',
25+
'OCA\\Comments\\Search\\LegacyProvider' => $baseDir . '/../lib/Search/LegacyProvider.php',
2426
'OCA\\Comments\\Search\\Provider' => $baseDir . '/../lib/Search/Provider.php',
2527
'OCA\\Comments\\Search\\Result' => $baseDir . '/../lib/Search/Result.php',
2628
);

apps/comments/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ComposerStaticInitComments
3636
'OCA\\Comments\\Listener\\LoadSidebarScripts' => __DIR__ . '/..' . '/../lib/Listener/LoadSidebarScripts.php',
3737
'OCA\\Comments\\Notification\\Listener' => __DIR__ . '/..' . '/../lib/Notification/Listener.php',
3838
'OCA\\Comments\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php',
39+
'OCA\\Comments\\Search\\CommentsSearchResultEntry' => __DIR__ . '/..' . '/../lib/Search/CommentsSearchResultEntry.php',
40+
'OCA\\Comments\\Search\\LegacyProvider' => __DIR__ . '/..' . '/../lib/Search/LegacyProvider.php',
3941
'OCA\\Comments\\Search\\Provider' => __DIR__ . '/..' . '/../lib/Search/Provider.php',
4042
'OCA\\Comments\\Search\\Result' => __DIR__ . '/..' . '/../lib/Search/Result.php',
4143
);

apps/comments/lib/AppInfo/Application.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use OCA\Comments\Listener\LoadAdditionalScripts;
3636
use OCA\Comments\Listener\LoadSidebarScripts;
3737
use OCA\Comments\Notification\Notifier;
38+
use OCA\Comments\Search\LegacyProvider;
3839
use OCA\Comments\Search\Provider;
3940
use OCA\Files\Event\LoadAdditionalScriptsEvent;
4041
use OCA\Files\Event\LoadSidebar;
@@ -70,6 +71,7 @@ public function register(IRegistrationContext $context): void {
7071
CommentsEntityEvent::EVENT_ENTITY,
7172
CommentsEntityEventListener::class
7273
);
74+
$context->registerSearchProvider(Provider::class);
7375
}
7476

7577
public function boot(IBootContext $context): void {
@@ -79,7 +81,7 @@ public function boot(IBootContext $context): void {
7981
$jsSettingsHelper = new JSSettingsHelper($context->getServerContainer());
8082
Util::connectHook('\OCP\Config', 'js', $jsSettingsHelper, 'extend');
8183

82-
$context->getServerContainer()->getSearch()->registerProvider(Provider::class, ['apps' => ['files']]);
84+
$context->getServerContainer()->getSearch()->registerProvider(LegacyProvider::class, ['apps' => ['files']]);
8385
}
8486

8587
protected function registerNotifier(IServerContainer $container) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
7+
*
8+
* @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*/
25+
26+
namespace OCA\Comments\Search;
27+
28+
use OCP\Search\ASearchResultEntry;
29+
30+
class CommentsSearchResultEntry extends ASearchResultEntry {
31+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
7+
*
8+
* @author Joas Schilling <coding@schilljs.com>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\Comments\Search;
28+
29+
use OCP\Comments\IComment;
30+
use OCP\Files\Folder;
31+
use OCP\Files\Node;
32+
use OCP\Files\NotFoundException;
33+
use OCP\IUser;
34+
use OCP\Search\Provider;
35+
use function count;
36+
37+
class LegacyProvider extends Provider {
38+
39+
/**
40+
* Search for $query
41+
*
42+
* @param string $query
43+
* @return array An array of OCP\Search\Result's
44+
* @since 7.0.0
45+
*/
46+
public function search($query): array {
47+
$cm = \OC::$server->getCommentsManager();
48+
$us = \OC::$server->getUserSession();
49+
50+
$user = $us->getUser();
51+
if (!$user instanceof IUser) {
52+
return [];
53+
}
54+
$uf = \OC::$server->getUserFolder($user->getUID());
55+
56+
if ($uf === null) {
57+
return [];
58+
}
59+
60+
$result = [];
61+
$numComments = 50;
62+
$offset = 0;
63+
64+
while (count($result) < $numComments) {
65+
/** @var IComment[] $comments */
66+
$comments = $cm->search($query, 'files', '', 'comment', $offset, $numComments);
67+
68+
foreach ($comments as $comment) {
69+
if ($comment->getActorType() !== 'users') {
70+
continue;
71+
}
72+
73+
$displayName = $cm->resolveDisplayName('user', $comment->getActorId());
74+
75+
try {
76+
$file = $this->getFileForComment($uf, $comment);
77+
$result[] = new Result($query,
78+
$comment,
79+
$displayName,
80+
$file->getPath()
81+
);
82+
} catch (NotFoundException $e) {
83+
continue;
84+
}
85+
}
86+
87+
if (count($comments) < $numComments) {
88+
// Didn't find more comments when we tried to get, so there are no more comments.
89+
return $result;
90+
}
91+
92+
$offset += $numComments;
93+
$numComments = 50 - count($result);
94+
}
95+
96+
return $result;
97+
}
98+
99+
/**
100+
* @param Folder $userFolder
101+
* @param IComment $comment
102+
* @return Node
103+
* @throws NotFoundException
104+
*/
105+
protected function getFileForComment(Folder $userFolder, IComment $comment): Node {
106+
$nodes = $userFolder->getById((int) $comment->getObjectId());
107+
if (empty($nodes)) {
108+
throw new NotFoundException('File not found');
109+
}
110+
111+
return array_shift($nodes);
112+
}
113+
}
Lines changed: 49 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
2+
3+
declare(strict_types=1);
4+
25
/**
3-
* @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
6+
* @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
47
*
5-
* @author Joas Schilling <coding@schilljs.com>
8+
* @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
69
*
710
* @license GNU AGPL version 3 or any later version
811
*
@@ -17,92 +20,62 @@
1720
* GNU Affero General Public License for more details.
1821
*
1922
* 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/>.
2224
*/
2325

2426
namespace OCA\Comments\Search;
2527

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;
3030
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;
3136

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 {
5438

55-
$result = [];
56-
$numComments = 50;
57-
$offset = 0;
39+
/** @var IL10N */
40+
private $l10n;
5841

59-
while (\count($result) < $numComments) {
60-
/** @var IComment[] $comments */
61-
$comments = $cm->search($query, 'files', '', 'comment', $offset, $numComments);
42+
/** @var IURLGenerator */
43+
private $urlGenerator;
6244

63-
foreach ($comments as $comment) {
64-
if ($comment->getActorType() !== 'users') {
65-
continue;
66-
}
45+
/** @var LegacyProvider */
46+
private $legacyProvider;
6747

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;
9254
}
9355

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+
}
10559

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+
);
10780
}
10881
}

apps/comments/lib/Search/Result.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,33 @@
2828
use OCP\Files\NotFoundException;
2929
use OCP\Search\Result as BaseResult;
3030

31+
/**
32+
* @deprecated 20.0.0
33+
*/
3134
class Result extends BaseResult {
35+
/**
36+
* @deprecated 20.0.0
37+
*/
3238
public $type = 'comment';
39+
/**
40+
* @deprecated 20.0.0
41+
*/
3342
public $comment;
43+
/**
44+
* @deprecated 20.0.0
45+
*/
3446
public $authorId;
47+
/**
48+
* @deprecated 20.0.0
49+
*/
3550
public $authorName;
51+
/**
52+
* @deprecated 20.0.0
53+
*/
3654
public $path;
55+
/**
56+
* @deprecated 20.0.0
57+
*/
3758
public $fileName;
3859

3960
/**
@@ -42,6 +63,7 @@ class Result extends BaseResult {
4263
* @param string $authorName
4364
* @param string $path
4465
* @throws NotFoundException
66+
* @deprecated 20.0.0
4567
*/
4668
public function __construct(string $search,
4769
IComment $comment,

0 commit comments

Comments
 (0)