HeartbeatController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. *
  53. * 200: Status successfully updated
  54. * 204: User has no status to keep alive
  55. * 400: Invalid status to update
  56. */
  57. #[NoAdminRequired]
  58. #[ApiRoute(verb: 'PUT', url: '/api/v1/heartbeat')]
  59. public function heartbeat(string $status): DataResponse {
  60. if (!\in_array($status, [IUserStatus::ONLINE, IUserStatus::AWAY], true)) {
  61. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  62. }
  63. $user = $this->userSession->getUser();
  64. if ($user === null) {
  65. return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
  66. }
  67. $event = new UserLiveStatusEvent(
  68. $user,
  69. $status,
  70. $this->timeFactory->getTime()
  71. );
  72. $this->eventDispatcher->dispatchTyped($event);
  73. $userStatus = $event->getUserStatus();
  74. if (!$userStatus) {
  75. return new DataResponse([], Http::STATUS_NO_CONTENT);
  76. }
  77. /** @psalm-suppress UndefinedInterfaceMethod */
  78. return new DataResponse($this->formatStatus($userStatus->getInternal()));
  79. }
  80. private function formatStatus(UserStatus $status): array {
  81. return [
  82. 'userId' => $status->getUserId(),
  83. 'message' => $status->getCustomMessage(),
  84. 'messageId' => $status->getMessageId(),
  85. 'messageIsPredefined' => $status->getMessageId() !== null,
  86. 'icon' => $status->getCustomIcon(),
  87. 'clearAt' => $status->getClearAt(),
  88. 'status' => $status->getStatus(),
  89. 'statusIsUserDefined' => $status->getIsUserDefined(),
  90. ];
  91. }
  92. }