diff options
author | Morris Jobke <hey@morrisjobke.de> | 2018-07-25 20:30:00 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-25 20:30:00 +0200 |
commit | 61397ee091287e983d0ff345b0e43fb4a3329f19 (patch) | |
tree | 2a04e9f5076d351dc7884c6ba946c80bf002d147 /apps | |
parent | 8255faada4101b91b342bcf929d9a0e4c759cf34 (diff) | |
parent | 2dd55b0550c42355a6188554c3e217870ce37eb5 (diff) | |
download | nextcloud-server-61397ee091287e983d0ff345b0e43fb4a3329f19.tar.gz nextcloud-server-61397ee091287e983d0ff345b0e43fb4a3329f19.zip |
Merge pull request #9222 from nextcloud/feature/noid/search-for-files-by-comments
Allow to search files by comments
Diffstat (limited to 'apps')
-rw-r--r-- | apps/comments/appinfo/app.php | 59 | ||||
-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 | 134 | ||||
-rw-r--r-- | apps/comments/lib/AppInfo/Application.php | 56 | ||||
-rw-r--r-- | apps/comments/lib/Search/Provider.php | 106 | ||||
-rw-r--r-- | apps/comments/lib/Search/Result.php | 109 |
8 files changed, 421 insertions, 48 deletions
diff --git a/apps/comments/appinfo/app.php b/apps/comments/appinfo/app.php index 109063cd22e..6d6775dd152 100644 --- a/apps/comments/appinfo/app.php +++ b/apps/comments/appinfo/app.php @@ -1,62 +1,25 @@ <?php /** - * @copyright Copyright (c) 2016, ownCloud, Inc. + * @copyright Copyright (c) 2018, Joas Schilling <coding@schilljs.com> * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> * @author Joas Schilling <coding@schilljs.com> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Vincent Petry <pvince81@owncloud.com> * - * @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 <http://www.gnu.org/licenses/> + * 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/>. * */ -$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 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..11a96594580 --- /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 = $('<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') + ')') + .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 e60f0cbf36b..3ad00562736 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 @@ class Application extends App { $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 00000000000..ac5afef6669 --- /dev/null +++ b/apps/comments/lib/Search/Provider.php @@ -0,0 +1,106 @@ +<?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 []; + } + + $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 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; + } + +} |