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 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\UserStatus\Controller;
  24. use OCP\AppFramework\Controller;
  25. use OCP\AppFramework\Http;
  26. use OCP\AppFramework\Http\JSONResponse;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\EventDispatcher\IEventDispatcher;
  29. use OCP\IRequest;
  30. use OCP\IUserSession;
  31. use OCP\User\Events\UserLiveStatusEvent;
  32. class HeartbeatController extends Controller {
  33. /** @var IEventDispatcher */
  34. private $eventDispatcher;
  35. /** @var IUserSession */
  36. private $userSession;
  37. /** @var ITimeFactory */
  38. private $timeFactory;
  39. /**
  40. * HeartbeatController constructor.
  41. *
  42. * @param string $appName
  43. * @param IRequest $request
  44. * @param IEventDispatcher $eventDispatcher
  45. */
  46. public function __construct(string $appName,
  47. IRequest $request,
  48. IEventDispatcher $eventDispatcher,
  49. IUserSession $userSession,
  50. ITimeFactory $timeFactory) {
  51. parent::__construct($appName, $request);
  52. $this->eventDispatcher = $eventDispatcher;
  53. $this->userSession = $userSession;
  54. $this->timeFactory = $timeFactory;
  55. }
  56. /**
  57. * @NoAdminRequired
  58. *
  59. * @param string $status
  60. * @return JSONResponse
  61. */
  62. public function heartbeat(string $status): JSONResponse {
  63. if (!\in_array($status, ['online', 'away'])) {
  64. return new JSONResponse([], Http::STATUS_BAD_REQUEST);
  65. }
  66. $user = $this->userSession->getUser();
  67. if ($user === null) {
  68. return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
  69. }
  70. $this->eventDispatcher->dispatchTyped(
  71. new UserLiveStatusEvent(
  72. $user,
  73. $status,
  74. $this->timeFactory->getTime()
  75. )
  76. );
  77. return new JSONResponse([], Http::STATUS_NO_CONTENT);
  78. }
  79. }