HeartbeatController.php 3.6 KB

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