HeartbeatController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * @author Kate Döen <kate.doeen@nextcloud.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\ResponseDefinitions;
  28. use OCA\UserStatus\Service\StatusService;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\Attribute\ApiRoute;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\AppFramework\OCSController;
  33. use OCP\AppFramework\Utility\ITimeFactory;
  34. use OCP\EventDispatcher\IEventDispatcher;
  35. use OCP\IRequest;
  36. use OCP\IUserSession;
  37. use OCP\User\Events\UserLiveStatusEvent;
  38. use OCP\UserStatus\IUserStatus;
  39. /**
  40. * @psalm-import-type UserStatusPrivate from ResponseDefinitions
  41. */
  42. class HeartbeatController extends OCSController {
  43. /** @var IEventDispatcher */
  44. private $eventDispatcher;
  45. /** @var IUserSession */
  46. private $userSession;
  47. /** @var ITimeFactory */
  48. private $timeFactory;
  49. /** @var StatusService */
  50. private $service;
  51. public function __construct(string $appName,
  52. IRequest $request,
  53. IEventDispatcher $eventDispatcher,
  54. IUserSession $userSession,
  55. ITimeFactory $timeFactory,
  56. StatusService $service) {
  57. parent::__construct($appName, $request);
  58. $this->eventDispatcher = $eventDispatcher;
  59. $this->userSession = $userSession;
  60. $this->timeFactory = $timeFactory;
  61. $this->service = $service;
  62. }
  63. /**
  64. * Keep the status alive
  65. *
  66. * @NoAdminRequired
  67. *
  68. * @param string $status Only online, away
  69. *
  70. * @return DataResponse<Http::STATUS_OK, UserStatusPrivate, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NO_CONTENT, array<empty>, array{}>
  71. * 200: Status successfully updated
  72. * 204: User has no status to keep alive
  73. * 400: Invalid status to update
  74. */
  75. #[ApiRoute(verb: 'PUT', url: '/api/v1/heartbeat')]
  76. public function heartbeat(string $status): DataResponse {
  77. if (!\in_array($status, [IUserStatus::ONLINE, IUserStatus::AWAY], true)) {
  78. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  79. }
  80. $user = $this->userSession->getUser();
  81. if ($user === null) {
  82. return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
  83. }
  84. $event = new UserLiveStatusEvent(
  85. $user,
  86. $status,
  87. $this->timeFactory->getTime()
  88. );
  89. $this->eventDispatcher->dispatchTyped($event);
  90. $userStatus = $event->getUserStatus();
  91. if (!$userStatus) {
  92. return new DataResponse([], Http::STATUS_NO_CONTENT);
  93. }
  94. /** @psalm-suppress UndefinedInterfaceMethod */
  95. return new DataResponse($this->formatStatus($userStatus->getInternal()));
  96. }
  97. private function formatStatus(UserStatus $status): array {
  98. return [
  99. 'userId' => $status->getUserId(),
  100. 'message' => $status->getCustomMessage(),
  101. 'messageId' => $status->getMessageId(),
  102. 'messageIsPredefined' => $status->getMessageId() !== null,
  103. 'icon' => $status->getCustomIcon(),
  104. 'clearAt' => $status->getClearAt(),
  105. 'status' => $status->getStatus(),
  106. 'statusIsUserDefined' => $status->getIsUserDefined(),
  107. ];
  108. }
  109. }