diff --git a/.drone.yml b/.drone.yml index 6173c155c2b8a..709079bac0168 100644 --- a/.drone.yml +++ b/.drone.yml @@ -472,6 +472,15 @@ pipeline: when: matrix: TESTS: integration-comments + integration-comments-search: + image: nextcloudci/integration-php7.0:integration-php7.0-8 + commands: + - ./occ maintenance:install --admin-pass=admin --data-dir=/dev/shm/nc_int + - cd build/integration + - ./run.sh features/comments-search.feature + when: + matrix: + TESTS: integration-comments-search integration-favorites: image: nextcloudci/integration-php7.0:integration-php7.0-8 commands: @@ -783,6 +792,7 @@ matrix: - TESTS: integration-tags - TESTS: integration-caldav - TESTS: integration-comments + - TESTS: integration-comments-search - TESTS: integration-favorites - TESTS: integration-provisioning-v2 - TESTS: integration-webdav-related diff --git a/apps/comments/appinfo/app.php b/apps/comments/appinfo/app.php index 109063cd22eb2..6d6775dd1527b 100644 --- a/apps/comments/appinfo/app.php +++ b/apps/comments/appinfo/app.php @@ -1,62 +1,25 @@ * - * @author Arthur Schiwon * @author Joas Schilling - * @author Lukas Reschke - * @author Vincent Petry * - * @license AGPL-3.0 + * @license GNU AGPL version 3 or any later version * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . * */ -$eventDispatcher = \OC::$server->getEventDispatcher(); -$eventDispatcher->addListener( - 'OCA\Files::loadAdditionalScripts', - function() { - \OCP\Util::addScript('oc-backbone-webdav'); - \OCP\Util::addScript('comments', 'merged'); - \OCP\Util::addStyle('comments', 'autocomplete'); - \OCP\Util::addStyle('comments', 'comments'); - } -); - -$eventDispatcher->addListener(\OCP\Comments\CommentsEntityEvent::EVENT_ENTITY, function(\OCP\Comments\CommentsEntityEvent $event) { - $event->addEntityCollection('files', function($name) { - $nodes = \OC::$server->getUserFolder()->getById((int)$name); - return !empty($nodes); - }); -}); - -$notificationManager = \OC::$server->getNotificationManager(); -$notificationManager->registerNotifier( - function() { - $application = new \OCP\AppFramework\App('comments'); - return $application->getContainer()->query(\OCA\Comments\Notification\Notifier::class); - }, - function () { - $l = \OC::$server->getL10N('comments'); - return ['id' => 'comments', 'name' => $l->t('Comments')]; - } -); - -$commentsManager = \OC::$server->getCommentsManager(); -$commentsManager->registerEventHandler(function () { - $application = new \OCP\AppFramework\App('comments'); - /** @var \OCA\Comments\EventHandler $handler */ - $handler = $application->getContainer()->query(\OCA\Comments\EventHandler::class); - return $handler; -}); +$application = new \OCA\Comments\AppInfo\Application(); +$application->register(); diff --git a/apps/comments/composer/composer/autoload_classmap.php b/apps/comments/composer/composer/autoload_classmap.php index 0000ab9081a69..580d38a843940 100644 --- a/apps/comments/composer/composer/autoload_classmap.php +++ b/apps/comments/composer/composer/autoload_classmap.php @@ -17,4 +17,6 @@ 'OCA\\Comments\\JSSettingsHelper' => $baseDir . '/../lib/JSSettingsHelper.php', 'OCA\\Comments\\Notification\\Listener' => $baseDir . '/../lib/Notification/Listener.php', 'OCA\\Comments\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php', + 'OCA\\Comments\\Search\\Provider' => $baseDir . '/../lib/Search/Provider.php', + 'OCA\\Comments\\Search\\Result' => $baseDir . '/../lib/Search/Result.php', ); diff --git a/apps/comments/composer/composer/autoload_static.php b/apps/comments/composer/composer/autoload_static.php index 662f77f89dc4f..46074d6ab8096 100644 --- a/apps/comments/composer/composer/autoload_static.php +++ b/apps/comments/composer/composer/autoload_static.php @@ -32,6 +32,8 @@ class ComposerStaticInitComments 'OCA\\Comments\\JSSettingsHelper' => __DIR__ . '/..' . '/../lib/JSSettingsHelper.php', 'OCA\\Comments\\Notification\\Listener' => __DIR__ . '/..' . '/../lib/Notification/Listener.php', 'OCA\\Comments\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php', + 'OCA\\Comments\\Search\\Provider' => __DIR__ . '/..' . '/../lib/Search/Provider.php', + 'OCA\\Comments\\Search\\Result' => __DIR__ . '/..' . '/../lib/Search/Result.php', ); public static function getInitializer(ClassLoader $loader) diff --git a/apps/comments/js/merged.json b/apps/comments/js/merged.json index 6e77d9cf80a5e..d5b2b88233433 100644 --- a/apps/comments/js/merged.json +++ b/apps/comments/js/merged.json @@ -7,6 +7,7 @@ "commentsmodifymenu.js", "filesplugin.js", "activitytabviewplugin.js", + "search.js", "vendor/Caret.js/dist/jquery.caret.min.js", "vendor/At.js/dist/js/jquery.atwho.min.js" ] diff --git a/apps/comments/js/search.js b/apps/comments/js/search.js new file mode 100644 index 0000000000000..11a965945802e --- /dev/null +++ b/apps/comments/js/search.js @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2014 + * + * This file is licensed under the Affero General Public License version 3 + * or later. + * + * See the COPYING-README file. + * + */ +(function(OC, OCA, $) { + "use strict"; + + /** + * Construct a new FileActions instance + * @constructs Files + */ + var Comment = function() { + this.initialize(); + }; + + Comment.prototype = { + + fileList: null, + + /** + * Initialize the file search + */ + initialize: function() { + + var self = this; + + this.fileAppLoaded = function() { + return !!OCA.Files && !!OCA.Files.App; + }; + function inFileList($row, result) { + return false; + + if (! self.fileAppLoaded()) { + return false; + } + var dir = self.fileList.getCurrentDirectory().replace(/\/+$/,''); + var resultDir = OC.dirname(result.path); + return dir === resultDir && self.fileList.inList(result.name); + } + function hideNoFilterResults() { + var $nofilterresults = $('.nofilterresults'); + if ( ! $nofilterresults.hasClass('hidden') ) { + $nofilterresults.addClass('hidden'); + } + } + + /** + * + * @param {jQuery} $row + * @param {Object} result + * @param {int} result.id + * @param {string} result.comment + * @param {string} result.authorId + * @param {string} result.authorName + * @param {string} result.link + * @param {string} result.fileName + * @param {string} result.path + * @returns {*} + */ + this.renderCommentResult = function($row, result) { + if (inFileList($row, result)) { + return null; + } + hideNoFilterResults(); + /*render preview icon, show path beneath filename, + show size and last modified date on the right */ + this.updateLegacyMimetype(result); + + var $pathDiv = $('
').addClass('path').text(result.path); + + var $avatar = $('
'); + $avatar.addClass('avatar') + .css('display', 'inline-block') + .css('vertical-align', 'middle') + .css('margin', '0 5px 2px 3px'); + + if (result.authorName) { + $avatar.avatar(result.authorId, 21, undefined, false, undefined, result.authorName); + } else { + $avatar.avatar(result.authorId, 21); + } + + $row.find('td.info div.name').after($pathDiv).text(result.comment).prepend($('').addClass('path').css('margin-right', '5px').text(result.authorName)).prepend($avatar); + $row.find('td.result a').attr('href', result.link); + + $row.find('td.icon') + .css('background-image', 'url(' + OC.imagePath('core', 'actions/comment') + ')') + .css('opacity', '.4'); + var dir = OC.dirname(result.path); + if (dir === '') { + dir = '/'; + } + $row.find('td.info a').attr('href', + OC.generateUrl('/apps/files/?dir={dir}&scrollto={scrollto}', {dir: dir, scrollto: result.fileName}) + ); + + return $row; + }; + + this.handleCommentClick = function($row, result, event) { + if (self.fileAppLoaded() && self.fileList.id === 'files') { + self.fileList.changeDirectory(OC.dirname(result.path)); + self.fileList.scrollTo(result.name); + return false; + } else { + return true; + } + }; + + this.updateLegacyMimetype = function (result) { + // backward compatibility: + if (!result.mime && result.mime_type) { + result.mime = result.mime_type; + } + }; + this.setFileList = function (fileList) { + this.fileList = fileList; + }; + + OC.Plugins.register('OCA.Search.Core', this); + }, + attach: function(search) { + search.setRenderer('comment', this.renderCommentResult.bind(this)); + search.setHandler('comment', this.handleCommentClick.bind(this)); + } + }; + + OCA.Search.comment = new Comment(); +})(OC, OCA, $); diff --git a/apps/comments/lib/AppInfo/Application.php b/apps/comments/lib/AppInfo/Application.php index e60f0cbf36b16..3ad00562736ef 100644 --- a/apps/comments/lib/AppInfo/Application.php +++ b/apps/comments/lib/AppInfo/Application.php @@ -24,9 +24,14 @@ namespace OCA\Comments\AppInfo; use OCA\Comments\Controller\Notifications; +use OCA\Comments\EventHandler; use OCA\Comments\JSSettingsHelper; +use OCA\Comments\Notification\Notifier; +use OCA\Comments\Search\Provider; use OCP\AppFramework\App; +use OCP\Comments\CommentsEntityEvent; use OCP\Util; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; class Application extends App { @@ -39,4 +44,55 @@ public function __construct (array $urlParams = array()) { $jsSettingsHelper = new JSSettingsHelper($container->getServer()); Util::connectHook('\OCP\Config', 'js', $jsSettingsHelper, 'extend'); } + + public function register() { + $server = $this->getContainer()->getServer(); + + $dispatcher = $server->getEventDispatcher(); + $this->registerSidebarScripts($dispatcher); + $this->registerDavEntity($dispatcher); + $this->registerNotifier(); + $this->registerCommentsEventHandler(); + + $server->getSearch()->registerProvider(Provider::class, ['apps' => ['files']]); + } + + protected function registerSidebarScripts(EventDispatcherInterface $dispatcher) { + $dispatcher->addListener( + 'OCA\Files::loadAdditionalScripts', + function() { + Util::addScript('oc-backbone-webdav'); + Util::addScript('comments', 'merged'); + Util::addStyle('comments', 'autocomplete'); + Util::addStyle('comments', 'comments'); + } + ); + } + + protected function registerDavEntity(EventDispatcherInterface $dispatcher) { + $dispatcher->addListener(CommentsEntityEvent::EVENT_ENTITY, function(CommentsEntityEvent $event) { + $event->addEntityCollection('files', function($name) { + $nodes = \OC::$server->getUserFolder()->getById((int)$name); + return !empty($nodes); + }); + }); + } + + protected function registerNotifier() { + $this->getContainer()->getServer()->getNotificationManager()->registerNotifier( + function() { + return $this->getContainer()->query(Notifier::class); + }, + function () { + $l = $this->getContainer()->getServer()->getL10NFactory()->get('comments'); + return ['id' => 'comments', 'name' => $l->t('Comments')]; + } + ); + } + + protected function registerCommentsEventHandler() { + $this->getContainer()->getServer()->getCommentsManager()->registerEventHandler(function () { + return $this->getContainer()->query(EventHandler::class); + }); + } } diff --git a/apps/comments/lib/Search/Provider.php b/apps/comments/lib/Search/Provider.php new file mode 100644 index 0000000000000..ac5afef6669ae --- /dev/null +++ b/apps/comments/lib/Search/Provider.php @@ -0,0 +1,106 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Comments\Search; + +use OCP\Comments\IComment; +use OCP\Files\Folder; +use OCP\Files\Node; +use OCP\Files\NotFoundException; +use OCP\IUser; + +class Provider extends \OCP\Search\Provider { + + /** + * Search for $query + * + * @param string $query + * @return array An array of OCP\Search\Result's + * @since 7.0.0 + */ + public function search($query): array { + $cm = \OC::$server->getCommentsManager(); + $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) { + /** @var IComment[] $comments */ + $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) { + // Didn't find more comments when we tried to get, so there are no more comments. + return $result; + } + + $offset += $numComments; + $numComments = 50 - \count($result); + } + + return $result; + } + + /** + * @param Folder $userFolder + * @param IComment $comment + * @return Node + * @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); + } +} diff --git a/apps/comments/lib/Search/Result.php b/apps/comments/lib/Search/Result.php new file mode 100644 index 0000000000000..0a48f9d7b5a28 --- /dev/null +++ b/apps/comments/lib/Search/Result.php @@ -0,0 +1,109 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Comments\Search; + +use OCP\Comments\IComment; +use OCP\Files\NotFoundException; +use OCP\Search\Result as BaseResult; + +class Result extends BaseResult { + + public $type = 'comment'; + public $comment; + public $authorId; + public $authorName; + public $path; + public $fileName; + + /** + * @param string $search + * @param IComment $comment + * @param string $authorName + * @param string $path + * @throws NotFoundException + */ + public function __construct(string $search, + IComment $comment, + string $authorName, + string $path) { + parent::__construct( + (int) $comment->getId(), + $comment->getMessage() + /* @todo , [link to file] */ + ); + + $this->comment = $this->getRelevantMessagePart($comment->getMessage(), $search); + $this->authorId = $comment->getActorId(); + $this->authorName = $authorName; + $this->fileName = basename($path); + $this->path = $this->getVisiblePath($path); + } + + /** + * @param string $path + * @return string + * @throws NotFoundException + */ + protected function getVisiblePath(string $path): string { + $segments = explode('/', trim($path, '/'), 3); + + if (!isset($segments[2])) { + throw new NotFoundException('Path not inside visible section'); + } + + return $segments[2]; + } + + /** + * @param string $message + * @param string $search + * @return string + * @throws NotFoundException + */ + protected function getRelevantMessagePart(string $message, string $search): string { + $start = stripos($message, $search); + if ($start === false) { + throw new NotFoundException('Comment section not found'); + } + + $end = $start + strlen($search); + + if ($start <= 25) { + $start = 0; + $prefix = ''; + } else { + $start -= 25; + $prefix = '…'; + } + + if ((strlen($message) - $end) <= 25) { + $end = strlen($message); + $suffix = ''; + } else { + $end += 25; + $suffix = '…'; + } + + return $prefix . substr($message, $start, $end - $start) . $suffix; + } + +} diff --git a/build/integration/features/bootstrap/FeatureContext.php b/build/integration/features/bootstrap/FeatureContext.php index 6b0b199ec6ea8..5a6cab235e52e 100644 --- a/build/integration/features/bootstrap/FeatureContext.php +++ b/build/integration/features/bootstrap/FeatureContext.php @@ -32,5 +32,6 @@ * Features context. */ class FeatureContext implements Context, SnippetAcceptingContext { + use Search; use WebDav; } diff --git a/build/integration/features/bootstrap/Search.php b/build/integration/features/bootstrap/Search.php new file mode 100644 index 0000000000000..e42cde19126cd --- /dev/null +++ b/build/integration/features/bootstrap/Search.php @@ -0,0 +1,90 @@ +. + * + */ + +use Behat\Gherkin\Node\TableNode; +use PHPUnit\Framework\Assert; + +trait Search { + + // BasicStructure trait is expected to be used in the class that uses this + // trait. + + /** + * @When /^searching for "([^"]*)"$/ + * @param string $query + */ + public function searchingFor(string $query) { + $this->searchForInApp($query, ''); + } + + /** + * @When /^searching for "([^"]*)" in app "([^"]*)"$/ + * @param string $query + * @param string $app + */ + public function searchingForInApp(string $query, string $app) { + $url = '/index.php/core/search'; + + $parameters[] = 'query=' . $query; + $parameters[] = 'inApps[]=' . $app; + + $url .= '?' . implode('&', $parameters); + + $this->sendingAToWithRequesttoken('GET', $url); + } + + /** + * @Then /^the list of search results has "(\d+)" results$/ + */ + public function theListOfSearchResultsHasResults(int $count) { + $this->theHTTPStatusCodeShouldBe(200); + + $searchResults = json_decode($this->response->getBody()); + + Assert::assertEquals($count, count($searchResults)); + } + + /** + * @Then /^search result "(\d+)" contains$/ + * + * @param int $number + * @param TableNode $body + */ + public function searchResultXContains(int $number, TableNode $body) { + if (!($body instanceof TableNode)) { + return; + } + + $searchResults = json_decode($this->response->getBody(), $asAssociativeArray = true); + $searchResult = $searchResults[$number]; + + foreach ($body->getRowsHash() as $expectedField => $expectedValue) { + if (!array_key_exists($expectedField, $searchResult)) { + Assert::fail("$expectedField was not found in response"); + } + + Assert::assertEquals($expectedValue, $searchResult[$expectedField], "Field '$expectedField' does not match ({$searchResult[$expectedField]})"); + } + } + +} diff --git a/build/integration/features/comments-search.feature b/build/integration/features/comments-search.feature new file mode 100644 index 0000000000000..1886cb531b933 --- /dev/null +++ b/build/integration/features/comments-search.feature @@ -0,0 +1,266 @@ +Feature: comments-search + + Scenario: Search my own comment on a file belonging to myself + Given user "user0" exists + And User "user0" uploads file "data/textfile.txt" to "/myFileToComment.txt" + And "user0" posts a comment with content "My first comment" on the file named "/myFileToComment.txt" it should return "201" + When Logging in using web as "user0" + And searching for "first" in app "files" + Then the list of search results has "1" results + And search result "0" contains + | type | comment | + | comment | My first comment | + | authorId | user0 | + | authorName | user0 | + | path | myFileToComment.txt | + | fileName | myFileToComment.txt | + | name | My first comment | + + Scenario: Search my own comment on a file shared by someone with me + Given user "user0" exists + And user "user1" exists + And User "user1" uploads file "data/textfile.txt" to "/sharedFileToComment.txt" + And As "user1" sending "POST" to "/apps/files_sharing/api/v1/shares" with + | path | sharedFileToComment.txt | + | shareWith | user0 | + | shareType | 0 | + And "user0" posts a comment with content "My first comment" on the file named "/sharedFileToComment.txt" it should return "201" + When Logging in using web as "user0" + And searching for "first" in app "files" + Then the list of search results has "1" results + And search result "0" contains + | type | comment | + | comment | My first comment | + | authorId | user0 | + | authorName | user0 | + | path | sharedFileToComment.txt | + | fileName | sharedFileToComment.txt | + | name | My first comment | + + Scenario: Search other user's comment on a file shared by me + Given user "user0" exists + And user "user1" exists + And User "user0" uploads file "data/textfile.txt" to "/mySharedFileToComment.txt" + And As "user0" sending "POST" to "/apps/files_sharing/api/v1/shares" with + | path | mySharedFileToComment.txt | + | shareWith | user1 | + | shareType | 0 | + And "user1" posts a comment with content "Other's first comment" on the file named "/mySharedFileToComment.txt" it should return "201" + When Logging in using web as "user0" + And searching for "first" in app "files" + Then the list of search results has "1" results + And search result "0" contains + | type | comment | + | comment | Other's first comment | + | authorId | user1 | + | authorName | user1 | + | path | mySharedFileToComment.txt | + | fileName | mySharedFileToComment.txt | + | name | Other's first comment | + + Scenario: Search other user's comment on a file shared by someone with me + Given user "user0" exists + And user "user1" exists + And User "user1" uploads file "data/textfile.txt" to "/sharedFileToComment.txt" + And As "user1" sending "POST" to "/apps/files_sharing/api/v1/shares" with + | path | sharedFileToComment.txt | + | shareWith | user0 | + | shareType | 0 | + And "user1" posts a comment with content "Other's first comment" on the file named "/sharedFileToComment.txt" it should return "201" + When Logging in using web as "user0" + And searching for "first" in app "files" + Then the list of search results has "1" results + And search result "0" contains + | type | comment | + | comment | Other's first comment | + | authorId | user1 | + | authorName | user1 | + | path | sharedFileToComment.txt | + | fileName | sharedFileToComment.txt | + | name | Other's first comment | + + Scenario: Search several comments on a file belonging to myself + Given user "user0" exists + And User "user0" uploads file "data/textfile.txt" to "/myFileToComment.txt" + And "user0" posts a comment with content "My first comment to be found" on the file named "/myFileToComment.txt" it should return "201" + And "user0" posts a comment with content "The second comment should not be found" on the file named "/myFileToComment.txt" it should return "201" + And "user0" posts a comment with content "My third comment to be found" on the file named "/myFileToComment.txt" it should return "201" + When Logging in using web as "user0" + And searching for "comment to be found" in app "files" + Then the list of search results has "2" results + And search result "0" contains + | type | comment | + | comment | My third comment to be found | + | authorId | user0 | + | authorName | user0 | + | path | myFileToComment.txt | + | fileName | myFileToComment.txt | + | name | My third comment to be found | + And search result "1" contains + | type | comment | + | comment | My first comment to be found | + | authorId | user0 | + | authorName | user0 | + | path | myFileToComment.txt | + | fileName | myFileToComment.txt | + | name | My first comment to be found | + + Scenario: Search comment with a large message ellipsized on the right + Given user "user0" exists + And User "user0" uploads file "data/textfile.txt" to "/myFileToComment.txt" + And "user0" posts a comment with content "A very verbose message that is meant to be used to test the ellipsized message returned when searching for long comments" on the file named "/myFileToComment.txt" it should return "201" + When Logging in using web as "user0" + And searching for "verbose" in app "files" + Then the list of search results has "1" results + And search result "0" contains + | type | comment | + | comment | A very verbose message that is meant to… | + | authorId | user0 | + | authorName | user0 | + | path | myFileToComment.txt | + | fileName | myFileToComment.txt | + | name | A very verbose message that is meant to be used to test the ellipsized message returned when searching for long comments | + + Scenario: Search comment with a large message ellipsized on the left + Given user "user0" exists + And User "user0" uploads file "data/textfile.txt" to "/myFileToComment.txt" + And "user0" posts a comment with content "A very verbose message that is meant to be used to test the ellipsized message returned when searching for long comments" on the file named "/myFileToComment.txt" it should return "201" + When Logging in using web as "user0" + And searching for "searching" in app "files" + Then the list of search results has "1" results + And search result "0" contains + | type | comment | + | comment | …ed message returned when searching for long comments | + | authorId | user0 | + | authorName | user0 | + | path | myFileToComment.txt | + | fileName | myFileToComment.txt | + | name | A very verbose message that is meant to be used to test the ellipsized message returned when searching for long comments | + + Scenario: Search comment with a large message ellipsized on both ends + Given user "user0" exists + And User "user0" uploads file "data/textfile.txt" to "/myFileToComment.txt" + And "user0" posts a comment with content "A very verbose message that is meant to be used to test the ellipsized message returned when searching for long comments" on the file named "/myFileToComment.txt" it should return "201" + When Logging in using web as "user0" + And searching for "ellipsized" in app "files" + Then the list of search results has "1" results + And search result "0" contains + | type | comment | + | comment | …t to be used to test the ellipsized message returned when se… | + | authorId | user0 | + | authorName | user0 | + | path | myFileToComment.txt | + | fileName | myFileToComment.txt | + | name | A very verbose message that is meant to be used to test the ellipsized message returned when searching for long comments | + + Scenario: Search comment on a file in a subfolder + Given user "user0" exists + And user "user0" created a folder "/subfolder" + And User "user0" uploads file "data/textfile.txt" to "/subfolder/myFileToComment.txt" + And "user0" posts a comment with content "My first comment" on the file named "/subfolder/myFileToComment.txt" it should return "201" + When Logging in using web as "user0" + And searching for "first" in app "files" + Then the list of search results has "1" results + And search result "0" contains + | type | comment | + | comment | My first comment | + | authorId | user0 | + | authorName | user0 | + | path | subfolder/myFileToComment.txt | + | fileName | myFileToComment.txt | + | name | My first comment | + + Scenario: Search several comments + Given user "user0" exists + And user "user1" exists + And User "user0" uploads file "data/textfile.txt" to "/myFileToComment.txt" + And User "user0" uploads file "data/textfile.txt" to "/mySharedFileToComment.txt" + And As "user0" sending "POST" to "/apps/files_sharing/api/v1/shares" with + | path | mySharedFileToComment.txt | + | shareWith | user1 | + | shareType | 0 | + And User "user1" uploads file "data/textfile.txt" to "/sharedFileToComment.txt" + And As "user1" sending "POST" to "/apps/files_sharing/api/v1/shares" with + | path | sharedFileToComment.txt | + | shareWith | user0 | + | shareType | 0 | + And "user0" posts a comment with content "My first comment to be found" on the file named "/myFileToComment.txt" it should return "201" + And "user0" posts a comment with content "The second comment should not be found" on the file named "/myFileToComment.txt" it should return "201" + And "user0" posts a comment with content "My first comment to be found" on the file named "/mySharedFileToComment.txt" it should return "201" + And "user1" posts a comment with content "Other's first comment that should not be found" on the file named "/mySharedFileToComment.txt" it should return "201" + And "user1" posts a comment with content "Other's second comment to be found" on the file named "/mySharedFileToComment.txt" it should return "201" + And "user0" posts a comment with content "My first comment that should not be found" on the file named "/sharedFileToComment.txt" it should return "201" + And "user1" posts a comment with content "Other's first comment to be found" on the file named "/sharedFileToComment.txt" it should return "201" + And "user0" posts a comment with content "My second comment to be found that happens to be more verbose than the others and thus should be ellipsized" on the file named "/sharedFileToComment.txt" it should return "201" + And "user0" posts a comment with content "My third comment to be found" on the file named "/myFileToComment.txt" it should return "201" + When Logging in using web as "user0" + And searching for "comment to be found" in app "files" + Then the list of search results has "6" results + And search result "0" contains + | type | comment | + | comment | My third comment to be found | + | authorId | user0 | + | authorName | user0 | + | path | myFileToComment.txt | + | fileName | myFileToComment.txt | + | name | My third comment to be found | + And search result "1" contains + | type | comment | + | comment | My second comment to be found that happens to be more … | + | authorId | user0 | + | authorName | user0 | + | path | sharedFileToComment.txt | + | fileName | sharedFileToComment.txt | + | name | My second comment to be found that happens to be more verbose than the others and thus should be ellipsized | + And search result "2" contains + | type | comment | + | comment | Other's first comment to be found | + | authorId | user1 | + | authorName | user1 | + | path | sharedFileToComment.txt | + | fileName | sharedFileToComment.txt | + | name | Other's first comment to be found | + And search result "3" contains + | type | comment | + | comment | Other's second comment to be found | + | authorId | user1 | + | authorName | user1 | + | path | mySharedFileToComment.txt | + | fileName | mySharedFileToComment.txt | + | name | Other's second comment to be found | + And search result "4" contains + | type | comment | + | comment | My first comment to be found | + | authorId | user0 | + | authorName | user0 | + | path | mySharedFileToComment.txt | + | fileName | mySharedFileToComment.txt | + | name | My first comment to be found | + And search result "5" contains + | type | comment | + | comment | My first comment to be found | + | authorId | user0 | + | authorName | user0 | + | path | myFileToComment.txt | + | fileName | myFileToComment.txt | + | name | My first comment to be found | + + Scenario: Search comment with a query that also matches a file name + Given user "user0" exists + And User "user0" uploads file "data/textfile.txt" to "/myFileToComment.txt" + And "user0" posts a comment with content "A comment in myFileToComment.txt" on the file named "/myFileToComment.txt" it should return "201" + When Logging in using web as "user0" + And searching for "myFileToComment" in app "files" + Then the list of search results has "2" results + And search result "0" contains + | type | file | + | path | /myFileToComment.txt | + | name | myFileToComment.txt | + And search result "1" contains + | type | comment | + | comment | A comment in myFileToComment.txt | + | authorId | user0 | + | authorName | user0 | + | path | myFileToComment.txt | + | fileName | myFileToComment.txt | + | name | A comment in myFileToComment.txt | diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php index d96c22aad515e..8f76d49b192cc 100644 --- a/lib/private/Comments/Manager.php +++ b/lib/private/Comments/Manager.php @@ -493,6 +493,54 @@ protected function getLastKnownComment(string $objectType, return null; } + /** + * Search for comments with a given content + * + * @param string $search content to search for + * @param string $objectType Limit the search by object type + * @param string $objectId Limit the search by object id + * @param string $verb Limit the verb of the comment + * @param int $offset + * @param int $limit + * @return IComment[] + */ + public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array { + $query = $this->dbConn->getQueryBuilder(); + + $query->select('*') + ->from('comments') + ->where($query->expr()->iLike('message', $query->createNamedParameter( + '%' . $this->dbConn->escapeLikeParameter($search). '%' + ))) + ->orderBy('creation_timestamp', 'DESC') + ->addOrderBy('id', 'DESC') + ->setMaxResults($limit); + + if ($objectType !== '') { + $query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType))); + } + if ($objectId !== '') { + $query->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))); + } + if ($verb !== '') { + $query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb))); + } + if ($offset !== 0) { + $query->setFirstResult($offset); + } + + $comments = []; + $result = $query->execute(); + while ($data = $result->fetch()) { + $comment = new Comment($this->normalizeDatabaseData($data)); + $this->cache($comment); + $comments[] = $comment; + } + $result->closeCursor(); + + return $comments; + } + /** * @param $objectType string the object type, e.g. 'files' * @param $objectId string the id of the object diff --git a/lib/public/Comments/ICommentsManager.php b/lib/public/Comments/ICommentsManager.php index b3ed176b3b5ea..ca98214cd72d3 100644 --- a/lib/public/Comments/ICommentsManager.php +++ b/lib/public/Comments/ICommentsManager.php @@ -138,6 +138,20 @@ public function getForObjectSince( int $limit = 30 ): array; + /** + * Search for comments with a given content + * + * @param string $search content to search for + * @param string $objectType Limit the search by object type + * @param string $objectId Limit the search by object id + * @param string $verb Limit the verb of the comment + * @param int $offset + * @param int $limit + * @return IComment[] + * @since 14.0.0 + */ + public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array; + /** * @param $objectType string the object type, e.g. 'files' * @param $objectId string the id of the object diff --git a/tests/lib/Comments/FakeManager.php b/tests/lib/Comments/FakeManager.php index 3ba66e966926d..e758a951e8bf0 100644 --- a/tests/lib/Comments/FakeManager.php +++ b/tests/lib/Comments/FakeManager.php @@ -32,6 +32,10 @@ public function getForObjectSince( public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null) {} + public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array { + return []; + } + public function create($actorType, $actorId, $objectType, $objectId) {} public function delete($id) {}