diff options
author | Joas Schilling <coding@schilljs.com> | 2018-04-18 11:29:49 +0200 |
---|---|---|
committer | Daniel Calviño Sánchez <danxuliu@gmail.com> | 2018-07-25 18:53:44 +0200 |
commit | 8c7969e7301cb88dc6a94565ef8903c70117e750 (patch) | |
tree | b6b8026ac451449f4137feb9115b26628d86ec62 | |
parent | 80207d72fa5de7b88dd8fd180acc528a3d9fa8da (diff) | |
download | nextcloud-server-8c7969e7301cb88dc6a94565ef8903c70117e750.tar.gz nextcloud-server-8c7969e7301cb88dc6a94565ef8903c70117e750.zip |
Allow to search by comments
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | apps/comments/composer/composer/autoload_classmap.php | 2 | ||||
-rw-r--r-- | apps/comments/composer/composer/autoload_static.php | 2 | ||||
-rw-r--r-- | apps/comments/js/merged.json | 1 | ||||
-rw-r--r-- | apps/comments/js/search.js | 132 | ||||
-rw-r--r-- | apps/comments/lib/AppInfo/Application.php | 7 | ||||
-rw-r--r-- | apps/comments/lib/Search/Provider.php | 93 | ||||
-rw-r--r-- | apps/comments/lib/Search/Result.php | 109 | ||||
-rw-r--r-- | lib/private/Comments/Manager.php | 42 | ||||
-rw-r--r-- | lib/public/Comments/ICommentsManager.php | 12 | ||||
-rw-r--r-- | tests/lib/Comments/FakeManager.php | 4 |
10 files changed, 403 insertions, 1 deletions
diff --git a/apps/comments/composer/composer/autoload_classmap.php b/apps/comments/composer/composer/autoload_classmap.php index 0000ab9081a..580d38a8439 100644 --- a/apps/comments/composer/composer/autoload_classmap.php +++ b/apps/comments/composer/composer/autoload_classmap.php @@ -17,4 +17,6 @@ return array( '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 662f77f89dc..46074d6ab80 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 6e77d9cf80a..d5b2b882334 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 00000000000..44b741d8363 --- /dev/null +++ b/apps/comments/js/search.js @@ -0,0 +1,132 @@ +/* + * 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 = $('<div>').addClass('path').text(result.path); + + var $avatar = $('<div>'); + $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($('<span>').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') + ')'); + 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 cbf2733cf3d..3ad00562736 100644 --- a/apps/comments/lib/AppInfo/Application.php +++ b/apps/comments/lib/AppInfo/Application.php @@ -27,6 +27,7 @@ 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; @@ -45,11 +46,15 @@ class Application extends App { } public function register() { - $dispatcher = $this->getContainer()->getServer()->getEventDispatcher(); + $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) { diff --git a/apps/comments/lib/Search/Provider.php b/apps/comments/lib/Search/Provider.php new file mode 100644 index 00000000000..78442e3ce0b --- /dev/null +++ b/apps/comments/lib/Search/Provider.php @@ -0,0 +1,93 @@ +<?php +/** + * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +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 []; + } + + /** @var IComment[] $comments */ + $comments = $cm->search($query, 'files', '', 'comment'); + + $result = []; + 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; + } + } + + 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 00000000000..0a48f9d7b5a --- /dev/null +++ b/apps/comments/lib/Search/Result.php @@ -0,0 +1,109 @@ +<?php +/** + * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com> + * + * @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 <http://www.gnu.org/licenses/>. + * + */ + +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/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php index d96c22aad51..a06a2a0ec59 100644 --- a/lib/private/Comments/Manager.php +++ b/lib/private/Comments/Manager.php @@ -494,6 +494,48 @@ class Manager implements ICommentsManager { } /** + * 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 + * @return IComment[] + */ + public function search(string $search, string $objectType, string $objectId, string $verb): 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'); + + 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))); + } + + $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 * @param \DateTime $notOlderThan optional, timestamp of the oldest comments diff --git a/lib/public/Comments/ICommentsManager.php b/lib/public/Comments/ICommentsManager.php index b3ed176b3b5..2ea5ef29291 100644 --- a/lib/public/Comments/ICommentsManager.php +++ b/lib/public/Comments/ICommentsManager.php @@ -139,6 +139,18 @@ interface ICommentsManager { ): 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 + * @return IComment[] + * @since 14.0.0 + */ + public function search(string $search, string $objectType, string $objectId, string $verb): array; + + /** * @param $objectType string the object type, e.g. 'files' * @param $objectId string the id of the object * @param \DateTime|null $notOlderThan optional, timestamp of the oldest comments diff --git a/tests/lib/Comments/FakeManager.php b/tests/lib/Comments/FakeManager.php index 3ba66e96692..62a44a53f11 100644 --- a/tests/lib/Comments/FakeManager.php +++ b/tests/lib/Comments/FakeManager.php @@ -32,6 +32,10 @@ class FakeManager implements ICommentsManager { public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null) {} + public function search(string $search, string $objectType, string $objectId, string $verb): array { + return []; + } + public function create($actorType, $actorId, $objectType, $objectId) {} public function delete($id) {} |