]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add a function to get the unread count for multiple objects in one go
authorJoas Schilling <coding@schilljs.com>
Fri, 30 Oct 2020 11:10:59 +0000 (12:10 +0100)
committerRoeland Jago Douma <roeland@famdouma.nl>
Wed, 4 Nov 2020 10:01:21 +0000 (11:01 +0100)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/Comments/Manager.php
lib/public/Comments/ICommentsManager.php
tests/lib/Comments/FakeManager.php

index 1830fb00f3c1c840d43fc40734c7f46c3939197a..98c600add209e0e7a96213982e5e4a4d1d499248 100644 (file)
@@ -624,6 +624,46 @@ class Manager implements ICommentsManager {
                return (int)$data[0];
        }
 
+       /**
+        * @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;
+       }
+
        /**
         * @param string $objectType
         * @param string $objectId
index b179b8a746459ea16a3b38f58ef622496b6012c7..c34358ddf939bb1e7020f13ef65b670aa0606e0b 100644 (file)
@@ -180,6 +180,17 @@ interface ICommentsManager {
         */
        public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null, $verb = '');
 
+       /**
+        * @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;
+
        /**
         * @param string $objectType
         * @param string $objectId
index 29fb393436209441c7d570338c925ba62eb7ab78..2434945762c187d5e91e3ebb5c95bc94284ef250 100644 (file)
@@ -82,6 +82,11 @@ class FakeManager implements ICommentsManager {
        public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) {
        }
 
+       public function getNumberOfUnreadCommentsForObjects(string $objectType, array $objectIds, IUser $user, $verb = ''): array {
+               return [];
+       }
+
+
        public function getActorsInTree($id) {
        }