ReminderService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023 Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @author Christopher Ng <chrng8@gmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\FilesReminders\Service;
  25. use DateTime;
  26. use DateTimeZone;
  27. use OCA\FilesReminders\AppInfo\Application;
  28. use OCA\FilesReminders\Db\Reminder;
  29. use OCA\FilesReminders\Db\ReminderMapper;
  30. use OCA\FilesReminders\Exception\NodeNotFoundException;
  31. use OCA\FilesReminders\Exception\UserNotFoundException;
  32. use OCA\FilesReminders\Model\RichReminder;
  33. use OCP\AppFramework\Db\DoesNotExistException;
  34. use OCP\Files\IRootFolder;
  35. use OCP\Files\Node;
  36. use OCP\IURLGenerator;
  37. use OCP\IUser;
  38. use OCP\IUserManager;
  39. use OCP\Notification\IManager as INotificationManager;
  40. use Psr\Log\LoggerInterface;
  41. use Throwable;
  42. class ReminderService {
  43. public function __construct(
  44. protected IUserManager $userManager,
  45. protected IURLGenerator $urlGenerator,
  46. protected INotificationManager $notificationManager,
  47. protected ReminderMapper $reminderMapper,
  48. protected IRootFolder $root,
  49. protected LoggerInterface $logger,
  50. ) {}
  51. /**
  52. * @throws DoesNotExistException
  53. */
  54. public function get(int $id): RichReminder {
  55. $reminder = $this->reminderMapper->find($id);
  56. return new RichReminder($reminder, $this->root);
  57. }
  58. /**
  59. * @throws DoesNotExistException
  60. */
  61. public function getDueForUser(IUser $user, int $fileId): RichReminder {
  62. $reminder = $this->reminderMapper->findDueForUser($user, $fileId);
  63. return new RichReminder($reminder, $this->root);
  64. }
  65. /**
  66. * @return RichReminder[]
  67. */
  68. public function getAll(?IUser $user = null) {
  69. $reminders = ($user !== null)
  70. ? $this->reminderMapper->findAllForUser($user)
  71. : $this->reminderMapper->findAll();
  72. return array_map(
  73. fn (Reminder $reminder) => new RichReminder($reminder, $this->root),
  74. $reminders,
  75. );
  76. }
  77. /**
  78. * @return bool true if created, false if updated
  79. *
  80. * @throws NodeNotFoundException
  81. */
  82. public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): bool {
  83. $now = new DateTime('now', new DateTimeZone('UTC'));
  84. try {
  85. $reminder = $this->reminderMapper->findDueForUser($user, $fileId);
  86. $reminder->setDueDate($dueDate);
  87. $reminder->setUpdatedAt($now);
  88. $this->reminderMapper->update($reminder);
  89. return false;
  90. } catch (DoesNotExistException $e) {
  91. $nodes = $this->root->getUserFolder($user->getUID())->getById($fileId);
  92. if (empty($nodes)) {
  93. throw new NodeNotFoundException();
  94. }
  95. // Create new reminder if no reminder is found
  96. $reminder = new Reminder();
  97. $reminder->setUserId($user->getUID());
  98. $reminder->setFileId($fileId);
  99. $reminder->setDueDate($dueDate);
  100. $reminder->setUpdatedAt($now);
  101. $reminder->setCreatedAt($now);
  102. $this->reminderMapper->insert($reminder);
  103. return true;
  104. }
  105. }
  106. /**
  107. * @throws DoesNotExistException
  108. */
  109. public function remove(IUser $user, int $fileId): void {
  110. $reminder = $this->reminderMapper->findDueForUser($user, $fileId);
  111. $this->reminderMapper->delete($reminder);
  112. }
  113. public function removeAllForNode(Node $node): void {
  114. $reminders = $this->reminderMapper->findAllForNode($node);
  115. foreach ($reminders as $reminder) {
  116. $this->reminderMapper->delete($reminder);
  117. }
  118. }
  119. public function removeAllForUser(IUser $user): void {
  120. $reminders = $this->reminderMapper->findAllForUser($user);
  121. foreach ($reminders as $reminder) {
  122. $this->reminderMapper->delete($reminder);
  123. }
  124. }
  125. /**
  126. * @throws DoesNotExistException
  127. * @throws UserNotFoundException
  128. */
  129. public function send(Reminder $reminder): void {
  130. if ($reminder->getNotified()) {
  131. return;
  132. }
  133. $user = $this->userManager->get($reminder->getUserId());
  134. if ($user === null) {
  135. throw new UserNotFoundException();
  136. }
  137. $notification = $this->notificationManager->createNotification();
  138. $notification
  139. ->setApp(Application::APP_ID)
  140. ->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('files', 'folder.svg')))
  141. ->setUser($user->getUID())
  142. ->setObject('reminder', (string)$reminder->getId())
  143. ->setSubject('reminder-due', [
  144. 'fileId' => $reminder->getFileId(),
  145. ])
  146. ->setDateTime($reminder->getDueDate());
  147. try {
  148. $this->notificationManager->notify($notification);
  149. $this->reminderMapper->markNotified($reminder);
  150. } catch (Throwable $th) {
  151. $this->logger->error($th->getMessage(), $th->getTrace());
  152. }
  153. }
  154. public function cleanUp(?int $limit = null): void {
  155. $buffer = (new DateTime())
  156. ->setTimezone(new DateTimeZone('UTC'))
  157. ->modify('-1 day');
  158. $reminders = $this->reminderMapper->findNotified($buffer, $limit);
  159. foreach ($reminders as $reminder) {
  160. $this->reminderMapper->delete($reminder);
  161. }
  162. }
  163. }