1
0

ReminderService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\FilesReminders\Service;
  8. use DateTime;
  9. use DateTimeZone;
  10. use OCA\FilesReminders\AppInfo\Application;
  11. use OCA\FilesReminders\Db\Reminder;
  12. use OCA\FilesReminders\Db\ReminderMapper;
  13. use OCA\FilesReminders\Exception\NodeNotFoundException;
  14. use OCA\FilesReminders\Exception\UserNotFoundException;
  15. use OCA\FilesReminders\Model\RichReminder;
  16. use OCP\AppFramework\Db\DoesNotExistException;
  17. use OCP\Files\IRootFolder;
  18. use OCP\Files\Node;
  19. use OCP\IURLGenerator;
  20. use OCP\IUser;
  21. use OCP\IUserManager;
  22. use OCP\Notification\IManager as INotificationManager;
  23. use Psr\Log\LoggerInterface;
  24. use Throwable;
  25. class ReminderService {
  26. public function __construct(
  27. protected IUserManager $userManager,
  28. protected IURLGenerator $urlGenerator,
  29. protected INotificationManager $notificationManager,
  30. protected ReminderMapper $reminderMapper,
  31. protected IRootFolder $root,
  32. protected LoggerInterface $logger,
  33. ) {
  34. }
  35. /**
  36. * @throws DoesNotExistException
  37. */
  38. public function get(int $id): RichReminder {
  39. $reminder = $this->reminderMapper->find($id);
  40. return new RichReminder($reminder, $this->root);
  41. }
  42. /**
  43. * @throws DoesNotExistException
  44. */
  45. public function getDueForUser(IUser $user, int $fileId): RichReminder {
  46. $reminder = $this->reminderMapper->findDueForUser($user, $fileId);
  47. return new RichReminder($reminder, $this->root);
  48. }
  49. /**
  50. * @return RichReminder[]
  51. */
  52. public function getAll(?IUser $user = null) {
  53. $reminders = ($user !== null)
  54. ? $this->reminderMapper->findAllForUser($user)
  55. : $this->reminderMapper->findAll();
  56. return array_map(
  57. fn (Reminder $reminder) => new RichReminder($reminder, $this->root),
  58. $reminders,
  59. );
  60. }
  61. /**
  62. * @return bool true if created, false if updated
  63. *
  64. * @throws NodeNotFoundException
  65. */
  66. public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): bool {
  67. $now = new DateTime('now', new DateTimeZone('UTC'));
  68. try {
  69. $reminder = $this->reminderMapper->findDueForUser($user, $fileId);
  70. $reminder->setDueDate($dueDate);
  71. $reminder->setUpdatedAt($now);
  72. $this->reminderMapper->update($reminder);
  73. return false;
  74. } catch (DoesNotExistException $e) {
  75. $node = $this->root->getUserFolder($user->getUID())->getFirstNodeById($fileId);
  76. if (!$node) {
  77. throw new NodeNotFoundException();
  78. }
  79. // Create new reminder if no reminder is found
  80. $reminder = new Reminder();
  81. $reminder->setUserId($user->getUID());
  82. $reminder->setFileId($fileId);
  83. $reminder->setDueDate($dueDate);
  84. $reminder->setUpdatedAt($now);
  85. $reminder->setCreatedAt($now);
  86. $this->reminderMapper->insert($reminder);
  87. return true;
  88. }
  89. }
  90. /**
  91. * @throws DoesNotExistException
  92. */
  93. public function remove(IUser $user, int $fileId): void {
  94. $reminder = $this->reminderMapper->findDueForUser($user, $fileId);
  95. $this->reminderMapper->delete($reminder);
  96. }
  97. public function removeAllForNode(Node $node): void {
  98. $reminders = $this->reminderMapper->findAllForNode($node);
  99. foreach ($reminders as $reminder) {
  100. $this->reminderMapper->delete($reminder);
  101. }
  102. }
  103. public function removeAllForUser(IUser $user): void {
  104. $reminders = $this->reminderMapper->findAllForUser($user);
  105. foreach ($reminders as $reminder) {
  106. $this->reminderMapper->delete($reminder);
  107. }
  108. }
  109. /**
  110. * @throws DoesNotExistException
  111. * @throws UserNotFoundException
  112. */
  113. public function send(Reminder $reminder): void {
  114. if ($reminder->getNotified()) {
  115. return;
  116. }
  117. $user = $this->userManager->get($reminder->getUserId());
  118. if ($user === null) {
  119. throw new UserNotFoundException();
  120. }
  121. $notification = $this->notificationManager->createNotification();
  122. $notification
  123. ->setApp(Application::APP_ID)
  124. ->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('files', 'folder.svg')))
  125. ->setUser($user->getUID())
  126. ->setObject('reminder', (string)$reminder->getId())
  127. ->setSubject('reminder-due', [
  128. 'fileId' => $reminder->getFileId(),
  129. ])
  130. ->setDateTime($reminder->getDueDate());
  131. try {
  132. $this->notificationManager->notify($notification);
  133. $this->reminderMapper->markNotified($reminder);
  134. } catch (Throwable $th) {
  135. $this->logger->error($th->getMessage(), $th->getTrace());
  136. }
  137. }
  138. public function cleanUp(?int $limit = null): void {
  139. $buffer = (new DateTime())
  140. ->setTimezone(new DateTimeZone('UTC'))
  141. ->modify('-1 day');
  142. $reminders = $this->reminderMapper->findNotified($buffer, $limit);
  143. foreach ($reminders as $reminder) {
  144. $this->reminderMapper->delete($reminder);
  145. }
  146. }
  147. }