1
0

HeartbeatController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UserStatus\Controller;
  8. use OCA\UserStatus\Db\UserStatus;
  9. use OCA\UserStatus\ResponseDefinitions;
  10. use OCA\UserStatus\Service\StatusService;
  11. use OCP\AppFramework\Http;
  12. use OCP\AppFramework\Http\Attribute\ApiRoute;
  13. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\AppFramework\OCSController;
  16. use OCP\AppFramework\Utility\ITimeFactory;
  17. use OCP\EventDispatcher\IEventDispatcher;
  18. use OCP\IRequest;
  19. use OCP\IUserSession;
  20. use OCP\User\Events\UserLiveStatusEvent;
  21. use OCP\UserStatus\IUserStatus;
  22. /**
  23. * @psalm-import-type UserStatusPrivate from ResponseDefinitions
  24. */
  25. class HeartbeatController extends OCSController {
  26. /** @var IEventDispatcher */
  27. private $eventDispatcher;
  28. /** @var IUserSession */
  29. private $userSession;
  30. /** @var ITimeFactory */
  31. private $timeFactory;
  32. /** @var StatusService */
  33. private $service;
  34. public function __construct(string $appName,
  35. IRequest $request,
  36. IEventDispatcher $eventDispatcher,
  37. IUserSession $userSession,
  38. ITimeFactory $timeFactory,
  39. StatusService $service) {
  40. parent::__construct($appName, $request);
  41. $this->eventDispatcher = $eventDispatcher;
  42. $this->userSession = $userSession;
  43. $this->timeFactory = $timeFactory;
  44. $this->service = $service;
  45. }
  46. /**
  47. * Keep the status alive
  48. *
  49. * @param string $status Only online, away
  50. *
  51. * @return DataResponse<Http::STATUS_OK, UserStatusPrivate, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NO_CONTENT, array<empty>, array{}>
  52. * 200: Status successfully updated
  53. * 204: User has no status to keep alive
  54. * 400: Invalid status to update
  55. */
  56. #[NoAdminRequired]
  57. #[ApiRoute(verb: 'PUT', url: '/api/v1/heartbeat')]
  58. public function heartbeat(string $status): DataResponse {
  59. if (!\in_array($status, [IUserStatus::ONLINE, IUserStatus::AWAY], true)) {
  60. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  61. }
  62. $user = $this->userSession->getUser();
  63. if ($user === null) {
  64. return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
  65. }
  66. $event = new UserLiveStatusEvent(
  67. $user,
  68. $status,
  69. $this->timeFactory->getTime()
  70. );
  71. $this->eventDispatcher->dispatchTyped($event);
  72. $userStatus = $event->getUserStatus();
  73. if (!$userStatus) {
  74. return new DataResponse([], Http::STATUS_NO_CONTENT);
  75. }
  76. /** @psalm-suppress UndefinedInterfaceMethod */
  77. return new DataResponse($this->formatStatus($userStatus->getInternal()));
  78. }
  79. private function formatStatus(UserStatus $status): array {
  80. return [
  81. 'userId' => $status->getUserId(),
  82. 'message' => $status->getCustomMessage(),
  83. 'messageId' => $status->getMessageId(),
  84. 'messageIsPredefined' => $status->getMessageId() !== null,
  85. 'icon' => $status->getCustomIcon(),
  86. 'clearAt' => $status->getClearAt(),
  87. 'status' => $status->getStatus(),
  88. 'statusIsUserDefined' => $status->getIsUserDefined(),
  89. ];
  90. }
  91. }