HeartbeatController.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. public function __construct(
  27. string $appName,
  28. IRequest $request,
  29. private IEventDispatcher $eventDispatcher,
  30. private IUserSession $userSession,
  31. private ITimeFactory $timeFactory,
  32. private StatusService $service,
  33. ) {
  34. parent::__construct($appName, $request);
  35. }
  36. /**
  37. * Keep the status alive
  38. *
  39. * @param string $status Only online, away
  40. *
  41. * @return DataResponse<Http::STATUS_OK, UserStatusPrivate, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NO_CONTENT, list<empty>, array{}>
  42. *
  43. * 200: Status successfully updated
  44. * 204: User has no status to keep alive
  45. * 400: Invalid status to update
  46. */
  47. #[NoAdminRequired]
  48. #[ApiRoute(verb: 'PUT', url: '/api/v1/heartbeat')]
  49. public function heartbeat(string $status): DataResponse {
  50. if (!\in_array($status, [IUserStatus::ONLINE, IUserStatus::AWAY], true)) {
  51. return new DataResponse([], Http::STATUS_BAD_REQUEST);
  52. }
  53. $user = $this->userSession->getUser();
  54. if ($user === null) {
  55. return new DataResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
  56. }
  57. $event = new UserLiveStatusEvent(
  58. $user,
  59. $status,
  60. $this->timeFactory->getTime()
  61. );
  62. $this->eventDispatcher->dispatchTyped($event);
  63. $userStatus = $event->getUserStatus();
  64. if (!$userStatus) {
  65. return new DataResponse([], Http::STATUS_NO_CONTENT);
  66. }
  67. /** @psalm-suppress UndefinedInterfaceMethod */
  68. return new DataResponse($this->formatStatus($userStatus->getInternal()));
  69. }
  70. private function formatStatus(UserStatus $status): array {
  71. return [
  72. 'userId' => $status->getUserId(),
  73. 'message' => $status->getCustomMessage(),
  74. 'messageId' => $status->getMessageId(),
  75. 'messageIsPredefined' => $status->getMessageId() !== null,
  76. 'icon' => $status->getCustomIcon(),
  77. 'clearAt' => $status->getClearAt(),
  78. 'status' => $status->getStatus(),
  79. 'statusIsUserDefined' => $status->getIsUserDefined(),
  80. ];
  81. }
  82. }