StatusesController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UserStatus\Controller;
  8. use OCA\UserStatus\Db\UserStatus;
  9. use OCA\UserStatus\ResponseDefinitions;
  10. use OCA\UserStatus\Service\StatusService;
  11. use OCP\AppFramework\Db\DoesNotExistException;
  12. use OCP\AppFramework\Http;
  13. use OCP\AppFramework\Http\Attribute\ApiRoute;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\AppFramework\OCS\OCSNotFoundException;
  16. use OCP\AppFramework\OCSController;
  17. use OCP\IRequest;
  18. use OCP\UserStatus\IUserStatus;
  19. /**
  20. * @psalm-import-type UserStatusType from ResponseDefinitions
  21. * @psalm-import-type UserStatusPublic from ResponseDefinitions
  22. */
  23. class StatusesController extends OCSController {
  24. /** @var StatusService */
  25. private $service;
  26. /**
  27. * StatusesController constructor.
  28. *
  29. * @param string $appName
  30. * @param IRequest $request
  31. * @param StatusService $service
  32. */
  33. public function __construct(string $appName,
  34. IRequest $request,
  35. StatusService $service) {
  36. parent::__construct($appName, $request);
  37. $this->service = $service;
  38. }
  39. /**
  40. * Find statuses of users
  41. *
  42. * @NoAdminRequired
  43. *
  44. * @param int|null $limit Maximum number of statuses to find
  45. * @param int|null $offset Offset for finding statuses
  46. * @return DataResponse<Http::STATUS_OK, UserStatusPublic[], array{}>
  47. *
  48. * 200: Statuses returned
  49. */
  50. #[ApiRoute(verb: 'GET', url: '/api/v1/statuses')]
  51. public function findAll(?int $limit = null, ?int $offset = null): DataResponse {
  52. $allStatuses = $this->service->findAll($limit, $offset);
  53. return new DataResponse(array_map(function ($userStatus) {
  54. return $this->formatStatus($userStatus);
  55. }, $allStatuses));
  56. }
  57. /**
  58. * Find the status of a user
  59. *
  60. * @NoAdminRequired
  61. *
  62. * @param string $userId ID of the user
  63. * @return DataResponse<Http::STATUS_OK, UserStatusPublic, array{}>
  64. * @throws OCSNotFoundException The user was not found
  65. *
  66. * 200: Status returned
  67. */
  68. #[ApiRoute(verb: 'GET', url: '/api/v1/statuses/{userId}')]
  69. public function find(string $userId): DataResponse {
  70. try {
  71. $userStatus = $this->service->findByUserId($userId);
  72. } catch (DoesNotExistException $ex) {
  73. throw new OCSNotFoundException('No status for the requested userId');
  74. }
  75. return new DataResponse($this->formatStatus($userStatus));
  76. }
  77. /**
  78. * @param UserStatus $status
  79. * @return UserStatusPublic
  80. */
  81. private function formatStatus(UserStatus $status): array {
  82. /** @var UserStatusType $visibleStatus */
  83. $visibleStatus = $status->getStatus();
  84. if ($visibleStatus === IUserStatus::INVISIBLE) {
  85. $visibleStatus = IUserStatus::OFFLINE;
  86. }
  87. return [
  88. 'userId' => $status->getUserId(),
  89. 'message' => $status->getCustomMessage(),
  90. 'icon' => $status->getCustomIcon(),
  91. 'clearAt' => $status->getClearAt(),
  92. 'status' => $visibleStatus,
  93. ];
  94. }
  95. }