1
0

ApiController.php 3.7 KB

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