Allow to search by comments

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2018-04-18 11:29:49 +02:00 committed by Daniel Calviño Sánchez
parent 80207d72fa
commit 8c7969e730
10 changed files with 403 additions and 1 deletions

View File

@ -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',
);

View File

@ -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)

View File

@ -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"
]

132
apps/comments/js/search.js Normal file
View File

@ -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, $);

View File

@ -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) {

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -493,6 +493,48 @@ class Manager implements ICommentsManager {
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
* @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

View File

@ -138,6 +138,18 @@ interface ICommentsManager {
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
* @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

View File

@ -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) {}