diff options
author | Carl Schwan <carl@carlschwan.eu> | 2022-04-05 21:21:02 +0300 |
---|---|---|
committer | Carl Schwan <carl@carlschwan.eu> | 2022-04-07 16:16:34 +0300 |
commit | 1dbe7dafe26f4a11af610f3ce0dea59c31227d78 (patch) | |
tree | d3d23bb86d3b4434c19dd925a97a53db704c8871 /apps | |
parent | 9c84aa5870204a871024ca18b4994ed40defdd9b (diff) |
Fetch status in heartbeat controller only once
Store the user status inside the event instead of fetching it again
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
Diffstat (limited to 'apps')
-rw-r--r-- | apps/user_status/lib/Connector/UserStatus.php | 13 | ||||
-rw-r--r-- | apps/user_status/lib/Controller/HeartbeatController.php | 20 | ||||
-rw-r--r-- | apps/user_status/lib/Listener/UserLiveStatusListener.php | 17 |
3 files changed, 23 insertions, 27 deletions
diff --git a/apps/user_status/lib/Connector/UserStatus.php b/apps/user_status/lib/Connector/UserStatus.php index 26dc2be7e93..ff05ded9e2b 100644 --- a/apps/user_status/lib/Connector/UserStatus.php +++ b/apps/user_status/lib/Connector/UserStatus.php @@ -46,12 +46,11 @@ class UserStatus implements IUserStatus { /** @var DateTimeImmutable|null */ private $clearAt; - /** - * UserStatus constructor. - * - * @param Db\UserStatus $status - */ + /** @var Db\UserStatus */ + private $internalStatus; + public function __construct(Db\UserStatus $status) { + $this->internalStatus = $status; $this->userId = $status->getUserId(); $this->status = $status->getStatus(); $this->message = $status->getCustomMessage(); @@ -99,4 +98,8 @@ class UserStatus implements IUserStatus { public function getClearAt(): ?DateTimeImmutable { return $this->clearAt; } + + public function getInternal(): Db\UserStatus { + return $this->internalStatus; + } } diff --git a/apps/user_status/lib/Controller/HeartbeatController.php b/apps/user_status/lib/Controller/HeartbeatController.php index 223ff4a0f45..c11a63b4420 100644 --- a/apps/user_status/lib/Controller/HeartbeatController.php +++ b/apps/user_status/lib/Controller/HeartbeatController.php @@ -81,21 +81,21 @@ class HeartbeatController extends Controller { return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); } - $this->eventDispatcher->dispatchTyped( - new UserLiveStatusEvent( - $user, - $status, - $this->timeFactory->getTime() - ) + $event = new UserLiveStatusEvent( + $user, + $status, + $this->timeFactory->getTime() ); - try { - $userStatus = $this->service->findByUserId($user->getUID()); - } catch (DoesNotExistException $ex) { + $this->eventDispatcher->dispatchTyped($event); + + $userStatus = $event->getUserStatus(); + if (!$userStatus) { return new JSONResponse([], Http::STATUS_NO_CONTENT); } - return new JSONResponse($this->formatStatus($userStatus)); + /** @psalm-suppress UndefinedInterfaceMethod */ + return new JSONResponse($this->formatStatus($userStatus->getInternal())); } private function formatStatus(UserStatus $status): array { diff --git a/apps/user_status/lib/Listener/UserLiveStatusListener.php b/apps/user_status/lib/Listener/UserLiveStatusListener.php index 60b5fb7f3a4..c015e684142 100644 --- a/apps/user_status/lib/Listener/UserLiveStatusListener.php +++ b/apps/user_status/lib/Listener/UserLiveStatusListener.php @@ -26,6 +26,7 @@ declare(strict_types=1); namespace OCA\UserStatus\Listener; use OCA\UserStatus\Db\UserStatus; +use OCA\UserStatus\Connector\UserStatus as ConnectorUserStatus; use OCA\UserStatus\Db\UserStatusMapper; use OCA\UserStatus\Service\StatusService; use OCP\AppFramework\Db\DoesNotExistException; @@ -41,19 +42,9 @@ use OCP\UserStatus\IUserStatus; * @package OCA\UserStatus\Listener */ class UserLiveStatusListener implements IEventListener { + private UserStatusMapper $mapper; + private ITimeFactory $timeFactory; - /** @var UserStatusMapper */ - private $mapper; - - /** @var ITimeFactory */ - private $timeFactory; - - /** - * UserLiveStatusListener constructor. - * - * @param UserStatusMapper $mapper - * @param ITimeFactory $timeFactory - */ public function __construct(UserStatusMapper $mapper, ITimeFactory $timeFactory) { $this->mapper = $mapper; @@ -112,5 +103,7 @@ class UserLiveStatusListener implements IEventListener { $this->mapper->update($userStatus); } } + + $event->setUserStatus(new ConnectorUserStatus($userStatus)); } } |