HeartbeatController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\UserStatus\Controller;
  25. use OCA\UserStatus\Db\UserStatus;
  26. use OCA\UserStatus\Service\StatusService;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Db\DoesNotExistException;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\JSONResponse;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\EventDispatcher\IEventDispatcher;
  33. use OCP\IRequest;
  34. use OCP\IUserSession;
  35. use OCP\User\Events\UserLiveStatusEvent;
  36. use OCP\UserStatus\IUserStatus;
  37. class HeartbeatController extends Controller {
  38. /** @var IEventDispatcher */
  39. private $eventDispatcher;
  40. /** @var IUserSession */
  41. private $userSession;
  42. /** @var ITimeFactory */
  43. private $timeFactory;
  44. /** @var StatusService */
  45. private $service;
  46. public function __construct(string $appName,
  47. IRequest $request,
  48. IEventDispatcher $eventDispatcher,
  49. IUserSession $userSession,
  50. ITimeFactory $timeFactory,
  51. StatusService $service) {
  52. parent::__construct($appName, $request);
  53. $this->eventDispatcher = $eventDispatcher;
  54. $this->userSession = $userSession;
  55. $this->timeFactory = $timeFactory;
  56. $this->service = $service;
  57. }
  58. /**
  59. * @NoAdminRequired
  60. *
  61. * @param string $status
  62. * @return JSONResponse
  63. */
  64. public function heartbeat(string $status): JSONResponse {
  65. if (!\in_array($status, [IUserStatus::ONLINE, IUserStatus::AWAY], true)) {
  66. return new JSONResponse([], Http::STATUS_BAD_REQUEST);
  67. }
  68. $user = $this->userSession->getUser();
  69. if ($user === null) {
  70. return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
  71. }
  72. $event = new UserLiveStatusEvent(
  73. $user,
  74. $status,
  75. $this->timeFactory->getTime()
  76. );
  77. $this->eventDispatcher->dispatchTyped($event);
  78. $userStatus = $event->getUserStatus();
  79. if (!$userStatus) {
  80. return new JSONResponse([], Http::STATUS_NO_CONTENT);
  81. }
  82. /** @psalm-suppress UndefinedInterfaceMethod */
  83. return new JSONResponse($this->formatStatus($userStatus->getInternal()));
  84. }
  85. private function formatStatus(UserStatus $status): array {
  86. return [
  87. 'userId' => $status->getUserId(),
  88. 'message' => $status->getCustomMessage(),
  89. 'messageId' => $status->getMessageId(),
  90. 'messageIsPredefined' => $status->getMessageId() !== null,
  91. 'icon' => $status->getCustomIcon(),
  92. 'clearAt' => $status->getClearAt(),
  93. 'status' => $status->getStatus(),
  94. 'statusIsUserDefined' => $status->getIsUserDefined(),
  95. ];
  96. }
  97. }