Save workspace changes

This commit is contained in:
2026-04-18 17:02:56 +02:00
parent f02ea9a711
commit 87d60af5a9
4220 changed files with 1388603 additions and 1554 deletions

View File

@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
namespace App\Observers;
use App\Jobs\RecalculateArtworkMedalStatsJob;
use App\Models\ArtworkAward;
use App\Services\UserStatsService;
use Illuminate\Support\Facades\DB;
class ArtworkAwardObserver
{
public function __construct(
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
{
RecalculateArtworkMedalStatsJob::dispatchSync((int) $award->artwork_id);
}
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,85 @@
<?php
declare(strict_types=1);
namespace App\Observers;
use App\Models\ArtworkComment;
use App\Services\UserStatsService;
use App\Services\UserMentionSyncService;
use App\Services\XPService;
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,
private readonly UserMentionSyncService $mentionSync,
private readonly XPService $xp,
) {}
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);
$this->xp->awardCommentCreated((int) $comment->user_id, (int) $comment->id, 'artwork');
$this->mentionSync->syncForComment($comment);
}
public function updated(ArtworkComment $comment): void
{
if ($comment->wasChanged(['content', 'raw_content', 'rendered_content', 'parent_id'])) {
$this->mentionSync->syncForComment($comment);
}
}
/** Soft delete. */
public function deleted(ArtworkComment $comment): void
{
$creatorId = $this->creatorId($comment->artwork_id);
if ($creatorId) {
$this->userStats->decrementCommentsReceived($creatorId);
}
$this->mentionSync->deleteForComment((int) $comment->id);
}
/** 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);
}
}
$this->mentionSync->deleteForComment((int) $comment->id);
}
public function restored(ArtworkComment $comment): void
{
$this->mentionSync->syncForComment($comment);
}
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,68 @@
<?php
declare(strict_types=1);
namespace App\Observers;
use App\Jobs\RecComputeSimilarByBehaviorJob;
use App\Jobs\RecComputeSimilarHybridJob;
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);
}
// §7.5 On-demand: recompute behavior similarity when artwork reaches threshold
$this->maybeRecomputeBehavior($favourite->artwork_id);
}
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;
}
/**
* Dispatch on-demand behavior recomputation when an artwork crosses a
* favourites threshold (5, 10, 25, 50 ).
*/
private function maybeRecomputeBehavior(int $artworkId): void
{
$count = (int) DB::table('artwork_favourites')
->where('artwork_id', $artworkId)
->count();
$thresholds = [5, 10, 25, 50, 100];
if (in_array($count, $thresholds, true)) {
RecComputeSimilarByBehaviorJob::dispatch($artworkId)->delay(now()->addSeconds(30));
RecComputeSimilarHybridJob::dispatch($artworkId)->delay(now()->addMinute());
}
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Observers;
use App\Models\ArtworkFeature;
use App\Services\HomepageService;
final class ArtworkFeatureObserver
{
public function __construct(private readonly HomepageService $homepage)
{
}
public function created(ArtworkFeature $feature): void
{
$this->homepage->clearFeaturedAndMedalCaches();
}
public function updated(ArtworkFeature $feature): void
{
$this->homepage->clearFeaturedAndMedalCaches();
}
public function deleted(ArtworkFeature $feature): void
{
$this->homepage->clearFeaturedAndMedalCaches();
}
public function restored(ArtworkFeature $feature): void
{
$this->homepage->clearFeaturedAndMedalCaches();
}
public function forceDeleted(ArtworkFeature $feature): void
{
$this->homepage->clearFeaturedAndMedalCaches();
}
}

View File

@@ -0,0 +1,128 @@
<?php
declare(strict_types=1);
namespace App\Observers;
use App\Events\Achievements\AchievementCheckRequested;
use App\Models\Artwork;
use App\Jobs\RecComputeSimilarByTagsJob;
use App\Jobs\RecComputeSimilarHybridJob;
use App\Jobs\Posts\AutoUploadPostJob;
use App\Services\ArtworkSearchIndexer;
use App\Services\HomepageService;
use App\Services\UserStatsService;
use App\Services\XPService;
/**
* 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,
) {}
/** 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);
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);
// §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();
}
}
/** 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);
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);
// 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);
if ($artwork->features()->exists()) {
$this->homepage->clearFeaturedAndMedalCaches();
}
}
private function shouldClearFeaturedCaches(Artwork $artwork): bool
{
if (! $artwork->wasChanged(['published_at', 'is_public', 'is_approved', 'deleted_at', 'has_missing_thumbnails'])) {
return false;
}
return $artwork->features()->exists();
}
}

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

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Observers;
use App\Models\ContentType;
use App\Models\ContentTypeSlugHistory;
use App\Services\ContentTypes\ContentTypeSlugResolver;
class ContentTypeObserver
{
public function created(ContentType $contentType): void
{
app(ContentTypeSlugResolver::class)->flushCaches();
}
public function updated(ContentType $contentType): void
{
if ($contentType->wasChanged('slug')) {
$oldSlug = strtolower(trim((string) $contentType->getOriginal('slug')));
$newSlug = strtolower(trim((string) $contentType->slug));
if ($oldSlug !== '' && $oldSlug !== $newSlug) {
ContentTypeSlugHistory::query()->updateOrCreate(
['old_slug' => $oldSlug],
['content_type_id' => $contentType->id],
);
}
}
app(ContentTypeSlugResolver::class)->flushCaches();
}
public function deleted(ContentType $contentType): void
{
app(ContentTypeSlugResolver::class)->flushCaches();
}
}