Files
SkinbaseNova/app/Http/Resources/ArtworkListResource.php
Gregor Klevze eee7df1f8c feat: artwork page carousels, recommendations, avatars & fixes
- Infinite loop carousels for Similar Artworks & Trending rails
- Mouse wheel horizontal scrolling on both carousels
- Author avatar shown on hover in RailCard (similar + trending)
- Removed "View" badge from RailCard hover overlay
- Added `id` to Meilisearch filterable attributes
- Auto-prepend Scout prefix in meilisearch:configure-index command
- Added author name + avatar to Similar Artworks API response
- Added avatar_url to ArtworkListResource author object
- Added direct /art/{id}/{slug} URL to ArtworkListResource
- Fixed race condition: Similar Artworks no longer briefly shows trending items
- Fixed user_profiles eager load (user_id primary key, not id)
- Bumped /api/art/{id}/similar rate limit to 300/min
- Removed decorative heart icons from tag pills
- Moved ReactionBar under artwork description
2026-02-28 14:05:39 +01:00

92 lines
3.4 KiB
PHP

<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\MissingValue;
use App\Services\ThumbnailService;
class ArtworkListResource extends JsonResource
{
/**
* Transform the resource into an array for listings (browse feed).
*/
public function toArray($request): array
{
if ($this instanceof MissingValue || $this->resource instanceof MissingValue) {
return [];
}
// Safe accessor to avoid magic __get which may trigger MissingValue errors
$get = function ($key) {
$r = $this->resource;
if ($r instanceof MissingValue || $r === null) {
return null;
}
if (method_exists($r, 'getAttribute')) {
return $r->getAttribute($key);
}
if (is_array($r)) {
return $r[$key] ?? null;
}
if (is_object($r)) {
return $r->{$key} ?? null;
}
return null;
};
$primaryCategory = $this->whenLoaded('categories', function () {
return $this->categories->sortBy('sort_order')->first();
});
// Normalize MissingValue into null so later checks are straightforward
if ($primaryCategory instanceof MissingValue) {
$primaryCategory = null;
}
$contentTypeSlug = null;
$categoryPath = null;
if ($primaryCategory) {
$contentTypeSlug = optional($primaryCategory->contentType)->slug ?? null;
$categoryPath = $primaryCategory->full_slug_path ?? null;
}
$slugVal = $get('slug');
$hash = (string) ($get('hash') ?? '');
$thumbExt = (string) ($get('thumb_ext') ?? '');
$webUrl = $contentTypeSlug && $categoryPath && $slugVal
? '/' . strtolower($contentTypeSlug) . '/' . strtolower($categoryPath) . '/' . $slugVal
: null;
$artId = $get('id');
$directUrl = $artId && $slugVal ? '/art/' . $artId . '/' . $slugVal : null;
$decode = static fn (?string $v): string => html_entity_decode((string) ($v ?? ''), ENT_QUOTES | ENT_HTML5, 'UTF-8');
return [
'id' => $artId,
'slug' => $slugVal,
'title' => $decode($get('title')),
'description' => $this->when($request->boolean('include_description'), fn() => $decode($get('description'))),
'dimensions' => [
'width' => $get('width'),
'height' => $get('height'),
],
'thumbnail_url' => $this->when(! empty($hash) && ! empty($thumbExt), fn() => ThumbnailService::fromHash($hash, $thumbExt, 'md')),
'author' => $this->whenLoaded('user', function () use ($decode) {
return [
'name' => $decode($this->user->name ?? null),
'avatar_url' => $this->user?->profile?->avatar_url,
];
}),
'category' => $primaryCategory ? [
'slug' => $primaryCategory->slug ?? null,
'name' => $decode($primaryCategory->name ?? null),
'content_type' => $contentTypeSlug,
'url' => $webUrl,
] : null,
'urls' => [
'web' => $webUrl ?? $directUrl,
'direct' => $directUrl,
'canonical' => $webUrl ?? $directUrl,
],
];
}
}