Files
SkinbaseNova/app/Observers/ArtworkObserver.php

177 lines
7.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Observers;
use App\Events\Achievements\AchievementCheckRequested;
use App\Models\Artwork;
use App\Jobs\GenerateFeaturedArtworkThumbnailsJob;
use App\Jobs\RecComputeSimilarByTagsJob;
use App\Jobs\RecComputeSimilarHybridJob;
use App\Jobs\Posts\AutoUploadPostJob;
use App\Services\ArtworkSearchIndexer;
use App\Services\HomepageService;
use App\Services\Profile\CreatorJourneyService;
use App\Services\UserStatsService;
use App\Services\XPService;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
/**
* 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 UserStatsService $userStats,
private readonly XPService $xp,
private readonly HomepageService $homepage,
private readonly CreatorJourneyService $journeys,
) {}
/** 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);
$this->journeys->requestRebuild((int) $artwork->user_id);
if ($artwork->is_public && $artwork->is_approved && $artwork->published_at !== null) {
$this->bumpExploreCacheVersion();
}
if ($artwork->published_at !== null) {
$this->xp->awardArtworkPublished((int) $artwork->user_id, (int) $artwork->id);
event(new AchievementCheckRequested((int) $artwork->user_id));
}
}
/** Artwork updated — covers publish, approval, metadata changes. */
public function updated(Artwork $artwork): void
{
// When soft-deleted, remove from index immediately.
if ($artwork->isDirty('deleted_at') && $artwork->deleted_at !== null) {
$this->indexer->delete($artwork->id);
return;
}
$this->indexer->update($artwork);
// A changed hash/extension means any previously persisted
// featured_thumbnail_variants_json now refers to the WRONG object paths (they
// were computed from the old hash) — the variant names would still read as
// "available" while pointing at objects that don't exist under the new hash,
// which would surface as a broken hero image. Reset the audit state immediately
// (via a direct query, not a model save, to avoid re-entering this observer) so
// hasFeaturedThumbnail() safely reports "not audited" until the queued job below
// re-verifies and repopulates it; the public homepage never blocks on that check.
if ($artwork->wasChanged(['hash', 'file_ext'])) {
DB::table('artworks')->where('id', $artwork->id)->update([
'featured_thumbnail_variants_json' => null,
'featured_thumbnails_checked_at' => null,
]);
if (! empty($artwork->hash) && ! empty($artwork->file_ext) && $artwork->features()->exists()) {
GenerateFeaturedArtworkThumbnailsJob::dispatch((int) $artwork->id, true);
}
}
// §7.5 On-demand: recompute similarity when tags/categories could have changed.
// The pivot sync happens outside this observer, so we dispatch on every
// meaningful update and let the job be idempotent (cheap if nothing changed).
if ($artwork->is_public && $artwork->published_at) {
if ($artwork->wasChanged('published_at') || $artwork->wasChanged('created_at')) {
$this->userStats->setLastUploadAt($artwork->user_id, $artwork->created_at ?? $artwork->published_at);
}
RecComputeSimilarByTagsJob::dispatch($artwork->id)->delay(now()->addSeconds(30));
RecComputeSimilarHybridJob::dispatch($artwork->id)->delay(now()->addMinutes(1));
// Auto-upload post: fire only when artwork transitions to published for the first time
if ($artwork->wasChanged('published_at') && $artwork->published_at !== null) {
$this->xp->awardArtworkPublished((int) $artwork->user_id, (int) $artwork->id);
event(new AchievementCheckRequested((int) $artwork->user_id));
$user = $artwork->user;
$autoPost = $user?->profile?->auto_post_upload ?? true;
if ($autoPost) {
AutoUploadPostJob::dispatch($artwork->id, $artwork->user_id)
->delay(now()->addSeconds(5));
}
}
}
if ($this->shouldClearFeaturedCaches($artwork)) {
$this->homepage->clearFeaturedAndMedalCaches();
}
if ($artwork->wasChanged(['published_at', 'is_public', 'is_approved', 'deleted_at'])) {
$this->bumpExploreCacheVersion();
}
if ($artwork->wasChanged(['published_at', 'is_public', 'is_approved', 'visibility', 'deleted_at', 'published_as_type', 'published_as_id'])) {
$this->journeys->requestRebuild((int) $artwork->user_id);
}
}
/** 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);
$this->journeys->requestRebuild((int) $artwork->user_id);
$this->bumpExploreCacheVersion();
if ($artwork->features()->exists()) {
$this->homepage->clearFeaturedAndMedalCaches();
}
}
/** Force delete — ensure removal from index; only decrement if NOT already soft-deleted. */
public function forceDeleted(Artwork $artwork): void
{
$this->indexer->delete($artwork->id);
$this->journeys->requestRebuild((int) $artwork->user_id);
$this->bumpExploreCacheVersion();
// 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 and re-increment uploads_count. */
public function restored(Artwork $artwork): void
{
$this->indexer->index($artwork);
$this->userStats->incrementUploads($artwork->user_id);
$this->journeys->requestRebuild((int) $artwork->user_id);
$this->bumpExploreCacheVersion();
if ($artwork->features()->exists()) {
$this->homepage->clearFeaturedAndMedalCaches();
}
}
private function bumpExploreCacheVersion(): void
{
Cache::forever('explore.cache.version', ((int) Cache::get('explore.cache.version', 1)) + 1);
}
private function shouldClearFeaturedCaches(Artwork $artwork): bool
{
if (! $artwork->wasChanged(['published_at', 'is_public', 'is_approved', 'deleted_at', 'has_missing_thumbnails', 'hash', 'file_ext'])) {
return false;
}
return $artwork->features()->exists();
}
}