]> source.dussan.org Git - nextcloud-server.git/commitdiff
Allow to expire comments of multiple objects with one call
authorJoas Schilling <coding@schilljs.com>
Fri, 1 Jul 2022 13:53:37 +0000 (15:53 +0200)
committerJoas Schilling <coding@schilljs.com>
Fri, 1 Jul 2022 13:59:29 +0000 (15:59 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
lib/private/Comments/Manager.php
lib/public/Comments/ICommentsManager.php
tests/lib/Comments/ManagerTest.php

index f21f9ec76b23fc1daeff38e08996399dfe3fd129..50b5fabc6a2321341b64fff0c843b0e7648cb012 100644 (file)
@@ -1650,14 +1650,18 @@ class Manager implements ICommentsManager {
        /**
         * @inheritDoc
         */
-       public function deleteMessageExpiredAtObject(string $objectType, string $objectId): bool {
+       public function deleteCommentsExpiredAtObject(string $objectType, string $objectId = ''): bool {
                $qb = $this->dbConn->getQueryBuilder();
-               $affectedRows = $qb->delete('comments')
+               $qb->delete('comments')
                        ->where($qb->expr()->lt('expire_date',
                                $qb->createNamedParameter($this->timeFactory->getDateTime(), IQueryBuilder::PARAM_DATE)))
-                       ->andWhere($qb->expr()->eq('object_type', $qb->createNamedParameter($objectType)))
-                       ->andWhere($qb->expr()->eq('object_id', $qb->createNamedParameter($objectId)))
-                       ->executeStatement();
+                       ->andWhere($qb->expr()->eq('object_type', $qb->createNamedParameter($objectType)));
+
+               if ($objectId !== '') {
+                       $qb->andWhere($qb->expr()->eq('object_id', $qb->createNamedParameter($objectId)));
+               }
+
+               $affectedRows = $qb->executeStatement();
 
                $this->commentsCache = [];
 
index 814ca3e8f9c7e966c58783703f1a303f4e148025..da9e4d76a386ceb514028d01392b585c926b0c8d 100644 (file)
@@ -488,9 +488,9 @@ interface ICommentsManager {
         * Only will delete the message related with the object.
         *
         * @param string $objectType the object type (e.g. 'files')
-        * @param string $objectId e.g. the file id
+        * @param string $objectId e.g. the file id, leave empty to expire on all objects of this type
         * @return boolean true if at least one row was deleted
         * @since 25.0.0
         */
-       public function deleteMessageExpiredAtObject(string $objectType, string $objectId): bool;
+       public function deleteCommentsExpiredAtObject(string $objectType, string $objectId = ''): bool;
 }
index 7c6971476c7113ef6d11761eee8d5d8c3d2da726..ae91efb8498968b1081717c3b27eb699d9e20965 100644 (file)
@@ -703,7 +703,7 @@ class ManagerTest extends TestCase {
                $this->assertTrue($wasSuccessful);
        }
 
-       public function testDeleteMessageExpiredAtObject(): void {
+       public function testDeleteCommentsExpiredAtObjectTypeAndId(): void {
                $ids = [];
                $ids[] = $this->addDatabaseEntry(0, 0, null, null, null, new \DateTime('+2 hours'));
                $ids[] = $this->addDatabaseEntry(0, 0, null, null, null, new \DateTime('+2 hours'));
@@ -726,7 +726,7 @@ class ManagerTest extends TestCase {
                $this->assertSame($comment->getObjectType(), 'files');
                $this->assertSame($comment->getObjectId(), 'file64');
 
-               $deleted = $manager->deleteMessageExpiredAtObject('files', 'file64');
+               $deleted = $manager->deleteCommentsExpiredAtObject('files', 'file64');
                $this->assertTrue($deleted);
 
                $deleted = 0;
@@ -744,7 +744,44 @@ class ManagerTest extends TestCase {
 
                // actor info is gone from DB, but when database interaction is alright,
                // we still expect to get true back
-               $deleted = $manager->deleteMessageExpiredAtObject('files', 'file64');
+               $deleted = $manager->deleteCommentsExpiredAtObject('files', 'file64');
+               $this->assertFalse($deleted);
+       }
+
+       public function testDeleteCommentsExpiredAtObjectType(): void {
+               $ids = [];
+               $ids[] = $this->addDatabaseEntry(0, 0, null, null, 'file1', new \DateTime('-2 hours'));
+               $ids[] = $this->addDatabaseEntry(0, 0, null, null, 'file2', new \DateTime('-2 hours'));
+               $ids[] = $this->addDatabaseEntry(0, 0, null, null, 'file3', new \DateTime('-2 hours'));
+
+               $manager = new Manager(
+                       $this->connection,
+                       $this->createMock(LoggerInterface::class),
+                       $this->createMock(IConfig::class),
+                       Server::get(ITimeFactory::class),
+                       new EmojiHelper($this->connection),
+                       $this->createMock(IInitialStateService::class)
+               );
+
+               $deleted = $manager->deleteCommentsExpiredAtObject('files');
+               $this->assertTrue($deleted);
+
+               $deleted = 0;
+               $exists = 0;
+               foreach ($ids as $id) {
+                       try {
+                               $manager->get((string) $id);
+                               $exists++;
+                       } catch (NotFoundException $e) {
+                               $deleted++;
+                       }
+               }
+               $this->assertSame($exists, 0);
+               $this->assertSame($deleted, 3);
+
+               // actor info is gone from DB, but when database interaction is alright,
+               // we still expect to get true back
+               $deleted = $manager->deleteCommentsExpiredAtObject('files');
                $this->assertFalse($deleted);
        }