1
0

StatusesController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\Attribute\NoAdminRequired;
  15. use OCP\AppFramework\Http\DataResponse;
  16. use OCP\AppFramework\OCS\OCSNotFoundException;
  17. use OCP\AppFramework\OCSController;
  18. use OCP\IRequest;
  19. use OCP\UserStatus\IUserStatus;
  20. /**
  21. * @psalm-import-type UserStatusType from ResponseDefinitions
  22. * @psalm-import-type UserStatusPublic from ResponseDefinitions
  23. */
  24. class StatusesController extends OCSController {
  25. /** @var StatusService */
  26. private $service;
  27. /**
  28. * StatusesController constructor.
  29. *
  30. * @param string $appName
  31. * @param IRequest $request
  32. * @param StatusService $service
  33. */
  34. public function __construct(string $appName,
  35. IRequest $request,
  36. StatusService $service) {
  37. parent::__construct($appName, $request);
  38. $this->service = $service;
  39. }
  40. /**
  41. * Find statuses of users
  42. *
  43. * @param int|null $limit Maximum number of statuses to find
  44. * @param int|null $offset Offset for finding statuses
  45. * @return DataResponse<Http::STATUS_OK, UserStatusPublic[], array{}>
  46. *
  47. * 200: Statuses returned
  48. */
  49. #[NoAdminRequired]
  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. * @param string $userId ID of the user
  61. * @return DataResponse<Http::STATUS_OK, UserStatusPublic, array{}>
  62. * @throws OCSNotFoundException The user was not found
  63. *
  64. * 200: Status returned
  65. */
  66. #[NoAdminRequired]
  67. #[ApiRoute(verb: 'GET', url: '/api/v1/statuses/{userId}')]
  68. public function find(string $userId): DataResponse {
  69. try {
  70. $userStatus = $this->service->findByUserId($userId);
  71. } catch (DoesNotExistException $ex) {
  72. throw new OCSNotFoundException('No status for the requested userId');
  73. }
  74. return new DataResponse($this->formatStatus($userStatus));
  75. }
  76. /**
  77. * @param UserStatus $status
  78. * @return UserStatusPublic
  79. */
  80. private function formatStatus(UserStatus $status): array {
  81. /** @var UserStatusType $visibleStatus */
  82. $visibleStatus = $status->getStatus();
  83. if ($visibleStatus === IUserStatus::INVISIBLE) {
  84. $visibleStatus = IUserStatus::OFFLINE;
  85. }
  86. return [
  87. 'userId' => $status->getUserId(),
  88. 'message' => $status->getCustomMessage(),
  89. 'icon' => $status->getCustomIcon(),
  90. 'clearAt' => $status->getClearAt(),
  91. 'status' => $visibleStatus,
  92. ];
  93. }
  94. }