From 1ceba648635024c7b198d29239e23595347e7685 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Fri, 21 Jan 2022 13:42:04 +0100 Subject: [PATCH] Allow to get comments with a given set of verbs Signed-off-by: Joas Schilling --- lib/private/Comments/Manager.php | 55 ++++++++++++++++++------ lib/public/Comments/ICommentsManager.php | 24 +++++++++++ 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php index d62db2926fb..680c04dab03 100644 --- a/lib/private/Comments/Manager.php +++ b/lib/private/Comments/Manager.php @@ -411,6 +411,37 @@ class Manager implements ICommentsManager { string $sortDirection = 'asc', int $limit = 30, bool $includeLastKnown = false + ): array { + return $this->getCommentsWithVerbForObjectSinceComment( + $objectType, + $objectId, + [], + $lastKnownCommentId, + $sortDirection, + $limit, + $includeLastKnown + ); + } + + /** + * @param string $objectType the object type, e.g. 'files' + * @param string $objectId the id of the object + * @param string[] $verbs List of verbs to filter by + * @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[] + */ + public function getCommentsWithVerbForObjectSinceComment( + string $objectType, + string $objectId, + array $verbs, + int $lastKnownCommentId, + string $sortDirection = 'asc', + int $limit = 30, + bool $includeLastKnown = false ): array { $comments = []; @@ -426,6 +457,10 @@ class Manager implements ICommentsManager { $query->setMaxResults($limit); } + if (!empty($verbs)) { + $query->andWhere($query->expr()->in('verb', $query->createNamedParameter($verbs, IQueryBuilder::PARAM_STR_ARRAY))); + } + $lastKnownComment = $lastKnownCommentId > 0 ? $this->getLastKnownComment( $objectType, $objectId, @@ -681,18 +716,7 @@ class Manager implements ICommentsManager { return $this->getNumberOfCommentsWithVerbsForObjectSinceComment($objectType, $objectId, $lastRead, [$verb]); } - $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))); - - $result = $query->executeQuery(); - $data = $result->fetch(); - $result->closeCursor(); - - return (int) ($data['num_messages'] ?? 0); + return $this->getNumberOfCommentsWithVerbsForObjectSinceComment($objectType, $objectId, $lastRead, []); } /** @@ -709,8 +733,11 @@ class Manager implements ICommentsManager { ->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))) - ->andWhere($query->expr()->in('verb', $query->createNamedParameter($verbs, IQueryBuilder::PARAM_STR_ARRAY))); + ->andWhere($query->expr()->gt('id', $query->createNamedParameter($lastRead))); + + if (!empty($verbs)) { + $query->andWhere($query->expr()->in('verb', $query->createNamedParameter($verbs, IQueryBuilder::PARAM_STR_ARRAY))); + } $result = $query->executeQuery(); $data = $result->fetch(); diff --git a/lib/public/Comments/ICommentsManager.php b/lib/public/Comments/ICommentsManager.php index 9aacf028b4e..781ec87892c 100644 --- a/lib/public/Comments/ICommentsManager.php +++ b/lib/public/Comments/ICommentsManager.php @@ -131,6 +131,7 @@ interface ICommentsManager { * @param bool $includeLastKnown * @return IComment[] * @since 14.0.0 + * @depreacted 24.0.0 - Use getCommentsWithVerbForObjectSinceComment instead */ public function getForObjectSince( string $objectType, @@ -141,6 +142,28 @@ interface ICommentsManager { bool $includeLastKnown = false ): array; + /** + * @param string $objectType the object type, e.g. 'files' + * @param string $objectId the id of the object + * @param string[] $verbs List of verbs to filter by + * @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[] + * @since 24.0.0 + */ + public function getCommentsWithVerbForObjectSinceComment( + string $objectType, + string $objectId, + array $verbs, + int $lastKnownCommentId, + string $sortDirection = 'asc', + int $limit = 30, + bool $includeLastKnown = false + ): array; + /** * Search for comments with a given content * @@ -198,6 +221,7 @@ interface ICommentsManager { * @param string $verb * @return int * @since 21.0.0 + * @depreacted 24.0.0 - Use getNumberOfCommentsWithVerbsForObjectSinceComment instead */ public function getNumberOfCommentsForObjectSinceComment(string $objectType, string $objectId, int $lastRead, string $verb = ''): int; -- 2.39.5