StatusesController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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\DataResponse;
  33. use OCP\AppFramework\OCS\OCSNotFoundException;
  34. use OCP\AppFramework\OCSController;
  35. use OCP\IRequest;
  36. use OCP\UserStatus\IUserStatus;
  37. /**
  38. * @psalm-import-type UserStatusType from ResponseDefinitions
  39. * @psalm-import-type UserStatusPublic from ResponseDefinitions
  40. */
  41. class StatusesController extends OCSController {
  42. /** @var StatusService */
  43. private $service;
  44. /**
  45. * StatusesController constructor.
  46. *
  47. * @param string $appName
  48. * @param IRequest $request
  49. * @param StatusService $service
  50. */
  51. public function __construct(string $appName,
  52. IRequest $request,
  53. StatusService $service) {
  54. parent::__construct($appName, $request);
  55. $this->service = $service;
  56. }
  57. /**
  58. * Find statuses of users
  59. *
  60. * @NoAdminRequired
  61. *
  62. * @param int|null $limit Maximum number of statuses to find
  63. * @param int|null $offset Offset for finding statuses
  64. * @return DataResponse<Http::STATUS_OK, UserStatusPublic[], array{}>
  65. *
  66. * 200: Statuses returned
  67. */
  68. public function findAll(?int $limit = null, ?int $offset = null): DataResponse {
  69. $allStatuses = $this->service->findAll($limit, $offset);
  70. return new DataResponse(array_map(function ($userStatus) {
  71. return $this->formatStatus($userStatus);
  72. }, $allStatuses));
  73. }
  74. /**
  75. * Find the status of a user
  76. *
  77. * @NoAdminRequired
  78. *
  79. * @param string $userId ID of the user
  80. * @return DataResponse<Http::STATUS_OK, UserStatusPublic, array{}>
  81. * @throws OCSNotFoundException The user was not found
  82. *
  83. * 200: Status returned
  84. */
  85. public function find(string $userId): DataResponse {
  86. try {
  87. $userStatus = $this->service->findByUserId($userId);
  88. } catch (DoesNotExistException $ex) {
  89. throw new OCSNotFoundException('No status for the requested userId');
  90. }
  91. return new DataResponse($this->formatStatus($userStatus));
  92. }
  93. /**
  94. * @param UserStatus $status
  95. * @return UserStatusPublic
  96. */
  97. private function formatStatus(UserStatus $status): array {
  98. /** @var UserStatusType $visibleStatus */
  99. $visibleStatus = $status->getStatus();
  100. if ($visibleStatus === IUserStatus::INVISIBLE) {
  101. $visibleStatus = IUserStatus::OFFLINE;
  102. }
  103. return [
  104. 'userId' => $status->getUserId(),
  105. 'message' => $status->getCustomMessage(),
  106. 'icon' => $status->getCustomIcon(),
  107. 'clearAt' => $status->getClearAt(),
  108. 'status' => $visibleStatus,
  109. ];
  110. }
  111. }