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\Controller;
  30. use OCP\AppFramework\Db\DoesNotExistException;
  31. use OCP\AppFramework\Http;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\AppFramework\OCSController;
  34. use OCP\AppFramework\Utility\ITimeFactory;
  35. use OCP\EventDispatcher\IEventDispatcher;
  36. use OCP\IRequest;
  37. use OCP\IUserSession;
  38. use OCP\User\Events\UserLiveStatusEvent;
  39. use OCP\UserStatus\IUserStatus;
  40. /**
  41. * @psalm-import-type UserStatusPrivate from ResponseDefinitions
  42. */
  43. class HeartbeatController extends OCSController {
  44. /** @var IEventDispatcher */
  45. private $eventDispatcher;
  46. /** @var IUserSession */
  47. private $userSession;
  48. /** @var ITimeFactory */
  49. private $timeFactory;
  50. /** @var StatusService */
  51. private $service;
  52. public function __construct(string $appName,
  53. IRequest $request,
  54. IEventDispatcher $eventDispatcher,
  55. IUserSession $userSession,
  56. ITimeFactory $timeFactory,
  57. StatusService $service) {
  58. parent::__construct($appName, $request);
  59. $this->eventDispatcher = $eventDispatcher;
  60. $this->userSession = $userSession;
  61. $this->timeFactory = $timeFactory;
  62. $this->service = $service;
  63. }
  64. /**
  65. * Keep the status alive
  66. *
  67. * @NoAdminRequired
  68. *
  69. * @param string $status Only online, away
  70. *
  71. * @return DataResponse<Http::STATUS_OK, UserStatusPrivate, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NO_CONTENT, array<empty>, array{}>
  72. * 200: Status successfully updated
  73. * 204: User has no status to keep alive
  74. * 400: Invalid status to update
  75. */
  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. }