Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Event/MessageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ public function process(): void
return;
}

// Capture potato left after validation to avoid re-querying in notification
$potatoLeftAfterValidation = $fromUser->potatoLeftToday();

$toUsers = [];
foreach ($this->receivers as $receiver) {
$toUser = $userService->getOrCreateUser($receiver);
Expand All @@ -94,6 +97,7 @@ public function process(): void
fromUser: $fromUser,
toUsers: $toUsers,
event: $this,
potatoLeftToday: $potatoLeftAfterValidation,
);

$stored = $quickWinService->store(
Expand Down
34 changes: 18 additions & 16 deletions src/Model/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class User extends Entity
'*' => false,
];

/**
* Cache for potatoSentToday() to avoid duplicate queries
*
* @var int|null
*/
protected ?int $_potatoSentTodayCache = null;

public const STATUS_ACTIVE = 'active';
public const STATUS_DELETED = 'deleted';

Expand Down Expand Up @@ -121,6 +128,11 @@ public function potatoReceived(): int
*/
public function potatoSentToday(): int
{
// Return cached value if already computed
if ($this->_potatoSentTodayCache !== null) {
return $this->_potatoSentTodayCache;
}

$messagesTable = $this->fetchTable('Messages');

$query = $messagesTable->find();
Expand All @@ -135,7 +147,10 @@ public function potatoSentToday(): int
])
->first();

return (int)$result->sent;
// Cache the result for subsequent calls
$this->_potatoSentTodayCache = (int)$result->sent;

return $this->_potatoSentTodayCache;
}

/**
Expand Down Expand Up @@ -165,21 +180,8 @@ public function potatoReceivedToday(): int
*/
public function potatoLeftToday(): int
{
$messagesTable = $this->fetchTable('Messages');

$query = $messagesTable->find();
$result = $query
->select([
'sent' => $query->func()->sum('amount'),
])
->where([
'sender_user_id' => $this->id,
'type' => Message::TYPE_POTATO,
'created >=' => $this->getStartOfDay(),
])
->first();

return Message::MAX_AMOUNT - (int)$result->sent;
// Reuse the cached result from potatoSentToday() to avoid duplicate query
return Message::MAX_AMOUNT - $this->potatoSentToday();
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Service/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ public function __construct()
* @param \App\Model\Entity\User $fromUser User who did gib the potato
* @param array<\App\Model\Entity\User> $toUsers Users who will receive the potato
* @param \App\Event\MessageEvent|\App\Event\ReactionAddedEvent $event The event.
* @param int|null $potatoLeftToday Optional pre-computed potato left today to avoid re-querying
* @return void
*/
public function notifyUsers(
User $fromUser,
array $toUsers,
MessageEvent|ReactionAddedEvent $event,
?int $potatoLeftToday = null,
): void {
$toUserNames = [];
foreach ($toUsers as $toUser) {
Expand All @@ -58,7 +60,8 @@ public function notifyUsers(
}

if ($fromUser->notifications['sent'] === true) {
$potatoLeftToday = $fromUser->potatoLeftToday();
// Use pre-computed value if provided, otherwise query
$potatoLeft = $potatoLeftToday ?? $fromUser->potatoLeftToday();

$gibMessage = sprintf(
'You did gib *%s* %s to %s.',
Expand All @@ -69,7 +72,7 @@ public function notifyUsers(
$gibMessage .= PHP_EOL;
$gibMessage .= sprintf(
'You have *%s* :potato: left. Your potato do reset in *%s hours* and *%s minutes*.',
$potatoLeftToday,
$potatoLeft,
$fromUser->potatoResetInHours(),
$fromUser->potatoResetInMinutes(),
);
Expand Down
Loading