messages implemented
This commit is contained in:
49
app/Observers/ArtworkReactionObserver.php
Normal file
49
app/Observers/ArtworkReactionObserver.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\ArtworkReaction;
|
||||
use App\Services\UserStatsService;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Updates the artwork creator's reactions_received_count when
|
||||
* a reaction is added or removed.
|
||||
*/
|
||||
class ArtworkReactionObserver
|
||||
{
|
||||
public function __construct(
|
||||
private readonly UserStatsService $userStats,
|
||||
) {}
|
||||
|
||||
public function created(ArtworkReaction $reaction): void
|
||||
{
|
||||
$creatorId = $this->creatorId($reaction->artwork_id);
|
||||
if ($creatorId) {
|
||||
$this->userStats->incrementReactionsReceived($creatorId);
|
||||
}
|
||||
|
||||
// The reactor is "active"
|
||||
$this->userStats->ensureRow($reaction->user_id);
|
||||
$this->userStats->setLastActiveAt($reaction->user_id);
|
||||
}
|
||||
|
||||
public function deleted(ArtworkReaction $reaction): void
|
||||
{
|
||||
$creatorId = $this->creatorId($reaction->artwork_id);
|
||||
if ($creatorId) {
|
||||
$this->userStats->decrementReactionsReceived($creatorId);
|
||||
}
|
||||
}
|
||||
|
||||
private function creatorId(int $artworkId): ?int
|
||||
{
|
||||
$id = DB::table('artworks')
|
||||
->where('id', $artworkId)
|
||||
->value('user_id');
|
||||
|
||||
return $id !== null ? (int) $id : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user