OutOfOfficeController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
  5. *
  6. * @author Richard Steinmetz <richard@steinmetz.cloud>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (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 General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\Controller;
  25. use OCA\DAV\Db\AbsenceMapper;
  26. use OCA\DAV\ResponseDefinitions;
  27. use OCP\AppFramework\Db\DoesNotExistException;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\OCSController;
  31. use OCP\IRequest;
  32. /**
  33. * @psalm-import-type DAVOutOfOfficeData from ResponseDefinitions
  34. */
  35. class OutOfOfficeController extends OCSController {
  36. public function __construct(
  37. string $appName,
  38. IRequest $request,
  39. private AbsenceMapper $absenceMapper,
  40. ) {
  41. parent::__construct($appName, $request);
  42. }
  43. /**
  44. * Get the currently configured out-of-office data of a user.
  45. *
  46. * @NoAdminRequired
  47. * @NoCSRFRequired
  48. *
  49. * @param string $userId The user id to get out-of-office data for.
  50. * @return DataResponse<Http::STATUS_OK|Http::STATUS_NOT_FOUND, ?DAVOutOfOfficeData, array{}>
  51. *
  52. * 200: Out-of-office data
  53. * 404: No out-of-office data was found
  54. */
  55. public function getCurrentOutOfOfficeData(string $userId): DataResponse {
  56. try {
  57. $data = $this->absenceMapper->findByUserId($userId);
  58. } catch (DoesNotExistException) {
  59. return new DataResponse(null, Http::STATUS_NOT_FOUND);
  60. }
  61. return new DataResponse([
  62. 'id' => $data->getId(),
  63. 'userId' => $data->getUserId(),
  64. 'firstDay' => $data->getFirstDay(),
  65. 'lastDay' => $data->getLastDay(),
  66. 'status' => $data->getStatus(),
  67. 'message' => $data->getMessage(),
  68. ]);
  69. }
  70. }