]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fetch status in heartbeat controller only once 31850/head
authorCarl Schwan <carl@carlschwan.eu>
Tue, 5 Apr 2022 18:21:02 +0000 (20:21 +0200)
committerCarl Schwan <carl@carlschwan.eu>
Thu, 7 Apr 2022 13:16:34 +0000 (15:16 +0200)
Store the user status inside the event instead of fetching it again

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
apps/user_status/lib/Connector/UserStatus.php
apps/user_status/lib/Controller/HeartbeatController.php
apps/user_status/lib/Listener/UserLiveStatusListener.php
lib/public/User/Events/UserLiveStatusEvent.php

index 26dc2be7e937295f06372d5c75d7c68188b6c154..ff05ded9e2bb5778a0408f37226866d14c7bb435 100644 (file)
@@ -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;
+       }
 }
index 223ff4a0f451ba333607f3d2b45fafb80f48f618..c11a63b4420daf107a87769ec2248c26d97fb753 100644 (file)
@@ -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 {
index 60b5fb7f3a46505751f3602603399d691467e0ea..c015e6841420ac708c414fbfe94a639c609b01d1 100644 (file)
@@ -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));
        }
 }
index dd90400cb3b42b4b77ee3e607b6ffd2a61f511cb..4bba71f95b994cc24b7263a45a6b47f72d74ac83 100644 (file)
@@ -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;
+       }
 }