StatusesController.php 3.4 KB

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