]> source.dussan.org Git - nextcloud-server.git/commitdiff
Allow to get comments with a given set of verbs
authorJoas Schilling <coding@schilljs.com>
Fri, 21 Jan 2022 12:42:04 +0000 (13:42 +0100)
committerJoas Schilling <coding@schilljs.com>
Fri, 21 Jan 2022 12:42:04 +0000 (13:42 +0100)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/Comments/Manager.php
lib/public/Comments/ICommentsManager.php

index d62db2926fb7a8cd5a6e7b4b675add59097069ee..680c04dab03fb95b3a64a76967e708b31d325c71 100644 (file)
@@ -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();
index 9aacf028b4e31d05fc76465c34edeea7a4a1d151..781ec87892cb45cd40a0a8c4a96cc8b95e21bd86 100644 (file)
@@ -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;