2015-11-24 01:53:55 +03:00
|
|
|
<?php
|
2016-01-12 17:02:16 +03:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-03-01 19:25:15 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2016-01-12 17:02:16 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* 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 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, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-01-12 17:02:16 +03:00
|
|
|
*
|
|
|
|
*/
|
2017-03-29 16:04:11 +03:00
|
|
|
|
2015-11-24 01:53:55 +03:00
|
|
|
namespace OC\Comments;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\Exception\DriverException;
|
2020-03-11 14:12:07 +03:00
|
|
|
use Doctrine\DBAL\Exception\InvalidFieldNameException;
|
2020-10-19 10:49:04 +03:00
|
|
|
use OCA\Comments\AppInfo\Application;
|
2020-10-21 10:18:51 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2016-02-05 12:45:16 +03:00
|
|
|
use OCP\Comments\CommentsEvent;
|
2015-11-24 01:53:55 +03:00
|
|
|
use OCP\Comments\IComment;
|
2016-05-09 11:02:07 +03:00
|
|
|
use OCP\Comments\ICommentsEventHandler;
|
2015-11-24 01:53:55 +03:00
|
|
|
use OCP\Comments\ICommentsManager;
|
|
|
|
use OCP\Comments\NotFoundException;
|
2016-02-29 11:44:40 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2016-01-27 20:30:09 +03:00
|
|
|
use OCP\IConfig;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IDBConnection;
|
2016-01-27 20:30:09 +03:00
|
|
|
use OCP\IUser;
|
2020-10-19 10:49:04 +03:00
|
|
|
use OCP\IInitialStateService;
|
|
|
|
use OCP\Util;
|
2020-10-07 09:57:45 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-11-24 01:53:55 +03:00
|
|
|
|
|
|
|
class Manager implements ICommentsManager {
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
protected $dbConn;
|
|
|
|
|
2020-10-07 09:57:45 +03:00
|
|
|
/** @var LoggerInterface */
|
2015-11-24 01:53:55 +03:00
|
|
|
protected $logger;
|
|
|
|
|
2016-01-27 20:30:09 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
protected $config;
|
|
|
|
|
2020-10-21 10:18:51 +03:00
|
|
|
/** @var ITimeFactory */
|
|
|
|
protected $timeFactory;
|
|
|
|
|
2020-10-19 10:49:04 +03:00
|
|
|
/** @var IInitialStateService */
|
|
|
|
protected $initialStateService;
|
|
|
|
|
2017-03-29 16:04:11 +03:00
|
|
|
/** @var IComment[] */
|
2016-02-05 12:45:16 +03:00
|
|
|
protected $commentsCache = [];
|
|
|
|
|
2016-05-09 11:02:07 +03:00
|
|
|
/** @var \Closure[] */
|
|
|
|
protected $eventHandlerClosures = [];
|
|
|
|
|
|
|
|
/** @var ICommentsEventHandler[] */
|
|
|
|
protected $eventHandlers = [];
|
|
|
|
|
2016-10-16 21:28:36 +03:00
|
|
|
/** @var \Closure[] */
|
|
|
|
protected $displayNameResolvers = [];
|
|
|
|
|
2020-10-19 10:49:04 +03:00
|
|
|
public function __construct(IDBConnection $dbConn,
|
|
|
|
LoggerInterface $logger,
|
|
|
|
IConfig $config,
|
2020-10-21 10:18:51 +03:00
|
|
|
ITimeFactory $timeFactory,
|
2020-10-19 10:49:04 +03:00
|
|
|
IInitialStateService $initialStateService) {
|
2015-11-24 01:53:55 +03:00
|
|
|
$this->dbConn = $dbConn;
|
|
|
|
$this->logger = $logger;
|
2016-01-27 20:30:09 +03:00
|
|
|
$this->config = $config;
|
2020-10-21 10:18:51 +03:00
|
|
|
$this->timeFactory = $timeFactory;
|
2020-10-19 10:49:04 +03:00
|
|
|
$this->initialStateService = $initialStateService;
|
2015-11-24 01:53:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* converts data base data into PHP native, proper types as defined by
|
|
|
|
* IComment interface.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function normalizeDatabaseData(array $data) {
|
2018-01-26 01:06:53 +03:00
|
|
|
$data['id'] = (string)$data['id'];
|
|
|
|
$data['parent_id'] = (string)$data['parent_id'];
|
|
|
|
$data['topmost_parent_id'] = (string)$data['topmost_parent_id'];
|
2015-11-24 01:53:55 +03:00
|
|
|
$data['creation_timestamp'] = new \DateTime($data['creation_timestamp']);
|
2016-01-11 20:09:00 +03:00
|
|
|
if (!is_null($data['latest_child_timestamp'])) {
|
|
|
|
$data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']);
|
|
|
|
}
|
2018-01-26 01:06:53 +03:00
|
|
|
$data['children_count'] = (int)$data['children_count'];
|
2020-03-11 14:11:01 +03:00
|
|
|
$data['reference_id'] = $data['reference_id'] ?? null;
|
2015-11-24 01:53:55 +03:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2019-07-01 16:48:26 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
* @return IComment
|
|
|
|
*/
|
|
|
|
public function getCommentFromData(array $data): IComment {
|
|
|
|
return new Comment($this->normalizeDatabaseData($data));
|
|
|
|
}
|
|
|
|
|
2015-11-24 01:53:55 +03:00
|
|
|
/**
|
|
|
|
* prepares a comment for an insert or update operation after making sure
|
|
|
|
* all necessary fields have a value assigned.
|
|
|
|
*
|
|
|
|
* @param IComment $comment
|
2015-12-08 16:58:18 +03:00
|
|
|
* @return IComment returns the same updated IComment instance as provided
|
|
|
|
* by parameter for convenience
|
2015-11-24 01:53:55 +03:00
|
|
|
* @throws \UnexpectedValueException
|
|
|
|
*/
|
|
|
|
protected function prepareCommentForDatabaseWrite(IComment $comment) {
|
2017-03-29 16:04:11 +03:00
|
|
|
if (!$comment->getActorType()
|
2019-09-25 10:07:07 +03:00
|
|
|
|| $comment->getActorId() === ''
|
2015-12-03 19:13:18 +03:00
|
|
|
|| !$comment->getObjectType()
|
2019-09-25 10:07:07 +03:00
|
|
|
|| $comment->getObjectId() === ''
|
2015-12-03 19:13:18 +03:00
|
|
|
|| !$comment->getVerb()
|
2015-11-24 01:53:55 +03:00
|
|
|
) {
|
|
|
|
throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving');
|
|
|
|
}
|
|
|
|
|
2017-03-29 16:04:11 +03:00
|
|
|
if ($comment->getId() === '') {
|
2015-11-24 01:53:55 +03:00
|
|
|
$comment->setChildrenCount(0);
|
|
|
|
$comment->setLatestChildDateTime(new \DateTime('0000-00-00 00:00:00', new \DateTimeZone('UTC')));
|
|
|
|
$comment->setLatestChildDateTime(null);
|
|
|
|
}
|
|
|
|
|
2017-03-29 16:04:11 +03:00
|
|
|
if (is_null($comment->getCreationDateTime())) {
|
2015-11-24 01:53:55 +03:00
|
|
|
$comment->setCreationDateTime(new \DateTime());
|
|
|
|
}
|
|
|
|
|
2017-03-29 16:04:11 +03:00
|
|
|
if ($comment->getParentId() !== '0') {
|
2015-11-24 01:53:55 +03:00
|
|
|
$comment->setTopmostParentId($this->determineTopmostParentId($comment->getParentId()));
|
|
|
|
} else {
|
|
|
|
$comment->setTopmostParentId('0');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cache($comment);
|
|
|
|
|
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the topmost parent id of a given comment identified by ID
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return string
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
protected function determineTopmostParentId($id) {
|
|
|
|
$comment = $this->get($id);
|
2017-03-29 16:04:11 +03:00
|
|
|
if ($comment->getParentId() === '0') {
|
2015-11-24 01:53:55 +03:00
|
|
|
return $comment->getId();
|
|
|
|
}
|
2019-07-24 11:39:57 +03:00
|
|
|
|
|
|
|
return $this->determineTopmostParentId($comment->getParentId());
|
2015-11-24 01:53:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* updates child information of a comment
|
|
|
|
*
|
2017-03-29 16:04:11 +03:00
|
|
|
* @param string $id
|
|
|
|
* @param \DateTime $cDateTime the date time of the most recent child
|
2015-11-24 01:53:55 +03:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
protected function updateChildrenInformation($id, \DateTime $cDateTime) {
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
2018-10-19 17:44:28 +03:00
|
|
|
$query = $qb->select($qb->func()->count('id'))
|
2017-03-29 16:04:11 +03:00
|
|
|
->from('comments')
|
|
|
|
->where($qb->expr()->eq('parent_id', $qb->createParameter('id')))
|
|
|
|
->setParameter('id', $id);
|
2015-11-24 01:53:55 +03:00
|
|
|
|
|
|
|
$resultStatement = $query->execute();
|
|
|
|
$data = $resultStatement->fetch(\PDO::FETCH_NUM);
|
|
|
|
$resultStatement->closeCursor();
|
2018-01-26 01:06:53 +03:00
|
|
|
$children = (int)$data[0];
|
2015-11-24 01:53:55 +03:00
|
|
|
|
|
|
|
$comment = $this->get($id);
|
|
|
|
$comment->setChildrenCount($children);
|
|
|
|
$comment->setLatestChildDateTime($cDateTime);
|
|
|
|
$this->save($comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests whether actor or object type and id parameters are acceptable.
|
|
|
|
* Throws exception if not.
|
|
|
|
*
|
|
|
|
* @param string $role
|
|
|
|
* @param string $type
|
|
|
|
* @param string $id
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
protected function checkRoleParameters($role, $type, $id) {
|
2017-03-29 16:04:11 +03:00
|
|
|
if (
|
|
|
|
!is_string($type) || empty($type)
|
2015-11-24 01:53:55 +03:00
|
|
|
|| !is_string($id) || empty($id)
|
|
|
|
) {
|
|
|
|
throw new \InvalidArgumentException($role . ' parameters must be string and not empty');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* run-time caches a comment
|
|
|
|
*
|
|
|
|
* @param IComment $comment
|
|
|
|
*/
|
|
|
|
protected function cache(IComment $comment) {
|
|
|
|
$id = $comment->getId();
|
2017-03-29 16:04:11 +03:00
|
|
|
if (empty($id)) {
|
2015-11-24 01:53:55 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-01-26 01:06:53 +03:00
|
|
|
$this->commentsCache[(string)$id] = $comment;
|
2015-11-24 01:53:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* removes an entry from the comments run time cache
|
|
|
|
*
|
|
|
|
* @param mixed $id the comment's id
|
|
|
|
*/
|
|
|
|
protected function uncache($id) {
|
2018-01-26 01:06:53 +03:00
|
|
|
$id = (string)$id;
|
2015-11-24 01:53:55 +03:00
|
|
|
if (isset($this->commentsCache[$id])) {
|
|
|
|
unset($this->commentsCache[$id]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns a comment instance
|
|
|
|
*
|
|
|
|
* @param string $id the ID of the comment
|
|
|
|
* @return IComment
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
|
|
|
public function get($id) {
|
2018-01-26 01:06:53 +03:00
|
|
|
if ((int)$id === 0) {
|
2015-11-24 01:53:55 +03:00
|
|
|
throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.');
|
|
|
|
}
|
|
|
|
|
2017-03-29 16:04:11 +03:00
|
|
|
if (isset($this->commentsCache[$id])) {
|
2015-11-24 01:53:55 +03:00
|
|
|
return $this->commentsCache[$id];
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$resultStatement = $qb->select('*')
|
|
|
|
->from('comments')
|
|
|
|
->where($qb->expr()->eq('id', $qb->createParameter('id')))
|
2016-02-29 11:44:40 +03:00
|
|
|
->setParameter('id', $id, IQueryBuilder::PARAM_INT)
|
2015-11-24 01:53:55 +03:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$data = $resultStatement->fetch();
|
|
|
|
$resultStatement->closeCursor();
|
2017-03-29 16:04:11 +03:00
|
|
|
if (!$data) {
|
2015-11-24 01:53:55 +03:00
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
2019-07-01 16:48:26 +03:00
|
|
|
|
|
|
|
$comment = $this->getCommentFromData($data);
|
2015-11-24 01:53:55 +03:00
|
|
|
$this->cache($comment);
|
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the comment specified by the id and all it's child comments.
|
|
|
|
* At this point of time, we do only support one level depth.
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @param int $limit max number of entries to return, 0 returns all
|
|
|
|
* @param int $offset the start entry
|
|
|
|
* @return array
|
|
|
|
* @since 9.0.0
|
|
|
|
*
|
|
|
|
* The return array looks like this
|
|
|
|
* [
|
|
|
|
* 'comment' => IComment, // root comment
|
|
|
|
* 'replies' =>
|
|
|
|
* [
|
|
|
|
* 0 =>
|
|
|
|
* [
|
|
|
|
* 'comment' => IComment,
|
|
|
|
* 'replies' => []
|
|
|
|
* ]
|
|
|
|
* 1 =>
|
|
|
|
* [
|
|
|
|
* 'comment' => IComment,
|
|
|
|
* 'replies'=> []
|
|
|
|
* ],
|
|
|
|
* …
|
|
|
|
* ]
|
|
|
|
* ]
|
|
|
|
*/
|
|
|
|
public function getTree($id, $limit = 0, $offset = 0) {
|
|
|
|
$tree = [];
|
|
|
|
$tree['comment'] = $this->get($id);
|
|
|
|
$tree['replies'] = [];
|
|
|
|
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$query = $qb->select('*')
|
2017-03-29 16:04:11 +03:00
|
|
|
->from('comments')
|
|
|
|
->where($qb->expr()->eq('topmost_parent_id', $qb->createParameter('id')))
|
|
|
|
->orderBy('creation_timestamp', 'DESC')
|
|
|
|
->setParameter('id', $id);
|
2015-11-24 01:53:55 +03:00
|
|
|
|
2017-03-29 16:04:11 +03:00
|
|
|
if ($limit > 0) {
|
2015-11-24 01:53:55 +03:00
|
|
|
$query->setMaxResults($limit);
|
|
|
|
}
|
2017-03-29 16:04:11 +03:00
|
|
|
if ($offset > 0) {
|
2015-11-24 01:53:55 +03:00
|
|
|
$query->setFirstResult($offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
$resultStatement = $query->execute();
|
2017-03-29 16:04:11 +03:00
|
|
|
while ($data = $resultStatement->fetch()) {
|
2019-07-01 16:48:26 +03:00
|
|
|
$comment = $this->getCommentFromData($data);
|
2015-11-24 01:53:55 +03:00
|
|
|
$this->cache($comment);
|
|
|
|
$tree['replies'][] = [
|
|
|
|
'comment' => $comment,
|
|
|
|
'replies' => []
|
|
|
|
];
|
|
|
|
}
|
|
|
|
$resultStatement->closeCursor();
|
|
|
|
|
|
|
|
return $tree;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns comments for a specific object (e.g. a file).
|
|
|
|
*
|
|
|
|
* The sort order is always newest to oldest.
|
|
|
|
*
|
|
|
|
* @param string $objectType the object type, e.g. 'files'
|
|
|
|
* @param string $objectId the id of the object
|
|
|
|
* @param int $limit optional, number of maximum comments to be returned. if
|
|
|
|
* not specified, all comments are returned.
|
|
|
|
* @param int $offset optional, starting point
|
|
|
|
* @param \DateTime $notOlderThan optional, timestamp of the oldest comments
|
|
|
|
* that may be returned
|
|
|
|
* @return IComment[]
|
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
|
|
|
public function getForObject(
|
2017-03-29 16:04:11 +03:00
|
|
|
$objectType,
|
|
|
|
$objectId,
|
|
|
|
$limit = 0,
|
|
|
|
$offset = 0,
|
|
|
|
\DateTime $notOlderThan = null
|
2015-11-24 01:53:55 +03:00
|
|
|
) {
|
|
|
|
$comments = [];
|
|
|
|
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$query = $qb->select('*')
|
2017-03-29 16:04:11 +03:00
|
|
|
->from('comments')
|
|
|
|
->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
|
|
|
|
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
|
|
|
|
->orderBy('creation_timestamp', 'DESC')
|
|
|
|
->setParameter('type', $objectType)
|
|
|
|
->setParameter('id', $objectId);
|
|
|
|
|
|
|
|
if ($limit > 0) {
|
2015-11-24 01:53:55 +03:00
|
|
|
$query->setMaxResults($limit);
|
|
|
|
}
|
2017-03-29 16:04:11 +03:00
|
|
|
if ($offset > 0) {
|
2015-11-24 01:53:55 +03:00
|
|
|
$query->setFirstResult($offset);
|
|
|
|
}
|
2017-03-29 16:04:11 +03:00
|
|
|
if (!is_null($notOlderThan)) {
|
2015-11-24 01:53:55 +03:00
|
|
|
$query
|
|
|
|
->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan')))
|
|
|
|
->setParameter('notOlderThan', $notOlderThan, 'datetime');
|
|
|
|
}
|
|
|
|
|
|
|
|
$resultStatement = $query->execute();
|
2017-03-29 16:04:11 +03:00
|
|
|
while ($data = $resultStatement->fetch()) {
|
2019-07-01 16:48:26 +03:00
|
|
|
$comment = $this->getCommentFromData($data);
|
2015-11-24 01:53:55 +03:00
|
|
|
$this->cache($comment);
|
|
|
|
$comments[] = $comment;
|
|
|
|
}
|
|
|
|
$resultStatement->closeCursor();
|
|
|
|
|
|
|
|
return $comments;
|
|
|
|
}
|
|
|
|
|
2018-04-10 11:50:57 +03:00
|
|
|
/**
|
2018-04-19 18:10:03 +03:00
|
|
|
* @param string $objectType the object type, e.g. 'files'
|
|
|
|
* @param string $objectId the id of the object
|
|
|
|
* @param int $lastKnownCommentId the last known comment (will be used as offset)
|
|
|
|
* @param string $sortDirection direction of the comments (`asc` or `desc`)
|
|
|
|
* @param int $limit optional, number of maximum comments to be returned. if
|
|
|
|
* set to 0, all comments are returned.
|
2020-10-21 10:08:48 +03:00
|
|
|
* @param bool $includeLastKnown
|
2018-04-19 18:10:03 +03:00
|
|
|
* @return IComment[]
|
2018-04-10 11:50:57 +03:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getForObjectSince(
|
|
|
|
string $objectType,
|
|
|
|
string $objectId,
|
|
|
|
int $lastKnownCommentId,
|
|
|
|
string $sortDirection = 'asc',
|
2020-10-21 10:08:48 +03:00
|
|
|
int $limit = 30,
|
|
|
|
bool $includeLastKnown = false
|
2018-04-10 11:50:57 +03:00
|
|
|
): array {
|
|
|
|
$comments = [];
|
|
|
|
|
|
|
|
$query = $this->dbConn->getQueryBuilder();
|
|
|
|
$query->select('*')
|
|
|
|
->from('comments')
|
|
|
|
->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
|
|
|
|
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
|
|
|
|
->orderBy('creation_timestamp', $sortDirection === 'desc' ? 'DESC' : 'ASC')
|
|
|
|
->addOrderBy('id', $sortDirection === 'desc' ? 'DESC' : 'ASC');
|
|
|
|
|
|
|
|
if ($limit > 0) {
|
|
|
|
$query->setMaxResults($limit);
|
|
|
|
}
|
|
|
|
|
2018-04-23 12:50:30 +03:00
|
|
|
$lastKnownComment = $lastKnownCommentId > 0 ? $this->getLastKnownComment(
|
2018-04-10 11:50:57 +03:00
|
|
|
$objectType,
|
|
|
|
$objectId,
|
|
|
|
$lastKnownCommentId
|
2018-04-23 12:50:30 +03:00
|
|
|
) : null;
|
2018-04-10 11:50:57 +03:00
|
|
|
if ($lastKnownComment instanceof IComment) {
|
|
|
|
$lastKnownCommentDateTime = $lastKnownComment->getCreationDateTime();
|
|
|
|
if ($sortDirection === 'desc') {
|
2020-10-21 10:47:40 +03:00
|
|
|
if ($includeLastKnown) {
|
|
|
|
$idComparison = $query->expr()->lte('id', $query->createNamedParameter($lastKnownCommentId));
|
|
|
|
} else {
|
|
|
|
$idComparison = $query->expr()->lt('id', $query->createNamedParameter($lastKnownCommentId));
|
|
|
|
}
|
2018-04-10 11:50:57 +03:00
|
|
|
$query->andWhere(
|
|
|
|
$query->expr()->orX(
|
|
|
|
$query->expr()->lt(
|
|
|
|
'creation_timestamp',
|
|
|
|
$query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE),
|
|
|
|
IQueryBuilder::PARAM_DATE
|
|
|
|
),
|
|
|
|
$query->expr()->andX(
|
|
|
|
$query->expr()->eq(
|
|
|
|
'creation_timestamp',
|
|
|
|
$query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE),
|
|
|
|
IQueryBuilder::PARAM_DATE
|
|
|
|
),
|
2020-10-21 10:47:40 +03:00
|
|
|
$idComparison
|
2018-04-10 11:50:57 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
2020-10-21 10:47:40 +03:00
|
|
|
if ($includeLastKnown) {
|
|
|
|
$idComparison = $query->expr()->gte('id', $query->createNamedParameter($lastKnownCommentId));
|
|
|
|
} else {
|
|
|
|
$idComparison = $query->expr()->gt('id', $query->createNamedParameter($lastKnownCommentId));
|
|
|
|
}
|
2018-04-10 11:50:57 +03:00
|
|
|
$query->andWhere(
|
|
|
|
$query->expr()->orX(
|
|
|
|
$query->expr()->gt(
|
|
|
|
'creation_timestamp',
|
|
|
|
$query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE),
|
|
|
|
IQueryBuilder::PARAM_DATE
|
|
|
|
),
|
|
|
|
$query->expr()->andX(
|
|
|
|
$query->expr()->eq(
|
|
|
|
'creation_timestamp',
|
|
|
|
$query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE),
|
|
|
|
IQueryBuilder::PARAM_DATE
|
|
|
|
),
|
2020-10-21 10:47:40 +03:00
|
|
|
$idComparison
|
2018-04-10 11:50:57 +03:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$resultStatement = $query->execute();
|
|
|
|
while ($data = $resultStatement->fetch()) {
|
2019-07-01 16:48:26 +03:00
|
|
|
$comment = $this->getCommentFromData($data);
|
2018-04-10 11:50:57 +03:00
|
|
|
$this->cache($comment);
|
|
|
|
$comments[] = $comment;
|
|
|
|
}
|
|
|
|
$resultStatement->closeCursor();
|
|
|
|
|
|
|
|
return $comments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-19 18:10:03 +03:00
|
|
|
* @param string $objectType the object type, e.g. 'files'
|
|
|
|
* @param string $objectId the id of the object
|
|
|
|
* @param int $id the comment to look for
|
2018-04-10 11:50:57 +03:00
|
|
|
* @return Comment|null
|
|
|
|
*/
|
|
|
|
protected function getLastKnownComment(string $objectType,
|
|
|
|
string $objectId,
|
|
|
|
int $id) {
|
|
|
|
$query = $this->dbConn->getQueryBuilder();
|
|
|
|
$query->select('*')
|
|
|
|
->from('comments')
|
|
|
|
->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
|
|
|
|
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
|
|
|
|
->andWhere($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
|
|
|
|
|
|
|
|
$result = $query->execute();
|
|
|
|
$row = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
if ($row) {
|
2019-07-01 16:48:26 +03:00
|
|
|
$comment = $this->getCommentFromData($row);
|
2018-04-10 11:50:57 +03:00
|
|
|
$this->cache($comment);
|
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-04-18 12:29:49 +03:00
|
|
|
/**
|
|
|
|
* 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
|
2018-04-23 15:58:43 +03:00
|
|
|
* @param int $offset
|
|
|
|
* @param int $limit
|
2018-04-18 12:29:49 +03:00
|
|
|
* @return IComment[]
|
|
|
|
*/
|
2018-04-23 15:58:43 +03:00
|
|
|
public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array {
|
2020-10-21 10:16:42 +03:00
|
|
|
$objectIds = [];
|
|
|
|
if ($objectId) {
|
|
|
|
$objectIds[] = $objectIds;
|
|
|
|
}
|
|
|
|
return $this->searchForObjects($search, $objectType, $objectIds, $verb, $offset, $limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for comments on one or more objects with a given content
|
|
|
|
*
|
|
|
|
* @param string $search content to search for
|
|
|
|
* @param string $objectType Limit the search by object type
|
|
|
|
* @param array $objectIds Limit the search by object ids
|
|
|
|
* @param string $verb Limit the verb of the comment
|
|
|
|
* @param int $offset
|
|
|
|
* @param int $limit
|
|
|
|
* @return IComment[]
|
|
|
|
*/
|
|
|
|
public function searchForObjects(string $search, string $objectType, array $objectIds, string $verb, int $offset, int $limit = 50): array {
|
2018-04-18 12:29:49 +03:00
|
|
|
$query = $this->dbConn->getQueryBuilder();
|
|
|
|
|
|
|
|
$query->select('*')
|
|
|
|
->from('comments')
|
|
|
|
->where($query->expr()->iLike('message', $query->createNamedParameter(
|
|
|
|
'%' . $this->dbConn->escapeLikeParameter($search). '%'
|
|
|
|
)))
|
|
|
|
->orderBy('creation_timestamp', 'DESC')
|
2018-04-23 15:58:43 +03:00
|
|
|
->addOrderBy('id', 'DESC')
|
|
|
|
->setMaxResults($limit);
|
2018-04-18 12:29:49 +03:00
|
|
|
|
|
|
|
if ($objectType !== '') {
|
|
|
|
$query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType)));
|
|
|
|
}
|
2020-10-21 10:16:42 +03:00
|
|
|
if (!empty($objectIds)) {
|
|
|
|
$query->andWhere($query->expr()->in('object_id', $query->createNamedParameter($objectIds, IQueryBuilder::PARAM_STR_ARRAY)));
|
2018-04-18 12:29:49 +03:00
|
|
|
}
|
|
|
|
if ($verb !== '') {
|
|
|
|
$query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb)));
|
|
|
|
}
|
2018-04-23 15:58:43 +03:00
|
|
|
if ($offset !== 0) {
|
|
|
|
$query->setFirstResult($offset);
|
|
|
|
}
|
2018-04-18 12:29:49 +03:00
|
|
|
|
|
|
|
$comments = [];
|
|
|
|
$result = $query->execute();
|
|
|
|
while ($data = $result->fetch()) {
|
2019-07-01 16:48:26 +03:00
|
|
|
$comment = $this->getCommentFromData($data);
|
2018-04-18 12:29:49 +03:00
|
|
|
$this->cache($comment);
|
|
|
|
$comments[] = $comment;
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
return $comments;
|
|
|
|
}
|
|
|
|
|
2015-11-24 01:53:55 +03:00
|
|
|
/**
|
|
|
|
* @param $objectType string the object type, e.g. 'files'
|
|
|
|
* @param $objectId string the id of the object
|
2016-01-27 20:30:09 +03:00
|
|
|
* @param \DateTime $notOlderThan optional, timestamp of the oldest comments
|
|
|
|
* that may be returned
|
2018-07-30 10:41:51 +03:00
|
|
|
* @param string $verb Limit the verb of the comment - Added in 14.0.0
|
2015-11-24 01:53:55 +03:00
|
|
|
* @return Int
|
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
2018-07-30 10:41:51 +03:00
|
|
|
public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null, $verb = '') {
|
2015-11-24 01:53:55 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
2018-10-19 17:44:28 +03:00
|
|
|
$query = $qb->select($qb->func()->count('id'))
|
2017-03-29 16:04:11 +03:00
|
|
|
->from('comments')
|
|
|
|
->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
|
|
|
|
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
|
|
|
|
->setParameter('type', $objectType)
|
|
|
|
->setParameter('id', $objectId);
|
2015-11-24 01:53:55 +03:00
|
|
|
|
2017-03-29 16:04:11 +03:00
|
|
|
if (!is_null($notOlderThan)) {
|
2016-01-27 20:30:09 +03:00
|
|
|
$query
|
|
|
|
->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan')))
|
|
|
|
->setParameter('notOlderThan', $notOlderThan, 'datetime');
|
|
|
|
}
|
|
|
|
|
2018-07-30 10:41:51 +03:00
|
|
|
if ($verb !== '') {
|
|
|
|
$query->andWhere($qb->expr()->eq('verb', $qb->createNamedParameter($verb)));
|
|
|
|
}
|
|
|
|
|
2015-11-24 01:53:55 +03:00
|
|
|
$resultStatement = $query->execute();
|
|
|
|
$data = $resultStatement->fetch(\PDO::FETCH_NUM);
|
|
|
|
$resultStatement->closeCursor();
|
2018-01-26 01:06:53 +03:00
|
|
|
return (int)$data[0];
|
2015-11-24 01:53:55 +03:00
|
|
|
}
|
|
|
|
|
2020-10-30 14:10:59 +03:00
|
|
|
/**
|
|
|
|
* @param string $objectType the object type, e.g. 'files'
|
|
|
|
* @param string[] $objectIds the id of the object
|
|
|
|
* @param IUser $user
|
|
|
|
* @param string $verb Limit the verb of the comment - Added in 14.0.0
|
|
|
|
* @return array Map with object id => # of unread comments
|
|
|
|
* @psalm-return array<string, int>
|
|
|
|
* @since 21.0.0
|
|
|
|
*/
|
|
|
|
public function getNumberOfUnreadCommentsForObjects(string $objectType, array $objectIds, IUser $user, $verb = ''): array {
|
|
|
|
$query = $this->dbConn->getQueryBuilder();
|
|
|
|
$query->select('c.object_id', $query->func()->count('c.id', 'num_comments'))
|
|
|
|
->from('comments', 'c')
|
|
|
|
->leftJoin('c', 'comments_read_markers', 'm', $query->expr()->andX(
|
|
|
|
$query->expr()->eq('m.user_id', $query->createNamedParameter($user->getUID())),
|
|
|
|
$query->expr()->eq('c.object_type', 'm.object_type'),
|
|
|
|
$query->expr()->eq('c.object_id', 'm.object_id')
|
|
|
|
))
|
|
|
|
->where($query->expr()->eq('c.object_type', $query->createNamedParameter($objectType)))
|
|
|
|
->andWhere($query->expr()->in('c.object_id', $query->createNamedParameter($objectIds, IQueryBuilder::PARAM_STR_ARRAY)))
|
|
|
|
->andWhere($query->expr()->orX(
|
|
|
|
$query->expr()->gt('c.creation_timestamp', 'm.marker_datetime'),
|
|
|
|
$query->expr()->isNull('m.marker_datetime')
|
|
|
|
))
|
|
|
|
->groupBy('c.object_id');
|
|
|
|
|
|
|
|
if ($verb !== '') {
|
|
|
|
$query->andWhere($query->expr()->eq('c.verb', $query->createNamedParameter($verb)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $query->execute();
|
|
|
|
$unreadComments = array_fill_keys($objectIds, 0);
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
$unreadComments[$row['object_id']] = (int) $row['num_comments'];
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
return $unreadComments;
|
|
|
|
}
|
|
|
|
|
2020-10-21 10:15:38 +03:00
|
|
|
/**
|
|
|
|
* @param string $objectType
|
|
|
|
* @param string $objectId
|
|
|
|
* @param int $lastRead
|
|
|
|
* @param string $verb
|
|
|
|
* @return int
|
|
|
|
* @since 21.0.0
|
|
|
|
*/
|
|
|
|
public function getNumberOfCommentsForObjectSinceComment(string $objectType, string $objectId, int $lastRead, string $verb = ''): int {
|
|
|
|
$query = $this->dbConn->getQueryBuilder();
|
|
|
|
$query->select($query->func()->count('id', 'num_messages'))
|
|
|
|
->from('comments')
|
|
|
|
->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
|
|
|
|
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
|
|
|
|
->andWhere($query->expr()->gt('id', $query->createNamedParameter($lastRead)));
|
|
|
|
|
|
|
|
if ($verb !== '') {
|
|
|
|
$query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $query->execute();
|
|
|
|
$data = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
return (int) ($data['num_messages'] ?? 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $objectType
|
|
|
|
* @param string $objectId
|
|
|
|
* @param \DateTime $beforeDate
|
|
|
|
* @param string $verb
|
|
|
|
* @return int
|
|
|
|
* @since 21.0.0
|
|
|
|
*/
|
|
|
|
public function getLastCommentBeforeDate(string $objectType, string $objectId, \DateTime $beforeDate, string $verb = ''): int {
|
|
|
|
$query = $this->dbConn->getQueryBuilder();
|
|
|
|
$query->select('id')
|
|
|
|
->from('comments')
|
|
|
|
->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
|
|
|
|
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
|
|
|
|
->andWhere($query->expr()->lt('creation_timestamp', $query->createNamedParameter($beforeDate, IQueryBuilder::PARAM_DATE)))
|
|
|
|
->orderBy('creation_timestamp', 'desc');
|
|
|
|
|
|
|
|
if ($verb !== '') {
|
|
|
|
$query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb)));
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $query->execute();
|
|
|
|
$data = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
return (int) ($data['id'] ?? 0);
|
|
|
|
}
|
|
|
|
|
2020-10-21 10:18:51 +03:00
|
|
|
/**
|
|
|
|
* @param string $objectType
|
|
|
|
* @param string $objectId
|
|
|
|
* @param string $verb
|
|
|
|
* @param string $actorType
|
|
|
|
* @param string[] $actors
|
2020-10-21 12:02:05 +03:00
|
|
|
* @return \DateTime[] Map of "string actor" => "\DateTime most recent comment date"
|
|
|
|
* @psalm-return array<string, \DateTime>
|
2020-10-21 10:18:51 +03:00
|
|
|
* @since 21.0.0
|
|
|
|
*/
|
|
|
|
public function getLastCommentDateByActor(
|
|
|
|
string $objectType,
|
|
|
|
string $objectId,
|
|
|
|
string $verb,
|
|
|
|
string $actorType,
|
|
|
|
array $actors
|
|
|
|
): array {
|
|
|
|
$lastComments = [];
|
|
|
|
|
|
|
|
$query = $this->dbConn->getQueryBuilder();
|
|
|
|
$query->select('actor_id')
|
|
|
|
->selectAlias($query->createFunction('MAX(' . $query->getColumnName('creation_timestamp') . ')'), 'last_comment')
|
|
|
|
->from('comments')
|
|
|
|
->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
|
|
|
|
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
|
|
|
|
->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb)))
|
|
|
|
->andWhere($query->expr()->eq('actor_type', $query->createNamedParameter($actorType)))
|
|
|
|
->andWhere($query->expr()->in('actor_id', $query->createNamedParameter($actors, IQueryBuilder::PARAM_STR_ARRAY)))
|
|
|
|
->groupBy('actor_id');
|
|
|
|
|
|
|
|
$result = $query->execute();
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
$lastComments[$row['actor_id']] = $this->timeFactory->getDateTime($row['last_comment']);
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
return $lastComments;
|
|
|
|
}
|
|
|
|
|
2017-03-29 16:04:11 +03:00
|
|
|
/**
|
|
|
|
* Get the number of unread comments for all files in a folder
|
|
|
|
*
|
|
|
|
* @param int $folderId
|
|
|
|
* @param IUser $user
|
|
|
|
* @return array [$fileId => $unreadCount]
|
|
|
|
*/
|
|
|
|
public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) {
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
2018-12-19 23:01:48 +03:00
|
|
|
|
2017-07-22 13:21:00 +03:00
|
|
|
$query = $qb->select('f.fileid')
|
2018-10-19 17:44:28 +03:00
|
|
|
->addSelect($qb->func()->count('c.id', 'num_ids'))
|
2018-12-19 23:01:48 +03:00
|
|
|
->from('filecache', 'f')
|
2020-02-03 18:17:25 +03:00
|
|
|
->leftJoin('f', 'comments', 'c', $qb->expr()->andX(
|
|
|
|
$qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT)),
|
|
|
|
$qb->expr()->eq('c.object_type', $qb->createNamedParameter('files'))
|
2017-03-29 16:04:11 +03:00
|
|
|
))
|
2020-02-03 18:17:25 +03:00
|
|
|
->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX(
|
|
|
|
$qb->expr()->eq('c.object_id', 'm.object_id'),
|
|
|
|
$qb->expr()->eq('m.object_type', $qb->createNamedParameter('files'))
|
2017-03-29 16:04:11 +03:00
|
|
|
))
|
2018-12-19 23:01:48 +03:00
|
|
|
->where(
|
|
|
|
$qb->expr()->andX(
|
|
|
|
$qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId)),
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')),
|
|
|
|
$qb->expr()->isNull('c.object_type')
|
|
|
|
),
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')),
|
|
|
|
$qb->expr()->isNull('m.object_type')
|
|
|
|
),
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID())),
|
|
|
|
$qb->expr()->isNull('m.user_id')
|
|
|
|
),
|
|
|
|
$qb->expr()->orX(
|
|
|
|
$qb->expr()->gt('c.creation_timestamp', 'm.marker_datetime'),
|
|
|
|
$qb->expr()->isNull('m.marker_datetime')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)->groupBy('f.fileid');
|
2017-03-29 16:04:11 +03:00
|
|
|
|
|
|
|
$resultStatement = $query->execute();
|
2017-07-20 21:13:35 +03:00
|
|
|
|
|
|
|
$results = [];
|
|
|
|
while ($row = $resultStatement->fetch()) {
|
|
|
|
$results[$row['fileid']] = (int) $row['num_ids'];
|
|
|
|
}
|
|
|
|
$resultStatement->closeCursor();
|
|
|
|
return $results;
|
2017-03-29 16:04:11 +03:00
|
|
|
}
|
|
|
|
|
2015-11-24 01:53:55 +03:00
|
|
|
/**
|
|
|
|
* creates a new comment and returns it. At this point of time, it is not
|
|
|
|
* saved in the used data storage. Use save() after setting other fields
|
|
|
|
* of the comment (e.g. message or verb).
|
|
|
|
*
|
2016-02-03 21:28:15 +03:00
|
|
|
* @param string $actorType the actor type (e.g. 'users')
|
2015-11-24 01:53:55 +03:00
|
|
|
* @param string $actorId a user id
|
|
|
|
* @param string $objectType the object type the comment is attached to
|
|
|
|
* @param string $objectId the object id the comment is attached to
|
|
|
|
* @return IComment
|
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
|
|
|
public function create($actorType, $actorId, $objectType, $objectId) {
|
|
|
|
$comment = new Comment();
|
|
|
|
$comment
|
|
|
|
->setActor($actorType, $actorId)
|
|
|
|
->setObject($objectType, $objectId);
|
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* permanently deletes the comment specified by the ID
|
|
|
|
*
|
|
|
|
* When the comment has child comments, their parent ID will be changed to
|
|
|
|
* the parent ID of the item that is to be deleted.
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return bool
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
|
|
|
public function delete($id) {
|
2017-03-29 16:04:11 +03:00
|
|
|
if (!is_string($id)) {
|
2015-11-24 01:53:55 +03:00
|
|
|
throw new \InvalidArgumentException('Parameter must be string');
|
|
|
|
}
|
|
|
|
|
2016-02-05 12:45:16 +03:00
|
|
|
try {
|
|
|
|
$comment = $this->get($id);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
// Ignore exceptions, we just don't fire a hook then
|
|
|
|
$comment = null;
|
|
|
|
}
|
|
|
|
|
2015-11-24 01:53:55 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$query = $qb->delete('comments')
|
|
|
|
->where($qb->expr()->eq('id', $qb->createParameter('id')))
|
|
|
|
->setParameter('id', $id);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$affectedRows = $query->execute();
|
|
|
|
$this->uncache($id);
|
|
|
|
} catch (DriverException $e) {
|
2020-10-07 09:57:45 +03:00
|
|
|
$this->logger->error($e->getMessage(), [
|
|
|
|
'exception' => $e,
|
|
|
|
'app' => 'core_comments',
|
|
|
|
]);
|
2015-11-24 01:53:55 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-05 14:39:07 +03:00
|
|
|
|
|
|
|
if ($affectedRows > 0 && $comment instanceof IComment) {
|
2016-05-09 11:02:07 +03:00
|
|
|
$this->sendEvent(CommentsEvent::EVENT_DELETE, $comment);
|
2016-02-05 14:39:07 +03:00
|
|
|
}
|
|
|
|
|
2015-11-24 01:53:55 +03:00
|
|
|
return ($affectedRows > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-05 12:45:16 +03:00
|
|
|
* saves the comment permanently
|
2015-11-24 01:53:55 +03:00
|
|
|
*
|
|
|
|
* if the supplied comment has an empty ID, a new entry comment will be
|
|
|
|
* saved and the instance updated with the new ID.
|
|
|
|
*
|
|
|
|
* Otherwise, an existing comment will be updated.
|
|
|
|
*
|
|
|
|
* Throws NotFoundException when a comment that is to be updated does not
|
|
|
|
* exist anymore at this point of time.
|
|
|
|
*
|
2015-12-08 16:58:18 +03:00
|
|
|
* @param IComment $comment
|
2015-11-24 01:53:55 +03:00
|
|
|
* @return bool
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
2015-12-08 16:58:18 +03:00
|
|
|
public function save(IComment $comment) {
|
2017-03-29 16:04:11 +03:00
|
|
|
if ($this->prepareCommentForDatabaseWrite($comment)->getId() === '') {
|
2015-11-24 01:53:55 +03:00
|
|
|
$result = $this->insert($comment);
|
|
|
|
} else {
|
|
|
|
$result = $this->update($comment);
|
|
|
|
}
|
|
|
|
|
2017-03-29 16:04:11 +03:00
|
|
|
if ($result && !!$comment->getParentId()) {
|
2015-11-24 01:53:55 +03:00
|
|
|
$this->updateChildrenInformation(
|
2017-03-29 16:04:11 +03:00
|
|
|
$comment->getParentId(),
|
|
|
|
$comment->getCreationDateTime()
|
2015-11-24 01:53:55 +03:00
|
|
|
);
|
|
|
|
$this->cache($comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* inserts the provided comment in the database
|
|
|
|
*
|
|
|
|
* @param IComment $comment
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-03-11 14:12:07 +03:00
|
|
|
protected function insert(IComment $comment): bool {
|
|
|
|
try {
|
|
|
|
$result = $this->insertQuery($comment, true);
|
|
|
|
} catch (InvalidFieldNameException $e) {
|
|
|
|
// The reference id field was only added in Nextcloud 19.
|
|
|
|
// In order to not cause too long waiting times on the update,
|
|
|
|
// it was decided to only add it lazy, as it is also not a critical
|
|
|
|
// feature, but only helps to have a better experience while commenting.
|
|
|
|
// So in case the reference_id field is missing,
|
|
|
|
// we simply save the comment without that field.
|
|
|
|
$result = $this->insertQuery($comment, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function insertQuery(IComment $comment, bool $tryWritingReferenceId): bool {
|
2015-11-24 01:53:55 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
2020-03-11 14:12:07 +03:00
|
|
|
|
|
|
|
$values = [
|
|
|
|
'parent_id' => $qb->createNamedParameter($comment->getParentId()),
|
|
|
|
'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()),
|
|
|
|
'children_count' => $qb->createNamedParameter($comment->getChildrenCount()),
|
|
|
|
'actor_type' => $qb->createNamedParameter($comment->getActorType()),
|
|
|
|
'actor_id' => $qb->createNamedParameter($comment->getActorId()),
|
|
|
|
'message' => $qb->createNamedParameter($comment->getMessage()),
|
|
|
|
'verb' => $qb->createNamedParameter($comment->getVerb()),
|
|
|
|
'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'),
|
|
|
|
'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'),
|
|
|
|
'object_type' => $qb->createNamedParameter($comment->getObjectType()),
|
|
|
|
'object_id' => $qb->createNamedParameter($comment->getObjectId()),
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($tryWritingReferenceId) {
|
|
|
|
$values['reference_id'] = $qb->createNamedParameter($comment->getReferenceId());
|
|
|
|
}
|
|
|
|
|
|
|
|
$affectedRows = $qb->insert('comments')
|
|
|
|
->values($values)
|
2015-11-24 01:53:55 +03:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
if ($affectedRows > 0) {
|
2018-01-26 01:06:53 +03:00
|
|
|
$comment->setId((string)$qb->getLastInsertId());
|
2016-05-09 11:02:07 +03:00
|
|
|
$this->sendEvent(CommentsEvent::EVENT_ADD, $comment);
|
2015-11-24 01:53:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $affectedRows > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* updates a Comment data row
|
|
|
|
*
|
|
|
|
* @param IComment $comment
|
|
|
|
* @return bool
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
protected function update(IComment $comment) {
|
2016-10-12 19:06:22 +03:00
|
|
|
// for properly working preUpdate Events we need the old comments as is
|
|
|
|
// in the DB and overcome caching. Also avoid that outdated information stays.
|
|
|
|
$this->uncache($comment->getId());
|
|
|
|
$this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId()));
|
|
|
|
$this->uncache($comment->getId());
|
|
|
|
|
2020-03-11 14:12:07 +03:00
|
|
|
try {
|
|
|
|
$result = $this->updateQuery($comment, true);
|
|
|
|
} catch (InvalidFieldNameException $e) {
|
|
|
|
// See function insert() for explanation
|
|
|
|
$result = $this->updateQuery($comment, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function updateQuery(IComment $comment, bool $tryWritingReferenceId): bool {
|
2015-11-24 01:53:55 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
2020-03-11 14:12:07 +03:00
|
|
|
$qb
|
2015-11-24 01:53:55 +03:00
|
|
|
->update('comments')
|
2017-03-29 16:04:11 +03:00
|
|
|
->set('parent_id', $qb->createNamedParameter($comment->getParentId()))
|
|
|
|
->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId()))
|
|
|
|
->set('children_count', $qb->createNamedParameter($comment->getChildrenCount()))
|
|
|
|
->set('actor_type', $qb->createNamedParameter($comment->getActorType()))
|
|
|
|
->set('actor_id', $qb->createNamedParameter($comment->getActorId()))
|
|
|
|
->set('message', $qb->createNamedParameter($comment->getMessage()))
|
|
|
|
->set('verb', $qb->createNamedParameter($comment->getVerb()))
|
|
|
|
->set('creation_timestamp', $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'))
|
|
|
|
->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'))
|
|
|
|
->set('object_type', $qb->createNamedParameter($comment->getObjectType()))
|
2020-03-11 14:12:07 +03:00
|
|
|
->set('object_id', $qb->createNamedParameter($comment->getObjectId()));
|
|
|
|
|
|
|
|
if ($tryWritingReferenceId) {
|
|
|
|
$qb->set('reference_id', $qb->createNamedParameter($comment->getReferenceId()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$affectedRows = $qb->where($qb->expr()->eq('id', $qb->createNamedParameter($comment->getId())))
|
2015-11-24 01:53:55 +03:00
|
|
|
->execute();
|
|
|
|
|
2017-03-29 16:04:11 +03:00
|
|
|
if ($affectedRows === 0) {
|
2015-11-24 01:53:55 +03:00
|
|
|
throw new NotFoundException('Comment to update does ceased to exist');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $affectedRows > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* removes references to specific actor (e.g. on user delete) of a comment.
|
|
|
|
* The comment itself must not get lost/deleted.
|
|
|
|
*
|
2016-02-03 21:28:15 +03:00
|
|
|
* @param string $actorType the actor type (e.g. 'users')
|
2015-11-24 01:53:55 +03:00
|
|
|
* @param string $actorId a user id
|
|
|
|
* @return boolean
|
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
|
|
|
public function deleteReferencesOfActor($actorType, $actorId) {
|
|
|
|
$this->checkRoleParameters('Actor', $actorType, $actorId);
|
|
|
|
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$affectedRows = $qb
|
|
|
|
->update('comments')
|
2017-03-29 16:04:11 +03:00
|
|
|
->set('actor_type', $qb->createNamedParameter(ICommentsManager::DELETED_USER))
|
|
|
|
->set('actor_id', $qb->createNamedParameter(ICommentsManager::DELETED_USER))
|
2015-11-24 01:53:55 +03:00
|
|
|
->where($qb->expr()->eq('actor_type', $qb->createParameter('type')))
|
|
|
|
->andWhere($qb->expr()->eq('actor_id', $qb->createParameter('id')))
|
|
|
|
->setParameter('type', $actorType)
|
|
|
|
->setParameter('id', $actorId)
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->commentsCache = [];
|
|
|
|
|
|
|
|
return is_int($affectedRows);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* deletes all comments made of a specific object (e.g. on file delete)
|
|
|
|
*
|
2016-02-03 21:28:15 +03:00
|
|
|
* @param string $objectType the object type (e.g. 'files')
|
2015-11-24 01:53:55 +03:00
|
|
|
* @param string $objectId e.g. the file id
|
|
|
|
* @return boolean
|
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
|
|
|
public function deleteCommentsAtObject($objectType, $objectId) {
|
|
|
|
$this->checkRoleParameters('Object', $objectType, $objectId);
|
|
|
|
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$affectedRows = $qb
|
|
|
|
->delete('comments')
|
|
|
|
->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
|
|
|
|
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
|
|
|
|
->setParameter('type', $objectType)
|
|
|
|
->setParameter('id', $objectId)
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->commentsCache = [];
|
|
|
|
|
|
|
|
return is_int($affectedRows);
|
|
|
|
}
|
2016-01-27 20:30:09 +03:00
|
|
|
|
2016-01-29 00:59:48 +03:00
|
|
|
/**
|
|
|
|
* deletes the read markers for the specified user
|
|
|
|
*
|
|
|
|
* @param \OCP\IUser $user
|
|
|
|
* @return bool
|
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
|
|
|
public function deleteReadMarksFromUser(IUser $user) {
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$query = $qb->delete('comments_read_markers')
|
|
|
|
->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
|
|
|
|
->setParameter('user_id', $user->getUID());
|
|
|
|
|
|
|
|
try {
|
|
|
|
$affectedRows = $query->execute();
|
|
|
|
} catch (DriverException $e) {
|
2020-10-07 09:57:45 +03:00
|
|
|
$this->logger->error($e->getMessage(), [
|
|
|
|
'exception' => $e,
|
|
|
|
'app' => 'core_comments',
|
|
|
|
]);
|
2016-01-29 00:59:48 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return ($affectedRows > 0);
|
|
|
|
}
|
|
|
|
|
2016-01-27 20:30:09 +03:00
|
|
|
/**
|
|
|
|
* sets the read marker for a given file to the specified date for the
|
|
|
|
* provided user
|
|
|
|
*
|
|
|
|
* @param string $objectType
|
|
|
|
* @param string $objectId
|
|
|
|
* @param \DateTime $dateTime
|
|
|
|
* @param IUser $user
|
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
|
|
|
public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) {
|
|
|
|
$this->checkRoleParameters('Object', $objectType, $objectId);
|
2016-01-29 00:59:48 +03:00
|
|
|
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$values = [
|
2017-03-29 16:04:11 +03:00
|
|
|
'user_id' => $qb->createNamedParameter($user->getUID()),
|
2016-01-29 00:59:48 +03:00
|
|
|
'marker_datetime' => $qb->createNamedParameter($dateTime, 'datetime'),
|
2017-03-29 16:04:11 +03:00
|
|
|
'object_type' => $qb->createNamedParameter($objectType),
|
|
|
|
'object_id' => $qb->createNamedParameter($objectId),
|
2016-01-29 00:59:48 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
// Strategy: try to update, if this does not return affected rows, do an insert.
|
|
|
|
$affectedRows = $qb
|
|
|
|
->update('comments_read_markers')
|
2017-03-29 16:04:11 +03:00
|
|
|
->set('user_id', $values['user_id'])
|
2016-02-02 00:45:52 +03:00
|
|
|
->set('marker_datetime', $values['marker_datetime'])
|
2017-03-29 16:04:11 +03:00
|
|
|
->set('object_type', $values['object_type'])
|
|
|
|
->set('object_id', $values['object_id'])
|
2016-02-02 00:45:52 +03:00
|
|
|
->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
|
|
|
|
->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
|
|
|
|
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
|
2016-02-29 11:44:40 +03:00
|
|
|
->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR)
|
|
|
|
->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR)
|
|
|
|
->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR)
|
2016-01-29 00:59:48 +03:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
if ($affectedRows > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb->insert('comments_read_markers')
|
|
|
|
->values($values)
|
|
|
|
->execute();
|
2016-01-27 20:30:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the read marker for a given file to the specified date for the
|
|
|
|
* provided user. It returns null, when the marker is not present, i.e.
|
|
|
|
* no comments were marked as read.
|
|
|
|
*
|
|
|
|
* @param string $objectType
|
|
|
|
* @param string $objectId
|
|
|
|
* @param IUser $user
|
|
|
|
* @return \DateTime|null
|
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
|
|
|
public function getReadMark($objectType, $objectId, IUser $user) {
|
2016-01-29 00:59:48 +03:00
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$resultStatement = $qb->select('marker_datetime')
|
|
|
|
->from('comments_read_markers')
|
|
|
|
->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
|
|
|
|
->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
|
|
|
|
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
|
2016-02-29 11:44:40 +03:00
|
|
|
->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR)
|
|
|
|
->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR)
|
|
|
|
->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR)
|
2016-01-29 00:59:48 +03:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$data = $resultStatement->fetch();
|
|
|
|
$resultStatement->closeCursor();
|
2017-03-29 16:04:11 +03:00
|
|
|
if (!$data || is_null($data['marker_datetime'])) {
|
2016-01-29 00:59:48 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new \DateTime($data['marker_datetime']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* deletes the read markers on the specified object
|
|
|
|
*
|
|
|
|
* @param string $objectType
|
|
|
|
* @param string $objectId
|
|
|
|
* @return bool
|
|
|
|
* @since 9.0.0
|
|
|
|
*/
|
|
|
|
public function deleteReadMarksOnObject($objectType, $objectId) {
|
2016-01-27 20:30:09 +03:00
|
|
|
$this->checkRoleParameters('Object', $objectType, $objectId);
|
2016-01-29 00:59:48 +03:00
|
|
|
|
|
|
|
$qb = $this->dbConn->getQueryBuilder();
|
|
|
|
$query = $qb->delete('comments_read_markers')
|
|
|
|
->where($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
|
|
|
|
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
|
|
|
|
->setParameter('object_type', $objectType)
|
|
|
|
->setParameter('object_id', $objectId);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$affectedRows = $query->execute();
|
|
|
|
} catch (DriverException $e) {
|
2020-10-07 09:57:45 +03:00
|
|
|
$this->logger->error($e->getMessage(), [
|
|
|
|
'exception' => $e,
|
|
|
|
'app' => 'core_comments',
|
|
|
|
]);
|
2016-01-29 00:59:48 +03:00
|
|
|
return false;
|
2016-01-27 20:30:09 +03:00
|
|
|
}
|
2016-01-29 00:59:48 +03:00
|
|
|
return ($affectedRows > 0);
|
2016-01-27 20:30:09 +03:00
|
|
|
}
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* registers an Entity to the manager, so event notifications can be send
|
|
|
|
* to consumers of the comments infrastructure
|
|
|
|
*
|
|
|
|
* @param \Closure $closure
|
|
|
|
*/
|
|
|
|
public function registerEventHandler(\Closure $closure) {
|
|
|
|
$this->eventHandlerClosures[] = $closure;
|
|
|
|
$this->eventHandlers = [];
|
|
|
|
}
|
|
|
|
|
2016-10-16 21:28:36 +03:00
|
|
|
/**
|
|
|
|
* registers a method that resolves an ID to a display name for a given type
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param \Closure $closure
|
|
|
|
* @throws \OutOfBoundsException
|
2016-11-15 20:51:52 +03:00
|
|
|
* @since 11.0.0
|
2016-10-16 21:28:36 +03:00
|
|
|
*
|
|
|
|
* Only one resolver shall be registered per type. Otherwise a
|
|
|
|
* \OutOfBoundsException has to thrown.
|
|
|
|
*/
|
|
|
|
public function registerDisplayNameResolver($type, \Closure $closure) {
|
2017-03-29 16:04:11 +03:00
|
|
|
if (!is_string($type)) {
|
2016-10-16 21:28:36 +03:00
|
|
|
throw new \InvalidArgumentException('String expected.');
|
|
|
|
}
|
2017-03-29 16:04:11 +03:00
|
|
|
if (isset($this->displayNameResolvers[$type])) {
|
2016-10-16 21:28:36 +03:00
|
|
|
throw new \OutOfBoundsException('Displayname resolver for this type already registered');
|
|
|
|
}
|
|
|
|
$this->displayNameResolvers[$type] = $closure;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* resolves a given ID of a given Type to a display name.
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string $id
|
|
|
|
* @return string
|
|
|
|
* @throws \OutOfBoundsException
|
2016-11-15 20:51:52 +03:00
|
|
|
* @since 11.0.0
|
2016-10-16 21:28:36 +03:00
|
|
|
*
|
|
|
|
* If a provided type was not registered, an \OutOfBoundsException shall
|
|
|
|
* be thrown. It is upon the resolver discretion what to return of the
|
|
|
|
* provided ID is unknown. It must be ensured that a string is returned.
|
|
|
|
*/
|
|
|
|
public function resolveDisplayName($type, $id) {
|
2017-03-29 16:04:11 +03:00
|
|
|
if (!is_string($type)) {
|
2016-10-16 21:28:36 +03:00
|
|
|
throw new \InvalidArgumentException('String expected.');
|
|
|
|
}
|
2017-03-29 16:04:11 +03:00
|
|
|
if (!isset($this->displayNameResolvers[$type])) {
|
2016-10-16 21:28:36 +03:00
|
|
|
throw new \OutOfBoundsException('No Displayname resolver for this type registered');
|
|
|
|
}
|
|
|
|
return (string)$this->displayNameResolvers[$type]($id);
|
|
|
|
}
|
|
|
|
|
2016-05-09 11:02:07 +03:00
|
|
|
/**
|
|
|
|
* returns valid, registered entities
|
|
|
|
*
|
|
|
|
* @return \OCP\Comments\ICommentsEventHandler[]
|
|
|
|
*/
|
|
|
|
private function getEventHandlers() {
|
2017-03-29 16:04:11 +03:00
|
|
|
if (!empty($this->eventHandlers)) {
|
2016-05-09 11:02:07 +03:00
|
|
|
return $this->eventHandlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->eventHandlers = [];
|
|
|
|
foreach ($this->eventHandlerClosures as $name => $closure) {
|
|
|
|
$entity = $closure();
|
|
|
|
if (!($entity instanceof ICommentsEventHandler)) {
|
|
|
|
throw new \InvalidArgumentException('The given entity does not implement the ICommentsEntity interface');
|
|
|
|
}
|
|
|
|
$this->eventHandlers[$name] = $entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->eventHandlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sends notifications to the registered entities
|
|
|
|
*
|
|
|
|
* @param $eventType
|
|
|
|
* @param IComment $comment
|
|
|
|
*/
|
|
|
|
private function sendEvent($eventType, IComment $comment) {
|
|
|
|
$entities = $this->getEventHandlers();
|
|
|
|
$event = new CommentsEvent($eventType, $comment);
|
|
|
|
foreach ($entities as $entity) {
|
|
|
|
$entity->handle($event);
|
|
|
|
}
|
|
|
|
}
|
2020-10-19 10:49:04 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the Comments app into the page
|
|
|
|
*
|
|
|
|
* @since 21.0.0
|
|
|
|
*/
|
|
|
|
public function load(): void {
|
|
|
|
$this->initialStateService->provideInitialState(Application::APP_ID, 'max-message-length', IComment::MAX_MESSAGE_LENGTH);
|
|
|
|
Util::addScript(Application::APP_ID, 'comments-app');
|
|
|
|
}
|
2015-11-24 01:53:55 +03:00
|
|
|
}
|