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

@@ -6,26 +6,32 @@ namespace App\Observers;
use App\Models\ArtworkAward;
use App\Services\ArtworkAwardService;
use App\Services\UserStatsService;
use Illuminate\Support\Facades\DB;
class ArtworkAwardObserver
{
public function __construct(
private readonly ArtworkAwardService $service
private readonly ArtworkAwardService $service,
private readonly UserStatsService $userStats,
) {}
public function created(ArtworkAward $award): void
{
$this->refresh($award);
$this->trackCreatorStats($award, +1);
}
public function updated(ArtworkAward $award): void
{
$this->refresh($award);
// Medal changed count stays the same; no stat change needed.
}
public function deleted(ArtworkAward $award): void
{
$this->refresh($award);
$this->trackCreatorStats($award, -1);
}
private function refresh(ArtworkAward $award): void
@@ -37,4 +43,21 @@ class ArtworkAwardObserver
$this->service->syncToSearch($artwork);
}
}
private function trackCreatorStats(ArtworkAward $award, int $delta): void
{
$creatorId = DB::table('artworks')
->where('id', $award->artwork_id)
->value('user_id');
if (! $creatorId) {
return;
}
if ($delta > 0) {
$this->userStats->incrementAwardsReceived((int) $creatorId);
} else {
$this->userStats->decrementAwardsReceived((int) $creatorId);
}
}
}

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

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace App\Observers;
use App\Models\ArtworkFavourite;
use App\Services\UserStatsService;
use Illuminate\Support\Facades\DB;
/**
* Updates the artwork creator's favorites_received_count and last_active_at
* whenever a favourite is added or removed.
*/
class ArtworkFavouriteObserver
{
public function __construct(
private readonly UserStatsService $userStats,
) {}
public function created(ArtworkFavourite $favourite): void
{
$creatorId = $this->creatorId($favourite->artwork_id);
if ($creatorId) {
$this->userStats->incrementFavoritesReceived($creatorId);
}
}
public function deleted(ArtworkFavourite $favourite): void
{
$creatorId = $this->creatorId($favourite->artwork_id);
if ($creatorId) {
$this->userStats->decrementFavoritesReceived($creatorId);
}
}
private function creatorId(int $artworkId): ?int
{
$id = DB::table('artworks')
->where('id', $artworkId)
->value('user_id');
return $id !== null ? (int) $id : null;
}
}

View File

@@ -6,22 +6,27 @@ namespace App\Observers;
use App\Models\Artwork;
use App\Services\ArtworkSearchIndexer;
use App\Services\UserStatsService;
/**
* Syncs artwork documents to Meilisearch on every relevant model event.
* Also keeps user_statistics.uploads_count and last_upload_at in sync.
*
* All operations are dispatched to the queue no blocking calls.
*/
class ArtworkObserver
{
public function __construct(
private readonly ArtworkSearchIndexer $indexer
private readonly ArtworkSearchIndexer $indexer,
private readonly UserStatsService $userStats,
) {}
/** New artwork created — index once published and approved. */
/** New artwork created — index; bump uploadscount + last_upload_at. */
public function created(Artwork $artwork): void
{
$this->indexer->index($artwork);
$this->userStats->incrementUploads($artwork->user_id);
$this->userStats->setLastUploadAt($artwork->user_id, $artwork->created_at);
}
/** Artwork updated — covers publish, approval, metadata changes. */
@@ -36,21 +41,29 @@ class ArtworkObserver
$this->indexer->update($artwork);
}
/** Soft delete — remove from search. */
/** Soft delete — remove from search and decrement uploads_count. */
public function deleted(Artwork $artwork): void
{
$this->indexer->delete($artwork->id);
$this->userStats->decrementUploads($artwork->user_id);
}
/** Force delete — ensure removal from index. */
/** Force delete — ensure removal from index; only decrement if NOT already soft-deleted. */
public function forceDeleted(Artwork $artwork): void
{
$this->indexer->delete($artwork->id);
// If deleted_at was null the artwork was not soft-deleted before;
// the deleted() event did NOT fire, so we decrement here.
if ($artwork->deleted_at === null) {
$this->userStats->decrementUploads($artwork->user_id);
}
}
/** Restored from soft-delete — re-index. */
/** Restored from soft-delete — re-index and re-increment uploads_count. */
public function restored(Artwork $artwork): void
{
$this->indexer->index($artwork);
$this->userStats->incrementUploads($artwork->user_id);
}
}

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