From 1dbe7dafe26f4a11af610f3ce0dea59c31227d78 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Tue, 5 Apr 2022 20:21:02 +0200 Subject: Fetch status in heartbeat controller only once Store the user status inside the event instead of fetching it again Signed-off-by: Carl Schwan --- apps/user_status/lib/Connector/UserStatus.php | 13 +++++---- .../lib/Controller/HeartbeatController.php | 20 +++++++------- .../lib/Listener/UserLiveStatusListener.php | 17 ++++-------- lib/public/User/Events/UserLiveStatusEvent.php | 31 ++++++++++++++-------- 4 files changed, 43 insertions(+), 38 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)); } } diff --git a/lib/public/User/Events/UserLiveStatusEvent.php b/lib/public/User/Events/UserLiveStatusEvent.php index dd90400cb3b..4bba71f95b9 100644 --- a/lib/public/User/Events/UserLiveStatusEvent.php +++ b/lib/public/User/Events/UserLiveStatusEvent.php @@ -27,6 +27,7 @@ namespace OCP\User\Events; use OCP\EventDispatcher\Event; use OCP\IUser; +use OCP\UserStatus\IUserStatus; /** * @since 20.0.0 @@ -51,19 +52,12 @@ class UserLiveStatusEvent extends Event { */ public const STATUS_OFFLINE = 'offline'; - /** @var IUser */ - private $user; - - /** @var string */ - private $status; - - /** @var int */ - private $timestamp; + private IUser $user; + private string $status; + private int $timestamp; + private ?IUserStatus $userStatus = null; /** - * @param IUser $user - * @param string $status - * @param int $timestamp * @since 20.0.0 */ public function __construct(IUser $user, @@ -98,4 +92,19 @@ class UserLiveStatusEvent extends Event { public function getTimestamp(): int { return $this->timestamp; } + + /** + * Get the user status that might be available after processing the event + * @since 24.0.0 + */ + public function getUserStatus(): ?IUserStatus { + return $this->userStatus; + } + + /** + * @since 24.0.0 + */ + public function setUserStatus(IUserStatus $userStatus) { + $this->userStatus = $userStatus; + } } -- cgit v1.2.3