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.7KB

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