From e16001b8eb6b5f87dfba57cca94adb88cf6b1d45 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 01/38] feat: init files_reminders migration Signed-off-by: Christopher Ng --- .gitignore | 1 + apps/files_reminders/lib/Db/Reminder.php | 56 +++++++++++++++ .../files_reminders/lib/Db/ReminderMapper.php | 67 +++++++++++++++++ .../Version10000Date20230725162149.php | 71 +++++++++++++++++++ 4 files changed, 195 insertions(+) create mode 100644 apps/files_reminders/lib/Db/Reminder.php create mode 100644 apps/files_reminders/lib/Db/ReminderMapper.php create mode 100644 apps/files_reminders/lib/Migration/Version10000Date20230725162149.php diff --git a/.gitignore b/.gitignore index e05e79eec6dc5..f56382cc7a255 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ !/apps/sharebymail !/apps/encryption !/apps/files_external +!/apps/files_reminders !/apps/files_sharing !/apps/files_trashbin !/apps/files_versions diff --git a/apps/files_reminders/lib/Db/Reminder.php b/apps/files_reminders/lib/Db/Reminder.php new file mode 100644 index 0000000000000..98e2cc5838f63 --- /dev/null +++ b/apps/files_reminders/lib/Db/Reminder.php @@ -0,0 +1,56 @@ + + * + * @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\Db; + +use OCP\AppFramework\Db\Entity; + +/** + * @method void setUserId(string $userId) + * @method string getUserId() + * + * @method void setFileId(int $fileId) + * @method int getFileId() + * + * @method void setRemindAt(int $remindAt) + * @method int getRemindAt() + * + * @method void setNotified(bool $notified) + * @method bool getNotified() + */ +class Reminder extends Entity { + protected string $userId; + protected int $fileId; + protected int $remindAt; + protected bool $notified = false; + + public function __construct() { + $this->addType('userId', 'string'); + $this->addType('fileId', 'integer'); + $this->addType('remindAt', 'integer'); + $this->addType('notified', 'boolean'); + } +} diff --git a/apps/files_reminders/lib/Db/ReminderMapper.php b/apps/files_reminders/lib/Db/ReminderMapper.php new file mode 100644 index 0000000000000..974416245fad0 --- /dev/null +++ b/apps/files_reminders/lib/Db/ReminderMapper.php @@ -0,0 +1,67 @@ + + * + * @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\Db; + +use OCP\AppFramework\Db\QBMapper; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; + +/** + * @template-extends QBMapper + */ +class ReminderMapper extends QBMapper { + public const TABLE_NAME = 'files_reminders'; + + public function __construct(IDBConnection $db) { + parent::__construct( + $db, + static::TABLE_NAME, + Reminder::class, + ); + } + + public function markNotified(Reminder $reminder): Reminder { + $reminderUpdate = new Reminder(); + $reminderUpdate->setId($reminder->getId()); + $reminderUpdate->setNotified(true); + return parent::update($reminderUpdate); + } + + /** + * @return Reminder[] + */ + public function findToRemind() { + $qb = $this->db->getQueryBuilder(); + + $qb->select('user_id', 'file_id', 'remind_at') + ->from($this->getTableName()) + ->where($qb->expr()->lt('remind_at', $qb->createFunction('NOW()'))) + ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))); + + return $this->findEntities($qb); + } +} diff --git a/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php b/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php new file mode 100644 index 0000000000000..ab0afd5aec6bc --- /dev/null +++ b/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php @@ -0,0 +1,71 @@ + + * + * @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\Migration; + +use Closure; +use OCA\FilesReminders\Db\ReminderMapper; +use OCP\DB\ISchemaWrapper; +use OCP\DB\Types; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version10000Date20230725162149 extends SimpleMigrationStep { + /** + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + $table = $schema->createTable(ReminderMapper::TABLE_NAME); + $table->addColumn('id', Types::BIGINT, [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 20, + 'unsigned' => true, + ]); + $table->addColumn('user_id', Types::STRING, [ + 'notnull' => true, + 'length' => 64, + ]); + $table->addColumn('file_id', Types::BIGINT, [ + 'notnull' => true, + 'length' => 20, + ]); + $table->addColumn('remind_at', Types::BIGINT, [ + 'notnull' => true, + ]); + $table->addColumn('notified', Types::BOOLEAN, [ + 'notnull' => false, + 'default' => false, + ]); + $table->setPrimaryKey(['id']); + $table->addUniqueIndex(['user_id', 'file_id', 'remind_at'], 'reminders_uniq_idx'); + + return $schema; + } +} From 59a2ef4ea1176c03a54e2eab83c6f81d6b2b87bb Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 02/38] feat(files_reminders): add Application Signed-off-by: Christopher Ng --- .../lib/AppInfo/Application.php | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 apps/files_reminders/lib/AppInfo/Application.php diff --git a/apps/files_reminders/lib/AppInfo/Application.php b/apps/files_reminders/lib/AppInfo/Application.php new file mode 100644 index 0000000000000..4d418562ef5fa --- /dev/null +++ b/apps/files_reminders/lib/AppInfo/Application.php @@ -0,0 +1,48 @@ + + * + * @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\AppInfo; + +use OCA\FilesReminders\Notification\Notifier; +use OCP\AppFramework\App; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; + +class Application extends App implements IBootstrap { + public const APP_ID = 'files_reminders'; + + public function __construct() { + parent::__construct(static::APP_ID); + } + + public function boot(IBootContext $context): void { + } + + public function register(IRegistrationContext $context): void { + $context->registerNotifierService(Notifier::class); + } +} From b75fac31aae8dcd65640ccce1540c3c5b22445f3 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 03/38] feat(files_reminders): add info xml Signed-off-by: Christopher Ng --- apps/files_reminders/appinfo/info.xml | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 apps/files_reminders/appinfo/info.xml diff --git a/apps/files_reminders/appinfo/info.xml b/apps/files_reminders/appinfo/info.xml new file mode 100644 index 0000000000000..d0a69c1ae7c00 --- /dev/null +++ b/apps/files_reminders/appinfo/info.xml @@ -0,0 +1,32 @@ + + + files_reminders + File reminders + Set file reminders + + 0.1.0-dev + agpl + Christopher Ng + FilesReminders + + files + + https://github.com/nextcloud/server/issues + + + + + + + OCA\FilesReminders\BackgroundJob\CleanUpReminders + OCA\FilesReminders\BackgroundJob\ScheduledNotifications + + + + OCA\FilesReminders\Command\ListCommand + + From a3ac1b82da2a1b295fc826ebc91245905fb9ecf7 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 04/38] feat(files_reminders): add background jobs Signed-off-by: Christopher Ng --- .../lib/BackgroundJob/CleanUpReminders.php | 50 +++++++++++++++ .../BackgroundJob/ScheduledNotifications.php | 62 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php create mode 100644 apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php diff --git a/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php b/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php new file mode 100644 index 0000000000000..43d0afe3e10f7 --- /dev/null +++ b/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php @@ -0,0 +1,50 @@ + + * + * @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\BackgroundJob; + +use OCA\FilesReminders\Service\ReminderService; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\IJob; +use OCP\BackgroundJob\TimedJob; + +class CleanUpReminders extends TimedJob { + public function __construct( + private ITimeFactory $time, + private ReminderService $reminderService, + ) { + parent::__construct($time); + $this->setInterval(60 * 60 * 24); + $this->setTimeSensitivity(IJob::TIME_INSENSITIVE); + } + + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + protected function run($argument) { + $this->reminderService->cleanUp(500); + } +} diff --git a/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php b/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php new file mode 100644 index 0000000000000..9efb6acb53b37 --- /dev/null +++ b/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php @@ -0,0 +1,62 @@ + + * + * @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\BackgroundJob; + +use OCA\FilesReminders\Db\ReminderMapper; +use OCA\FilesReminders\Service\ReminderService; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\Job; +use Psr\Log\LoggerInterface; +use Throwable; + +class ScheduledNotifications extends Job { + public function __construct( + protected ITimeFactory $time, + protected ReminderMapper $reminderMapper, + protected ReminderService $reminderService, + protected LoggerInterface $logger, + ) { + parent::__construct($time); + } + + /** + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function run($argument) { + $reminders = $this->reminderMapper->findToRemind(); + foreach ($reminders as $reminder) { + try { + $this->reminderService->send($reminder); + } catch (DoesNotExistException $e) { + $this->logger->debug('Could not send notification for reminder with id ' . $reminder->getId()); + } catch (Throwable $th) { + $this->logger->error($th->getMessage(), $th->getTrace()); + } + } + } +} From ea5e128fefba6d87c400cdaafa87cd566746c109 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 05/38] feat(files_reminders): add service and notifier Signed-off-by: Christopher Ng --- .../files_reminders/lib/Db/ReminderMapper.php | 18 ++- .../lib/Exception/NodeNotFoundException.php | 32 ++++ .../lib/Exception/UserNotFoundException.php | 32 ++++ .../lib/Notification/Notifier.php | 142 ++++++++++++++++++ .../lib/Service/ReminderService.php | 87 +++++++++++ 5 files changed, 310 insertions(+), 1 deletion(-) create mode 100644 apps/files_reminders/lib/Exception/NodeNotFoundException.php create mode 100644 apps/files_reminders/lib/Exception/UserNotFoundException.php create mode 100644 apps/files_reminders/lib/Notification/Notifier.php create mode 100644 apps/files_reminders/lib/Service/ReminderService.php diff --git a/apps/files_reminders/lib/Db/ReminderMapper.php b/apps/files_reminders/lib/Db/ReminderMapper.php index 974416245fad0..f383ee66511b2 100644 --- a/apps/files_reminders/lib/Db/ReminderMapper.php +++ b/apps/files_reminders/lib/Db/ReminderMapper.php @@ -60,7 +60,23 @@ public function findToRemind() { $qb->select('user_id', 'file_id', 'remind_at') ->from($this->getTableName()) ->where($qb->expr()->lt('remind_at', $qb->createFunction('NOW()'))) - ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))); + ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) + ->orderBy('remind_at', 'ASC'); + + return $this->findEntities($qb); + } + + /** + * @return Reminder[] + */ + public function findToDelete(?int $limit = null) { + $qb = $this->db->getQueryBuilder(); + + $qb->select('user_id', 'file_id', 'remind_at') + ->from($this->getTableName()) + ->where($qb->expr()->eq('notified', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))) + ->orderBy('remind_at', 'ASC') + ->setMaxResults($limit); return $this->findEntities($qb); } diff --git a/apps/files_reminders/lib/Exception/NodeNotFoundException.php b/apps/files_reminders/lib/Exception/NodeNotFoundException.php new file mode 100644 index 0000000000000..5dfe784f5fc54 --- /dev/null +++ b/apps/files_reminders/lib/Exception/NodeNotFoundException.php @@ -0,0 +1,32 @@ + + * + * @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\Exception; + +use Exception; + +class NodeNotFoundException extends Exception { +} diff --git a/apps/files_reminders/lib/Exception/UserNotFoundException.php b/apps/files_reminders/lib/Exception/UserNotFoundException.php new file mode 100644 index 0000000000000..3c57ea475e994 --- /dev/null +++ b/apps/files_reminders/lib/Exception/UserNotFoundException.php @@ -0,0 +1,32 @@ + + * + * @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\Exception; + +use Exception; + +class UserNotFoundException extends Exception { +} diff --git a/apps/files_reminders/lib/Notification/Notifier.php b/apps/files_reminders/lib/Notification/Notifier.php new file mode 100644 index 0000000000000..b3c1a98484a15 --- /dev/null +++ b/apps/files_reminders/lib/Notification/Notifier.php @@ -0,0 +1,142 @@ + + * + * @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\Notification; + +use InvalidArgumentException; +use OCA\FilesReminders\AppInfo\Application; +use OCA\FilesReminders\Exception\NodeNotFoundException; +use OCP\Files\FileInfo; +use OCP\Files\IRootFolder; +use OCP\Files\Node; +use OCP\IURLGenerator; +use OCP\L10N\IFactory; +use OCP\Notification\IAction; +use OCP\Notification\INotification; +use OCP\Notification\INotifier; + +class Notifier implements INotifier { + public function __construct( + protected IFactory $l10nFactory, + protected IURLGenerator $urlGenerator, + protected IRootFolder $root, + ) {} + + public function getID(): string { + return Application::APP_ID; + } + + public function getName(): string { + return $this->l10nFactory->get(Application::APP_ID)->t('File reminders'); + } + + /** + * @throws InvalidArgumentException + * @throws NodeNotFoundException + */ + public function prepare(INotification $notification, string $languageCode): INotification { + $l = $this->l10nFactory->get(Application::APP_ID, $languageCode); + + if ($notification->getApp() !== Application::APP_ID) { + throw new InvalidArgumentException(); + } + + switch ($notification->getSubject()) { + case 'reminder-due': + $fileId = $notification->getSubjectParameters()['fileId']; + $node = $this->getNode($fileId); + + $path = rtrim($node->getPath(), '/'); + if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { + // Remove /user/files/... + $fullPath = $path; + [,,, $path] = explode('/', $fullPath, 4); + } + + $link = $this->urlGenerator->linkToRouteAbsolute( + 'files.viewcontroller.showFile', + ['fileid' => $node->getId()], + ); + + $subject = $l->t('Reminder for {filename}'); + $notification + ->setRichSubject( + $subject, + [ + 'filename' => [ + 'type' => 'file', + 'id' => $node->getId(), + 'name' => $node->getName(), + 'path' => $path, + 'link' => $link, + ], + ], + ) + ->setParsedSubject(str_replace( + ['{filename}'], + [$node->getName()], + $subject, + )) + ->setLink($link); + + $label = match ($node->getType()) { + FileInfo::TYPE_FILE => $l->t('View file'), + FileInfo::TYPE_FOLDER => $l->t('View folder'), + }; + + $this->addActionButton($notification, $label); + break; + default: + throw new InvalidArgumentException(); + break; + } + + return $notification; + } + + protected function addActionButton(INotification $notification, string $label): void { + $action = $notification->createAction(); + + $action->setLabel($label) + ->setParsedLabel($label) + ->setLink($notification->getLink(), IAction::TYPE_WEB) + ->setPrimary(true); + + $notification->addParsedAction($action); + } + + /** + * @throws NodeNotFoundException + */ + protected function getNode(int $fileId): Node { + $nodes = $this->root->getById($fileId); + if (empty($nodes)) { + throw new NodeNotFoundException(); + } + $node = reset($nodes); + return $node; + } +} diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php new file mode 100644 index 0000000000000..69cef36785634 --- /dev/null +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -0,0 +1,87 @@ + + * + * @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\Service; + +use DateTime; +use InvalidArgumentException; +use OCA\FilesReminders\AppInfo\Application; +use OCA\FilesReminders\Db\Reminder; +use OCA\FilesReminders\Db\ReminderMapper; +use OCA\FilesReminders\Exception\UserNotFoundException; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\IURLGenerator; +use OCP\IUserManager; +use OCP\Notification\IManager as INotificationManager; +use Psr\Log\LoggerInterface; + +class ReminderService { + public function __construct( + protected IUserManager $userManager, + protected IURLGenerator $urlGenerator, + protected INotificationManager $notificationManager, + protected ReminderMapper $reminderMapper, + protected LoggerInterface $logger, + ) {} + + /** + * @throws DoesNotExistException + * @throws UserNotFoundException + */ + public function send(Reminder $reminder): void { + if ($reminder->getNotified()) { + return; + } + + $user = $this->userManager->get($reminder->getUserId()); + if ($user === null) { + throw new UserNotFoundException(); + } + + $notification = $this->notificationManager->createNotification(); + $notification + ->setApp(Application::APP_ID) + ->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('files', 'folder.svg'))) + ->setUser($user->getUID()) + ->setObject('reminder', (string)$reminder->getId()) + ->setSubject('reminder-due', ['fileId' => $reminder->getFileId()]) + ->setDateTime(DateTime::createFromFormat('U', (string)$reminder->getRemindAt())); + + try { + $this->notificationManager->notify($notification); + $this->reminderMapper->markNotified($reminder); + } catch (InvalidArgumentException $e) { + $this->logger->error('Failed to send reminder notification', $e->getTrace()); + } + } + + public function cleanUp(?int $limit = null): void { + $reminders = $this->reminderMapper->findToDelete($limit); + foreach ($reminders as $reminder) { + $this->reminderMapper->delete($reminder); + } + } +} From c8a32a70cd7564e739b7c60196f804d50a954b8a Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 06/38] feat(files_reminders): add list command Signed-off-by: Christopher Ng --- .../lib/Command/ListCommand.php | 92 +++++++++++++++++++ apps/files_reminders/lib/Db/Reminder.php | 8 +- .../files_reminders/lib/Db/ReminderMapper.php | 42 ++++++++- .../lib/Model/RichReminder.php | 62 +++++++++++++ .../lib/Notification/Notifier.php | 28 ++---- .../lib/Service/ReminderService.php | 24 ++++- 6 files changed, 229 insertions(+), 27 deletions(-) create mode 100644 apps/files_reminders/lib/Command/ListCommand.php create mode 100644 apps/files_reminders/lib/Model/RichReminder.php diff --git a/apps/files_reminders/lib/Command/ListCommand.php b/apps/files_reminders/lib/Command/ListCommand.php new file mode 100644 index 0000000000000..35a778e87d186 --- /dev/null +++ b/apps/files_reminders/lib/Command/ListCommand.php @@ -0,0 +1,92 @@ + + * + * @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\Command; + +use DateTime; +use DateTimeInterface; +use OC\Core\Command\Base; +use OCA\FilesReminders\Model\RichReminder; +use OCA\FilesReminders\Service\ReminderService; +use OCP\IUserManager; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +class ListCommand extends Base { + public function __construct( + private ReminderService $reminderService, + private IUserManager $userManager, + ) { + parent::__construct(); + } + + protected function configure(): void { + $this + ->setName('files:reminders') + ->setDescription('List file reminders') + ->addArgument( + 'user', + InputArgument::OPTIONAL, + 'list reminders for user', + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $io = new SymfonyStyle($input, $output); + + $uid = $input->getArgument('user'); + if ($uid !== null) { + /** @var string $uid */ + $user = $this->userManager->get($uid); + if ($user === null) { + $io->error("Unknown user <$uid>"); + return 1; + } + } + + $reminders = $this->reminderService->getAll($user ?? null); + if (empty($reminders)) { + $io->text('No reminders'); + return 0; + } + + $io->table( + ['UserId', 'Path', 'RemindAt', 'Notified'], + array_map( + fn (RichReminder $reminder) => [ + $reminder->getUserId(), + $reminder->getNode()->getPath(), + DateTime::createFromFormat('U', (string)$reminder->getRemindAt())->format(DateTimeInterface::ATOM), // ISO 8601 + $reminder->getNotified() ? 'true' : 'false', + ], + $reminders, + ) + ); + return 0; + } +} diff --git a/apps/files_reminders/lib/Db/Reminder.php b/apps/files_reminders/lib/Db/Reminder.php index 98e2cc5838f63..99cd9b590922c 100644 --- a/apps/files_reminders/lib/Db/Reminder.php +++ b/apps/files_reminders/lib/Db/Reminder.php @@ -42,10 +42,10 @@ * @method bool getNotified() */ class Reminder extends Entity { - protected string $userId; - protected int $fileId; - protected int $remindAt; - protected bool $notified = false; + protected $userId; + protected $fileId; + protected $remindAt; + protected $notified = false; public function __construct() { $this->addType('userId', 'string'); diff --git a/apps/files_reminders/lib/Db/ReminderMapper.php b/apps/files_reminders/lib/Db/ReminderMapper.php index f383ee66511b2..6b2e3a1f2b87c 100644 --- a/apps/files_reminders/lib/Db/ReminderMapper.php +++ b/apps/files_reminders/lib/Db/ReminderMapper.php @@ -29,6 +29,7 @@ use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use OCP\IUser; /** * @template-extends QBMapper @@ -51,13 +52,50 @@ public function markNotified(Reminder $reminder): Reminder { return parent::update($reminderUpdate); } + public function find(int $id): Reminder { + $qb = $this->db->getQueryBuilder(); + + $qb->select('user_id', 'file_id', 'remind_at', 'notified') + ->from($this->getTableName()) + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); + + return $this->findEntity($qb); + } + + /** + * @return Reminder[] + */ + public function findAll() { + $qb = $this->db->getQueryBuilder(); + + $qb->select('user_id', 'file_id', 'remind_at', 'notified') + ->from($this->getTableName()) + ->orderBy('remind_at', 'ASC'); + + return $this->findEntities($qb); + } + + /** + * @return Reminder[] + */ + public function findAllForUser(IUser $user) { + $qb = $this->db->getQueryBuilder(); + + $qb->select('user_id', 'file_id', 'remind_at', 'notified') + ->from($this->getTableName()) + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR))) + ->orderBy('remind_at', 'ASC'); + + return $this->findEntities($qb); + } + /** * @return Reminder[] */ public function findToRemind() { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'remind_at') + $qb->select('user_id', 'file_id', 'remind_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->lt('remind_at', $qb->createFunction('NOW()'))) ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) @@ -72,7 +110,7 @@ public function findToRemind() { public function findToDelete(?int $limit = null) { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'remind_at') + $qb->select('user_id', 'file_id', 'remind_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('notified', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))) ->orderBy('remind_at', 'ASC') diff --git a/apps/files_reminders/lib/Model/RichReminder.php b/apps/files_reminders/lib/Model/RichReminder.php new file mode 100644 index 0000000000000..21b42d9c2e786 --- /dev/null +++ b/apps/files_reminders/lib/Model/RichReminder.php @@ -0,0 +1,62 @@ + + * + * @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\Model; + +use OCA\FilesReminders\Db\Reminder; +use OCA\FilesReminders\Exception\NodeNotFoundException; +use OCP\Files\IRootFolder; +use OCP\Files\Node; + +class RichReminder extends Reminder { + public function __construct( + private Reminder $reminder, + private IRootFolder $root, + ) { + parent::__construct(); + } + + /** + * @throws NodeNotFoundException + */ + public function getNode(): Node { + $userFolder = $this->root->getUserFolder($this->getUserId()); + $nodes = $userFolder->getById($this->getFileId()); + if (empty($nodes)) { + throw new NodeNotFoundException(); + } + $node = reset($nodes); + return $node; + } + + protected function getter(string $name): mixed { + return $this->reminder->getter($name); + } + + public function __call(string $methodName, array $args) { + return $this->reminder->__call($methodName, $args); + } +} diff --git a/apps/files_reminders/lib/Notification/Notifier.php b/apps/files_reminders/lib/Notification/Notifier.php index b3c1a98484a15..fb1d3264dbda6 100644 --- a/apps/files_reminders/lib/Notification/Notifier.php +++ b/apps/files_reminders/lib/Notification/Notifier.php @@ -29,9 +29,8 @@ use InvalidArgumentException; use OCA\FilesReminders\AppInfo\Application; use OCA\FilesReminders\Exception\NodeNotFoundException; +use OCA\FilesReminders\Service\ReminderService; use OCP\Files\FileInfo; -use OCP\Files\IRootFolder; -use OCP\Files\Node; use OCP\IURLGenerator; use OCP\L10N\IFactory; use OCP\Notification\IAction; @@ -42,7 +41,7 @@ class Notifier implements INotifier { public function __construct( protected IFactory $l10nFactory, protected IURLGenerator $urlGenerator, - protected IRootFolder $root, + protected ReminderService $reminderService, ) {} public function getID(): string { @@ -66,8 +65,8 @@ public function prepare(INotification $notification, string $languageCode): INot switch ($notification->getSubject()) { case 'reminder-due': - $fileId = $notification->getSubjectParameters()['fileId']; - $node = $this->getNode($fileId); + $reminderId = (int)$notification->getObjectId(); + $node = $this->reminderService->get($reminderId)->getNode(); $path = rtrim($node->getPath(), '/'); if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { @@ -81,12 +80,13 @@ public function prepare(INotification $notification, string $languageCode): INot ['fileid' => $node->getId()], ); - $subject = $l->t('Reminder for {filename}'); + // TRANSLATORS The name placeholder is for a file or folder name + $subject = $l->t('Reminder for {name}'); $notification ->setRichSubject( $subject, [ - 'filename' => [ + 'name' => [ 'type' => 'file', 'id' => $node->getId(), 'name' => $node->getName(), @@ -96,7 +96,7 @@ public function prepare(INotification $notification, string $languageCode): INot ], ) ->setParsedSubject(str_replace( - ['{filename}'], + ['{name}'], [$node->getName()], $subject, )) @@ -127,16 +127,4 @@ protected function addActionButton(INotification $notification, string $label): $notification->addParsedAction($action); } - - /** - * @throws NodeNotFoundException - */ - protected function getNode(int $fileId): Node { - $nodes = $this->root->getById($fileId); - if (empty($nodes)) { - throw new NodeNotFoundException(); - } - $node = reset($nodes); - return $node; - } } diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index 69cef36785634..e65cdbfb86723 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -32,8 +32,11 @@ use OCA\FilesReminders\Db\Reminder; use OCA\FilesReminders\Db\ReminderMapper; use OCA\FilesReminders\Exception\UserNotFoundException; +use OCA\FilesReminders\Model\RichReminder; use OCP\AppFramework\Db\DoesNotExistException; +use OCP\Files\IRootFolder; use OCP\IURLGenerator; +use OCP\IUser; use OCP\IUserManager; use OCP\Notification\IManager as INotificationManager; use Psr\Log\LoggerInterface; @@ -44,9 +47,28 @@ public function __construct( protected IURLGenerator $urlGenerator, protected INotificationManager $notificationManager, protected ReminderMapper $reminderMapper, + protected IRootFolder $root, protected LoggerInterface $logger, ) {} + public function get(int $id): RichReminder { + $reminder = $this->reminderMapper->find($id); + return new RichReminder($reminder, $this->root); + } + + /** + * @return RichReminder[] + */ + public function getAll(?IUser $user = null) { + $reminders = ($user !== null) + ? $this->reminderMapper->findAllForUser($user) + : $this->reminderMapper->findAll(); + return array_map( + fn (Reminder $reminder) => new RichReminder($reminder, $this->root), + $reminders, + ); + } + /** * @throws DoesNotExistException * @throws UserNotFoundException @@ -67,7 +89,7 @@ public function send(Reminder $reminder): void { ->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('files', 'folder.svg'))) ->setUser($user->getUID()) ->setObject('reminder', (string)$reminder->getId()) - ->setSubject('reminder-due', ['fileId' => $reminder->getFileId()]) + ->setSubject('reminder-due') ->setDateTime(DateTime::createFromFormat('U', (string)$reminder->getRemindAt())); try { From 163b059c112d7c5eceb8a1cc1db55184855903df Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 07/38] enh: use datetime Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Command/ListCommand.php | 3 +-- apps/files_reminders/lib/Db/Reminder.php | 7 ++++--- .../lib/Migration/Version10000Date20230725162149.php | 5 +++-- apps/files_reminders/lib/Service/ReminderService.php | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/apps/files_reminders/lib/Command/ListCommand.php b/apps/files_reminders/lib/Command/ListCommand.php index 35a778e87d186..f284be5bf2a5f 100644 --- a/apps/files_reminders/lib/Command/ListCommand.php +++ b/apps/files_reminders/lib/Command/ListCommand.php @@ -26,7 +26,6 @@ namespace OCA\FilesReminders\Command; -use DateTime; use DateTimeInterface; use OC\Core\Command\Base; use OCA\FilesReminders\Model\RichReminder; @@ -81,7 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int fn (RichReminder $reminder) => [ $reminder->getUserId(), $reminder->getNode()->getPath(), - DateTime::createFromFormat('U', (string)$reminder->getRemindAt())->format(DateTimeInterface::ATOM), // ISO 8601 + $reminder->getRemindAt()->format(DateTimeInterface::ATOM), // ISO 8601 $reminder->getNotified() ? 'true' : 'false', ], $reminders, diff --git a/apps/files_reminders/lib/Db/Reminder.php b/apps/files_reminders/lib/Db/Reminder.php index 99cd9b590922c..fd20fb45f4620 100644 --- a/apps/files_reminders/lib/Db/Reminder.php +++ b/apps/files_reminders/lib/Db/Reminder.php @@ -26,6 +26,7 @@ namespace OCA\FilesReminders\Db; +use DateTime; use OCP\AppFramework\Db\Entity; /** @@ -35,8 +36,8 @@ * @method void setFileId(int $fileId) * @method int getFileId() * - * @method void setRemindAt(int $remindAt) - * @method int getRemindAt() + * @method void setRemindAt(DateTime $remindAt) + * @method DateTime getRemindAt() * * @method void setNotified(bool $notified) * @method bool getNotified() @@ -50,7 +51,7 @@ class Reminder extends Entity { public function __construct() { $this->addType('userId', 'string'); $this->addType('fileId', 'integer'); - $this->addType('remindAt', 'integer'); + $this->addType('remindAt', 'datetime'); $this->addType('notified', 'boolean'); } } diff --git a/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php b/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php index ab0afd5aec6bc..6a8cb783931c5 100644 --- a/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php +++ b/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php @@ -55,9 +55,10 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $table->addColumn('file_id', Types::BIGINT, [ 'notnull' => true, 'length' => 20, + 'unsigned' => true, ]); - $table->addColumn('remind_at', Types::BIGINT, [ - 'notnull' => true, + $table->addColumn('remind_at', Types::DATETIME, [ + 'notnull' => false, ]); $table->addColumn('notified', Types::BOOLEAN, [ 'notnull' => false, diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index e65cdbfb86723..8411cdb9cf743 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -90,7 +90,7 @@ public function send(Reminder $reminder): void { ->setUser($user->getUID()) ->setObject('reminder', (string)$reminder->getId()) ->setSubject('reminder-due') - ->setDateTime(DateTime::createFromFormat('U', (string)$reminder->getRemindAt())); + ->setDateTime($reminder->getRemindAt()); try { $this->notificationManager->notify($notification); From 4fabd7743230be80f82543edd4905ead97f14a59 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 08/38] enh: add created at Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Command/ListCommand.php | 3 ++- apps/files_reminders/lib/Db/Reminder.php | 5 +++++ apps/files_reminders/lib/Db/ReminderMapper.php | 14 +++++++------- .../Migration/Version10000Date20230725162149.php | 5 ++++- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/apps/files_reminders/lib/Command/ListCommand.php b/apps/files_reminders/lib/Command/ListCommand.php index f284be5bf2a5f..074aeb8723901 100644 --- a/apps/files_reminders/lib/Command/ListCommand.php +++ b/apps/files_reminders/lib/Command/ListCommand.php @@ -75,12 +75,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $io->table( - ['UserId', 'Path', 'RemindAt', 'Notified'], + ['UserId', 'Path', 'RemindAt', 'CreatedAt', 'Notified'], array_map( fn (RichReminder $reminder) => [ $reminder->getUserId(), $reminder->getNode()->getPath(), $reminder->getRemindAt()->format(DateTimeInterface::ATOM), // ISO 8601 + $reminder->getCreatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 $reminder->getNotified() ? 'true' : 'false', ], $reminders, diff --git a/apps/files_reminders/lib/Db/Reminder.php b/apps/files_reminders/lib/Db/Reminder.php index fd20fb45f4620..1abaa48d2008b 100644 --- a/apps/files_reminders/lib/Db/Reminder.php +++ b/apps/files_reminders/lib/Db/Reminder.php @@ -39,6 +39,9 @@ * @method void setRemindAt(DateTime $remindAt) * @method DateTime getRemindAt() * + * @method void setCreatedAt(DateTime $createdAt) + * @method DateTime getCreatedAt() + * * @method void setNotified(bool $notified) * @method bool getNotified() */ @@ -46,12 +49,14 @@ class Reminder extends Entity { protected $userId; protected $fileId; protected $remindAt; + protected $createdAt; protected $notified = false; public function __construct() { $this->addType('userId', 'string'); $this->addType('fileId', 'integer'); $this->addType('remindAt', 'datetime'); + $this->addType('createdAt', 'datetime'); $this->addType('notified', 'boolean'); } } diff --git a/apps/files_reminders/lib/Db/ReminderMapper.php b/apps/files_reminders/lib/Db/ReminderMapper.php index 6b2e3a1f2b87c..4d1ebab35b141 100644 --- a/apps/files_reminders/lib/Db/ReminderMapper.php +++ b/apps/files_reminders/lib/Db/ReminderMapper.php @@ -55,8 +55,8 @@ public function markNotified(Reminder $reminder): Reminder { public function find(int $id): Reminder { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'remind_at', 'notified') - ->from($this->getTableName()) + $qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified') + ->from($this->getTableName()) ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); return $this->findEntity($qb); @@ -68,7 +68,7 @@ public function find(int $id): Reminder { public function findAll() { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'remind_at', 'notified') + $qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified') ->from($this->getTableName()) ->orderBy('remind_at', 'ASC'); @@ -81,8 +81,8 @@ public function findAll() { public function findAllForUser(IUser $user) { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'remind_at', 'notified') - ->from($this->getTableName()) + $qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified') + ->from($this->getTableName()) ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR))) ->orderBy('remind_at', 'ASC'); @@ -95,7 +95,7 @@ public function findAllForUser(IUser $user) { public function findToRemind() { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'remind_at', 'notified') + $qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->lt('remind_at', $qb->createFunction('NOW()'))) ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) @@ -110,7 +110,7 @@ public function findToRemind() { public function findToDelete(?int $limit = null) { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'remind_at', 'notified') + $qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('notified', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))) ->orderBy('remind_at', 'ASC') diff --git a/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php b/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php index 6a8cb783931c5..1940d153763eb 100644 --- a/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php +++ b/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php @@ -58,7 +58,10 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt 'unsigned' => true, ]); $table->addColumn('remind_at', Types::DATETIME, [ - 'notnull' => false, + 'notnull' => true, + ]); + $table->addColumn('created_at', Types::DATETIME, [ + 'notnull' => true, ]); $table->addColumn('notified', Types::BOOLEAN, [ 'notnull' => false, From 774e3e6d4b9af4a6b329247561f6f85ce354f58d Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 09/38] enh: implement JsonSerializable Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Model/RichReminder.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/apps/files_reminders/lib/Model/RichReminder.php b/apps/files_reminders/lib/Model/RichReminder.php index 21b42d9c2e786..76c3d8dd22bc1 100644 --- a/apps/files_reminders/lib/Model/RichReminder.php +++ b/apps/files_reminders/lib/Model/RichReminder.php @@ -26,12 +26,14 @@ namespace OCA\FilesReminders\Model; +use DateTimeInterface; +use JsonSerializable; use OCA\FilesReminders\Db\Reminder; use OCA\FilesReminders\Exception\NodeNotFoundException; use OCP\Files\IRootFolder; use OCP\Files\Node; -class RichReminder extends Reminder { +class RichReminder extends Reminder implements JsonSerializable { public function __construct( private Reminder $reminder, private IRootFolder $root, @@ -59,4 +61,14 @@ protected function getter(string $name): mixed { public function __call(string $methodName, array $args) { return $this->reminder->__call($methodName, $args); } + + public function jsonSerialize(): array { + return [ + 'userId' => $this->getUserId(), + 'fileId' => $this->getFileId(), + 'remindAt' => $this->getRemindAt()->format(DateTimeInterface::ATOM), // ISO 8601 + 'createdAt' => $this->getCreatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 + 'notified' => $this->getNotified(), + ]; + } } From a7892fb68228578f865e559f9442432ada95cde0 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 10/38] feat(files_reminders): add api controller Signed-off-by: Christopher Ng --- apps/files_reminders/appinfo/routes.php | 36 ++++++ .../lib/Controller/ApiController.php | 107 ++++++++++++++++++ .../files_reminders/lib/Db/ReminderMapper.php | 18 +++ .../lib/Service/ReminderService.php | 18 +++ 4 files changed, 179 insertions(+) create mode 100644 apps/files_reminders/appinfo/routes.php create mode 100644 apps/files_reminders/lib/Controller/ApiController.php diff --git a/apps/files_reminders/appinfo/routes.php b/apps/files_reminders/appinfo/routes.php new file mode 100644 index 0000000000000..f36d6cf6391f2 --- /dev/null +++ b/apps/files_reminders/appinfo/routes.php @@ -0,0 +1,36 @@ + + * + * @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 . + * + */ + +$requirements = [ + 'version' => '1', +]; + +return [ + 'ocs' => [ + ['name' => 'Api#get', 'url' => '/api/v{version}/get', 'verb' => 'GET', 'requirements' => $requirements], + ['name' => 'Api#create', 'url' => '/api/v{version}/create', 'verb' => 'POST', 'requirements' => $requirements], + ], +]; diff --git a/apps/files_reminders/lib/Controller/ApiController.php b/apps/files_reminders/lib/Controller/ApiController.php new file mode 100644 index 0000000000000..b372a871e3bc1 --- /dev/null +++ b/apps/files_reminders/lib/Controller/ApiController.php @@ -0,0 +1,107 @@ + + * + * @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\Controller; + +use DateTime; +use DateTimeInterface; +use DateTimeZone; +use Exception; +use OCA\FilesReminders\Service\ReminderService; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\OCSController; +use OCP\IRequest; +use OCP\IUserSession; +use Psr\Log\LoggerInterface; +use Throwable; + +class ApiController extends OCSController { + public function __construct( + string $appName, + IRequest $request, + protected ReminderService $reminderService, + protected IUserSession $userSession, + protected LoggerInterface $logger, + ) { + parent::__construct($appName, $request); + } + + /** + * Get a reminder + */ + public function get(int $fileId): JSONResponse { + $user = $this->userSession->getUser(); + if ($user === null) { + return new JSONResponse([], Http::STATUS_UNAUTHORIZED); + } + + try { + $reminder = $this->reminderService->getDueForUser($user, $fileId); + $reminderData = [ + 'remindAt' => $reminder->getRemindAt()->format(DateTimeInterface::ATOM), // ISO 8601 + ]; + return new JSONResponse($reminderData, Http::STATUS_OK); + } catch (DoesNotExistException $e) { + // Return null when no reminder is found + $reminderData = [ + 'remindAt' => null, + ]; + return new JSONResponse($reminderData, Http::STATUS_OK); + } catch (Throwable $th) { + $this->logger->error($th->getMessage(), ['exception' => $th]); + return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); + } + } + + /** + * Create a reminder + * + * @param string $remindAt ISO 8601 formatted date time string + */ + public function create(int $fileId, string $remindAt): JSONResponse { + try { + $remindAt = (new DateTime($remindAt))->setTimezone(new DateTimeZone('UTC')); + } catch (Exception $e) { + $this->logger->error($e->getMessage(), ['exception' => $e]); + return new JSONResponse([], Http::STATUS_BAD_REQUEST); + } + + $user = $this->userSession->getUser(); + if ($user === null) { + return new JSONResponse([], Http::STATUS_UNAUTHORIZED); + } + + try { + $this->reminderService->create($user, $fileId, $remindAt); + return new JSONResponse([], Http::STATUS_OK); + } catch (Throwable $th) { + $this->logger->error($th->getMessage(), ['exception' => $th]); + return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); + } + } +} diff --git a/apps/files_reminders/lib/Db/ReminderMapper.php b/apps/files_reminders/lib/Db/ReminderMapper.php index 4d1ebab35b141..7368ef2acc7d5 100644 --- a/apps/files_reminders/lib/Db/ReminderMapper.php +++ b/apps/files_reminders/lib/Db/ReminderMapper.php @@ -26,6 +26,7 @@ namespace OCA\FilesReminders\Db; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; @@ -62,6 +63,23 @@ public function find(int $id): Reminder { return $this->findEntity($qb); } + /** + * @throws DoesNotExistException + */ + public function findDueForUser(IUser $user, int $fileId): Reminder { + $qb = $this->db->getQueryBuilder(); + + $qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified') + ->from($this->getTableName()) + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR))) + ->andWhere($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) + ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) + ->orderBy('created_at', 'DESC') + ->setMaxResults(1); + + return $this->findEntity($qb); + } + /** * @return Reminder[] */ diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index 8411cdb9cf743..7106c8e5514f2 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -27,6 +27,7 @@ namespace OCA\FilesReminders\Service; use DateTime; +use DateTimeZone; use InvalidArgumentException; use OCA\FilesReminders\AppInfo\Application; use OCA\FilesReminders\Db\Reminder; @@ -56,6 +57,14 @@ public function get(int $id): RichReminder { return new RichReminder($reminder, $this->root); } + /** + * @throws DoesNotExistException + */ + public function getDueForUser(IUser $user, int $fileId): RichReminder { + $reminder = $this->reminderMapper->findDueForUser($user, $fileId); + return new RichReminder($reminder, $this->root); + } + /** * @return RichReminder[] */ @@ -69,6 +78,15 @@ public function getAll(?IUser $user = null) { ); } + public function create(IUser $user, int $fileId, DateTime $remindAt): void { + $reminder = new Reminder(); + $reminder->setUserId($user->getUID()); + $reminder->setFileId($fileId); + $reminder->setRemindAt($remindAt); + $reminder->setCreatedAt(new DateTime('now', new DateTimeZone('UTC'))); + $this->reminderMapper->insert($reminder); + } + /** * @throws DoesNotExistException * @throws UserNotFoundException From cfbac9b73c1142a2661e386511acbeb41c517c26 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 11/38] fix: add composer autoload Signed-off-by: Christopher Ng --- apps/files_reminders/composer/autoload.php | 25 + apps/files_reminders/composer/composer.json | 13 + apps/files_reminders/composer/composer.lock | 18 + .../composer/composer/ClassLoader.php | 579 ++++++++++++++++++ .../composer/composer/InstalledVersions.php | 359 +++++++++++ .../files_reminders/composer/composer/LICENSE | 21 + .../composer/composer/autoload_classmap.php | 23 + .../composer/composer/autoload_namespaces.php | 9 + .../composer/composer/autoload_psr4.php | 10 + .../composer/composer/autoload_real.php | 37 ++ .../composer/composer/autoload_static.php | 49 ++ .../composer/composer/installed.json | 5 + .../composer/composer/installed.php | 23 + 13 files changed, 1171 insertions(+) create mode 100644 apps/files_reminders/composer/autoload.php create mode 100644 apps/files_reminders/composer/composer.json create mode 100644 apps/files_reminders/composer/composer.lock create mode 100644 apps/files_reminders/composer/composer/ClassLoader.php create mode 100644 apps/files_reminders/composer/composer/InstalledVersions.php create mode 100644 apps/files_reminders/composer/composer/LICENSE create mode 100644 apps/files_reminders/composer/composer/autoload_classmap.php create mode 100644 apps/files_reminders/composer/composer/autoload_namespaces.php create mode 100644 apps/files_reminders/composer/composer/autoload_psr4.php create mode 100644 apps/files_reminders/composer/composer/autoload_real.php create mode 100644 apps/files_reminders/composer/composer/autoload_static.php create mode 100644 apps/files_reminders/composer/composer/installed.json create mode 100644 apps/files_reminders/composer/composer/installed.php diff --git a/apps/files_reminders/composer/autoload.php b/apps/files_reminders/composer/autoload.php new file mode 100644 index 0000000000000..ec4c8a9b85faa --- /dev/null +++ b/apps/files_reminders/composer/autoload.php @@ -0,0 +1,25 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Autoload; + +/** + * ClassLoader implements a PSR-0, PSR-4 and classmap class loader. + * + * $loader = new \Composer\Autoload\ClassLoader(); + * + * // register classes with namespaces + * $loader->add('Symfony\Component', __DIR__.'/component'); + * $loader->add('Symfony', __DIR__.'/framework'); + * + * // activate the autoloader + * $loader->register(); + * + * // to enable searching the include path (eg. for PEAR packages) + * $loader->setUseIncludePath(true); + * + * In this example, if you try to use a class in the Symfony\Component + * namespace or one of its children (Symfony\Component\Console for instance), + * the autoloader will first look for the class under the component/ + * directory, and it will then fallback to the framework/ directory if not + * found before giving up. + * + * This class is loosely based on the Symfony UniversalClassLoader. + * + * @author Fabien Potencier + * @author Jordi Boggiano + * @see https://www.php-fig.org/psr/psr-0/ + * @see https://www.php-fig.org/psr/psr-4/ + */ +class ClassLoader +{ + /** @var \Closure(string):void */ + private static $includeFile; + + /** @var string|null */ + private $vendorDir; + + // PSR-4 + /** + * @var array> + */ + private $prefixLengthsPsr4 = array(); + /** + * @var array> + */ + private $prefixDirsPsr4 = array(); + /** + * @var list + */ + private $fallbackDirsPsr4 = array(); + + // PSR-0 + /** + * List of PSR-0 prefixes + * + * Structured as array('F (first letter)' => array('Foo\Bar (full prefix)' => array('path', 'path2'))) + * + * @var array>> + */ + private $prefixesPsr0 = array(); + /** + * @var list + */ + private $fallbackDirsPsr0 = array(); + + /** @var bool */ + private $useIncludePath = false; + + /** + * @var array + */ + private $classMap = array(); + + /** @var bool */ + private $classMapAuthoritative = false; + + /** + * @var array + */ + private $missingClasses = array(); + + /** @var string|null */ + private $apcuPrefix; + + /** + * @var array + */ + private static $registeredLoaders = array(); + + /** + * @param string|null $vendorDir + */ + public function __construct($vendorDir = null) + { + $this->vendorDir = $vendorDir; + self::initializeIncludeClosure(); + } + + /** + * @return array> + */ + public function getPrefixes() + { + if (!empty($this->prefixesPsr0)) { + return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); + } + + return array(); + } + + /** + * @return array> + */ + public function getPrefixesPsr4() + { + return $this->prefixDirsPsr4; + } + + /** + * @return list + */ + public function getFallbackDirs() + { + return $this->fallbackDirsPsr0; + } + + /** + * @return list + */ + public function getFallbackDirsPsr4() + { + return $this->fallbackDirsPsr4; + } + + /** + * @return array Array of classname => path + */ + public function getClassMap() + { + return $this->classMap; + } + + /** + * @param array $classMap Class to filename map + * + * @return void + */ + public function addClassMap(array $classMap) + { + if ($this->classMap) { + $this->classMap = array_merge($this->classMap, $classMap); + } else { + $this->classMap = $classMap; + } + } + + /** + * Registers a set of PSR-0 directories for a given prefix, either + * appending or prepending to the ones previously set for this prefix. + * + * @param string $prefix The prefix + * @param list|string $paths The PSR-0 root directories + * @param bool $prepend Whether to prepend the directories + * + * @return void + */ + public function add($prefix, $paths, $prepend = false) + { + $paths = (array) $paths; + if (!$prefix) { + if ($prepend) { + $this->fallbackDirsPsr0 = array_merge( + $paths, + $this->fallbackDirsPsr0 + ); + } else { + $this->fallbackDirsPsr0 = array_merge( + $this->fallbackDirsPsr0, + $paths + ); + } + + return; + } + + $first = $prefix[0]; + if (!isset($this->prefixesPsr0[$first][$prefix])) { + $this->prefixesPsr0[$first][$prefix] = $paths; + + return; + } + if ($prepend) { + $this->prefixesPsr0[$first][$prefix] = array_merge( + $paths, + $this->prefixesPsr0[$first][$prefix] + ); + } else { + $this->prefixesPsr0[$first][$prefix] = array_merge( + $this->prefixesPsr0[$first][$prefix], + $paths + ); + } + } + + /** + * Registers a set of PSR-4 directories for a given namespace, either + * appending or prepending to the ones previously set for this namespace. + * + * @param string $prefix The prefix/namespace, with trailing '\\' + * @param list|string $paths The PSR-4 base directories + * @param bool $prepend Whether to prepend the directories + * + * @throws \InvalidArgumentException + * + * @return void + */ + public function addPsr4($prefix, $paths, $prepend = false) + { + $paths = (array) $paths; + if (!$prefix) { + // Register directories for the root namespace. + if ($prepend) { + $this->fallbackDirsPsr4 = array_merge( + $paths, + $this->fallbackDirsPsr4 + ); + } else { + $this->fallbackDirsPsr4 = array_merge( + $this->fallbackDirsPsr4, + $paths + ); + } + } elseif (!isset($this->prefixDirsPsr4[$prefix])) { + // Register directories for a new namespace. + $length = strlen($prefix); + if ('\\' !== $prefix[$length - 1]) { + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); + } + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; + $this->prefixDirsPsr4[$prefix] = $paths; + } elseif ($prepend) { + // Prepend directories for an already registered namespace. + $this->prefixDirsPsr4[$prefix] = array_merge( + $paths, + $this->prefixDirsPsr4[$prefix] + ); + } else { + // Append directories for an already registered namespace. + $this->prefixDirsPsr4[$prefix] = array_merge( + $this->prefixDirsPsr4[$prefix], + $paths + ); + } + } + + /** + * Registers a set of PSR-0 directories for a given prefix, + * replacing any others previously set for this prefix. + * + * @param string $prefix The prefix + * @param list|string $paths The PSR-0 base directories + * + * @return void + */ + public function set($prefix, $paths) + { + if (!$prefix) { + $this->fallbackDirsPsr0 = (array) $paths; + } else { + $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; + } + } + + /** + * Registers a set of PSR-4 directories for a given namespace, + * replacing any others previously set for this namespace. + * + * @param string $prefix The prefix/namespace, with trailing '\\' + * @param list|string $paths The PSR-4 base directories + * + * @throws \InvalidArgumentException + * + * @return void + */ + public function setPsr4($prefix, $paths) + { + if (!$prefix) { + $this->fallbackDirsPsr4 = (array) $paths; + } else { + $length = strlen($prefix); + if ('\\' !== $prefix[$length - 1]) { + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); + } + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; + $this->prefixDirsPsr4[$prefix] = (array) $paths; + } + } + + /** + * Turns on searching the include path for class files. + * + * @param bool $useIncludePath + * + * @return void + */ + public function setUseIncludePath($useIncludePath) + { + $this->useIncludePath = $useIncludePath; + } + + /** + * Can be used to check if the autoloader uses the include path to check + * for classes. + * + * @return bool + */ + public function getUseIncludePath() + { + return $this->useIncludePath; + } + + /** + * Turns off searching the prefix and fallback directories for classes + * that have not been registered with the class map. + * + * @param bool $classMapAuthoritative + * + * @return void + */ + public function setClassMapAuthoritative($classMapAuthoritative) + { + $this->classMapAuthoritative = $classMapAuthoritative; + } + + /** + * Should class lookup fail if not found in the current class map? + * + * @return bool + */ + public function isClassMapAuthoritative() + { + return $this->classMapAuthoritative; + } + + /** + * APCu prefix to use to cache found/not-found classes, if the extension is enabled. + * + * @param string|null $apcuPrefix + * + * @return void + */ + public function setApcuPrefix($apcuPrefix) + { + $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; + } + + /** + * The APCu prefix in use, or null if APCu caching is not enabled. + * + * @return string|null + */ + public function getApcuPrefix() + { + return $this->apcuPrefix; + } + + /** + * Registers this instance as an autoloader. + * + * @param bool $prepend Whether to prepend the autoloader or not + * + * @return void + */ + public function register($prepend = false) + { + spl_autoload_register(array($this, 'loadClass'), true, $prepend); + + if (null === $this->vendorDir) { + return; + } + + if ($prepend) { + self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; + } else { + unset(self::$registeredLoaders[$this->vendorDir]); + self::$registeredLoaders[$this->vendorDir] = $this; + } + } + + /** + * Unregisters this instance as an autoloader. + * + * @return void + */ + public function unregister() + { + spl_autoload_unregister(array($this, 'loadClass')); + + if (null !== $this->vendorDir) { + unset(self::$registeredLoaders[$this->vendorDir]); + } + } + + /** + * Loads the given class or interface. + * + * @param string $class The name of the class + * @return true|null True if loaded, null otherwise + */ + public function loadClass($class) + { + if ($file = $this->findFile($class)) { + $includeFile = self::$includeFile; + $includeFile($file); + + return true; + } + + return null; + } + + /** + * Finds the path to the file where the class is defined. + * + * @param string $class The name of the class + * + * @return string|false The path if found, false otherwise + */ + public function findFile($class) + { + // class map lookup + if (isset($this->classMap[$class])) { + return $this->classMap[$class]; + } + if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { + return false; + } + if (null !== $this->apcuPrefix) { + $file = apcu_fetch($this->apcuPrefix.$class, $hit); + if ($hit) { + return $file; + } + } + + $file = $this->findFileWithExtension($class, '.php'); + + // Search for Hack files if we are running on HHVM + if (false === $file && defined('HHVM_VERSION')) { + $file = $this->findFileWithExtension($class, '.hh'); + } + + if (null !== $this->apcuPrefix) { + apcu_add($this->apcuPrefix.$class, $file); + } + + if (false === $file) { + // Remember that this class does not exist. + $this->missingClasses[$class] = true; + } + + return $file; + } + + /** + * Returns the currently registered loaders keyed by their corresponding vendor directories. + * + * @return array + */ + public static function getRegisteredLoaders() + { + return self::$registeredLoaders; + } + + /** + * @param string $class + * @param string $ext + * @return string|false + */ + private function findFileWithExtension($class, $ext) + { + // PSR-4 lookup + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; + + $first = $class[0]; + if (isset($this->prefixLengthsPsr4[$first])) { + $subPath = $class; + while (false !== $lastPos = strrpos($subPath, '\\')) { + $subPath = substr($subPath, 0, $lastPos); + $search = $subPath . '\\'; + if (isset($this->prefixDirsPsr4[$search])) { + $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); + foreach ($this->prefixDirsPsr4[$search] as $dir) { + if (file_exists($file = $dir . $pathEnd)) { + return $file; + } + } + } + } + } + + // PSR-4 fallback dirs + foreach ($this->fallbackDirsPsr4 as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { + return $file; + } + } + + // PSR-0 lookup + if (false !== $pos = strrpos($class, '\\')) { + // namespaced class name + $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) + . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); + } else { + // PEAR-like class name + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; + } + + if (isset($this->prefixesPsr0[$first])) { + foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { + if (0 === strpos($class, $prefix)) { + foreach ($dirs as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { + return $file; + } + } + } + } + } + + // PSR-0 fallback dirs + foreach ($this->fallbackDirsPsr0 as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { + return $file; + } + } + + // PSR-0 include paths. + if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { + return $file; + } + + return false; + } + + /** + * @return void + */ + private static function initializeIncludeClosure() + { + if (self::$includeFile !== null) { + return; + } + + /** + * Scope isolated include. + * + * Prevents access to $this/self from included files. + * + * @param string $file + * @return void + */ + self::$includeFile = \Closure::bind(static function($file) { + include $file; + }, null, null); + } +} diff --git a/apps/files_reminders/composer/composer/InstalledVersions.php b/apps/files_reminders/composer/composer/InstalledVersions.php new file mode 100644 index 0000000000000..51e734a774b3e --- /dev/null +++ b/apps/files_reminders/composer/composer/InstalledVersions.php @@ -0,0 +1,359 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer; + +use Composer\Autoload\ClassLoader; +use Composer\Semver\VersionParser; + +/** + * This class is copied in every Composer installed project and available to all + * + * See also https://getcomposer.org/doc/07-runtime.md#installed-versions + * + * To require its presence, you can require `composer-runtime-api ^2.0` + * + * @final + */ +class InstalledVersions +{ + /** + * @var mixed[]|null + * @psalm-var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array}|array{}|null + */ + private static $installed; + + /** + * @var bool|null + */ + private static $canGetVendors; + + /** + * @var array[] + * @psalm-var array}> + */ + private static $installedByVendor = array(); + + /** + * Returns a list of all package names which are present, either by being installed, replaced or provided + * + * @return string[] + * @psalm-return list + */ + public static function getInstalledPackages() + { + $packages = array(); + foreach (self::getInstalled() as $installed) { + $packages[] = array_keys($installed['versions']); + } + + if (1 === \count($packages)) { + return $packages[0]; + } + + return array_keys(array_flip(\call_user_func_array('array_merge', $packages))); + } + + /** + * Returns a list of all package names with a specific type e.g. 'library' + * + * @param string $type + * @return string[] + * @psalm-return list + */ + public static function getInstalledPackagesByType($type) + { + $packagesByType = array(); + + foreach (self::getInstalled() as $installed) { + foreach ($installed['versions'] as $name => $package) { + if (isset($package['type']) && $package['type'] === $type) { + $packagesByType[] = $name; + } + } + } + + return $packagesByType; + } + + /** + * Checks whether the given package is installed + * + * This also returns true if the package name is provided or replaced by another package + * + * @param string $packageName + * @param bool $includeDevRequirements + * @return bool + */ + public static function isInstalled($packageName, $includeDevRequirements = true) + { + foreach (self::getInstalled() as $installed) { + if (isset($installed['versions'][$packageName])) { + return $includeDevRequirements || !isset($installed['versions'][$packageName]['dev_requirement']) || $installed['versions'][$packageName]['dev_requirement'] === false; + } + } + + return false; + } + + /** + * Checks whether the given package satisfies a version constraint + * + * e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call: + * + * Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3') + * + * @param VersionParser $parser Install composer/semver to have access to this class and functionality + * @param string $packageName + * @param string|null $constraint A version constraint to check for, if you pass one you have to make sure composer/semver is required by your package + * @return bool + */ + public static function satisfies(VersionParser $parser, $packageName, $constraint) + { + $constraint = $parser->parseConstraints((string) $constraint); + $provided = $parser->parseConstraints(self::getVersionRanges($packageName)); + + return $provided->matches($constraint); + } + + /** + * Returns a version constraint representing all the range(s) which are installed for a given package + * + * It is easier to use this via isInstalled() with the $constraint argument if you need to check + * whether a given version of a package is installed, and not just whether it exists + * + * @param string $packageName + * @return string Version constraint usable with composer/semver + */ + public static function getVersionRanges($packageName) + { + foreach (self::getInstalled() as $installed) { + if (!isset($installed['versions'][$packageName])) { + continue; + } + + $ranges = array(); + if (isset($installed['versions'][$packageName]['pretty_version'])) { + $ranges[] = $installed['versions'][$packageName]['pretty_version']; + } + if (array_key_exists('aliases', $installed['versions'][$packageName])) { + $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']); + } + if (array_key_exists('replaced', $installed['versions'][$packageName])) { + $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']); + } + if (array_key_exists('provided', $installed['versions'][$packageName])) { + $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']); + } + + return implode(' || ', $ranges); + } + + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); + } + + /** + * @param string $packageName + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present + */ + public static function getVersion($packageName) + { + foreach (self::getInstalled() as $installed) { + if (!isset($installed['versions'][$packageName])) { + continue; + } + + if (!isset($installed['versions'][$packageName]['version'])) { + return null; + } + + return $installed['versions'][$packageName]['version']; + } + + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); + } + + /** + * @param string $packageName + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present + */ + public static function getPrettyVersion($packageName) + { + foreach (self::getInstalled() as $installed) { + if (!isset($installed['versions'][$packageName])) { + continue; + } + + if (!isset($installed['versions'][$packageName]['pretty_version'])) { + return null; + } + + return $installed['versions'][$packageName]['pretty_version']; + } + + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); + } + + /** + * @param string $packageName + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference + */ + public static function getReference($packageName) + { + foreach (self::getInstalled() as $installed) { + if (!isset($installed['versions'][$packageName])) { + continue; + } + + if (!isset($installed['versions'][$packageName]['reference'])) { + return null; + } + + return $installed['versions'][$packageName]['reference']; + } + + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); + } + + /** + * @param string $packageName + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as install path. Packages of type metapackages also have a null install path. + */ + public static function getInstallPath($packageName) + { + foreach (self::getInstalled() as $installed) { + if (!isset($installed['versions'][$packageName])) { + continue; + } + + return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null; + } + + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); + } + + /** + * @return array + * @psalm-return array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool} + */ + public static function getRootPackage() + { + $installed = self::getInstalled(); + + return $installed[0]['root']; + } + + /** + * Returns the raw installed.php data for custom implementations + * + * @deprecated Use getAllRawData() instead which returns all datasets for all autoloaders present in the process. getRawData only returns the first dataset loaded, which may not be what you expect. + * @return array[] + * @psalm-return array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array} + */ + public static function getRawData() + { + @trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED); + + if (null === self::$installed) { + // only require the installed.php file if this file is loaded from its dumped location, + // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 + if (substr(__DIR__, -8, 1) !== 'C') { + self::$installed = include __DIR__ . '/installed.php'; + } else { + self::$installed = array(); + } + } + + return self::$installed; + } + + /** + * Returns the raw data of all installed.php which are currently loaded for custom implementations + * + * @return array[] + * @psalm-return list}> + */ + public static function getAllRawData() + { + return self::getInstalled(); + } + + /** + * Lets you reload the static array from another file + * + * This is only useful for complex integrations in which a project needs to use + * this class but then also needs to execute another project's autoloader in process, + * and wants to ensure both projects have access to their version of installed.php. + * + * A typical case would be PHPUnit, where it would need to make sure it reads all + * the data it needs from this class, then call reload() with + * `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure + * the project in which it runs can then also use this class safely, without + * interference between PHPUnit's dependencies and the project's dependencies. + * + * @param array[] $data A vendor/composer/installed.php data set + * @return void + * + * @psalm-param array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array} $data + */ + public static function reload($data) + { + self::$installed = $data; + self::$installedByVendor = array(); + } + + /** + * @return array[] + * @psalm-return list}> + */ + private static function getInstalled() + { + if (null === self::$canGetVendors) { + self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders'); + } + + $installed = array(); + + if (self::$canGetVendors) { + foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) { + if (isset(self::$installedByVendor[$vendorDir])) { + $installed[] = self::$installedByVendor[$vendorDir]; + } elseif (is_file($vendorDir.'/composer/installed.php')) { + /** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array} $required */ + $required = require $vendorDir.'/composer/installed.php'; + $installed[] = self::$installedByVendor[$vendorDir] = $required; + if (null === self::$installed && strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) { + self::$installed = $installed[count($installed) - 1]; + } + } + } + } + + if (null === self::$installed) { + // only require the installed.php file if this file is loaded from its dumped location, + // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 + if (substr(__DIR__, -8, 1) !== 'C') { + /** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array} $required */ + $required = require __DIR__ . '/installed.php'; + self::$installed = $required; + } else { + self::$installed = array(); + } + } + + if (self::$installed !== array()) { + $installed[] = self::$installed; + } + + return $installed; + } +} diff --git a/apps/files_reminders/composer/composer/LICENSE b/apps/files_reminders/composer/composer/LICENSE new file mode 100644 index 0000000000000..f27399a042d95 --- /dev/null +++ b/apps/files_reminders/composer/composer/LICENSE @@ -0,0 +1,21 @@ + +Copyright (c) Nils Adermann, Jordi Boggiano + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/apps/files_reminders/composer/composer/autoload_classmap.php b/apps/files_reminders/composer/composer/autoload_classmap.php new file mode 100644 index 0000000000000..29f781d0006a0 --- /dev/null +++ b/apps/files_reminders/composer/composer/autoload_classmap.php @@ -0,0 +1,23 @@ + $vendorDir . '/composer/InstalledVersions.php', + 'OCA\\FilesReminders\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php', + 'OCA\\FilesReminders\\BackgroundJob\\CleanUpReminders' => $baseDir . '/../lib/BackgroundJob/CleanUpReminders.php', + 'OCA\\FilesReminders\\BackgroundJob\\ScheduledNotifications' => $baseDir . '/../lib/BackgroundJob/ScheduledNotifications.php', + 'OCA\\FilesReminders\\Command\\ListCommand' => $baseDir . '/../lib/Command/ListCommand.php', + 'OCA\\FilesReminders\\Controller\\ApiController' => $baseDir . '/../lib/Controller/ApiController.php', + 'OCA\\FilesReminders\\Db\\Reminder' => $baseDir . '/../lib/Db/Reminder.php', + '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\\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', + 'OCA\\FilesReminders\\Service\\ReminderService' => $baseDir . '/../lib/Service/ReminderService.php', +); diff --git a/apps/files_reminders/composer/composer/autoload_namespaces.php b/apps/files_reminders/composer/composer/autoload_namespaces.php new file mode 100644 index 0000000000000..3f5c929625125 --- /dev/null +++ b/apps/files_reminders/composer/composer/autoload_namespaces.php @@ -0,0 +1,9 @@ + array($baseDir . '/../lib'), +); diff --git a/apps/files_reminders/composer/composer/autoload_real.php b/apps/files_reminders/composer/composer/autoload_real.php new file mode 100644 index 0000000000000..f7b0857737eb5 --- /dev/null +++ b/apps/files_reminders/composer/composer/autoload_real.php @@ -0,0 +1,37 @@ +setClassMapAuthoritative(true); + $loader->register(true); + + return $loader; + } +} diff --git a/apps/files_reminders/composer/composer/autoload_static.php b/apps/files_reminders/composer/composer/autoload_static.php new file mode 100644 index 0000000000000..efea8791020d8 --- /dev/null +++ b/apps/files_reminders/composer/composer/autoload_static.php @@ -0,0 +1,49 @@ + + array ( + 'OCA\\FilesReminders\\' => 19, + ), + ); + + public static $prefixDirsPsr4 = array ( + 'OCA\\FilesReminders\\' => + array ( + 0 => __DIR__ . '/..' . '/../lib', + ), + ); + + public static $classMap = array ( + 'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php', + 'OCA\\FilesReminders\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php', + 'OCA\\FilesReminders\\BackgroundJob\\CleanUpReminders' => __DIR__ . '/..' . '/../lib/BackgroundJob/CleanUpReminders.php', + 'OCA\\FilesReminders\\BackgroundJob\\ScheduledNotifications' => __DIR__ . '/..' . '/../lib/BackgroundJob/ScheduledNotifications.php', + 'OCA\\FilesReminders\\Command\\ListCommand' => __DIR__ . '/..' . '/../lib/Command/ListCommand.php', + 'OCA\\FilesReminders\\Controller\\ApiController' => __DIR__ . '/..' . '/../lib/Controller/ApiController.php', + 'OCA\\FilesReminders\\Db\\Reminder' => __DIR__ . '/..' . '/../lib/Db/Reminder.php', + '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\\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', + 'OCA\\FilesReminders\\Service\\ReminderService' => __DIR__ . '/..' . '/../lib/Service/ReminderService.php', + ); + + public static function getInitializer(ClassLoader $loader) + { + return \Closure::bind(function () use ($loader) { + $loader->prefixLengthsPsr4 = ComposerStaticInitFilesReminders::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInitFilesReminders::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInitFilesReminders::$classMap; + + }, null, ClassLoader::class); + } +} diff --git a/apps/files_reminders/composer/composer/installed.json b/apps/files_reminders/composer/composer/installed.json new file mode 100644 index 0000000000000..f20a6c47c6d4f --- /dev/null +++ b/apps/files_reminders/composer/composer/installed.json @@ -0,0 +1,5 @@ +{ + "packages": [], + "dev": false, + "dev-package-names": [] +} diff --git a/apps/files_reminders/composer/composer/installed.php b/apps/files_reminders/composer/composer/installed.php new file mode 100644 index 0000000000000..c398e796c3212 --- /dev/null +++ b/apps/files_reminders/composer/composer/installed.php @@ -0,0 +1,23 @@ + array( + 'name' => '__root__', + 'pretty_version' => '1.0.0+no-version-set', + 'version' => '1.0.0.0', + 'reference' => NULL, + 'type' => 'library', + 'install_path' => __DIR__ . '/../', + 'aliases' => array(), + 'dev' => false, + ), + 'versions' => array( + '__root__' => array( + 'pretty_version' => '1.0.0+no-version-set', + 'version' => '1.0.0.0', + 'reference' => NULL, + 'type' => 'library', + 'install_path' => __DIR__ . '/../', + 'aliases' => array(), + 'dev_requirement' => false, + ), + ), +); From beb8a27d9493ac97cb390a101c4ee022d0f10d82 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 12/38] enh: add to shipped Signed-off-by: Christopher Ng --- core/shipped.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/shipped.json b/core/shipped.json index c44d71c8103ed..2cd0fbd94667b 100644 --- a/core/shipped.json +++ b/core/shipped.json @@ -15,6 +15,7 @@ "files", "files_external", "files_pdfviewer", + "files_reminders", "files_rightclick", "files_sharing", "files_trashbin", @@ -59,6 +60,7 @@ "federation", "files", "files_pdfviewer", + "files_reminders", "files_rightclick", "files_sharing", "files_trashbin", From fa775641525ab834481f3b3fb5dd6a7ed75593dc Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 13/38] enh: rename to due date Signed-off-by: Christopher Ng --- .../lib/BackgroundJob/CleanUpReminders.php | 1 + .../BackgroundJob/ScheduledNotifications.php | 2 +- .../lib/Command/ListCommand.php | 4 +-- .../lib/Controller/ApiController.php | 12 ++++---- apps/files_reminders/lib/Db/Reminder.php | 8 +++--- .../files_reminders/lib/Db/ReminderMapper.php | 28 +++++++++---------- .../Version10000Date20230725162149.php | 4 +-- .../lib/Model/RichReminder.php | 2 +- .../lib/Service/ReminderService.php | 8 +++--- 9 files changed, 35 insertions(+), 34 deletions(-) diff --git a/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php b/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php index 43d0afe3e10f7..581b4c8935128 100644 --- a/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php +++ b/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php @@ -37,6 +37,7 @@ public function __construct( private ReminderService $reminderService, ) { parent::__construct($time); + $this->setInterval(60 * 60 * 24); $this->setTimeSensitivity(IJob::TIME_INSENSITIVE); } diff --git a/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php b/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php index 9efb6acb53b37..df801cea06369 100644 --- a/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php +++ b/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php @@ -48,7 +48,7 @@ public function __construct( * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function run($argument) { - $reminders = $this->reminderMapper->findToRemind(); + $reminders = $this->reminderMapper->findOverdue(); foreach ($reminders as $reminder) { try { $this->reminderService->send($reminder); diff --git a/apps/files_reminders/lib/Command/ListCommand.php b/apps/files_reminders/lib/Command/ListCommand.php index 074aeb8723901..7d6044f21c690 100644 --- a/apps/files_reminders/lib/Command/ListCommand.php +++ b/apps/files_reminders/lib/Command/ListCommand.php @@ -75,12 +75,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $io->table( - ['UserId', 'Path', 'RemindAt', 'CreatedAt', 'Notified'], + ['UserId', 'Path', 'DueDate', 'CreatedAt', 'Notified'], array_map( fn (RichReminder $reminder) => [ $reminder->getUserId(), $reminder->getNode()->getPath(), - $reminder->getRemindAt()->format(DateTimeInterface::ATOM), // ISO 8601 + $reminder->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601 $reminder->getCreatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 $reminder->getNotified() ? 'true' : 'false', ], diff --git a/apps/files_reminders/lib/Controller/ApiController.php b/apps/files_reminders/lib/Controller/ApiController.php index b372a871e3bc1..10a6654d6715a 100644 --- a/apps/files_reminders/lib/Controller/ApiController.php +++ b/apps/files_reminders/lib/Controller/ApiController.php @@ -63,13 +63,13 @@ public function get(int $fileId): JSONResponse { try { $reminder = $this->reminderService->getDueForUser($user, $fileId); $reminderData = [ - 'remindAt' => $reminder->getRemindAt()->format(DateTimeInterface::ATOM), // ISO 8601 + 'dueDate' => $reminder->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601 ]; return new JSONResponse($reminderData, Http::STATUS_OK); } catch (DoesNotExistException $e) { // Return null when no reminder is found $reminderData = [ - 'remindAt' => null, + 'dueDate' => null, ]; return new JSONResponse($reminderData, Http::STATUS_OK); } catch (Throwable $th) { @@ -81,11 +81,11 @@ public function get(int $fileId): JSONResponse { /** * Create a reminder * - * @param string $remindAt ISO 8601 formatted date time string + * @param string $dueDate ISO 8601 formatted date time string */ - public function create(int $fileId, string $remindAt): JSONResponse { + public function create(int $fileId, string $dueDate): JSONResponse { try { - $remindAt = (new DateTime($remindAt))->setTimezone(new DateTimeZone('UTC')); + $dueDate = (new DateTime($dueDate))->setTimezone(new DateTimeZone('UTC')); } catch (Exception $e) { $this->logger->error($e->getMessage(), ['exception' => $e]); return new JSONResponse([], Http::STATUS_BAD_REQUEST); @@ -97,7 +97,7 @@ public function create(int $fileId, string $remindAt): JSONResponse { } try { - $this->reminderService->create($user, $fileId, $remindAt); + $this->reminderService->create($user, $fileId, $dueDate); return new JSONResponse([], Http::STATUS_OK); } catch (Throwable $th) { $this->logger->error($th->getMessage(), ['exception' => $th]); diff --git a/apps/files_reminders/lib/Db/Reminder.php b/apps/files_reminders/lib/Db/Reminder.php index 1abaa48d2008b..3a3641644f7d2 100644 --- a/apps/files_reminders/lib/Db/Reminder.php +++ b/apps/files_reminders/lib/Db/Reminder.php @@ -36,8 +36,8 @@ * @method void setFileId(int $fileId) * @method int getFileId() * - * @method void setRemindAt(DateTime $remindAt) - * @method DateTime getRemindAt() + * @method void setDueDate(DateTime $dueDate) + * @method DateTime getDueDate() * * @method void setCreatedAt(DateTime $createdAt) * @method DateTime getCreatedAt() @@ -48,14 +48,14 @@ class Reminder extends Entity { protected $userId; protected $fileId; - protected $remindAt; + protected $dueDate; protected $createdAt; protected $notified = false; public function __construct() { $this->addType('userId', 'string'); $this->addType('fileId', 'integer'); - $this->addType('remindAt', 'datetime'); + $this->addType('dueDate', 'datetime'); $this->addType('createdAt', 'datetime'); $this->addType('notified', 'boolean'); } diff --git a/apps/files_reminders/lib/Db/ReminderMapper.php b/apps/files_reminders/lib/Db/ReminderMapper.php index 7368ef2acc7d5..422082702622a 100644 --- a/apps/files_reminders/lib/Db/ReminderMapper.php +++ b/apps/files_reminders/lib/Db/ReminderMapper.php @@ -50,13 +50,13 @@ public function markNotified(Reminder $reminder): Reminder { $reminderUpdate = new Reminder(); $reminderUpdate->setId($reminder->getId()); $reminderUpdate->setNotified(true); - return parent::update($reminderUpdate); + return $this->update($reminderUpdate); } public function find(int $id): Reminder { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified') + $qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); @@ -69,7 +69,7 @@ public function find(int $id): Reminder { public function findDueForUser(IUser $user, int $fileId): Reminder { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified') + $qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR))) ->andWhere($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) @@ -86,9 +86,9 @@ public function findDueForUser(IUser $user, int $fileId): Reminder { public function findAll() { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified') + $qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified') ->from($this->getTableName()) - ->orderBy('remind_at', 'ASC'); + ->orderBy('due_date', 'ASC'); return $this->findEntities($qb); } @@ -99,10 +99,10 @@ public function findAll() { public function findAllForUser(IUser $user) { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified') + $qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR))) - ->orderBy('remind_at', 'ASC'); + ->orderBy('due_date', 'ASC'); return $this->findEntities($qb); } @@ -110,14 +110,14 @@ public function findAllForUser(IUser $user) { /** * @return Reminder[] */ - public function findToRemind() { + public function findOverdue() { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified') + $qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified') ->from($this->getTableName()) - ->where($qb->expr()->lt('remind_at', $qb->createFunction('NOW()'))) + ->where($qb->expr()->lt('due_date', $qb->createFunction('NOW()'))) ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) - ->orderBy('remind_at', 'ASC'); + ->orderBy('due_date', 'ASC'); return $this->findEntities($qb); } @@ -125,13 +125,13 @@ public function findToRemind() { /** * @return Reminder[] */ - public function findToDelete(?int $limit = null) { + public function findNotified(?int $limit = null) { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'remind_at', 'created_at', 'notified') + $qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('notified', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))) - ->orderBy('remind_at', 'ASC') + ->orderBy('due_date', 'ASC') ->setMaxResults($limit); return $this->findEntities($qb); diff --git a/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php b/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php index 1940d153763eb..20b9331fc66dd 100644 --- a/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php +++ b/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php @@ -57,7 +57,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt 'length' => 20, 'unsigned' => true, ]); - $table->addColumn('remind_at', Types::DATETIME, [ + $table->addColumn('due_date', Types::DATETIME, [ 'notnull' => true, ]); $table->addColumn('created_at', Types::DATETIME, [ @@ -68,7 +68,7 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt 'default' => false, ]); $table->setPrimaryKey(['id']); - $table->addUniqueIndex(['user_id', 'file_id', 'remind_at'], 'reminders_uniq_idx'); + $table->addUniqueIndex(['user_id', 'file_id', 'due_date'], 'reminders_uniq_idx'); return $schema; } diff --git a/apps/files_reminders/lib/Model/RichReminder.php b/apps/files_reminders/lib/Model/RichReminder.php index 76c3d8dd22bc1..9f7e3f3655614 100644 --- a/apps/files_reminders/lib/Model/RichReminder.php +++ b/apps/files_reminders/lib/Model/RichReminder.php @@ -66,7 +66,7 @@ public function jsonSerialize(): array { return [ 'userId' => $this->getUserId(), 'fileId' => $this->getFileId(), - 'remindAt' => $this->getRemindAt()->format(DateTimeInterface::ATOM), // ISO 8601 + 'dueDate' => $this->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601 'createdAt' => $this->getCreatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 'notified' => $this->getNotified(), ]; diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index 7106c8e5514f2..b24a685e3bcbb 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -78,11 +78,11 @@ public function getAll(?IUser $user = null) { ); } - public function create(IUser $user, int $fileId, DateTime $remindAt): void { + public function create(IUser $user, int $fileId, DateTime $dueDate): void { $reminder = new Reminder(); $reminder->setUserId($user->getUID()); $reminder->setFileId($fileId); - $reminder->setRemindAt($remindAt); + $reminder->setDueDate($dueDate); $reminder->setCreatedAt(new DateTime('now', new DateTimeZone('UTC'))); $this->reminderMapper->insert($reminder); } @@ -108,7 +108,7 @@ public function send(Reminder $reminder): void { ->setUser($user->getUID()) ->setObject('reminder', (string)$reminder->getId()) ->setSubject('reminder-due') - ->setDateTime($reminder->getRemindAt()); + ->setDateTime($reminder->getDueDate()); try { $this->notificationManager->notify($notification); @@ -119,7 +119,7 @@ public function send(Reminder $reminder): void { } public function cleanUp(?int $limit = null): void { - $reminders = $this->reminderMapper->findToDelete($limit); + $reminders = $this->reminderMapper->findNotified($limit); foreach ($reminders as $reminder) { $this->reminderMapper->delete($reminder); } From 7637bf2ae04d5558b5771b62cd17b6bc7321ba44 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 14/38] fix: catch Throwable Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Service/ReminderService.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index b24a685e3bcbb..ad230474923a1 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -28,7 +28,6 @@ use DateTime; use DateTimeZone; -use InvalidArgumentException; use OCA\FilesReminders\AppInfo\Application; use OCA\FilesReminders\Db\Reminder; use OCA\FilesReminders\Db\ReminderMapper; @@ -41,6 +40,7 @@ use OCP\IUserManager; use OCP\Notification\IManager as INotificationManager; use Psr\Log\LoggerInterface; +use Throwable; class ReminderService { public function __construct( @@ -113,8 +113,8 @@ public function send(Reminder $reminder): void { try { $this->notificationManager->notify($notification); $this->reminderMapper->markNotified($reminder); - } catch (InvalidArgumentException $e) { - $this->logger->error('Failed to send reminder notification', $e->getTrace()); + } catch (Throwable $th) { + $this->logger->error($th->getMessage(), $th->getTrace()); } } From 5ff178a753c335d2ab6d10cee531490f1026ec42 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 15/38] enh: add updated at Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Command/ListCommand.php | 3 ++- apps/files_reminders/lib/Db/Reminder.php | 5 +++++ apps/files_reminders/lib/Db/ReminderMapper.php | 14 +++++++------- .../Migration/Version10000Date20230725162149.php | 3 +++ apps/files_reminders/lib/Model/RichReminder.php | 1 + 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/apps/files_reminders/lib/Command/ListCommand.php b/apps/files_reminders/lib/Command/ListCommand.php index 7d6044f21c690..b066c6ed8cfa6 100644 --- a/apps/files_reminders/lib/Command/ListCommand.php +++ b/apps/files_reminders/lib/Command/ListCommand.php @@ -75,12 +75,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $io->table( - ['UserId', 'Path', 'DueDate', 'CreatedAt', 'Notified'], + ['User Id', 'Path', 'Due Date', 'Updated At', 'Created At', 'Notified'], array_map( fn (RichReminder $reminder) => [ $reminder->getUserId(), $reminder->getNode()->getPath(), $reminder->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601 + $reminder->getUpdatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 $reminder->getCreatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 $reminder->getNotified() ? 'true' : 'false', ], diff --git a/apps/files_reminders/lib/Db/Reminder.php b/apps/files_reminders/lib/Db/Reminder.php index 3a3641644f7d2..36f967f1434b8 100644 --- a/apps/files_reminders/lib/Db/Reminder.php +++ b/apps/files_reminders/lib/Db/Reminder.php @@ -39,6 +39,9 @@ * @method void setDueDate(DateTime $dueDate) * @method DateTime getDueDate() * + * @method void setUpdatedAt(DateTime $updatedAt) + * @method DateTime getUpdatedAt() + * * @method void setCreatedAt(DateTime $createdAt) * @method DateTime getCreatedAt() * @@ -49,6 +52,7 @@ class Reminder extends Entity { protected $userId; protected $fileId; protected $dueDate; + protected $updatedAt; protected $createdAt; protected $notified = false; @@ -56,6 +60,7 @@ public function __construct() { $this->addType('userId', 'string'); $this->addType('fileId', 'integer'); $this->addType('dueDate', 'datetime'); + $this->addType('updatedAt', 'datetime'); $this->addType('createdAt', 'datetime'); $this->addType('notified', 'boolean'); } diff --git a/apps/files_reminders/lib/Db/ReminderMapper.php b/apps/files_reminders/lib/Db/ReminderMapper.php index 422082702622a..4ddea3b303cde 100644 --- a/apps/files_reminders/lib/Db/ReminderMapper.php +++ b/apps/files_reminders/lib/Db/ReminderMapper.php @@ -56,7 +56,7 @@ public function markNotified(Reminder $reminder): Reminder { public function find(int $id): Reminder { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified') + $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); @@ -69,12 +69,12 @@ public function find(int $id): Reminder { public function findDueForUser(IUser $user, int $fileId): Reminder { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified') + $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR))) ->andWhere($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) - ->orderBy('created_at', 'DESC') + ->orderBy('updated_at', 'DESC') ->setMaxResults(1); return $this->findEntity($qb); @@ -86,7 +86,7 @@ public function findDueForUser(IUser $user, int $fileId): Reminder { public function findAll() { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified') + $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->orderBy('due_date', 'ASC'); @@ -99,7 +99,7 @@ public function findAll() { public function findAllForUser(IUser $user) { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified') + $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR))) ->orderBy('due_date', 'ASC'); @@ -113,7 +113,7 @@ public function findAllForUser(IUser $user) { public function findOverdue() { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified') + $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->lt('due_date', $qb->createFunction('NOW()'))) ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) @@ -128,7 +128,7 @@ public function findOverdue() { public function findNotified(?int $limit = null) { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'created_at', 'notified') + $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('notified', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))) ->orderBy('due_date', 'ASC') diff --git a/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php b/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php index 20b9331fc66dd..0b7505d64e3e7 100644 --- a/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php +++ b/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php @@ -60,6 +60,9 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt $table->addColumn('due_date', Types::DATETIME, [ 'notnull' => true, ]); + $table->addColumn('updated_at', Types::DATETIME, [ + 'notnull' => true, + ]); $table->addColumn('created_at', Types::DATETIME, [ 'notnull' => true, ]); diff --git a/apps/files_reminders/lib/Model/RichReminder.php b/apps/files_reminders/lib/Model/RichReminder.php index 9f7e3f3655614..1ac1baae370db 100644 --- a/apps/files_reminders/lib/Model/RichReminder.php +++ b/apps/files_reminders/lib/Model/RichReminder.php @@ -67,6 +67,7 @@ public function jsonSerialize(): array { 'userId' => $this->getUserId(), 'fileId' => $this->getFileId(), 'dueDate' => $this->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601 + 'updatedAt' => $this->getUpdatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 'createdAt' => $this->getCreatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 'notified' => $this->getNotified(), ]; From 887058591cd6e34d99fd08616dd35b284f3ec9df Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 16/38] feat(files_reminders): create or update Signed-off-by: Christopher Ng --- apps/files_reminders/appinfo/routes.php | 4 ++-- .../lib/Controller/ApiController.php | 4 ++-- .../files_reminders/lib/Db/ReminderMapper.php | 12 +++++----- .../lib/Service/ReminderService.php | 24 +++++++++++++------ 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/apps/files_reminders/appinfo/routes.php b/apps/files_reminders/appinfo/routes.php index f36d6cf6391f2..e912efe0284a1 100644 --- a/apps/files_reminders/appinfo/routes.php +++ b/apps/files_reminders/appinfo/routes.php @@ -30,7 +30,7 @@ return [ 'ocs' => [ - ['name' => 'Api#get', 'url' => '/api/v{version}/get', 'verb' => 'GET', 'requirements' => $requirements], - ['name' => 'Api#create', 'url' => '/api/v{version}/create', 'verb' => 'POST', 'requirements' => $requirements], + ['name' => 'Api#get', 'url' => '/api/v{version}/get/{fileId}', 'verb' => 'GET', 'requirements' => $requirements], + ['name' => 'Api#set', 'url' => '/api/v{version}/set/{fileId}', 'verb' => 'PUT', 'requirements' => $requirements], ], ]; diff --git a/apps/files_reminders/lib/Controller/ApiController.php b/apps/files_reminders/lib/Controller/ApiController.php index 10a6654d6715a..de69997f61669 100644 --- a/apps/files_reminders/lib/Controller/ApiController.php +++ b/apps/files_reminders/lib/Controller/ApiController.php @@ -83,7 +83,7 @@ public function get(int $fileId): JSONResponse { * * @param string $dueDate ISO 8601 formatted date time string */ - public function create(int $fileId, string $dueDate): JSONResponse { + public function set(int $fileId, string $dueDate): JSONResponse { try { $dueDate = (new DateTime($dueDate))->setTimezone(new DateTimeZone('UTC')); } catch (Exception $e) { @@ -97,7 +97,7 @@ public function create(int $fileId, string $dueDate): JSONResponse { } try { - $this->reminderService->create($user, $fileId, $dueDate); + $this->reminderService->createOrUpdate($user, $fileId, $dueDate); return new JSONResponse([], Http::STATUS_OK); } catch (Throwable $th) { $this->logger->error($th->getMessage(), ['exception' => $th]); diff --git a/apps/files_reminders/lib/Db/ReminderMapper.php b/apps/files_reminders/lib/Db/ReminderMapper.php index 4ddea3b303cde..db753a50b5c98 100644 --- a/apps/files_reminders/lib/Db/ReminderMapper.php +++ b/apps/files_reminders/lib/Db/ReminderMapper.php @@ -56,7 +56,7 @@ public function markNotified(Reminder $reminder): Reminder { public function find(int $id): Reminder { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') + $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT))); @@ -69,7 +69,7 @@ public function find(int $id): Reminder { public function findDueForUser(IUser $user, int $fileId): Reminder { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') + $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR))) ->andWhere($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) @@ -86,7 +86,7 @@ public function findDueForUser(IUser $user, int $fileId): Reminder { public function findAll() { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') + $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->orderBy('due_date', 'ASC'); @@ -99,7 +99,7 @@ public function findAll() { public function findAllForUser(IUser $user) { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') + $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR))) ->orderBy('due_date', 'ASC'); @@ -113,7 +113,7 @@ public function findAllForUser(IUser $user) { public function findOverdue() { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') + $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->lt('due_date', $qb->createFunction('NOW()'))) ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) @@ -128,7 +128,7 @@ public function findOverdue() { public function findNotified(?int $limit = null) { $qb = $this->db->getQueryBuilder(); - $qb->select('user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') + $qb->select('id', 'user_id', 'file_id', 'due_date', 'updated_at', 'created_at', 'notified') ->from($this->getTableName()) ->where($qb->expr()->eq('notified', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))) ->orderBy('due_date', 'ASC') diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index ad230474923a1..0142802f3f163 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -78,13 +78,23 @@ public function getAll(?IUser $user = null) { ); } - public function create(IUser $user, int $fileId, DateTime $dueDate): void { - $reminder = new Reminder(); - $reminder->setUserId($user->getUID()); - $reminder->setFileId($fileId); - $reminder->setDueDate($dueDate); - $reminder->setCreatedAt(new DateTime('now', new DateTimeZone('UTC'))); - $this->reminderMapper->insert($reminder); + public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): void { + $now = new DateTime('now', new DateTimeZone('UTC')); + try { + $reminder = $this->reminderMapper->findDueForUser($user, $fileId); + $reminder->setDueDate($dueDate); + $reminder->setUpdatedAt($now); + $this->reminderMapper->update($reminder); + } catch (DoesNotExistException $e) { + // Create new reminder if no reminder is found + $reminder = new Reminder(); + $reminder->setUserId($user->getUID()); + $reminder->setFileId($fileId); + $reminder->setDueDate($dueDate); + $reminder->setUpdatedAt($now); + $reminder->setCreatedAt($now); + $this->reminderMapper->insert($reminder); + } } /** From 05e454ee614705ec0c8be264c456b7c647be7c03 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 17/38] fix: update find due query Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Db/ReminderMapper.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/apps/files_reminders/lib/Db/ReminderMapper.php b/apps/files_reminders/lib/Db/ReminderMapper.php index db753a50b5c98..cb079320e5f29 100644 --- a/apps/files_reminders/lib/Db/ReminderMapper.php +++ b/apps/files_reminders/lib/Db/ReminderMapper.php @@ -73,9 +73,7 @@ public function findDueForUser(IUser $user, int $fileId): Reminder { ->from($this->getTableName()) ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID(), IQueryBuilder::PARAM_STR))) ->andWhere($qb->expr()->eq('file_id', $qb->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))) - ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))) - ->orderBy('updated_at', 'DESC') - ->setMaxResults(1); + ->andWhere($qb->expr()->eq('notified', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))); return $this->findEntity($qb); } From 777a791e72b9e8982228ff5a39cd61b7f051d8e4 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 18/38] feat(files_reminders): add remove endpoint Signed-off-by: Christopher Ng --- apps/files_reminders/appinfo/routes.php | 1 + .../lib/Controller/ApiController.php | 26 +++++++++++++++---- .../lib/Service/ReminderService.php | 8 ++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/apps/files_reminders/appinfo/routes.php b/apps/files_reminders/appinfo/routes.php index e912efe0284a1..8dfd785669ba1 100644 --- a/apps/files_reminders/appinfo/routes.php +++ b/apps/files_reminders/appinfo/routes.php @@ -32,5 +32,6 @@ 'ocs' => [ ['name' => 'Api#get', 'url' => '/api/v{version}/get/{fileId}', 'verb' => 'GET', 'requirements' => $requirements], ['name' => 'Api#set', 'url' => '/api/v{version}/set/{fileId}', 'verb' => 'PUT', 'requirements' => $requirements], + ['name' => 'Api#remove', 'url' => '/api/v{version}/remove/{fileId}', 'verb' => 'DELETE', 'requirements' => $requirements], ], ]; diff --git a/apps/files_reminders/lib/Controller/ApiController.php b/apps/files_reminders/lib/Controller/ApiController.php index de69997f61669..ddc34b16c6b20 100644 --- a/apps/files_reminders/lib/Controller/ApiController.php +++ b/apps/files_reminders/lib/Controller/ApiController.php @@ -67,11 +67,7 @@ public function get(int $fileId): JSONResponse { ]; return new JSONResponse($reminderData, Http::STATUS_OK); } catch (DoesNotExistException $e) { - // Return null when no reminder is found - $reminderData = [ - 'dueDate' => null, - ]; - return new JSONResponse($reminderData, Http::STATUS_OK); + return new JSONResponse([], Http::STATUS_NOT_FOUND); } catch (Throwable $th) { $this->logger->error($th->getMessage(), ['exception' => $th]); return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); @@ -104,4 +100,24 @@ public function set(int $fileId, string $dueDate): JSONResponse { return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); } } + + /** + * Remove a reminder + */ + public function remove(int $fileId): JSONResponse { + $user = $this->userSession->getUser(); + if ($user === null) { + return new JSONResponse([], Http::STATUS_UNAUTHORIZED); + } + + try { + $this->reminderService->remove($user, $fileId); + return new JSONResponse([], Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new JSONResponse([], Http::STATUS_NOT_FOUND); + } catch (Throwable $th) { + $this->logger->error($th->getMessage(), ['exception' => $th]); + return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); + } + } } diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index 0142802f3f163..bf4cd5da14d78 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -97,6 +97,14 @@ public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): voi } } + /** + * @throws DoesNotExistException + */ + public function remove(IUser $user, int $fileId): void { + $reminder = $this->reminderMapper->findDueForUser($user, $fileId); + $this->reminderMapper->delete($reminder); + } + /** * @throws DoesNotExistException * @throws UserNotFoundException From d31302e72c8c6ec4281453e245b2e4a367b1c603 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 19/38] fix: create only if file exists Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Controller/ApiController.php | 3 +++ apps/files_reminders/lib/Service/ReminderService.php | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/apps/files_reminders/lib/Controller/ApiController.php b/apps/files_reminders/lib/Controller/ApiController.php index ddc34b16c6b20..30465f6a69b9e 100644 --- a/apps/files_reminders/lib/Controller/ApiController.php +++ b/apps/files_reminders/lib/Controller/ApiController.php @@ -30,6 +30,7 @@ use DateTimeInterface; use DateTimeZone; use Exception; +use OCA\FilesReminders\Exception\NodeNotFoundException; use OCA\FilesReminders\Service\ReminderService; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; @@ -95,6 +96,8 @@ public function set(int $fileId, string $dueDate): JSONResponse { try { $this->reminderService->createOrUpdate($user, $fileId, $dueDate); return new JSONResponse([], Http::STATUS_OK); + } catch (NodeNotFoundException $e) { + return new JSONResponse([], Http::STATUS_NOT_FOUND); } catch (Throwable $th) { $this->logger->error($th->getMessage(), ['exception' => $th]); return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index bf4cd5da14d78..fe746dc75ca0a 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -31,6 +31,7 @@ use OCA\FilesReminders\AppInfo\Application; use OCA\FilesReminders\Db\Reminder; use OCA\FilesReminders\Db\ReminderMapper; +use OCA\FilesReminders\Exception\NodeNotFoundException; use OCA\FilesReminders\Exception\UserNotFoundException; use OCA\FilesReminders\Model\RichReminder; use OCP\AppFramework\Db\DoesNotExistException; @@ -78,6 +79,9 @@ public function getAll(?IUser $user = null) { ); } + /** + * @throws NodeNotFoundException + */ public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): void { $now = new DateTime('now', new DateTimeZone('UTC')); try { @@ -86,6 +90,10 @@ public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): voi $reminder->setUpdatedAt($now); $this->reminderMapper->update($reminder); } catch (DoesNotExistException $e) { + $nodes = $this->root->getUserFolder($user->getUID())->getById($fileId); + if (empty($nodes)) { + throw new NodeNotFoundException(); + } // Create new reminder if no reminder is found $reminder = new Reminder(); $reminder->setUserId($user->getUID()); From bdf07413d02a23aa460aa821191325b04561d597 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 20/38] enh: return created status code Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Controller/ApiController.php | 5 ++++- apps/files_reminders/lib/Service/ReminderService.php | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/apps/files_reminders/lib/Controller/ApiController.php b/apps/files_reminders/lib/Controller/ApiController.php index 30465f6a69b9e..fc1b647148daf 100644 --- a/apps/files_reminders/lib/Controller/ApiController.php +++ b/apps/files_reminders/lib/Controller/ApiController.php @@ -94,7 +94,10 @@ public function set(int $fileId, string $dueDate): JSONResponse { } try { - $this->reminderService->createOrUpdate($user, $fileId, $dueDate); + $created = $this->reminderService->createOrUpdate($user, $fileId, $dueDate); + if ($created) { + return new JSONResponse([], Http::STATUS_CREATED); + } return new JSONResponse([], Http::STATUS_OK); } catch (NodeNotFoundException $e) { return new JSONResponse([], Http::STATUS_NOT_FOUND); diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index fe746dc75ca0a..8dbec6d5df70c 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -80,15 +80,18 @@ public function getAll(?IUser $user = null) { } /** + * @return bool true if created, false if updated + * * @throws NodeNotFoundException */ - public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): void { + public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): bool { $now = new DateTime('now', new DateTimeZone('UTC')); try { $reminder = $this->reminderMapper->findDueForUser($user, $fileId); $reminder->setDueDate($dueDate); $reminder->setUpdatedAt($now); $this->reminderMapper->update($reminder); + return false; } catch (DoesNotExistException $e) { $nodes = $this->root->getUserFolder($user->getUID())->getById($fileId); if (empty($nodes)) { @@ -102,6 +105,7 @@ public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): voi $reminder->setUpdatedAt($now); $reminder->setCreatedAt($now); $this->reminderMapper->insert($reminder); + return true; } } From b04d1a70e2d5b112e75b73edec5f13b76ee0dec0 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 21/38] enh: comment interval Signed-off-by: Christopher Ng --- apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php b/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php index 581b4c8935128..cf55ee54509c9 100644 --- a/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php +++ b/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php @@ -38,7 +38,7 @@ public function __construct( ) { parent::__construct($time); - $this->setInterval(60 * 60 * 24); + $this->setInterval(24 * 60 * 60); // 1 day $this->setTimeSensitivity(IJob::TIME_INSENSITIVE); } From db7f5a29f36cddb793ee813d21ebdc717dfbb0ae Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 22/38] enh: serialize path Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Model/RichReminder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/files_reminders/lib/Model/RichReminder.php b/apps/files_reminders/lib/Model/RichReminder.php index 1ac1baae370db..10dc89799fe99 100644 --- a/apps/files_reminders/lib/Model/RichReminder.php +++ b/apps/files_reminders/lib/Model/RichReminder.php @@ -45,8 +45,7 @@ public function __construct( * @throws NodeNotFoundException */ public function getNode(): Node { - $userFolder = $this->root->getUserFolder($this->getUserId()); - $nodes = $userFolder->getById($this->getFileId()); + $nodes = $this->root->getUserFolder($this->getUserId())->getById($this->getFileId()); if (empty($nodes)) { throw new NodeNotFoundException(); } @@ -66,6 +65,7 @@ public function jsonSerialize(): array { return [ 'userId' => $this->getUserId(), 'fileId' => $this->getFileId(), + 'path' => $this->getNode()->getPath(), 'dueDate' => $this->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601 'updatedAt' => $this->getUpdatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 'createdAt' => $this->getCreatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 From 9bd7ddd0747aa32a4741fe99e70f6b1ea20cb564 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 23/38] enh: does not exist return null Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Controller/ApiController.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apps/files_reminders/lib/Controller/ApiController.php b/apps/files_reminders/lib/Controller/ApiController.php index fc1b647148daf..7f6e24debe256 100644 --- a/apps/files_reminders/lib/Controller/ApiController.php +++ b/apps/files_reminders/lib/Controller/ApiController.php @@ -68,7 +68,10 @@ public function get(int $fileId): JSONResponse { ]; return new JSONResponse($reminderData, Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new JSONResponse([], Http::STATUS_NOT_FOUND); + $reminderData = [ + 'dueDate' => null, + ]; + return new JSONResponse($reminderData, Http::STATUS_OK); } catch (Throwable $th) { $this->logger->error($th->getMessage(), ['exception' => $th]); return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); From 3ade06cd9c1ddcf8a5d0a140d40b7fab0ecfccd3 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 24/38] fix: return ocs data Signed-off-by: Christopher Ng --- .../lib/Controller/ApiController.php | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/apps/files_reminders/lib/Controller/ApiController.php b/apps/files_reminders/lib/Controller/ApiController.php index 7f6e24debe256..af5cc1b32a767 100644 --- a/apps/files_reminders/lib/Controller/ApiController.php +++ b/apps/files_reminders/lib/Controller/ApiController.php @@ -34,7 +34,7 @@ use OCA\FilesReminders\Service\ReminderService; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; -use OCP\AppFramework\Http\JSONResponse; +use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCSController; use OCP\IRequest; use OCP\IUserSession; @@ -55,10 +55,10 @@ public function __construct( /** * Get a reminder */ - public function get(int $fileId): JSONResponse { + public function get(int $fileId): DataResponse { $user = $this->userSession->getUser(); if ($user === null) { - return new JSONResponse([], Http::STATUS_UNAUTHORIZED); + return new DataResponse([], Http::STATUS_UNAUTHORIZED); } try { @@ -66,15 +66,15 @@ public function get(int $fileId): JSONResponse { $reminderData = [ 'dueDate' => $reminder->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601 ]; - return new JSONResponse($reminderData, Http::STATUS_OK); + return new DataResponse($reminderData, Http::STATUS_OK); } catch (DoesNotExistException $e) { $reminderData = [ 'dueDate' => null, ]; - return new JSONResponse($reminderData, Http::STATUS_OK); + return new DataResponse($reminderData, Http::STATUS_OK); } catch (Throwable $th) { $this->logger->error($th->getMessage(), ['exception' => $th]); - return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); + return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); } } @@ -83,50 +83,50 @@ public function get(int $fileId): JSONResponse { * * @param string $dueDate ISO 8601 formatted date time string */ - public function set(int $fileId, string $dueDate): JSONResponse { + public function set(int $fileId, string $dueDate): DataResponse { try { $dueDate = (new DateTime($dueDate))->setTimezone(new DateTimeZone('UTC')); } catch (Exception $e) { $this->logger->error($e->getMessage(), ['exception' => $e]); - return new JSONResponse([], Http::STATUS_BAD_REQUEST); + return new DataResponse([], Http::STATUS_BAD_REQUEST); } $user = $this->userSession->getUser(); if ($user === null) { - return new JSONResponse([], Http::STATUS_UNAUTHORIZED); + return new DataResponse([], Http::STATUS_UNAUTHORIZED); } try { $created = $this->reminderService->createOrUpdate($user, $fileId, $dueDate); if ($created) { - return new JSONResponse([], Http::STATUS_CREATED); + return new DataResponse([], Http::STATUS_CREATED); } - return new JSONResponse([], Http::STATUS_OK); + return new DataResponse([], Http::STATUS_OK); } catch (NodeNotFoundException $e) { - return new JSONResponse([], Http::STATUS_NOT_FOUND); + return new DataResponse([], Http::STATUS_NOT_FOUND); } catch (Throwable $th) { $this->logger->error($th->getMessage(), ['exception' => $th]); - return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); + return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); } } /** * Remove a reminder */ - public function remove(int $fileId): JSONResponse { + public function remove(int $fileId): DataResponse { $user = $this->userSession->getUser(); if ($user === null) { - return new JSONResponse([], Http::STATUS_UNAUTHORIZED); + return new DataResponse([], Http::STATUS_UNAUTHORIZED); } try { $this->reminderService->remove($user, $fileId); - return new JSONResponse([], Http::STATUS_OK); + return new DataResponse([], Http::STATUS_OK); } catch (DoesNotExistException $e) { - return new JSONResponse([], Http::STATUS_NOT_FOUND); + return new DataResponse([], Http::STATUS_NOT_FOUND); } catch (Throwable $th) { $this->logger->error($th->getMessage(), ['exception' => $th]); - return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); + return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); } } } From 7daf11f8f69cc7562bc46285b9eb4952b6f73b8b Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 12:10:50 -0700 Subject: [PATCH 25/38] fix: remove throwable handling Signed-off-by: Christopher Ng --- .../lib/BackgroundJob/ScheduledNotifications.php | 3 --- apps/files_reminders/lib/Controller/ApiController.php | 10 ---------- 2 files changed, 13 deletions(-) diff --git a/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php b/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php index df801cea06369..17b4c90202959 100644 --- a/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php +++ b/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php @@ -32,7 +32,6 @@ use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\Job; use Psr\Log\LoggerInterface; -use Throwable; class ScheduledNotifications extends Job { public function __construct( @@ -54,8 +53,6 @@ public function run($argument) { $this->reminderService->send($reminder); } catch (DoesNotExistException $e) { $this->logger->debug('Could not send notification for reminder with id ' . $reminder->getId()); - } catch (Throwable $th) { - $this->logger->error($th->getMessage(), $th->getTrace()); } } } diff --git a/apps/files_reminders/lib/Controller/ApiController.php b/apps/files_reminders/lib/Controller/ApiController.php index af5cc1b32a767..a0dbd8401114f 100644 --- a/apps/files_reminders/lib/Controller/ApiController.php +++ b/apps/files_reminders/lib/Controller/ApiController.php @@ -39,7 +39,6 @@ use OCP\IRequest; use OCP\IUserSession; use Psr\Log\LoggerInterface; -use Throwable; class ApiController extends OCSController { public function __construct( @@ -72,9 +71,6 @@ public function get(int $fileId): DataResponse { 'dueDate' => null, ]; return new DataResponse($reminderData, Http::STATUS_OK); - } catch (Throwable $th) { - $this->logger->error($th->getMessage(), ['exception' => $th]); - return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); } } @@ -104,9 +100,6 @@ public function set(int $fileId, string $dueDate): DataResponse { return new DataResponse([], Http::STATUS_OK); } catch (NodeNotFoundException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); - } catch (Throwable $th) { - $this->logger->error($th->getMessage(), ['exception' => $th]); - return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); } } @@ -124,9 +117,6 @@ public function remove(int $fileId): DataResponse { return new DataResponse([], Http::STATUS_OK); } catch (DoesNotExistException $e) { return new DataResponse([], Http::STATUS_NOT_FOUND); - } catch (Throwable $th) { - $this->logger->error($th->getMessage(), ['exception' => $th]); - return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); } } } From 761751950253e7222976125ffafec34a0ebe2a7c Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 16:49:42 -0700 Subject: [PATCH 26/38] fix: construct background jobs Signed-off-by: Christopher Ng --- apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php | 2 +- .../lib/BackgroundJob/ScheduledNotifications.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php b/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php index cf55ee54509c9..afa8c514a0aa2 100644 --- a/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php +++ b/apps/files_reminders/lib/BackgroundJob/CleanUpReminders.php @@ -33,7 +33,7 @@ class CleanUpReminders extends TimedJob { public function __construct( - private ITimeFactory $time, + ITimeFactory $time, private ReminderService $reminderService, ) { parent::__construct($time); diff --git a/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php b/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php index 17b4c90202959..15ae56f069869 100644 --- a/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php +++ b/apps/files_reminders/lib/BackgroundJob/ScheduledNotifications.php @@ -35,7 +35,7 @@ class ScheduledNotifications extends Job { public function __construct( - protected ITimeFactory $time, + ITimeFactory $time, protected ReminderMapper $reminderMapper, protected ReminderService $reminderService, protected LoggerInterface $logger, From f865c3ad21e03483a6f16b90a4383faef0356499 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 17:27:02 -0700 Subject: [PATCH 27/38] 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 29f781d0006a0..fb17eb7a1b625 100644 --- a/apps/files_reminders/composer/composer/autoload_classmap.php +++ b/apps/files_reminders/composer/composer/autoload_classmap.php @@ -16,6 +16,7 @@ '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 efea8791020d8..1a2bc0d1bd182 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 c398e796c3212..f120393f41b6f 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 4d418562ef5fa..1e4813e1ec768 100644 --- a/apps/files_reminders/lib/AppInfo/Application.php +++ b/apps/files_reminders/lib/AppInfo/Application.php @@ -26,11 +26,13 @@ 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 @@ public function boot(IBootContext $context): void { 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 cb079320e5f29..97f7ed0900b65 100644 --- a/apps/files_reminders/lib/Db/ReminderMapper.php +++ b/apps/files_reminders/lib/Db/ReminderMapper.php @@ -29,6 +29,7 @@ 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 @@ public function findAllForUser(IUser $user) { 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 0000000000000..88aed08bba82f --- /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 8dbec6d5df70c..eec2f1780bd38 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -36,6 +36,7 @@ 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 @@ public function remove(IUser $user, int $fileId): void { $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 From 9e8354e0ac35090995a0451ccd1166f178815ea1 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Mon, 31 Jul 2023 18:40:13 -0700 Subject: [PATCH 28/38] fix: exit on reminder not found Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Notification/Notifier.php | 8 +++++++- apps/files_reminders/lib/Service/ReminderService.php | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/apps/files_reminders/lib/Notification/Notifier.php b/apps/files_reminders/lib/Notification/Notifier.php index fb1d3264dbda6..c7384b43224ad 100644 --- a/apps/files_reminders/lib/Notification/Notifier.php +++ b/apps/files_reminders/lib/Notification/Notifier.php @@ -30,6 +30,7 @@ use OCA\FilesReminders\AppInfo\Application; use OCA\FilesReminders\Exception\NodeNotFoundException; use OCA\FilesReminders\Service\ReminderService; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\Files\FileInfo; use OCP\IURLGenerator; use OCP\L10N\IFactory; @@ -66,8 +67,13 @@ public function prepare(INotification $notification, string $languageCode): INot switch ($notification->getSubject()) { case 'reminder-due': $reminderId = (int)$notification->getObjectId(); - $node = $this->reminderService->get($reminderId)->getNode(); + try { + $reminder = $this->reminderService->get($reminderId); + } catch (DoesNotExistException $e) { + throw new InvalidArgumentException(); + } + $node = $reminder->getNode(); $path = rtrim($node->getPath(), '/'); if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { // Remove /user/files/... diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index eec2f1780bd38..91652a4e0be5d 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -54,6 +54,9 @@ public function __construct( protected LoggerInterface $logger, ) {} + /** + * @throws DoesNotExistException + */ public function get(int $id): RichReminder { $reminder = $this->reminderMapper->find($id); return new RichReminder($reminder, $this->root); From c4b7056f58e3f7773ac95dfc38bf46217b91b8c0 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Tue, 1 Aug 2023 11:13:59 -0700 Subject: [PATCH 29/38] fix: catch NodeNotFoundException in notifier Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Notification/Notifier.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/apps/files_reminders/lib/Notification/Notifier.php b/apps/files_reminders/lib/Notification/Notifier.php index c7384b43224ad..08aa8b2d67c93 100644 --- a/apps/files_reminders/lib/Notification/Notifier.php +++ b/apps/files_reminders/lib/Notification/Notifier.php @@ -55,7 +55,6 @@ public function getName(): string { /** * @throws InvalidArgumentException - * @throws NodeNotFoundException */ public function prepare(INotification $notification, string $languageCode): INotification { $l = $this->l10nFactory->get(Application::APP_ID, $languageCode); @@ -73,7 +72,11 @@ public function prepare(INotification $notification, string $languageCode): INot throw new InvalidArgumentException(); } - $node = $reminder->getNode(); + try { + $node = $reminder->getNode(); + } catch (NodeNotFoundException $e) { + throw new InvalidArgumentException(); + } $path = rtrim($node->getPath(), '/'); if (strpos($path, '/' . $notification->getUser() . '/files/') === 0) { // Remove /user/files/... From 7beef657e4c6b1b9f24f03a224c61f6cbff6c723 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Tue, 1 Aug 2023 11:18:51 -0700 Subject: [PATCH 30/38] enh: highlight filename Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Notification/Notifier.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/apps/files_reminders/lib/Notification/Notifier.php b/apps/files_reminders/lib/Notification/Notifier.php index 08aa8b2d67c93..6e639b78c1bbd 100644 --- a/apps/files_reminders/lib/Notification/Notifier.php +++ b/apps/files_reminders/lib/Notification/Notifier.php @@ -96,11 +96,9 @@ public function prepare(INotification $notification, string $languageCode): INot $subject, [ 'name' => [ - 'type' => 'file', + 'type' => 'highlight', 'id' => $node->getId(), 'name' => $node->getName(), - 'path' => $path, - 'link' => $link, ], ], ) From 7381c80cafcfbb9584f6f247049f1bdcc91acc05 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Tue, 1 Aug 2023 11:33:32 -0700 Subject: [PATCH 31/38] fix: remove unnecessary parsed subject Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Notification/Notifier.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/apps/files_reminders/lib/Notification/Notifier.php b/apps/files_reminders/lib/Notification/Notifier.php index 6e639b78c1bbd..2a919d42cae83 100644 --- a/apps/files_reminders/lib/Notification/Notifier.php +++ b/apps/files_reminders/lib/Notification/Notifier.php @@ -102,11 +102,6 @@ public function prepare(INotification $notification, string $languageCode): INot ], ], ) - ->setParsedSubject(str_replace( - ['{name}'], - [$node->getName()], - $subject, - )) ->setLink($link); $label = match ($node->getType()) { From d61916621d566f5dee91a759767f86062841d13c Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Tue, 1 Aug 2023 11:41:44 -0700 Subject: [PATCH 32/38] fix: return null if table exists Signed-off-by: Christopher Ng --- .../lib/Migration/Version10000Date20230725162149.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php b/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php index 0b7505d64e3e7..76999329d64f0 100644 --- a/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php +++ b/apps/files_reminders/lib/Migration/Version10000Date20230725162149.php @@ -41,6 +41,10 @@ public function changeSchema(IOutput $output, Closure $schemaClosure, array $opt /** @var ISchemaWrapper $schema */ $schema = $schemaClosure(); + if ($schema->hasTable(ReminderMapper::TABLE_NAME)) { + return null; + } + $table = $schema->createTable(ReminderMapper::TABLE_NAME); $table->addColumn('id', Types::BIGINT, [ 'autoincrement' => true, From 113d061919de4bb0c88fbf0f44d006e0c8717d6c Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Tue, 1 Aug 2023 14:36:33 -0700 Subject: [PATCH 33/38] enh: add codeowner Signed-off-by: Christopher Ng --- .github/CODEOWNERS | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 55c003a65790f..273ed8dde4c2f 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -11,6 +11,7 @@ /apps/federation/appinfo/info.xml @datenangebot /apps/files/appinfo/info.xml @skjnldsv @Pytal @ArtificialOwl @come-nc @artonge @icewind1991 @szaimen @susnux @Fenn-CS /apps/files_external/appinfo/info.xml @icewind1991 @artonge +/apps/files_reminders/appinfo/info.xml @Pytal /apps/files_sharing/appinfo/info.xml @skjnldsv @come-nc /apps/files_trashbin/appinfo/info.xml @Pytal @icewind1991 /apps/files_versions/appinfo/info.xml @artonge @icewind1991 From e320166b151ef308a9a289c77bea2a4e50b4e994 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Tue, 1 Aug 2023 14:58:57 -0700 Subject: [PATCH 34/38] enh: add json output to command Signed-off-by: Christopher Ng --- .../lib/Command/ListCommand.php | 55 ++++++++++++++----- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/apps/files_reminders/lib/Command/ListCommand.php b/apps/files_reminders/lib/Command/ListCommand.php index b066c6ed8cfa6..3f3ce13b8578a 100644 --- a/apps/files_reminders/lib/Command/ListCommand.php +++ b/apps/files_reminders/lib/Command/ListCommand.php @@ -33,6 +33,7 @@ use OCP\IUserManager; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -52,6 +53,13 @@ protected function configure(): void { 'user', InputArgument::OPTIONAL, 'list reminders for user', + ) + ->addOption( + 'output', + null, + InputOption::VALUE_OPTIONAL, + 'Output format (plain, json or json_pretty, default is plain)', + $this->defaultOutputFormat, ); } @@ -74,20 +82,37 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } - $io->table( - ['User Id', 'Path', 'Due Date', 'Updated At', 'Created At', 'Notified'], - array_map( - fn (RichReminder $reminder) => [ - $reminder->getUserId(), - $reminder->getNode()->getPath(), - $reminder->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601 - $reminder->getUpdatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 - $reminder->getCreatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 - $reminder->getNotified() ? 'true' : 'false', - ], - $reminders, - ) - ); - return 0; + $outputOption = $input->getOption('output'); + switch ($outputOption) { + case static::OUTPUT_FORMAT_JSON: + case static::OUTPUT_FORMAT_JSON_PRETTY: + $this->writeArrayInOutputFormat( + $input, + $io, + array_map( + fn (RichReminder $reminder) => $reminder->jsonSerialize(), + $reminders, + ), + '', + ); + return 0; + default: + $io->table( + ['User Id', 'File Id', 'Path', 'Due Date', 'Updated At', 'Created At', 'Notified'], + array_map( + fn (RichReminder $reminder) => [ + $reminder->getUserId(), + $reminder->getFileId(), + $reminder->getNode()->getPath(), + $reminder->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601 + $reminder->getUpdatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 + $reminder->getCreatedAt()->format(DateTimeInterface::ATOM), // ISO 8601 + $reminder->getNotified() ? 'true' : 'false', + ], + $reminders, + ), + ); + return 0; + } } } From 2c4b562cdc9405d67f692cae009f6a282d085fd7 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Tue, 1 Aug 2023 15:33:15 -0700 Subject: [PATCH 35/38] fix: ignore non-existing Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Listener/NodeDeletedListener.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/apps/files_reminders/lib/Listener/NodeDeletedListener.php b/apps/files_reminders/lib/Listener/NodeDeletedListener.php index 88aed08bba82f..460ddfd4abe78 100644 --- a/apps/files_reminders/lib/Listener/NodeDeletedListener.php +++ b/apps/files_reminders/lib/Listener/NodeDeletedListener.php @@ -26,6 +26,8 @@ namespace OCA\FilesReminders\Listener; +use OC\Files\Node\NonExistingFile; +use OC\Files\Node\NonExistingFolder; use OCA\FilesReminders\Service\ReminderService; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; @@ -42,6 +44,10 @@ public function handle(Event $event): void { } $node = $event->getNode(); + if ($node instanceof NonExistingFile || $node instanceof NonExistingFolder) { + return; + } + $this->reminderService->removeAllForNode($node); } } From af98c702e0464d52f56591ea873caabfec612acd Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Tue, 1 Aug 2023 15:36:55 -0700 Subject: [PATCH 36/38] fix(ci): add to enabled apps Signed-off-by: Christopher Ng --- build/integration/features/provisioning-v1.feature | 1 + 1 file changed, 1 insertion(+) diff --git a/build/integration/features/provisioning-v1.feature b/build/integration/features/provisioning-v1.feature index 5ba6b7f63dd4f..0d1d5b1a3455e 100644 --- a/build/integration/features/provisioning-v1.feature +++ b/build/integration/features/provisioning-v1.feature @@ -588,6 +588,7 @@ Feature: provisioning | federatedfilesharing | | federation | | files | + | files_reminders | | files_sharing | | files_trashbin | | files_versions | From 5a11535c51ae0277f6bb0af048215e329b6068d0 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Tue, 1 Aug 2023 17:35:52 -0700 Subject: [PATCH 37/38] fix: set endpoint description Signed-off-by: Christopher Ng --- apps/files_reminders/lib/Controller/ApiController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/files_reminders/lib/Controller/ApiController.php b/apps/files_reminders/lib/Controller/ApiController.php index a0dbd8401114f..873088177c195 100644 --- a/apps/files_reminders/lib/Controller/ApiController.php +++ b/apps/files_reminders/lib/Controller/ApiController.php @@ -75,7 +75,7 @@ public function get(int $fileId): DataResponse { } /** - * Create a reminder + * Set a reminder * * @param string $dueDate ISO 8601 formatted date time string */ From a806bd0d3c53d7d12fe2c0e9a6559f1ac71ca86f Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Thu, 3 Aug 2023 15:42:14 -0700 Subject: [PATCH 38/38] enh: handle user deleted Signed-off-by: Christopher Ng --- .../composer/composer/autoload_classmap.php | 1 + .../composer/composer/autoload_static.php | 1 + .../composer/composer/installed.php | 4 +- .../lib/AppInfo/Application.php | 4 ++ .../lib/Listener/UserDeletedListener.php | 47 +++++++++++++++++++ .../lib/Service/ReminderService.php | 7 +++ 6 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 apps/files_reminders/lib/Listener/UserDeletedListener.php diff --git a/apps/files_reminders/composer/composer/autoload_classmap.php b/apps/files_reminders/composer/composer/autoload_classmap.php index fb17eb7a1b625..4f81cd387da29 100644 --- a/apps/files_reminders/composer/composer/autoload_classmap.php +++ b/apps/files_reminders/composer/composer/autoload_classmap.php @@ -17,6 +17,7 @@ '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\\Listener\\UserDeletedListener' => $baseDir . '/../lib/Listener/UserDeletedListener.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 1a2bc0d1bd182..eff14f8f56950 100644 --- a/apps/files_reminders/composer/composer/autoload_static.php +++ b/apps/files_reminders/composer/composer/autoload_static.php @@ -32,6 +32,7 @@ class ComposerStaticInitFilesReminders '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\\Listener\\UserDeletedListener' => __DIR__ . '/..' . '/../lib/Listener/UserDeletedListener.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 f120393f41b6f..b962688d0b992 100644 --- a/apps/files_reminders/composer/composer/installed.php +++ b/apps/files_reminders/composer/composer/installed.php @@ -3,7 +3,7 @@ 'name' => '__root__', 'pretty_version' => 'dev-master', 'version' => 'dev-master', - 'reference' => 'cce2e1543b16a04e6b9fc1ba3f89fbc47f12e773', + 'reference' => '5a11535c51ae0277f6bb0af048215e329b6068d0', 'type' => 'library', 'install_path' => __DIR__ . '/../', 'aliases' => array(), @@ -13,7 +13,7 @@ '__root__' => array( 'pretty_version' => 'dev-master', 'version' => 'dev-master', - 'reference' => 'cce2e1543b16a04e6b9fc1ba3f89fbc47f12e773', + 'reference' => '5a11535c51ae0277f6bb0af048215e329b6068d0', '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 1e4813e1ec768..d35bd5c5de2b3 100644 --- a/apps/files_reminders/lib/AppInfo/Application.php +++ b/apps/files_reminders/lib/AppInfo/Application.php @@ -27,12 +27,14 @@ namespace OCA\FilesReminders\AppInfo; use OCA\FilesReminders\Listener\NodeDeletedListener; +use OCA\FilesReminders\Listener\UserDeletedListener; 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; +use OCP\User\Events\UserDeletedEvent; class Application extends App implements IBootstrap { public const APP_ID = 'files_reminders'; @@ -46,6 +48,8 @@ public function boot(IBootContext $context): void { public function register(IRegistrationContext $context): void { $context->registerNotifierService(Notifier::class); + $context->registerEventListener(NodeDeletedEvent::class, NodeDeletedListener::class); + $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class); } } diff --git a/apps/files_reminders/lib/Listener/UserDeletedListener.php b/apps/files_reminders/lib/Listener/UserDeletedListener.php new file mode 100644 index 0000000000000..b95102c1cc278 --- /dev/null +++ b/apps/files_reminders/lib/Listener/UserDeletedListener.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\User\Events\UserDeletedEvent; + +class UserDeletedListener implements IEventListener { + public function __construct( + private ReminderService $reminderService, + ) {} + + public function handle(Event $event): void { + if (!($event instanceof UserDeletedEvent)) { + return; + } + + $user = $event->getUser(); + $this->reminderService->removeAllForUser($user); + } +} diff --git a/apps/files_reminders/lib/Service/ReminderService.php b/apps/files_reminders/lib/Service/ReminderService.php index 91652a4e0be5d..5c7193259c12c 100644 --- a/apps/files_reminders/lib/Service/ReminderService.php +++ b/apps/files_reminders/lib/Service/ReminderService.php @@ -128,6 +128,13 @@ public function removeAllForNode(Node $node): void { } } + public function removeAllForUser(IUser $user): void { + $reminders = $this->reminderMapper->findAllForUser($user); + foreach ($reminders as $reminder) { + $this->reminderMapper->delete($reminder); + } + } + /** * @throws DoesNotExistException * @throws UserNotFoundException