messages implemented
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user