ApiController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\Controller;
  25. use DateTime;
  26. use DateTimeInterface;
  27. use DateTimeZone;
  28. use Exception;
  29. use OCA\FilesReminders\Exception\NodeNotFoundException;
  30. use OCA\FilesReminders\Service\ReminderService;
  31. use OCP\AppFramework\Db\DoesNotExistException;
  32. use OCP\AppFramework\Http;
  33. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  34. use OCP\AppFramework\Http\DataResponse;
  35. use OCP\AppFramework\OCSController;
  36. use OCP\IRequest;
  37. use OCP\IUserSession;
  38. use Psr\Log\LoggerInterface;
  39. class ApiController extends OCSController {
  40. public function __construct(
  41. string $appName,
  42. IRequest $request,
  43. protected ReminderService $reminderService,
  44. protected IUserSession $userSession,
  45. protected LoggerInterface $logger,
  46. ) {
  47. parent::__construct($appName, $request);
  48. }
  49. /**
  50. * Get a reminder
  51. */
  52. #[NoAdminRequired]
  53. public function get(int $fileId): DataResponse {
  54. $user = $this->userSession->getUser();
  55. if ($user === null) {
  56. return new DataResponse([], Http::STATUS_UNAUTHORIZED);
  57. }
  58. try {
  59. $reminder = $this->reminderService->getDueForUser($user, $fileId);
  60. $reminderData = [
  61. 'dueDate' => $reminder->getDueDate()->format(DateTimeInterface::ATOM), // ISO 8601
  62. ];
  63. return new DataResponse($reminderData, Http::STATUS_OK);
  64. } catch (DoesNotExistException $e) {
  65. $reminderData = [
  66. 'dueDate' => null,
  67. ];
  68. return new DataResponse($reminderData, Http::STATUS_OK);
  69. }
  70. }
  71. /**
  72. * Set a reminder
  73. *
  74. * @param string $dueDate ISO 8601 formatted date time string
  75. */
  76. #[NoAdminRequired]
  77. public function set(int $fileId, string $dueDate): DataResponse {
  78. try {
  79. $dueDate = (new DateTime($dueDate))->setTimezone(new DateTimeZone('UTC'));
  80. } catch (Exception $e) {
  81. $this->logger->error($e->getMessage(), ['exception' => $e]);
  82. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  83. }
  84. $user = $this->userSession->getUser();
  85. if ($user === null) {
  86. return new DataResponse([], Http::STATUS_UNAUTHORIZED);
  87. }
  88. try {
  89. $created = $this->reminderService->createOrUpdate($user, $fileId, $dueDate);
  90. if ($created) {
  91. return new DataResponse([], Http::STATUS_CREATED);
  92. }
  93. return new DataResponse([], Http::STATUS_OK);
  94. } catch (NodeNotFoundException $e) {
  95. return new DataResponse([], Http::STATUS_NOT_FOUND);
  96. }
  97. }
  98. /**
  99. * Remove a reminder
  100. */
  101. #[NoAdminRequired]
  102. public function remove(int $fileId): DataResponse {
  103. $user = $this->userSession->getUser();
  104. if ($user === null) {
  105. return new DataResponse([], Http::STATUS_UNAUTHORIZED);
  106. }
  107. try {
  108. $this->reminderService->remove($user, $fileId);
  109. return new DataResponse([], Http::STATUS_OK);
  110. } catch (DoesNotExistException $e) {
  111. return new DataResponse([], Http::STATUS_NOT_FOUND);
  112. }
  113. }
  114. }