messages implemented

This commit is contained in:
2026-02-26 21:12:32 +01:00
parent d0aefc5ddc
commit 15b7b77d20
168 changed files with 14728 additions and 6786 deletions

View File

@@ -0,0 +1,63 @@
<?php
declare(strict_types=1);
namespace App\Observers;
use App\Models\ArtworkComment;
use App\Services\UserStatsService;
use Illuminate\Support\Facades\DB;
/**
* Updates the artwork creator's comments_received_count and last_active_at
* when a comment is created or (soft-)deleted.
*/
class ArtworkCommentObserver
{
public function __construct(
private readonly UserStatsService $userStats,
) {}
public function created(ArtworkComment $comment): void
{
$creatorId = $this->creatorId($comment->artwork_id);
if ($creatorId) {
$this->userStats->incrementCommentsReceived($creatorId);
}
// The commenter is "active"
$this->userStats->ensureRow($comment->user_id);
$this->userStats->setLastActiveAt($comment->user_id);
}
/** Soft delete. */
public function deleted(ArtworkComment $comment): void
{
$creatorId = $this->creatorId($comment->artwork_id);
if ($creatorId) {
$this->userStats->decrementCommentsReceived($creatorId);
}
}
/** Hard delete after soft delete — already decremented; nothing to do. */
public function forceDeleted(ArtworkComment $comment): void
{
// Only decrement if the comment was NOT already soft-deleted
// (to avoid double-decrement).
if ($comment->deleted_at === null) {
$creatorId = $this->creatorId($comment->artwork_id);
if ($creatorId) {
$this->userStats->decrementCommentsReceived($creatorId);
}
}
}
private function creatorId(int $artworkId): ?int
{
$id = DB::table('artworks')
->where('id', $artworkId)
->value('user_id');
return $id !== null ? (int) $id : null;
}
}