1
0

StatusesController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /**
  26. * StatusesController constructor.
  27. *
  28. * @param string $appName
  29. * @param IRequest $request
  30. * @param StatusService $service
  31. */
  32. public function __construct(
  33. string $appName,
  34. IRequest $request,
  35. private StatusService $service,
  36. ) {
  37. parent::__construct($appName, $request);
  38. }
  39. /**
  40. * Find statuses of users
  41. *
  42. * @param int|null $limit Maximum number of statuses to find
  43. * @param int|null $offset Offset for finding statuses
  44. * @return DataResponse<Http::STATUS_OK, list<UserStatusPublic>, array{}>
  45. *
  46. * 200: Statuses returned
  47. */
  48. #[NoAdminRequired]
  49. #[ApiRoute(verb: 'GET', url: '/api/v1/statuses')]
  50. public function findAll(?int $limit = null, ?int $offset = null): DataResponse {
  51. $allStatuses = $this->service->findAll($limit, $offset);
  52. return new DataResponse(array_values(array_map(function ($userStatus) {
  53. return $this->formatStatus($userStatus);
  54. }, $allStatuses)));
  55. }
  56. /**
  57. * Find the status of a user
  58. *
  59. * @param string $userId ID of the user
  60. * @return DataResponse<Http::STATUS_OK, UserStatusPublic, array{}>
  61. * @throws OCSNotFoundException The user was not found
  62. *
  63. * 200: Status returned
  64. */
  65. #[NoAdminRequired]
  66. #[ApiRoute(verb: 'GET', url: '/api/v1/statuses/{userId}')]
  67. public function find(string $userId): DataResponse {
  68. try {
  69. $userStatus = $this->service->findByUserId($userId);
  70. } catch (DoesNotExistException $ex) {
  71. throw new OCSNotFoundException('No status for the requested userId');
  72. }
  73. return new DataResponse($this->formatStatus($userStatus));
  74. }
  75. /**
  76. * @param UserStatus $status
  77. * @return UserStatusPublic
  78. */
  79. private function formatStatus(UserStatus $status): array {
  80. /** @var UserStatusType $visibleStatus */
  81. $visibleStatus = $status->getStatus();
  82. if ($visibleStatus === IUserStatus::INVISIBLE) {
  83. $visibleStatus = IUserStatus::OFFLINE;
  84. }
  85. return [
  86. 'userId' => $status->getUserId(),
  87. 'message' => $status->getCustomMessage(),
  88. 'icon' => $status->getCustomIcon(),
  89. 'clearAt' => $status->getClearAt(),
  90. 'status' => $visibleStatus,
  91. ];
  92. }
  93. }