StatusesController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Kate Döen <kate.doeen@nextcloud.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\UserStatus\Controller;
  27. use OCA\UserStatus\Db\UserStatus;
  28. use OCA\UserStatus\ResponseDefinitions;
  29. use OCA\UserStatus\Service\StatusService;
  30. use OCP\AppFramework\Db\DoesNotExistException;
  31. use OCP\AppFramework\Http;
  32. use OCP\AppFramework\Http\Attribute\ApiRoute;
  33. use OCP\AppFramework\Http\DataResponse;
  34. use OCP\AppFramework\OCS\OCSNotFoundException;
  35. use OCP\AppFramework\OCSController;
  36. use OCP\IRequest;
  37. use OCP\UserStatus\IUserStatus;
  38. /**
  39. * @psalm-import-type UserStatusType from ResponseDefinitions
  40. * @psalm-import-type UserStatusPublic from ResponseDefinitions
  41. */
  42. class StatusesController extends OCSController {
  43. /** @var StatusService */
  44. private $service;
  45. /**
  46. * StatusesController constructor.
  47. *
  48. * @param string $appName
  49. * @param IRequest $request
  50. * @param StatusService $service
  51. */
  52. public function __construct(string $appName,
  53. IRequest $request,
  54. StatusService $service) {
  55. parent::__construct($appName, $request);
  56. $this->service = $service;
  57. }
  58. /**
  59. * Find statuses of users
  60. *
  61. * @NoAdminRequired
  62. *
  63. * @param int|null $limit Maximum number of statuses to find
  64. * @param int|null $offset Offset for finding statuses
  65. * @return DataResponse<Http::STATUS_OK, UserStatusPublic[], array{}>
  66. *
  67. * 200: Statuses returned
  68. */
  69. #[ApiRoute(verb: 'GET', url: '/api/v1/statuses')]
  70. public function findAll(?int $limit = null, ?int $offset = null): DataResponse {
  71. $allStatuses = $this->service->findAll($limit, $offset);
  72. return new DataResponse(array_map(function ($userStatus) {
  73. return $this->formatStatus($userStatus);
  74. }, $allStatuses));
  75. }
  76. /**
  77. * Find the status of a user
  78. *
  79. * @NoAdminRequired
  80. *
  81. * @param string $userId ID of the user
  82. * @return DataResponse<Http::STATUS_OK, UserStatusPublic, array{}>
  83. * @throws OCSNotFoundException The user was not found
  84. *
  85. * 200: Status returned
  86. */
  87. #[ApiRoute(verb: 'GET', url: '/api/v1/statuses/{userId}')]
  88. public function find(string $userId): DataResponse {
  89. try {
  90. $userStatus = $this->service->findByUserId($userId);
  91. } catch (DoesNotExistException $ex) {
  92. throw new OCSNotFoundException('No status for the requested userId');
  93. }
  94. return new DataResponse($this->formatStatus($userStatus));
  95. }
  96. /**
  97. * @param UserStatus $status
  98. * @return UserStatusPublic
  99. */
  100. private function formatStatus(UserStatus $status): array {
  101. /** @var UserStatusType $visibleStatus */
  102. $visibleStatus = $status->getStatus();
  103. if ($visibleStatus === IUserStatus::INVISIBLE) {
  104. $visibleStatus = IUserStatus::OFFLINE;
  105. }
  106. return [
  107. 'userId' => $status->getUserId(),
  108. 'message' => $status->getCustomMessage(),
  109. 'icon' => $status->getCustomIcon(),
  110. 'clearAt' => $status->getClearAt(),
  111. 'status' => $visibleStatus,
  112. ];
  113. }
  114. }