ReminderService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /**
  53. * @throws DoesNotExistException
  54. */
  55. public function get(int $id): RichReminder {
  56. $reminder = $this->reminderMapper->find($id);
  57. return new RichReminder($reminder, $this->root);
  58. }
  59. /**
  60. * @throws DoesNotExistException
  61. */
  62. public function getDueForUser(IUser $user, int $fileId): RichReminder {
  63. $reminder = $this->reminderMapper->findDueForUser($user, $fileId);
  64. return new RichReminder($reminder, $this->root);
  65. }
  66. /**
  67. * @return RichReminder[]
  68. */
  69. public function getAll(?IUser $user = null) {
  70. $reminders = ($user !== null)
  71. ? $this->reminderMapper->findAllForUser($user)
  72. : $this->reminderMapper->findAll();
  73. return array_map(
  74. fn (Reminder $reminder) => new RichReminder($reminder, $this->root),
  75. $reminders,
  76. );
  77. }
  78. /**
  79. * @return bool true if created, false if updated
  80. *
  81. * @throws NodeNotFoundException
  82. */
  83. public function createOrUpdate(IUser $user, int $fileId, DateTime $dueDate): bool {
  84. $now = new DateTime('now', new DateTimeZone('UTC'));
  85. try {
  86. $reminder = $this->reminderMapper->findDueForUser($user, $fileId);
  87. $reminder->setDueDate($dueDate);
  88. $reminder->setUpdatedAt($now);
  89. $this->reminderMapper->update($reminder);
  90. return false;
  91. } catch (DoesNotExistException $e) {
  92. $nodes = $this->root->getUserFolder($user->getUID())->getById($fileId);
  93. if (empty($nodes)) {
  94. throw new NodeNotFoundException();
  95. }
  96. // Create new reminder if no reminder is found
  97. $reminder = new Reminder();
  98. $reminder->setUserId($user->getUID());
  99. $reminder->setFileId($fileId);
  100. $reminder->setDueDate($dueDate);
  101. $reminder->setUpdatedAt($now);
  102. $reminder->setCreatedAt($now);
  103. $this->reminderMapper->insert($reminder);
  104. return true;
  105. }
  106. }
  107. /**
  108. * @throws DoesNotExistException
  109. */
  110. public function remove(IUser $user, int $fileId): void {
  111. $reminder = $this->reminderMapper->findDueForUser($user, $fileId);
  112. $this->reminderMapper->delete($reminder);
  113. }
  114. public function removeAllForNode(Node $node): void {
  115. $reminders = $this->reminderMapper->findAllForNode($node);
  116. foreach ($reminders as $reminder) {
  117. $this->reminderMapper->delete($reminder);
  118. }
  119. }
  120. public function removeAllForUser(IUser $user): void {
  121. $reminders = $this->reminderMapper->findAllForUser($user);
  122. foreach ($reminders as $reminder) {
  123. $this->reminderMapper->delete($reminder);
  124. }
  125. }
  126. /**
  127. * @throws DoesNotExistException
  128. * @throws UserNotFoundException
  129. */
  130. public function send(Reminder $reminder): void {
  131. if ($reminder->getNotified()) {
  132. return;
  133. }
  134. $user = $this->userManager->get($reminder->getUserId());
  135. if ($user === null) {
  136. throw new UserNotFoundException();
  137. }
  138. $notification = $this->notificationManager->createNotification();
  139. $notification
  140. ->setApp(Application::APP_ID)
  141. ->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('files', 'folder.svg')))
  142. ->setUser($user->getUID())
  143. ->setObject('reminder', (string)$reminder->getId())
  144. ->setSubject('reminder-due', [
  145. 'fileId' => $reminder->getFileId(),
  146. ])
  147. ->setDateTime($reminder->getDueDate());
  148. try {
  149. $this->notificationManager->notify($notification);
  150. $this->reminderMapper->markNotified($reminder);
  151. } catch (Throwable $th) {
  152. $this->logger->error($th->getMessage(), $th->getTrace());
  153. }
  154. }
  155. public function cleanUp(?int $limit = null): void {
  156. $buffer = (new DateTime())
  157. ->setTimezone(new DateTimeZone('UTC'))
  158. ->modify('-1 day');
  159. $reminders = $this->reminderMapper->findNotified($buffer, $limit);
  160. foreach ($reminders as $reminder) {
  161. $this->reminderMapper->delete($reminder);
  162. }
  163. }
  164. }