HeartbeatController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\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. class HeartbeatController extends OCSController {
  39. /** @var IEventDispatcher */
  40. private $eventDispatcher;
  41. /** @var IUserSession */
  42. private $userSession;
  43. /** @var ITimeFactory */
  44. private $timeFactory;
  45. /** @var StatusService */
  46. private $service;
  47. public function __construct(string $appName,
  48. IRequest $request,
  49. IEventDispatcher $eventDispatcher,
  50. IUserSession $userSession,
  51. ITimeFactory $timeFactory,
  52. StatusService $service) {
  53. parent::__construct($appName, $request);
  54. $this->eventDispatcher = $eventDispatcher;
  55. $this->userSession = $userSession;
  56. $this->timeFactory = $timeFactory;
  57. $this->service = $service;
  58. }
  59. /**
  60. * @NoAdminRequired
  61. *
  62. * @param string $status
  63. * @return JSONResponse
  64. */
  65. public function heartbeat(string $status): JSONResponse {
  66. if (!\in_array($status, [IUserStatus::ONLINE, IUserStatus::AWAY], true)) {
  67. return new JSONResponse([], Http::STATUS_BAD_REQUEST);
  68. }
  69. $user = $this->userSession->getUser();
  70. if ($user === null) {
  71. return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
  72. }
  73. $event = new UserLiveStatusEvent(
  74. $user,
  75. $status,
  76. $this->timeFactory->getTime()
  77. );
  78. $this->eventDispatcher->dispatchTyped($event);
  79. $userStatus = $event->getUserStatus();
  80. if (!$userStatus) {
  81. return new JSONResponse([], Http::STATUS_NO_CONTENT);
  82. }
  83. /** @psalm-suppress UndefinedInterfaceMethod */
  84. return new JSONResponse($this->formatStatus($userStatus->getInternal()));
  85. }
  86. private function formatStatus(UserStatus $status): array {
  87. return [
  88. 'userId' => $status->getUserId(),
  89. 'message' => $status->getCustomMessage(),
  90. 'messageId' => $status->getMessageId(),
  91. 'messageIsPredefined' => $status->getMessageId() !== null,
  92. 'icon' => $status->getCustomIcon(),
  93. 'clearAt' => $status->getClearAt(),
  94. 'status' => $status->getStatus(),
  95. 'statusIsUserDefined' => $status->getIsUserDefined(),
  96. ];
  97. }
  98. }