51e6722f4c
Two changes to the no-photo cover placeholder shipped last version: 1. Muted the gradient palette (100/40-opacity tints instead of solid -200/ -950 stops) — the original was too saturated next to real cover photos in the same grid, per feedback. 2. New coverIcon/coverColor columns on recipes (nullable text, migration 0048, additive-only) let the author pin a specific color+icon from the recipe editor instead of the automatic per-id pick. getRecipePlaceholder now checks these first, falling back to the deterministic hash pick when unset — existing recipes are unaffected until edited. Wired coverIcon/coverColor through every explicit-column recipe select that feeds a grid card (explore trending/recent, search, feed, for-you) — the relational-query call sites (recipes page, collections, profile pages) already return all columns and needed no changes. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
87 lines
3.6 KiB
TypeScript
87 lines
3.6 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { db, recipes, users, favorites, ratings, userFollows, eq, and, or, ne, gte, notInArray, inArray, desc, isNotNull } from "@epicure/db";
|
|
import { requireSessionOrApiKey } from "@/lib/api-auth";
|
|
import { buildPreferenceMap, rankForYou } from "@/lib/for-you-ranking";
|
|
import { attachCardExtras } from "@/lib/recipe-card-extras";
|
|
|
|
export async function GET(req: NextRequest) {
|
|
const { session, response } = await requireSessionOrApiKey(req);
|
|
if (response) return response;
|
|
const userId = session!.user.id;
|
|
|
|
const { searchParams } = new URL(req.url);
|
|
const limit = Math.min(parseInt(searchParams.get("limit") ?? "20"), 50);
|
|
const offsetRaw = parseInt(searchParams.get("offset") ?? "0");
|
|
const offset = Number.isNaN(offsetRaw) ? 0 : Math.max(0, offsetRaw);
|
|
|
|
// Recipes the user has favorited, or rated 4+, define their taste profile.
|
|
const [favoritedRows, highRatedRows] = await Promise.all([
|
|
db.select({ recipeId: favorites.recipeId }).from(favorites).where(eq(favorites.userId, userId)),
|
|
db.select({ recipeId: ratings.recipeId }).from(ratings).where(and(eq(ratings.userId, userId), gte(ratings.score, 4))),
|
|
]);
|
|
|
|
const likedIds = [...new Set([...favoritedRows.map((r) => r.recipeId), ...highRatedRows.map((r) => r.recipeId)])];
|
|
|
|
const likedRecipes = likedIds.length > 0
|
|
? await db.select({ tags: recipes.tags, dietaryTags: recipes.dietaryTags }).from(recipes).where(inArray(recipes.id, likedIds))
|
|
: [];
|
|
|
|
const preferences = buildPreferenceMap(likedRecipes);
|
|
|
|
const excludeIds = likedIds.length > 0 ? likedIds : ["__none__"];
|
|
|
|
const candidates = await db
|
|
.select({
|
|
id: recipes.id,
|
|
title: recipes.title,
|
|
description: recipes.description,
|
|
baseServings: recipes.baseServings,
|
|
prepMins: recipes.prepMins,
|
|
cookMins: recipes.cookMins,
|
|
difficulty: recipes.difficulty,
|
|
visibility: recipes.visibility,
|
|
aiGenerated: recipes.aiGenerated,
|
|
createdAt: recipes.createdAt,
|
|
authorId: recipes.authorId,
|
|
authorName: users.name,
|
|
authorUsername: users.username,
|
|
authorAvatarUrl: users.avatarUrl,
|
|
tags: recipes.tags,
|
|
dietaryTags: recipes.dietaryTags,
|
|
isBatchCook: recipes.isBatchCook,
|
|
sourceUrl: recipes.sourceUrl,
|
|
recipeType: recipes.recipeType,
|
|
coverIcon: recipes.coverIcon,
|
|
coverColor: recipes.coverColor,
|
|
})
|
|
.from(recipes)
|
|
.innerJoin(users, eq(recipes.authorId, users.id))
|
|
.leftJoin(
|
|
userFollows,
|
|
and(eq(userFollows.followingId, recipes.authorId), eq(userFollows.followerId, userId))
|
|
)
|
|
.where(and(
|
|
eq(recipes.visibility, "public"),
|
|
ne(recipes.authorId, userId),
|
|
notInArray(recipes.id, excludeIds),
|
|
// Private authors are excluded from discovery unless the viewer already follows them.
|
|
or(eq(users.isPrivate, false), isNotNull(userFollows.followerId))
|
|
))
|
|
.orderBy(desc(recipes.createdAt))
|
|
.limit(200); // score a bounded recent window rather than the whole table
|
|
|
|
const ranked = preferences.size > 0
|
|
? rankForYou(candidates, preferences)
|
|
: [...candidates].sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
|
|
|
const page = ranked.slice(offset, offset + limit).map(({ dietaryTags: _dietaryTags, ...r }) => ({
|
|
...r,
|
|
createdAt: r.createdAt.toISOString(),
|
|
}));
|
|
const data = await attachCardExtras(page, userId);
|
|
|
|
// `ranked` is capped to a bounded recent window (see `candidates` query above), so this
|
|
// total reflects the size of that ranked window rather than every eligible recipe.
|
|
return NextResponse.json({ data, total: ranked.length, limit, offset });
|
|
}
|