]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add comments expire date
authorVitor Mattos <vitor@php.rio>
Mon, 13 Jun 2022 15:22:36 +0000 (12:22 -0300)
committerVitor Mattos <vitor@php.rio>
Wed, 15 Jun 2022 14:58:29 +0000 (11:58 -0300)
https://github.com/nextcloud/spreed/pull/7327

Signed-off-by: Vitor Mattos <vitor@php.rio>
core/Migrations/Version25000Date20220602190540.php [new file with mode: 0644]
lib/composer/composer/autoload_classmap.php
lib/composer/composer/autoload_static.php
lib/private/Comments/Comment.php
lib/private/Comments/Manager.php
lib/public/Comments/IComment.php
lib/public/Comments/ICommentsManager.php
tests/lib/Comments/FakeManager.php
tests/lib/Comments/ManagerTest.php

diff --git a/core/Migrations/Version25000Date20220602190540.php b/core/Migrations/Version25000Date20220602190540.php
new file mode 100644 (file)
index 0000000..3a00423
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2022 Vitor Mattos <vitor@php.rio>
+ *
+ * @author Vitor Mattos <vitor@php.rio>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OC\Core\Migrations;
+
+use Closure;
+use OCP\DB\ISchemaWrapper;
+use OCP\DB\Types;
+use OCP\Migration\IOutput;
+use OCP\Migration\SimpleMigrationStep;
+
+class Version25000Date20220602190540 extends SimpleMigrationStep {
+
+       /**
+        * @param IOutput $output
+        * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
+        * @param array $options
+        * @return null|ISchemaWrapper
+        */
+       public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
+               /** @var ISchemaWrapper $schema */
+               $schema = $schemaClosure();
+
+               $comments = $schema->getTable('comments');
+               if (!$comments->hasColumn('expire_date')) {
+                       $comments->addColumn('expire_date', Types::DATETIME, [
+                               'notnull' => false,
+                       ]);
+                       $comments->addIndex(['expire_date'], 'expire_date');
+                       return $schema;
+               }
+               return null;
+       }
+}
index 1edc39c52f2f334dc665383abde8d5aaf36e4b7d..eae31d414493ab0cde5451f3b293810df39ba90d 100644 (file)
@@ -1036,6 +1036,7 @@ return array(
     'OC\\Core\\Migrations\\Version24000Date20220404230027' => $baseDir . '/core/Migrations/Version24000Date20220404230027.php',
     'OC\\Core\\Migrations\\Version24000Date20220425072957' => $baseDir . '/core/Migrations/Version24000Date20220425072957.php',
     'OC\\Core\\Migrations\\Version25000Date20220515204012' => $baseDir . '/core/Migrations/Version25000Date20220515204012.php',
+    'OC\\Core\\Migrations\\Version25000Date20220602190540' => $baseDir . '/core/Migrations/Version25000Date20220602190540.php',
     'OC\\Core\\Notification\\CoreNotifier' => $baseDir . '/core/Notification/CoreNotifier.php',
     'OC\\Core\\Service\\LoginFlowV2Service' => $baseDir . '/core/Service/LoginFlowV2Service.php',
     'OC\\DB\\Adapter' => $baseDir . '/lib/private/DB/Adapter.php',
index 2efd6effc9111034c11017c70546783ebce0fd27..8a2a72713ecd425d36919b342baf0f9cfeb63929 100644 (file)
@@ -1069,6 +1069,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
         'OC\\Core\\Migrations\\Version24000Date20220404230027' => __DIR__ . '/../../..' . '/core/Migrations/Version24000Date20220404230027.php',
         'OC\\Core\\Migrations\\Version24000Date20220425072957' => __DIR__ . '/../../..' . '/core/Migrations/Version24000Date20220425072957.php',
         'OC\\Core\\Migrations\\Version25000Date20220515204012' => __DIR__ . '/../../..' . '/core/Migrations/Version25000Date20220515204012.php',
+        'OC\\Core\\Migrations\\Version25000Date20220602190540' => __DIR__ . '/../../..' . '/core/Migrations/Version25000Date20220602190540.php',
         'OC\\Core\\Notification\\CoreNotifier' => __DIR__ . '/../../..' . '/core/Notification/CoreNotifier.php',
         'OC\\Core\\Service\\LoginFlowV2Service' => __DIR__ . '/../../..' . '/core/Service/LoginFlowV2Service.php',
         'OC\\DB\\Adapter' => __DIR__ . '/../../..' . '/lib/private/DB/Adapter.php',
index 2b338efc75f839ac6948362b2b1a72504322eeab..0128dc8defd77842a7bfa7e39dfe8c469030c6b9 100644 (file)
@@ -45,6 +45,7 @@ class Comment implements IComment {
                'creationDT' => null,
                'latestChildDT' => null,
                'reactions' => null,
+               'expire_date' => null,
        ];
 
        /**
@@ -446,6 +447,21 @@ class Comment implements IComment {
                return $this;
        }
 
+       /**
+        * @inheritDoc
+        */
+       public function setExpireDate(?\DateTime $dateTime): IComment {
+               $this->data['expire_date'] = $dateTime;
+               return $this;
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function getExpireDate(): ?\DateTime {
+               return $this->data['expire_date'];
+       }
+
        /**
         * sets the comment data based on an array with keys as taken from the
         * database.
index b7532222c33c7b007f21637b972976b2f1094435..f21f9ec76b23fc1daeff38e08996399dfe3fd129 100644 (file)
@@ -107,6 +107,9 @@ class Manager implements ICommentsManager {
                if (!is_null($data['latest_child_timestamp'])) {
                        $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']);
                }
+               if (!is_null($data['expire_date'])) {
+                       $data['expire_date'] = new \DateTime($data['expire_date']);
+               }
                $data['children_count'] = (int)$data['children_count'];
                $data['reference_id'] = $data['reference_id'] ?? null;
                if ($this->supportReactions()) {
@@ -1203,6 +1206,7 @@ class Manager implements ICommentsManager {
                        'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'),
                        'object_type' => $qb->createNamedParameter($comment->getObjectType()),
                        'object_id' => $qb->createNamedParameter($comment->getObjectId()),
+                       'expire_date' => $qb->createNamedParameter($comment->getExpireDate(), 'datetime'),
                ];
 
                if ($tryWritingReferenceId) {
@@ -1642,4 +1646,21 @@ class Manager implements ICommentsManager {
                $this->initialStateService->provideInitialState('comments', 'max-message-length', IComment::MAX_MESSAGE_LENGTH);
                Util::addScript('comments', 'comments-app');
        }
+
+       /**
+        * @inheritDoc
+        */
+       public function deleteMessageExpiredAtObject(string $objectType, string $objectId): bool {
+               $qb = $this->dbConn->getQueryBuilder();
+               $affectedRows = $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();
+
+               $this->commentsCache = [];
+
+               return $affectedRows > 0;
+       }
 }
index 8465eaf49f48def41d8f63c94df5927a7a0315ca..44d294bb07ce306beb5b531094cc6fc9f1822fed 100644 (file)
@@ -299,4 +299,21 @@ interface IComment {
         * @since 24.0.0
         */
        public function setReactions(?array $reactions): IComment;
+
+       /**
+        * Set message expire date
+        *
+        * @param \DateTime|null $dateTime
+        * @return IComment
+        * @since 25.0.0
+        */
+       public function setExpireDate(?\DateTime $dateTime): IComment;
+
+       /**
+        * Get message expire date
+        *
+        * @return ?\DateTime
+        * @since 25.0.0
+        */
+       public function getExpireDate(): ?\DateTime;
 }
index c34bd4718cc20b5232f5e2f9280a71f12b7fdb20..814ca3e8f9c7e966c58783703f1a303f4e148025 100644 (file)
@@ -482,4 +482,15 @@ interface ICommentsManager {
         * @since 21.0.0
         */
        public function load(): void;
+
+       /**
+        * Delete comments with field expire_date less than current date
+        * 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
+        * @return boolean true if at least one row was deleted
+        * @since 25.0.0
+        */
+       public function deleteMessageExpiredAtObject(string $objectType, string $objectId): bool;
 }
index 5406df96a96c48d2695e4397e18da63714eb70c3..0d615fd2632241f842cdc7fdc1fa72582cef16d8 100644 (file)
@@ -141,4 +141,8 @@ class FakeManager implements ICommentsManager {
        public function getLastCommentDateByActor(string $objectType, string $objectId, string $verb, string $actorType, array $actors): array {
                return [];
        }
+
+       public function deleteMessageExpiredAtObject(string $objectType, string $objectId): bool {
+               return true;
+       }
 }
index 6bcd0dec8edc3c88d923132c2fef625350ee26d8..29e08b55e10342f6947376a34d638f4fada8b3b1 100644 (file)
@@ -14,6 +14,7 @@ use OCP\IConfig;
 use OCP\IDBConnection;
 use OCP\IInitialStateService;
 use OCP\IUser;
+use OCP\Server;
 use Psr\Log\LoggerInterface;
 use Test\TestCase;
 
@@ -37,7 +38,7 @@ class ManagerTest extends TestCase {
                $this->connection->prepare($sql)->execute();
        }
 
-       protected function addDatabaseEntry($parentId, $topmostParentId, $creationDT = null, $latestChildDT = null, $objectId = null) {
+       protected function addDatabaseEntry($parentId, $topmostParentId, $creationDT = null, $latestChildDT = null, $objectId = null, $expireDate = null) {
                if (is_null($creationDT)) {
                        $creationDT = new \DateTime();
                }
@@ -63,6 +64,7 @@ class ManagerTest extends TestCase {
                                'latest_child_timestamp' => $qb->createNamedParameter($latestChildDT, 'datetime'),
                                'object_type' => $qb->createNamedParameter('files'),
                                'object_id' => $qb->createNamedParameter($objectId),
+                               'expire_date' => $qb->createNamedParameter($expireDate, 'datetime'),
                        ])
                        ->execute();
 
@@ -701,6 +703,51 @@ class ManagerTest extends TestCase {
                $this->assertTrue($wasSuccessful);
        }
 
+       public function testDeleteMessageExpiredAtObject(): 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'));
+               $ids[] = $this->addDatabaseEntry(0, 0, null, null, null, new \DateTime('+2 hours'));
+               $ids[] = $this->addDatabaseEntry(0, 0, null, null, null, new \DateTime('-2 hours'));
+               $ids[] = $this->addDatabaseEntry(0, 0, null, null, null, new \DateTime('-2 hours'));
+               $ids[] = $this->addDatabaseEntry(0, 0, null, null, null, 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)
+               );
+
+               // just to make sure they are really set, with correct actor data
+               $comment = $manager->get(strval($ids[1]));
+               $this->assertSame($comment->getObjectType(), 'files');
+               $this->assertSame($comment->getObjectId(), 'file64');
+
+               $deleted = $manager->deleteMessageExpiredAtObject('files', 'file64');
+               $this->assertTrue($deleted);
+
+               $deleted = 0;
+               $exists = 0;
+               foreach ($ids as $id) {
+                       try {
+                               $manager->get(strval($id));
+                               $exists++;
+                       } catch (NotFoundException $e) {
+                               $deleted++;
+                       }
+               }
+               $this->assertSame($exists, 3);
+               $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->deleteMessageExpiredAtObject('files', 'file64');
+               $this->assertFalse($deleted);
+       }
+
        public function testSetMarkRead() {
                /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
                $user = $this->createMock(IUser::class);