@@ -23,60 +23,60 @@ |
||
| 23 | 23 | |
| 24 | 24 | class PropFindPlugin extends ServerPlugin { |
| 25 | 25 | |
| 26 | - public const REMINDER_DUE_DATE_PROPERTY = '{http://nextcloud.org/ns}reminder-due-date'; |
|
| 26 | + public const REMINDER_DUE_DATE_PROPERTY = '{http://nextcloud.org/ns}reminder-due-date'; |
|
| 27 | 27 | |
| 28 | - public function __construct( |
|
| 29 | - private ReminderService $reminderService, |
|
| 30 | - private IUserSession $userSession, |
|
| 31 | - ) { |
|
| 32 | - } |
|
| 28 | + public function __construct( |
|
| 29 | + private ReminderService $reminderService, |
|
| 30 | + private IUserSession $userSession, |
|
| 31 | + ) { |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - public function initialize(Server $server): void { |
|
| 35 | - $server->on('propFind', [$this, 'propFind']); |
|
| 36 | - } |
|
| 34 | + public function initialize(Server $server): void { |
|
| 35 | + $server->on('propFind', [$this, 'propFind']); |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - public function propFind(PropFind $propFind, INode $node) { |
|
| 39 | - if (!in_array(static::REMINDER_DUE_DATE_PROPERTY, $propFind->getRequestedProperties())) { |
|
| 40 | - return; |
|
| 41 | - } |
|
| 38 | + public function propFind(PropFind $propFind, INode $node) { |
|
| 39 | + if (!in_array(static::REMINDER_DUE_DATE_PROPERTY, $propFind->getRequestedProperties())) { |
|
| 40 | + return; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - if (!($node instanceof Node)) { |
|
| 44 | - return; |
|
| 45 | - } |
|
| 43 | + if (!($node instanceof Node)) { |
|
| 44 | + return; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - if ( |
|
| 48 | - $node instanceof Directory |
|
| 49 | - && $propFind->getDepth() > 0 |
|
| 50 | - && $propFind->getStatus(static::REMINDER_DUE_DATE_PROPERTY) !== null |
|
| 51 | - ) { |
|
| 52 | - $folder = $node->getNode(); |
|
| 53 | - $this->cacheFolder($folder); |
|
| 54 | - } |
|
| 47 | + if ( |
|
| 48 | + $node instanceof Directory |
|
| 49 | + && $propFind->getDepth() > 0 |
|
| 50 | + && $propFind->getStatus(static::REMINDER_DUE_DATE_PROPERTY) !== null |
|
| 51 | + ) { |
|
| 52 | + $folder = $node->getNode(); |
|
| 53 | + $this->cacheFolder($folder); |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - $propFind->handle( |
|
| 57 | - static::REMINDER_DUE_DATE_PROPERTY, |
|
| 58 | - function () use ($node) { |
|
| 59 | - $user = $this->userSession->getUser(); |
|
| 60 | - if (!($user instanceof IUser)) { |
|
| 61 | - return ''; |
|
| 62 | - } |
|
| 56 | + $propFind->handle( |
|
| 57 | + static::REMINDER_DUE_DATE_PROPERTY, |
|
| 58 | + function () use ($node) { |
|
| 59 | + $user = $this->userSession->getUser(); |
|
| 60 | + if (!($user instanceof IUser)) { |
|
| 61 | + return ''; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - $fileId = $node->getId(); |
|
| 65 | - $reminder = $this->reminderService->getDueForUser($user, $fileId, false); |
|
| 66 | - if ($reminder === null) { |
|
| 67 | - return ''; |
|
| 68 | - } |
|
| 64 | + $fileId = $node->getId(); |
|
| 65 | + $reminder = $this->reminderService->getDueForUser($user, $fileId, false); |
|
| 66 | + if ($reminder === null) { |
|
| 67 | + return ''; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - return $reminder->getDueDate()->format(DateTimeInterface::ATOM); // ISO 8601 |
|
| 71 | - }, |
|
| 72 | - ); |
|
| 73 | - } |
|
| 70 | + return $reminder->getDueDate()->format(DateTimeInterface::ATOM); // ISO 8601 |
|
| 71 | + }, |
|
| 72 | + ); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - private function cacheFolder(Folder $folder): void { |
|
| 76 | - $user = $this->userSession->getUser(); |
|
| 77 | - if (!($user instanceof IUser)) { |
|
| 78 | - return; |
|
| 79 | - } |
|
| 80 | - $this->reminderService->cacheFolder($user, $folder); |
|
| 81 | - } |
|
| 75 | + private function cacheFolder(Folder $folder): void { |
|
| 76 | + $user = $this->userSession->getUser(); |
|
| 77 | + if (!($user instanceof IUser)) { |
|
| 78 | + return; |
|
| 79 | + } |
|
| 80 | + $this->reminderService->cacheFolder($user, $folder); |
|
| 81 | + } |
|
| 82 | 82 | } |
@@ -33,185 +33,185 @@ |
||
| 33 | 33 | |
| 34 | 34 | class ReminderService { |
| 35 | 35 | |
| 36 | - private ICache $cache; |
|
| 37 | - |
|
| 38 | - public function __construct( |
|
| 39 | - protected IUserManager $userManager, |
|
| 40 | - protected IURLGenerator $urlGenerator, |
|
| 41 | - protected INotificationManager $notificationManager, |
|
| 42 | - protected ReminderMapper $reminderMapper, |
|
| 43 | - protected IRootFolder $root, |
|
| 44 | - protected LoggerInterface $logger, |
|
| 45 | - protected ICacheFactory $cacheFactory, |
|
| 46 | - ) { |
|
| 47 | - $this->cache = $this->cacheFactory->createInMemory(); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - public function cacheFolder(IUser $user, Folder $folder): void { |
|
| 51 | - $reminders = $this->reminderMapper->findAllInFolder($user, $folder); |
|
| 52 | - $reminderMap = []; |
|
| 53 | - foreach ($reminders as $reminder) { |
|
| 54 | - $reminderMap[$reminder->getFileId()] = $reminder; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - $nodes = $folder->getDirectoryListing(); |
|
| 58 | - foreach ($nodes as $node) { |
|
| 59 | - $reminder = $reminderMap[$node->getId()] ?? false; |
|
| 60 | - $this->cache->set("{$user->getUID()}-{$node->getId()}", $reminder); |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @throws NodeNotFoundException |
|
| 66 | - */ |
|
| 67 | - public function getDueForUser(IUser $user, int $fileId, bool $checkNode = true): ?RichReminder { |
|
| 68 | - if ($checkNode) { |
|
| 69 | - $this->checkNode($user, $fileId); |
|
| 70 | - } |
|
| 71 | - /** @var null|false|Reminder $cachedReminder */ |
|
| 72 | - $cachedReminder = $this->cache->get("{$user->getUID()}-$fileId"); |
|
| 73 | - if ($cachedReminder === false) { |
|
| 74 | - return null; |
|
| 75 | - } |
|
| 76 | - if ($cachedReminder instanceof Reminder) { |
|
| 77 | - return new RichReminder($cachedReminder, $this->root); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - try { |
|
| 81 | - $reminder = $this->reminderMapper->findDueForUser($user, $fileId); |
|
| 82 | - $this->cache->set("{$user->getUID()}-$fileId", $reminder); |
|
| 83 | - return new RichReminder($reminder, $this->root); |
|
| 84 | - } catch (DoesNotExistException $e) { |
|
| 85 | - $this->cache->set("{$user->getUID()}-$fileId", false); |
|
| 86 | - return null; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @return RichReminder[] |
|
| 92 | - */ |
|
| 93 | - public function getAll(?IUser $user = null) { |
|
| 94 | - $reminders = ($user !== null) |
|
| 95 | - ? $this->reminderMapper->findAllForUser($user) |
|
| 96 | - : $this->reminderMapper->findAll(); |
|
| 97 | - return array_map( |
|
| 98 | - fn (Reminder $reminder) => new RichReminder($reminder, $this->root), |
|
| 99 | - $reminders, |
|
| 100 | - ); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @return bool true if created, false if updated |
|
| 105 | - * |
|
| 106 | - * @throws NodeNotFoundException |
|
| 107 | - */ |
|
| 108 | - public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): bool { |
|
| 109 | - $now = new DateTime('now', new DateTimeZone('UTC')); |
|
| 110 | - $this->checkNode($user, $fileId); |
|
| 111 | - $reminder = $this->getDueForUser($user, $fileId); |
|
| 112 | - if ($reminder === null) { |
|
| 113 | - $reminder = new Reminder(); |
|
| 114 | - $reminder->setUserId($user->getUID()); |
|
| 115 | - $reminder->setFileId($fileId); |
|
| 116 | - $reminder->setDueDate($dueDate); |
|
| 117 | - $reminder->setUpdatedAt($now); |
|
| 118 | - $reminder->setCreatedAt($now); |
|
| 119 | - $this->reminderMapper->insert($reminder); |
|
| 120 | - $this->cache->set("{$user->getUID()}-$fileId", $reminder); |
|
| 121 | - return true; |
|
| 122 | - } |
|
| 123 | - $reminder->setDueDate($dueDate); |
|
| 124 | - $reminder->setUpdatedAt($now); |
|
| 125 | - $this->reminderMapper->update($reminder); |
|
| 126 | - $this->cache->set("{$user->getUID()}-$fileId", $reminder); |
|
| 127 | - return false; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @throws NodeNotFoundException |
|
| 132 | - * @throws ReminderNotFoundException |
|
| 133 | - */ |
|
| 134 | - public function remove(IUser $user, int $fileId): void { |
|
| 135 | - $this->checkNode($user, $fileId); |
|
| 136 | - $reminder = $this->getDueForUser($user, $fileId); |
|
| 137 | - if ($reminder === null) { |
|
| 138 | - throw new ReminderNotFoundException(); |
|
| 139 | - } |
|
| 140 | - $this->deleteReminder($reminder); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - public function removeAllForNode(Node $node): void { |
|
| 144 | - $reminders = $this->reminderMapper->findAllForNode($node); |
|
| 145 | - foreach ($reminders as $reminder) { |
|
| 146 | - $this->deleteReminder($reminder); |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - public function removeAllForUser(IUser $user): void { |
|
| 151 | - $reminders = $this->reminderMapper->findAllForUser($user); |
|
| 152 | - foreach ($reminders as $reminder) { |
|
| 153 | - $this->deleteReminder($reminder); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @throws DoesNotExistException |
|
| 159 | - * @throws UserNotFoundException |
|
| 160 | - */ |
|
| 161 | - public function send(Reminder $reminder): void { |
|
| 162 | - if ($reminder->getNotified()) { |
|
| 163 | - return; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - $user = $this->userManager->get($reminder->getUserId()); |
|
| 167 | - if ($user === null) { |
|
| 168 | - throw new UserNotFoundException(); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - $notification = $this->notificationManager->createNotification(); |
|
| 172 | - $notification |
|
| 173 | - ->setApp(Application::APP_ID) |
|
| 174 | - ->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('files', 'folder.svg'))) |
|
| 175 | - ->setUser($user->getUID()) |
|
| 176 | - ->setObject('reminder', (string)$reminder->getId()) |
|
| 177 | - ->setSubject('reminder-due', [ |
|
| 178 | - 'fileId' => $reminder->getFileId(), |
|
| 179 | - ]) |
|
| 180 | - ->setDateTime($reminder->getDueDate()); |
|
| 181 | - |
|
| 182 | - try { |
|
| 183 | - $this->notificationManager->notify($notification); |
|
| 184 | - $this->reminderMapper->markNotified($reminder); |
|
| 185 | - $this->cache->set("{$user->getUID()}-{$reminder->getFileId()}", $reminder); |
|
| 186 | - } catch (Throwable $th) { |
|
| 187 | - $this->logger->error($th->getMessage(), $th->getTrace()); |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - public function cleanUp(?int $limit = null): void { |
|
| 192 | - $buffer = (new DateTime()) |
|
| 193 | - ->setTimezone(new DateTimeZone('UTC')) |
|
| 194 | - ->modify('-1 day'); |
|
| 195 | - $reminders = $this->reminderMapper->findNotified($buffer, $limit); |
|
| 196 | - foreach ($reminders as $reminder) { |
|
| 197 | - $this->deleteReminder($reminder); |
|
| 198 | - } |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - private function deleteReminder(Reminder $reminder): void { |
|
| 202 | - $this->reminderMapper->delete($reminder); |
|
| 203 | - $this->cache->set("{$reminder->getUserId()}-{$reminder->getFileId()}", false); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * @throws NodeNotFoundException |
|
| 209 | - */ |
|
| 210 | - private function checkNode(IUser $user, int $fileId): void { |
|
| 211 | - $userFolder = $this->root->getUserFolder($user->getUID()); |
|
| 212 | - $node = $userFolder->getFirstNodeById($fileId); |
|
| 213 | - if ($node === null) { |
|
| 214 | - throw new NodeNotFoundException(); |
|
| 215 | - } |
|
| 216 | - } |
|
| 36 | + private ICache $cache; |
|
| 37 | + |
|
| 38 | + public function __construct( |
|
| 39 | + protected IUserManager $userManager, |
|
| 40 | + protected IURLGenerator $urlGenerator, |
|
| 41 | + protected INotificationManager $notificationManager, |
|
| 42 | + protected ReminderMapper $reminderMapper, |
|
| 43 | + protected IRootFolder $root, |
|
| 44 | + protected LoggerInterface $logger, |
|
| 45 | + protected ICacheFactory $cacheFactory, |
|
| 46 | + ) { |
|
| 47 | + $this->cache = $this->cacheFactory->createInMemory(); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + public function cacheFolder(IUser $user, Folder $folder): void { |
|
| 51 | + $reminders = $this->reminderMapper->findAllInFolder($user, $folder); |
|
| 52 | + $reminderMap = []; |
|
| 53 | + foreach ($reminders as $reminder) { |
|
| 54 | + $reminderMap[$reminder->getFileId()] = $reminder; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + $nodes = $folder->getDirectoryListing(); |
|
| 58 | + foreach ($nodes as $node) { |
|
| 59 | + $reminder = $reminderMap[$node->getId()] ?? false; |
|
| 60 | + $this->cache->set("{$user->getUID()}-{$node->getId()}", $reminder); |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @throws NodeNotFoundException |
|
| 66 | + */ |
|
| 67 | + public function getDueForUser(IUser $user, int $fileId, bool $checkNode = true): ?RichReminder { |
|
| 68 | + if ($checkNode) { |
|
| 69 | + $this->checkNode($user, $fileId); |
|
| 70 | + } |
|
| 71 | + /** @var null|false|Reminder $cachedReminder */ |
|
| 72 | + $cachedReminder = $this->cache->get("{$user->getUID()}-$fileId"); |
|
| 73 | + if ($cachedReminder === false) { |
|
| 74 | + return null; |
|
| 75 | + } |
|
| 76 | + if ($cachedReminder instanceof Reminder) { |
|
| 77 | + return new RichReminder($cachedReminder, $this->root); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + try { |
|
| 81 | + $reminder = $this->reminderMapper->findDueForUser($user, $fileId); |
|
| 82 | + $this->cache->set("{$user->getUID()}-$fileId", $reminder); |
|
| 83 | + return new RichReminder($reminder, $this->root); |
|
| 84 | + } catch (DoesNotExistException $e) { |
|
| 85 | + $this->cache->set("{$user->getUID()}-$fileId", false); |
|
| 86 | + return null; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @return RichReminder[] |
|
| 92 | + */ |
|
| 93 | + public function getAll(?IUser $user = null) { |
|
| 94 | + $reminders = ($user !== null) |
|
| 95 | + ? $this->reminderMapper->findAllForUser($user) |
|
| 96 | + : $this->reminderMapper->findAll(); |
|
| 97 | + return array_map( |
|
| 98 | + fn (Reminder $reminder) => new RichReminder($reminder, $this->root), |
|
| 99 | + $reminders, |
|
| 100 | + ); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @return bool true if created, false if updated |
|
| 105 | + * |
|
| 106 | + * @throws NodeNotFoundException |
|
| 107 | + */ |
|
| 108 | + public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): bool { |
|
| 109 | + $now = new DateTime('now', new DateTimeZone('UTC')); |
|
| 110 | + $this->checkNode($user, $fileId); |
|
| 111 | + $reminder = $this->getDueForUser($user, $fileId); |
|
| 112 | + if ($reminder === null) { |
|
| 113 | + $reminder = new Reminder(); |
|
| 114 | + $reminder->setUserId($user->getUID()); |
|
| 115 | + $reminder->setFileId($fileId); |
|
| 116 | + $reminder->setDueDate($dueDate); |
|
| 117 | + $reminder->setUpdatedAt($now); |
|
| 118 | + $reminder->setCreatedAt($now); |
|
| 119 | + $this->reminderMapper->insert($reminder); |
|
| 120 | + $this->cache->set("{$user->getUID()}-$fileId", $reminder); |
|
| 121 | + return true; |
|
| 122 | + } |
|
| 123 | + $reminder->setDueDate($dueDate); |
|
| 124 | + $reminder->setUpdatedAt($now); |
|
| 125 | + $this->reminderMapper->update($reminder); |
|
| 126 | + $this->cache->set("{$user->getUID()}-$fileId", $reminder); |
|
| 127 | + return false; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @throws NodeNotFoundException |
|
| 132 | + * @throws ReminderNotFoundException |
|
| 133 | + */ |
|
| 134 | + public function remove(IUser $user, int $fileId): void { |
|
| 135 | + $this->checkNode($user, $fileId); |
|
| 136 | + $reminder = $this->getDueForUser($user, $fileId); |
|
| 137 | + if ($reminder === null) { |
|
| 138 | + throw new ReminderNotFoundException(); |
|
| 139 | + } |
|
| 140 | + $this->deleteReminder($reminder); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + public function removeAllForNode(Node $node): void { |
|
| 144 | + $reminders = $this->reminderMapper->findAllForNode($node); |
|
| 145 | + foreach ($reminders as $reminder) { |
|
| 146 | + $this->deleteReminder($reminder); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + public function removeAllForUser(IUser $user): void { |
|
| 151 | + $reminders = $this->reminderMapper->findAllForUser($user); |
|
| 152 | + foreach ($reminders as $reminder) { |
|
| 153 | + $this->deleteReminder($reminder); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @throws DoesNotExistException |
|
| 159 | + * @throws UserNotFoundException |
|
| 160 | + */ |
|
| 161 | + public function send(Reminder $reminder): void { |
|
| 162 | + if ($reminder->getNotified()) { |
|
| 163 | + return; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + $user = $this->userManager->get($reminder->getUserId()); |
|
| 167 | + if ($user === null) { |
|
| 168 | + throw new UserNotFoundException(); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + $notification = $this->notificationManager->createNotification(); |
|
| 172 | + $notification |
|
| 173 | + ->setApp(Application::APP_ID) |
|
| 174 | + ->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('files', 'folder.svg'))) |
|
| 175 | + ->setUser($user->getUID()) |
|
| 176 | + ->setObject('reminder', (string)$reminder->getId()) |
|
| 177 | + ->setSubject('reminder-due', [ |
|
| 178 | + 'fileId' => $reminder->getFileId(), |
|
| 179 | + ]) |
|
| 180 | + ->setDateTime($reminder->getDueDate()); |
|
| 181 | + |
|
| 182 | + try { |
|
| 183 | + $this->notificationManager->notify($notification); |
|
| 184 | + $this->reminderMapper->markNotified($reminder); |
|
| 185 | + $this->cache->set("{$user->getUID()}-{$reminder->getFileId()}", $reminder); |
|
| 186 | + } catch (Throwable $th) { |
|
| 187 | + $this->logger->error($th->getMessage(), $th->getTrace()); |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + public function cleanUp(?int $limit = null): void { |
|
| 192 | + $buffer = (new DateTime()) |
|
| 193 | + ->setTimezone(new DateTimeZone('UTC')) |
|
| 194 | + ->modify('-1 day'); |
|
| 195 | + $reminders = $this->reminderMapper->findNotified($buffer, $limit); |
|
| 196 | + foreach ($reminders as $reminder) { |
|
| 197 | + $this->deleteReminder($reminder); |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + private function deleteReminder(Reminder $reminder): void { |
|
| 202 | + $this->reminderMapper->delete($reminder); |
|
| 203 | + $this->cache->set("{$reminder->getUserId()}-{$reminder->getFileId()}", false); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * @throws NodeNotFoundException |
|
| 209 | + */ |
|
| 210 | + private function checkNode(IUser $user, int $fileId): void { |
|
| 211 | + $userFolder = $this->root->getUserFolder($user->getUID()); |
|
| 212 | + $node = $userFolder->getFirstNodeById($fileId); |
|
| 213 | + if ($node === null) { |
|
| 214 | + throw new NodeNotFoundException(); |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | 217 | } |