diff options
author | Joas Schilling <213943+nickvergessen@users.noreply.github.com> | 2022-01-28 16:41:42 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-28 16:41:42 +0100 |
commit | e8c66d0f899a252ec56edcb2bfe31b8bfcc89a30 (patch) | |
tree | 8fc953e12aa25e4a999a25197e701647081375ec /lib/private/Comments/Manager.php | |
parent | c2f21b5b98e6eb6ce775e23535cac70cd0b972f8 (diff) | |
parent | d2833a6552725dd664bd7e2fcab577ee8e7a8eec (diff) | |
download | nextcloud-server-e8c66d0f899a252ec56edcb2bfe31b8bfcc89a30.tar.gz nextcloud-server-e8c66d0f899a252ec56edcb2bfe31b8bfcc89a30.zip |
Merge pull request #30794 from nextcloud/techdebt/noid/allow-to-get-comments-with-verb
Allow to get comments with a given set of verbs
Diffstat (limited to 'lib/private/Comments/Manager.php')
-rw-r--r-- | lib/private/Comments/Manager.php | 55 |
1 files changed, 41 insertions, 14 deletions
diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php index e87ac5cd5cc..554a2485965 100644 --- a/lib/private/Comments/Manager.php +++ b/lib/private/Comments/Manager.php @@ -431,6 +431,37 @@ class Manager implements ICommentsManager { 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 = []; $query = $this->dbConn->getQueryBuilder(); @@ -445,6 +476,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, @@ -700,18 +735,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, []); } /** @@ -728,8 +752,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(); |