refactor: unify artwork card rendering
This commit is contained in:
@@ -4,9 +4,11 @@ namespace App\Http\Controllers\User;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Artwork;
|
||||
use App\Support\AvatarUrl;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class TodayInHistoryController extends Controller
|
||||
{
|
||||
@@ -61,21 +63,68 @@ class TodayInHistoryController extends Controller
|
||||
|
||||
// ── Enrich with CDN thumbnails (batch load to avoid N+1) ─────────────────
|
||||
if ($artworks && method_exists($artworks, 'getCollection') && $artworks->count() > 0) {
|
||||
$ids = $artworks->getCollection()->pluck('id')->all();
|
||||
$modelsById = Artwork::whereIn('id', $ids)->get()->keyBy('id');
|
||||
$ids = $artworks->getCollection()->pluck('id')->filter()->map(fn ($id) => (int) $id)->all();
|
||||
$modelsById = Artwork::query()
|
||||
->with([
|
||||
'user:id,name,username',
|
||||
'categories' => function ($query) {
|
||||
$query->select('categories.id', 'categories.name', 'categories.slug', 'categories.sort_order');
|
||||
},
|
||||
])
|
||||
->whereIn('id', $ids)
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
$artworks->getCollection()->transform(function ($row) use ($modelsById) {
|
||||
/** @var ?Artwork $art */
|
||||
$art = $modelsById->get($row->id);
|
||||
$row->slug = $row->slug ?? Str::slug($row->name ?? '');
|
||||
|
||||
if ($art) {
|
||||
$row->thumb_url = $art->thumbUrl('md') ?? 'https://files.skinbase.org/default/missing_md.webp';
|
||||
$row->art_url = '/art/' . $art->id . '/' . $art->slug;
|
||||
$row->name = $art->title ?: ($row->name ?? 'Untitled');
|
||||
$primaryCategory = $art->categories?->sortBy('sort_order')->first();
|
||||
$author = $art->user;
|
||||
|
||||
try {
|
||||
$present = \App\Services\ThumbnailPresenter::present($art, 'md');
|
||||
$row->thumb_url = $present['url'];
|
||||
$row->thumb_srcset = $present['srcset'] ?? $present['url'];
|
||||
} catch (\Throwable $e) {
|
||||
$row->thumb_url = $art->thumbUrl('md') ?? 'https://files.skinbase.org/default/missing_md.webp';
|
||||
$row->thumb_srcset = $row->thumb_url;
|
||||
}
|
||||
|
||||
$row->url = url('/art/' . $art->id . '/' . ($art->slug ?: Str::slug($art->title ?: ($row->name ?? 'artwork'))));
|
||||
$row->art_url = $row->url;
|
||||
$row->name = $art->title ?: ($row->name ?? 'Untitled');
|
||||
$row->slug = $art->slug ?: $row->slug;
|
||||
$row->width = $art->width;
|
||||
$row->height = $art->height;
|
||||
$row->content_type_name = $primaryCategory?->contentType?->name ?? '';
|
||||
$row->content_type_slug = $primaryCategory?->contentType?->slug ?? '';
|
||||
$row->category_name = $primaryCategory->name ?? '';
|
||||
$row->category_slug = $primaryCategory->slug ?? '';
|
||||
$row->uname = $author->name ?? 'Skinbase';
|
||||
$row->username = $author->username ?? $author->name ?? '';
|
||||
$row->avatar_url = $author
|
||||
? AvatarUrl::forUser((int) $author->getKey(), null, 64)
|
||||
: AvatarUrl::default();
|
||||
} else {
|
||||
$row->thumb_url = 'https://files.skinbase.org/default/missing_md.webp';
|
||||
$row->art_url = '/art/' . $row->id;
|
||||
$row->name = $row->name ?? 'Untitled';
|
||||
$row->thumb_srcset = $row->thumb_url;
|
||||
$row->url = url('/art/' . $row->id . '/' . ($row->slug ?: Str::slug($row->name ?? 'artwork')));
|
||||
$row->art_url = $row->url;
|
||||
$row->name = $row->name ?? 'Untitled';
|
||||
$row->content_type_name = $row->content_type_name ?? '';
|
||||
$row->content_type_slug = $row->content_type_slug ?? '';
|
||||
$row->category_name = $row->category_name ?? '';
|
||||
$row->category_slug = $row->category_slug ?? '';
|
||||
$row->uname = $row->uname ?? 'Skinbase';
|
||||
$row->username = $row->username ?? '';
|
||||
$row->avatar_url = $row->avatar_url ?? AvatarUrl::default();
|
||||
$row->width = $row->width ?? null;
|
||||
$row->height = $row->height ?? null;
|
||||
}
|
||||
|
||||
return $row;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
namespace App\Http\Controllers\User;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Artwork;
|
||||
use App\Support\AvatarUrl;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Services\LegacyService;
|
||||
|
||||
class TopFavouritesController extends Controller
|
||||
{
|
||||
@@ -28,14 +29,30 @@ class TopFavouritesController extends Controller
|
||||
}
|
||||
|
||||
if ($paginator && method_exists($paginator, 'getCollection')) {
|
||||
$paginator->getCollection()->transform(function ($row) {
|
||||
$artworkLookup = Artwork::query()
|
||||
->with([
|
||||
'user:id,name,username',
|
||||
'categories' => function ($query) {
|
||||
$query->select('categories.id', 'categories.name', 'categories.slug', 'categories.sort_order');
|
||||
},
|
||||
])
|
||||
->whereIn('id', $paginator->getCollection()->pluck('id')->filter()->map(fn ($id) => (int) $id)->all())
|
||||
->get()
|
||||
->keyBy('id');
|
||||
|
||||
$paginator->getCollection()->transform(function ($row) use ($artworkLookup) {
|
||||
$row->slug = $row->slug ?? Str::slug($row->name ?? '');
|
||||
$ext = pathinfo($row->picture ?? '', PATHINFO_EXTENSION) ?: 'jpg';
|
||||
$encoded = \App\Helpers\Thumb::encodeId((int) $row->id);
|
||||
$row->encoded = $encoded;
|
||||
$row->ext = $ext;
|
||||
|
||||
/** @var \App\Models\Artwork|null $art */
|
||||
$art = $artworkLookup->get((int) $row->id);
|
||||
$primaryCategory = $art?->categories?->sortBy('sort_order')->first();
|
||||
$author = $art?->user;
|
||||
|
||||
try {
|
||||
$art = \App\Models\Artwork::find($row->id);
|
||||
$present = \App\Services\ThumbnailPresenter::present($art ?: (array) $row, 'md');
|
||||
$row->thumb = $row->thumb ?? $present['url'];
|
||||
$row->thumb_srcset = $row->thumb_srcset ?? ($present['srcset'] ?? $present['url']);
|
||||
@@ -44,7 +61,23 @@ class TopFavouritesController extends Controller
|
||||
$row->thumb = $row->thumb ?? $present['url'];
|
||||
$row->thumb_srcset = $row->thumb_srcset ?? ($present['srcset'] ?? $present['url']);
|
||||
}
|
||||
|
||||
$row->thumb_url = $row->thumb ?? null;
|
||||
$row->gid_num = ((int)($row->category ?? 0) % 5) * 5;
|
||||
$row->url = url('/art/' . (int) $row->id . '/' . ($row->slug ?: Str::slug($row->name ?? 'artwork')));
|
||||
$row->width = $art?->width;
|
||||
$row->height = $art?->height;
|
||||
$row->content_type_name = $primaryCategory?->contentType?->name ?? '';
|
||||
$row->content_type_slug = $primaryCategory?->contentType?->slug ?? '';
|
||||
$row->category_name = $primaryCategory->name ?? '';
|
||||
$row->category_slug = $primaryCategory->slug ?? '';
|
||||
$row->uname = $author->name ?? 'Skinbase';
|
||||
$row->username = $author->username ?? $author->name ?? '';
|
||||
$row->avatar_url = $author
|
||||
? AvatarUrl::forUser((int) $author->getKey(), null, 64)
|
||||
: AvatarUrl::default();
|
||||
$row->favourites = (int) ($row->num ?? 0);
|
||||
|
||||
return $row;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user