You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HeartbeatController.php 3.2KB

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. $this->eventDispatcher->dispatchTyped(
  73. new UserLiveStatusEvent(
  74. $user,
  75. $status,
  76. $this->timeFactory->getTime()
  77. )
  78. );
  79. try {
  80. $userStatus = $this->service->findByUserId($user->getUID());
  81. } catch (DoesNotExistException $ex) {
  82. return new JSONResponse([], Http::STATUS_NO_CONTENT);
  83. }
  84. return new JSONResponse($this->formatStatus($userStatus));
  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. }