Browse Source

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>
pull/31850/head
Carl Schwan 4 years ago
parent
commit
1dbe7dafe2
  1. 13
      apps/user_status/lib/Connector/UserStatus.php
  2. 20
      apps/user_status/lib/Controller/HeartbeatController.php
  3. 17
      apps/user_status/lib/Listener/UserLiveStatusListener.php
  4. 31
      lib/public/User/Events/UserLiveStatusEvent.php

13
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;
}
}

20
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 {

17
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));
}
}

31
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;
}
}
Loading…
Cancel
Save