diff options
author | Christopher Ng <chrng8@gmail.com> | 2023-07-31 17:27:02 -0700 |
---|---|---|
committer | Christopher Ng <chrng8@gmail.com> | 2023-08-08 16:29:12 -0700 |
commit | e12ae21745d464335ce3d4665f04ca564dac3bde (patch) | |
tree | e2854b43a9bc5b845ecc783e004d7cb9c5bd11f7 /apps/files_reminders/lib | |
parent | 1c6114b21ac6919bd888a21ec1f5076adcf527b6 (diff) | |
download | nextcloud-server-e12ae21745d464335ce3d4665f04ca564dac3bde.tar.gz nextcloud-server-e12ae21745d464335ce3d4665f04ca564dac3bde.zip |
enh: handle node deleted
Signed-off-by: Christopher Ng <chrng8@gmail.com>
(cherry picked from commit f865c3ad21e03483a6f16b90a4383faef0356499)
Diffstat (limited to 'apps/files_reminders/lib')
4 files changed, 73 insertions, 0 deletions
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; @@ -108,6 +109,20 @@ class ReminderMapper extends QBMapper { /** * @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[] + */ public function findOverdue() { $qb = $this->db->getQueryBuilder(); 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 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2023 Christopher Ng <chrng8@gmail.com> + * + * @author Christopher Ng <chrng8@gmail.com> + * + * @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 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 |