optimizations
This commit is contained in:
@@ -4,11 +4,13 @@ namespace App\Http\Controllers\Web;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Artwork;
|
||||
use App\Services\CommunityActivityService;
|
||||
use App\Services\ArtworkSearchService;
|
||||
use App\Services\ArtworkService;
|
||||
use App\Services\EarlyGrowth\FeedBlender;
|
||||
use App\Services\EarlyGrowth\GridFiller;
|
||||
use App\Services\Recommendation\RecommendationService;
|
||||
use App\Services\Recommendations\RecommendationFeedResolver;
|
||||
use App\Services\UserSuggestionService;
|
||||
use App\Services\ThumbnailPresenter;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
@@ -31,9 +33,11 @@ final class DiscoverController extends Controller
|
||||
public function __construct(
|
||||
private readonly ArtworkService $artworkService,
|
||||
private readonly ArtworkSearchService $searchService,
|
||||
private readonly RecommendationService $recoService,
|
||||
private readonly RecommendationFeedResolver $feedResolver,
|
||||
private readonly FeedBlender $feedBlender,
|
||||
private readonly GridFiller $gridFiller,
|
||||
private readonly CommunityActivityService $communityActivity,
|
||||
private readonly UserSuggestionService $userSuggestions,
|
||||
) {}
|
||||
|
||||
// ─── /discover/trending ──────────────────────────────────────────────────
|
||||
@@ -225,51 +229,38 @@ final class DiscoverController extends Controller
|
||||
/**
|
||||
* Personalised "For You" feed page.
|
||||
*
|
||||
* Uses RecommendationService (Phase 1 tag-affinity + creator-affinity pipeline)
|
||||
* and renders the standard discover grid view. Guest users are redirected
|
||||
* to the trending page per spec.
|
||||
* Uses the newer personalized feed service so the web surface stays aligned
|
||||
* with the API recommendation stack and discovery-event training loop.
|
||||
*/
|
||||
public function forYou(Request $request)
|
||||
{
|
||||
$user = $request->user();
|
||||
$limit = 40;
|
||||
$limit = max(1, min(50, (int) $request->query('limit', 40)));
|
||||
$cursor = $request->query('cursor') ?: null;
|
||||
|
||||
// Retrieve the paginated feed (service handles Meilisearch + reranking + cache)
|
||||
$feedResult = $this->recoService->forYouFeed(
|
||||
user: $user,
|
||||
limit: $limit,
|
||||
$feedResult = $this->feedResolver->getFeed(
|
||||
userId: (int) $user->id,
|
||||
limit: $limit,
|
||||
cursor: is_string($cursor) ? $cursor : null,
|
||||
algoVersion: $request->filled('algo_version') ? (string) $request->query('algo_version') : null,
|
||||
);
|
||||
|
||||
$artworkItems = $feedResult['data'] ?? [];
|
||||
|
||||
// Build a simple presentable collection
|
||||
$artworks = collect($artworkItems)->map(function (array $item) {
|
||||
$width = isset($item['width']) && $item['width'] > 0 ? (int) $item['width'] : null;
|
||||
$height = isset($item['height']) && $item['height'] > 0 ? (int) $item['height'] : null;
|
||||
$avatarUrl = \App\Support\AvatarUrl::forUser((int) ($item['author_id'] ?? 0), null, 64);
|
||||
|
||||
return (object) [
|
||||
'id' => $item['id'] ?? 0,
|
||||
'name' => $item['title'] ?? 'Untitled',
|
||||
'category_name' => $item['category_name'] ?? '',
|
||||
'category_slug' => $item['category_slug'] ?? '',
|
||||
'thumb_url' => $item['thumbnail_url'] ?? null,
|
||||
'thumb_srcset' => $item['thumbnail_url'] ?? null,
|
||||
'uname' => $item['author'] ?? 'Artist',
|
||||
'username' => $item['username'] ?? '',
|
||||
'avatar_url' => $avatarUrl,
|
||||
'published_at' => $item['published_at'] ?? null,
|
||||
'slug' => $item['slug'] ?? '',
|
||||
'width' => $width,
|
||||
'height' => $height,
|
||||
];
|
||||
});
|
||||
$artworks = collect($feedResult['data'] ?? [])->map(
|
||||
fn (array $item) => $this->presentRecommendedArtwork($item)
|
||||
)->values();
|
||||
|
||||
$meta = $feedResult['meta'] ?? [];
|
||||
$nextCursor = $meta['next_cursor'] ?? null;
|
||||
|
||||
if ($request->ajax()) {
|
||||
return response()->json([
|
||||
'artworks' => $artworks->map(fn (object $artwork) => (array) $artwork)->all(),
|
||||
'next_cursor' => $nextCursor,
|
||||
'has_more' => ! empty($nextCursor),
|
||||
'meta' => $meta,
|
||||
]);
|
||||
}
|
||||
|
||||
return view('web.discover.for-you', [
|
||||
'artworks' => $artworks,
|
||||
'page_title' => 'For You',
|
||||
@@ -277,6 +268,7 @@ final class DiscoverController extends Controller
|
||||
'description' => 'Artworks picked for you based on your taste.',
|
||||
'icon' => 'fa-wand-magic-sparkles',
|
||||
'next_cursor' => $nextCursor,
|
||||
'feed_meta' => $meta,
|
||||
'cache_status' => $meta['cache_status'] ?? null,
|
||||
]);
|
||||
}
|
||||
@@ -304,20 +296,7 @@ final class DiscoverController extends Controller
|
||||
}
|
||||
|
||||
// Suggested creators: most-followed users the viewer doesn't follow yet
|
||||
$suggestedCreators = DB::table('users')
|
||||
->join('user_statistics', 'users.id', '=', 'user_statistics.user_id')
|
||||
->where('users.id', '!=', $user->id)
|
||||
->whereNotNull('users.email_verified_at')
|
||||
->where('users.is_active', true)
|
||||
->orderByDesc('user_statistics.followers_count')
|
||||
->limit(8)
|
||||
->select(
|
||||
'users.id',
|
||||
'users.name',
|
||||
'users.username',
|
||||
'user_statistics.followers_count',
|
||||
)
|
||||
->get();
|
||||
$suggestedCreators = $this->userSuggestions->suggestFor($user, 6);
|
||||
|
||||
return view('web.discover.index', [
|
||||
'artworks' => collect(),
|
||||
@@ -328,6 +307,9 @@ final class DiscoverController extends Controller
|
||||
'empty' => true,
|
||||
'fallback_trending' => $fallbackArtworks,
|
||||
'fallback_creators' => $suggestedCreators,
|
||||
'following_activity' => [],
|
||||
'network_trending' => [],
|
||||
'suggested_users' => $suggestedCreators,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -347,12 +329,23 @@ final class DiscoverController extends Controller
|
||||
|
||||
$artworks->getCollection()->transform(fn ($a) => $this->presentArtwork($a));
|
||||
|
||||
$networkActivity = $this->communityActivity->getFeed(
|
||||
viewer: $user,
|
||||
filter: 'following',
|
||||
page: 1,
|
||||
perPage: 8,
|
||||
actorUserId: null,
|
||||
);
|
||||
|
||||
return view('web.discover.index', [
|
||||
'artworks' => $artworks,
|
||||
'page_title' => 'Following Feed',
|
||||
'section' => 'following',
|
||||
'description' => 'The latest artworks from creators you follow.',
|
||||
'icon' => 'fa-user-group',
|
||||
'following_activity' => $networkActivity['data'] ?? [],
|
||||
'network_trending' => $this->buildNetworkTrendingArtworks($followingIds->all(), 8),
|
||||
'suggested_users' => $this->userSuggestions->suggestFor($user, 6),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -443,4 +436,81 @@ final class DiscoverController extends Controller
|
||||
'height' => $artwork->height ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $item
|
||||
*/
|
||||
private function presentRecommendedArtwork(array $item): object
|
||||
{
|
||||
$width = isset($item['width']) && (int) $item['width'] > 0 ? (int) $item['width'] : null;
|
||||
$height = isset($item['height']) && (int) $item['height'] > 0 ? (int) $item['height'] : null;
|
||||
|
||||
return (object) [
|
||||
'id' => (int) ($item['id'] ?? 0),
|
||||
'name' => (string) ($item['title'] ?? 'Untitled'),
|
||||
'thumb_url' => $item['thumbnail_url'] ?? null,
|
||||
'thumb_srcset' => $item['thumbnail_srcset'] ?? null,
|
||||
'uname' => (string) ($item['author'] ?? 'Artist'),
|
||||
'username' => (string) ($item['username'] ?? ''),
|
||||
'avatar_url' => $item['avatar_url'] ?? null,
|
||||
'published_at' => $item['published_at'] ?? null,
|
||||
'slug' => (string) ($item['slug'] ?? ''),
|
||||
'url' => $item['url'] ?? null,
|
||||
'width' => $width,
|
||||
'height' => $height,
|
||||
'content_type_name' => (string) ($item['content_type_name'] ?? ''),
|
||||
'content_type_slug' => (string) ($item['content_type_slug'] ?? ''),
|
||||
'category_name' => (string) ($item['category_name'] ?? ''),
|
||||
'category_slug' => (string) ($item['category_slug'] ?? ''),
|
||||
'primary_tag' => $item['primary_tag'] ?? null,
|
||||
'tags' => is_array($item['tags'] ?? null) ? $item['tags'] : [],
|
||||
'recommendation_source' => (string) ($item['source'] ?? 'mixed'),
|
||||
'recommendation_reason' => (string) ($item['reason'] ?? 'Picked for you'),
|
||||
'recommendation_score' => isset($item['score']) ? round((float) $item['score'], 4) : null,
|
||||
'recommendation_algo_version' => (string) ($item['algo_version'] ?? ''),
|
||||
'hide_artwork_endpoint' => route('api.discovery.feedback.hide-artwork'),
|
||||
'dislike_tag_endpoint' => route('api.discovery.feedback.dislike-tag'),
|
||||
];
|
||||
}
|
||||
|
||||
private function buildNetworkTrendingArtworks(array $followingIds, int $limit = 8): array
|
||||
{
|
||||
if ($followingIds === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return Artwork::query()
|
||||
->public()
|
||||
->published()
|
||||
->with(['user:id,name,username', 'user.profile:user_id,avatar_hash', 'stats:artwork_id,views,favorites,comments_count,heat_score'])
|
||||
->whereIn('user_id', $followingIds)
|
||||
->where('published_at', '>=', now()->subDays(30))
|
||||
->leftJoin('artwork_stats as ast', 'ast.artwork_id', '=', 'artworks.id')
|
||||
->orderByDesc(DB::raw('COALESCE(ast.heat_score, 0)'))
|
||||
->orderByDesc(DB::raw('COALESCE(ast.favorites, 0)'))
|
||||
->orderByDesc('artworks.published_at')
|
||||
->limit(max(1, $limit))
|
||||
->select('artworks.*')
|
||||
->get()
|
||||
->map(fn (Artwork $artwork) => [
|
||||
'id' => (int) $artwork->id,
|
||||
'title' => (string) $artwork->title,
|
||||
'url' => route('art.show', ['id' => (int) $artwork->id, 'slug' => $artwork->slug]),
|
||||
'thumb_url' => $artwork->thumb_url,
|
||||
'published_at' => $artwork->published_at?->diffForHumans(),
|
||||
'author' => [
|
||||
'username' => $artwork->user?->username,
|
||||
'name' => $artwork->user?->name,
|
||||
'avatar_url' => $artwork->user?->profile?->avatar_url,
|
||||
'profile_url' => $artwork->user?->username ? '/@' . strtolower((string) $artwork->user->username) : null,
|
||||
],
|
||||
'stats' => [
|
||||
'favorites' => (int) ($artwork->stats?->favorites ?? 0),
|
||||
'views' => (int) ($artwork->stats?->views ?? 0),
|
||||
'comments' => (int) ($artwork->stats?->comments_count ?? 0),
|
||||
],
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user