From f865c3ad21e03483a6f16b90a4383faef0356499 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 17:27:02 -0700 Subject: [PATCH] enh: handle node deleted Signed-off-by: Christopher Ng --- .../composer/composer/autoload_classmap.php | 1 + .../composer/composer/autoload_static.php | 1 + .../composer/composer/installed.php | 12 ++--- .../lib/AppInfo/Application.php | 3 ++ .../files_reminders/lib/Db/ReminderMapper.php | 15 ++++++ .../lib/Listener/NodeDeletedListener.php | 47 +++++++++++++++++++ .../lib/Service/ReminderService.php | 8 ++++ 7 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 apps/files_reminders/lib/Listener/NodeDeletedListener.php diff --git a/apps/files_reminders/composer/composer/autoload_classmap.php b/apps/files_reminders/composer/composer/autoload_classmap.php index 29f781d0006..fb17eb7a1b6 100644 --- a/apps/files_reminders/composer/composer/autoload_classmap.php +++ b/apps/files_reminders/composer/composer/autoload_classmap.php @@ -16,6 +16,7 @@ return array( 'OCA\\FilesReminders\\Db\\ReminderMapper' => $baseDir . '/../lib/Db/ReminderMapper.php', 'OCA\\FilesReminders\\Exception\\NodeNotFoundException' => $baseDir . '/../lib/Exception/NodeNotFoundException.php', 'OCA\\FilesReminders\\Exception\\UserNotFoundException' => $baseDir . '/../lib/Exception/UserNotFoundException.php', + 'OCA\\FilesReminders\\Listener\\NodeDeletedListener' => $baseDir . '/../lib/Listener/NodeDeletedListener.php', 'OCA\\FilesReminders\\Migration\\Version10000Date20230725162149' => $baseDir . '/../lib/Migration/Version10000Date20230725162149.php', 'OCA\\FilesReminders\\Model\\RichReminder' => $baseDir . '/../lib/Model/RichReminder.php', 'OCA\\FilesReminders\\Notification\\Notifier' => $baseDir . '/../lib/Notification/Notifier.php', diff --git a/apps/files_reminders/composer/composer/autoload_static.php b/apps/files_reminders/composer/composer/autoload_static.php index efea8791020..1a2bc0d1bd1 100644 --- a/apps/files_reminders/composer/composer/autoload_static.php +++ b/apps/files_reminders/composer/composer/autoload_static.php @@ -31,6 +31,7 @@ class ComposerStaticInitFilesReminders 'OCA\\FilesReminders\\Db\\ReminderMapper' => __DIR__ . '/..' . '/../lib/Db/ReminderMapper.php', 'OCA\\FilesReminders\\Exception\\NodeNotFoundException' => __DIR__ . '/..' . '/../lib/Exception/NodeNotFoundException.php', 'OCA\\FilesReminders\\Exception\\UserNotFoundException' => __DIR__ . '/..' . '/../lib/Exception/UserNotFoundException.php', + 'OCA\\FilesReminders\\Listener\\NodeDeletedListener' => __DIR__ . '/..' . '/../lib/Listener/NodeDeletedListener.php', 'OCA\\FilesReminders\\Migration\\Version10000Date20230725162149' => __DIR__ . '/..' . '/../lib/Migration/Version10000Date20230725162149.php', 'OCA\\FilesReminders\\Model\\RichReminder' => __DIR__ . '/..' . '/../lib/Model/RichReminder.php', 'OCA\\FilesReminders\\Notification\\Notifier' => __DIR__ . '/..' . '/../lib/Notification/Notifier.php', diff --git a/apps/files_reminders/composer/composer/installed.php b/apps/files_reminders/composer/composer/installed.php index c398e796c32..f120393f41b 100644 --- a/apps/files_reminders/composer/composer/installed.php +++ b/apps/files_reminders/composer/composer/installed.php @@ -1,9 +1,9 @@ array( 'name' => '__root__', - 'pretty_version' => '1.0.0+no-version-set', - 'version' => '1.0.0.0', - 'reference' => NULL, + 'pretty_version' => 'dev-master', + 'version' => 'dev-master', + 'reference' => 'cce2e1543b16a04e6b9fc1ba3f89fbc47f12e773', 'type' => 'library', 'install_path' => __DIR__ . '/../', 'aliases' => array(), @@ -11,9 +11,9 @@ ), 'versions' => array( '__root__' => array( - 'pretty_version' => '1.0.0+no-version-set', - 'version' => '1.0.0.0', - 'reference' => NULL, + 'pretty_version' => 'dev-master', + 'version' => 'dev-master', + 'reference' => 'cce2e1543b16a04e6b9fc1ba3f89fbc47f12e773', 'type' => 'library', 'install_path' => __DIR__ . '/../', 'aliases' => array(), diff --git a/apps/files_reminders/lib/AppInfo/Application.php b/apps/files_reminders/lib/AppInfo/Application.php index 4d418562ef5..1e4813e1ec7 100644 --- a/apps/files_reminders/lib/AppInfo/Application.php +++ b/apps/files_reminders/lib/AppInfo/Application.php @@ -26,11 +26,13 @@ declare(strict_types=1); namespace OCA\FilesReminders\AppInfo; +use OCA\FilesReminders\Listener\NodeDeletedListener; use OCA\FilesReminders\Notification\Notifier; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\Files\Events\Node\NodeDeletedEvent; class Application extends App implements IBootstrap { public const APP_ID = 'files_reminders'; @@ -44,5 +46,6 @@ class Application extends App implements IBootstrap { public function register(IRegistrationContext $context): void { $context->registerNotifierService(Notifier::class); + $context->registerEventListener(NodeDeletedEvent::class, NodeDeletedListener::class); } } diff --git a/apps/files_reminders/lib/Db/ReminderMapper.php b/apps/files_reminders/lib/Db/ReminderMapper.php index cb079320e5f..97f7ed0900b 100644 --- a/apps/files_reminders/lib/Db/ReminderMapper.php +++ b/apps/files_reminders/lib/Db/ReminderMapper.php @@ -29,6 +29,7 @@ namespace OCA\FilesReminders\Db; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\Files\Node; use OCP\IDBConnection; use OCP\IUser; @@ -105,6 +106,20 @@ class ReminderMapper extends QBMapper { return $this->findEntities($qb); } + /** + * @return Reminder[] + */ + public function findAllForNode(Node $node) { + $qb = $this->db->getQueryBuilder(); + + $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') + ->from($this->getTableName()) + ->where($qb->expr()->eq('file_id', $qb->createNamedParameter($node->getId(), IQueryBuilder::PARAM_INT))) + ->orderBy('due_date', 'ASC'); + + return $this->findEntities($qb); + } + /** * @return Reminder[] */ diff --git a/apps/files_reminders/lib/Listener/NodeDeletedListener.php b/apps/files_reminders/lib/Listener/NodeDeletedListener.php new file mode 100644 index 00000000000..88aed08bba8 --- /dev/null +++ b/apps/files_reminders/lib/Listener/NodeDeletedListener.php @@ -0,0 +1,47 @@ + + * + * @author Christopher Ng + * + * @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 . + * + */ + +namespace OCA\FilesReminders\Listener; + +use OCA\FilesReminders\Service\ReminderService; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Files\Events\Node\NodeDeletedEvent; + +class NodeDeletedListener implements IEventListener { + public function __construct( + private ReminderService $reminderService, + ) {} + + public function handle(Event $event): void { + if (!($event instanceof NodeDeletedEvent)) { + return; + } + + $node = $event->getNode(); + $this->reminderService->removeAllForNode($node); + } +} diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index 8dbec6d5df7..eec2f1780bd 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -36,6 +36,7 @@ use OCA\FilesReminders\Exception\UserNotFoundException; use OCA\FilesReminders\Model\RichReminder; use OCP\AppFramework\Db\DoesNotExistException; use OCP\Files\IRootFolder; +use OCP\Files\Node; use OCP\IURLGenerator; use OCP\IUser; use OCP\IUserManager; @@ -117,6 +118,13 @@ class ReminderService { $this->reminderMapper->delete($reminder); } + public function removeAllForNode(Node $node): void { + $reminders = $this->reminderMapper->findAllForNode($node); + foreach ($reminders as $reminder) { + $this->reminderMapper->delete($reminder); + } + } + /** * @throws DoesNotExistException * @throws UserNotFoundException -- 2.39.5