diff options
author | Joas Schilling <coding@schilljs.com> | 2020-10-21 10:25:38 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2020-10-28 12:00:45 +0300 |
commit | 7a5fa70ad2cba93e6796cf401e8d254caf605d83 (patch) | |
tree | 0e56634bad9e3fd5da5b3e9c19771f0cffedc565 /lib | |
parent | 9562c6a1b7c749315096614b68b88d8d4924b59b (diff) |
Upstream our comments manager extensions to server
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Chat/CommentsManager.php | 237 |
1 files changed, 0 insertions, 237 deletions
diff --git a/lib/Chat/CommentsManager.php b/lib/Chat/CommentsManager.php index afc979ccb..12a758e86 100644 --- a/lib/Chat/CommentsManager.php +++ b/lib/Chat/CommentsManager.php @@ -25,30 +25,9 @@ namespace OCA\Talk\Chat; use OC\Comments\Comment; use OC\Comments\Manager; -use OCP\AppFramework\Utility\ITimeFactory; use OCP\Comments\IComment; -use OCP\DB\QueryBuilder\IQueryBuilder; -use OCP\IConfig; -use OCP\IDBConnection; -use OCP\IInitialStateService; -use Psr\Log\LoggerInterface; class CommentsManager extends Manager { - - /** @var ITimeFactory */ - protected $timeFactory; - - public function __construct( - IDBConnection $db, - LoggerInterface $logger, - IConfig $config, - IInitialStateService $initialStateService, - ITimeFactory $timeFactory - ) { - parent::__construct($db, $logger, $config, $initialStateService); - $this->timeFactory = $timeFactory; - } - /** * @param array $data * @return IComment @@ -60,220 +39,4 @@ class CommentsManager extends Manager { $comment->setMessage($message, ChatManager::MAX_CHAT_LENGTH); return $comment; } - - /** - * @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. - * @param bool $includeLastKnown - * @return IComment[] - * @return array - */ - public function getForObjectSince( - string $objectType, - string $objectId, - int $lastKnownCommentId, - string $sortDirection = 'asc', - int $limit = 30, - bool $includeLastKnown = false - ): 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); - } - - $lastKnownComment = $lastKnownCommentId > 0 ? $this->getLastKnownComment( - $objectType, - $objectId, - $lastKnownCommentId - ) : null; - if ($lastKnownComment instanceof IComment) { - $lastKnownCommentDateTime = $lastKnownComment->getCreationDateTime(); - if ($sortDirection === 'desc') { - $idComparison = $includeLastKnown ? 'lte' : 'lt'; - $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 - ), - $query->expr()->$idComparison('id', $query->createNamedParameter($lastKnownCommentId)) - ) - ) - ); - } else { - $idComparison = $includeLastKnown ? 'gte' : 'gt'; - $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 - ), - $query->expr()->$idComparison('id', $query->createNamedParameter($lastKnownCommentId)) - ) - ) - ); - } - } - - $resultStatement = $query->execute(); - while ($data = $resultStatement->fetch()) { - $comment = $this->getCommentFromData($data); - $this->cache($comment); - $comments[] = $comment; - } - $resultStatement->closeCursor(); - - return $comments; - } - - /** - * @param string $objectType - * @param string $objectId - * @param string $verb - * @param string $actorType - * @param string[] $actors - * @return array - */ - 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; - } - - public function getNumberOfCommentsForObjectSinceComment(string $objectType, string $objectId, int $lastRead, string $verb = ''): int { - $query = $this->dbConn->getQueryBuilder(); - $query->selectAlias($query->createFunction('COUNT(' . $query->getColumnName('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); - } - - 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); - } - - /** - * Search for comments 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 { - $query = $this->dbConn->getQueryBuilder(); - - $query->select('*') - ->from('comments') - ->where($query->expr()->iLike('message', $query->createNamedParameter( - '%' . $this->dbConn->escapeLikeParameter($search). '%' - ))) - ->orderBy('creation_timestamp', 'DESC') - ->addOrderBy('id', 'DESC') - ->setMaxResults($limit); - - if ($objectType !== '') { - $query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType))); - } - if (!empty($objectIds)) { - $query->andWhere($query->expr()->in('object_id', $query->createNamedParameter($objectIds, IQueryBuilder::PARAM_STR_ARRAY))); - } - if ($verb !== '') { - $query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb))); - } - if ($offset !== 0) { - $query->setFirstResult($offset); - } - - $comments = []; - $result = $query->execute(); - while ($data = $result->fetch()) { - $comment = $this->getCommentFromData($data); - $this->cache($comment); - $comments[] = $comment; - } - $result->closeCursor(); - - return $comments; - } } |