You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatusesController.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\UserStatus\Controller;
  26. use OCA\UserStatus\Db\UserStatus;
  27. use OCA\UserStatus\Service\StatusService;
  28. use OCP\AppFramework\Db\DoesNotExistException;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\OCS\OCSNotFoundException;
  31. use OCP\AppFramework\OCSController;
  32. use OCP\IRequest;
  33. use OCP\UserStatus\IUserStatus;
  34. class StatusesController extends OCSController {
  35. /** @var StatusService */
  36. private $service;
  37. /**
  38. * StatusesController constructor.
  39. *
  40. * @param string $appName
  41. * @param IRequest $request
  42. * @param StatusService $service
  43. */
  44. public function __construct(string $appName,
  45. IRequest $request,
  46. StatusService $service) {
  47. parent::__construct($appName, $request);
  48. $this->service = $service;
  49. }
  50. /**
  51. * @NoAdminRequired
  52. *
  53. * @param int|null $limit
  54. * @param int|null $offset
  55. * @return DataResponse
  56. */
  57. public function findAll(?int $limit = null, ?int $offset = null): DataResponse {
  58. $allStatuses = $this->service->findAll($limit, $offset);
  59. return new DataResponse(array_map(function ($userStatus) {
  60. return $this->formatStatus($userStatus);
  61. }, $allStatuses));
  62. }
  63. /**
  64. * @NoAdminRequired
  65. *
  66. * @param string $userId
  67. * @return DataResponse
  68. * @throws OCSNotFoundException
  69. */
  70. public function find(string $userId): DataResponse {
  71. try {
  72. $userStatus = $this->service->findByUserId($userId);
  73. } catch (DoesNotExistException $ex) {
  74. throw new OCSNotFoundException('No status for the requested userId');
  75. }
  76. return new DataResponse($this->formatStatus($userStatus));
  77. }
  78. /**
  79. * @NoAdminRequired
  80. *
  81. * @param UserStatus $status
  82. * @return array
  83. */
  84. private function formatStatus(UserStatus $status): array {
  85. $visibleStatus = $status->getStatus();
  86. if ($visibleStatus === IUserStatus::INVISIBLE) {
  87. $visibleStatus = IUserStatus::OFFLINE;
  88. }
  89. return [
  90. 'userId' => $status->getUserId(),
  91. 'message' => $status->getCustomMessage(),
  92. 'icon' => $status->getCustomIcon(),
  93. 'clearAt' => $status->getClearAt(),
  94. 'status' => $visibleStatus,
  95. ];
  96. }
  97. }