65 lines
2.3 KiB
TypeScript
65 lines
2.3 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/prisma';
|
|
import type { FeedItem, FeedResponse } from '@/app/types/feed';
|
|
|
|
// Contract
|
|
// Inputs: search params { before?: ISOString, limit?: number }
|
|
// Output: { items: FeedItem[], nextCursor: ISOString | null }
|
|
|
|
export async function GET(req: NextRequest) {
|
|
const { searchParams } = new URL(req.url);
|
|
const limitParam = searchParams.get('limit');
|
|
const beforeParam = searchParams.get('before');
|
|
|
|
const limit = Math.min(Math.max(Number(limitParam ?? '24'), 1), 60); // 1..60
|
|
const before = beforeParam ? new Date(beforeParam) : null;
|
|
|
|
// fetch chunk from both tables
|
|
const [videos, posts] = await Promise.all([
|
|
prisma.video.findMany({
|
|
where: before ? { created_at: { lt: before } } : undefined,
|
|
orderBy: { created_at: 'desc' },
|
|
take: limit,
|
|
include: { author: true },
|
|
}),
|
|
prisma.imagePost.findMany({
|
|
where: before ? { created_at: { lt: before } } : undefined,
|
|
orderBy: { created_at: 'desc' },
|
|
take: limit,
|
|
include: { author: true, images: { orderBy: { order: 'asc' }, take: 1 } },
|
|
}),
|
|
]);
|
|
|
|
const merged: FeedItem[] = [
|
|
...videos.map((v) => ({
|
|
type: 'video' as const,
|
|
aweme_id: v.aweme_id,
|
|
created_at: v.created_at,
|
|
desc: v.desc,
|
|
video_url: v.video_url,
|
|
cover_url: v.cover_url ?? null,
|
|
width: v.width ?? null,
|
|
height: v.height ?? null,
|
|
author: { nickname: v.author.nickname, avatar_url: v.author.avatar_url ?? null },
|
|
likes: Number(v.digg_count),
|
|
})),
|
|
...posts.map((p) => ({
|
|
type: 'image' as const,
|
|
aweme_id: p.aweme_id,
|
|
created_at: p.created_at,
|
|
desc: p.desc,
|
|
cover_url: p.images?.[0]?.url ?? null,
|
|
width: p.images?.[0]?.width ?? null,
|
|
height: p.images?.[0]?.height ?? null,
|
|
author: { nickname: p.author.nickname, avatar_url: p.author.avatar_url ?? null },
|
|
likes: Number(p.digg_count),
|
|
})),
|
|
]
|
|
.sort((a, b) => +new Date(b.created_at as any) - +new Date(a.created_at as any))
|
|
.slice(0, limit);
|
|
|
|
const nextCursor = merged.length > 0 ? new Date(merged[merged.length - 1].created_at as any).toISOString() : null;
|
|
const payload: FeedResponse = { items: merged, nextCursor };
|
|
return NextResponse.json(payload);
|
|
}
|