Skip to content

Commit 0f924e0

Browse files
authored
Merge pull request #31850 from nextcloud/performance/heartbeat-fetch-status-once
Fetch status in heartbeat controller only once
2 parents d7e0948 + 1dbe7da commit 0f924e0

File tree

4 files changed

+43
-38
lines changed

4 files changed

+43
-38
lines changed

apps/user_status/lib/Connector/UserStatus.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ class UserStatus implements IUserStatus {
4646
/** @var DateTimeImmutable|null */
4747
private $clearAt;
4848

49-
/**
50-
* UserStatus constructor.
51-
*
52-
* @param Db\UserStatus $status
53-
*/
49+
/** @var Db\UserStatus */
50+
private $internalStatus;
51+
5452
public function __construct(Db\UserStatus $status) {
53+
$this->internalStatus = $status;
5554
$this->userId = $status->getUserId();
5655
$this->status = $status->getStatus();
5756
$this->message = $status->getCustomMessage();
@@ -99,4 +98,8 @@ public function getIcon(): ?string {
9998
public function getClearAt(): ?DateTimeImmutable {
10099
return $this->clearAt;
101100
}
101+
102+
public function getInternal(): Db\UserStatus {
103+
return $this->internalStatus;
104+
}
102105
}

apps/user_status/lib/Controller/HeartbeatController.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,21 @@ public function heartbeat(string $status): JSONResponse {
8181
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
8282
}
8383

84-
$this->eventDispatcher->dispatchTyped(
85-
new UserLiveStatusEvent(
86-
$user,
87-
$status,
88-
$this->timeFactory->getTime()
89-
)
84+
$event = new UserLiveStatusEvent(
85+
$user,
86+
$status,
87+
$this->timeFactory->getTime()
9088
);
9189

92-
try {
93-
$userStatus = $this->service->findByUserId($user->getUID());
94-
} catch (DoesNotExistException $ex) {
90+
$this->eventDispatcher->dispatchTyped($event);
91+
92+
$userStatus = $event->getUserStatus();
93+
if (!$userStatus) {
9594
return new JSONResponse([], Http::STATUS_NO_CONTENT);
9695
}
9796

98-
return new JSONResponse($this->formatStatus($userStatus));
97+
/** @psalm-suppress UndefinedInterfaceMethod */
98+
return new JSONResponse($this->formatStatus($userStatus->getInternal()));
9999
}
100100

101101
private function formatStatus(UserStatus $status): array {

apps/user_status/lib/Listener/UserLiveStatusListener.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
namespace OCA\UserStatus\Listener;
2727

2828
use OCA\UserStatus\Db\UserStatus;
29+
use OCA\UserStatus\Connector\UserStatus as ConnectorUserStatus;
2930
use OCA\UserStatus\Db\UserStatusMapper;
3031
use OCA\UserStatus\Service\StatusService;
3132
use OCP\AppFramework\Db\DoesNotExistException;
@@ -41,19 +42,9 @@
4142
* @package OCA\UserStatus\Listener
4243
*/
4344
class UserLiveStatusListener implements IEventListener {
45+
private UserStatusMapper $mapper;
46+
private ITimeFactory $timeFactory;
4447

45-
/** @var UserStatusMapper */
46-
private $mapper;
47-
48-
/** @var ITimeFactory */
49-
private $timeFactory;
50-
51-
/**
52-
* UserLiveStatusListener constructor.
53-
*
54-
* @param UserStatusMapper $mapper
55-
* @param ITimeFactory $timeFactory
56-
*/
5748
public function __construct(UserStatusMapper $mapper,
5849
ITimeFactory $timeFactory) {
5950
$this->mapper = $mapper;
@@ -112,5 +103,7 @@ public function handle(Event $event): void {
112103
$this->mapper->update($userStatus);
113104
}
114105
}
106+
107+
$event->setUserStatus(new ConnectorUserStatus($userStatus));
115108
}
116109
}

lib/public/User/Events/UserLiveStatusEvent.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
use OCP\EventDispatcher\Event;
2929
use OCP\IUser;
30+
use OCP\UserStatus\IUserStatus;
3031

3132
/**
3233
* @since 20.0.0
@@ -51,19 +52,12 @@ class UserLiveStatusEvent extends Event {
5152
*/
5253
public const STATUS_OFFLINE = 'offline';
5354

54-
/** @var IUser */
55-
private $user;
56-
57-
/** @var string */
58-
private $status;
59-
60-
/** @var int */
61-
private $timestamp;
55+
private IUser $user;
56+
private string $status;
57+
private int $timestamp;
58+
private ?IUserStatus $userStatus = null;
6259

6360
/**
64-
* @param IUser $user
65-
* @param string $status
66-
* @param int $timestamp
6761
* @since 20.0.0
6862
*/
6963
public function __construct(IUser $user,
@@ -98,4 +92,19 @@ public function getStatus(): string {
9892
public function getTimestamp(): int {
9993
return $this->timestamp;
10094
}
95+
96+
/**
97+
* Get the user status that might be available after processing the event
98+
* @since 24.0.0
99+
*/
100+
public function getUserStatus(): ?IUserStatus {
101+
return $this->userStatus;
102+
}
103+
104+
/**
105+
* @since 24.0.0
106+
*/
107+
public function setUserStatus(IUserStatus $userStatus) {
108+
$this->userStatus = $userStatus;
109+
}
101110
}

0 commit comments

Comments
 (0)