cg / hackerproducts

64 files · 5,803 lines · 161.0 KB — Next.js 15, React 19, TypeScript, PostgreSQL, Object Storage, plain CSS.

Quick jump: page.tsx / page.tsx / globals.css / schema.sql / package.json

root / src / app / api / products / [slug] / comments / route.ts

src/app/api/products/[slug]/comments/route.ts · 99 lines · 2.8 KB← back to folder
1import crypto from "node:crypto";
2import { NextResponse } from "next/server";
3import { query } from "@/db";
4import { getCurrentUser } from "@/lib/auth";
5import { LIMITS } from "@/lib/limits";
6
7export async function POST(
8 request: Request,
9 context: { params: Promise<{ slug: string }> }
10) {
11 const user = await getCurrentUser();
12 if (!user) {
13 return NextResponse.json({ error: "Sign in to leave a comment." }, { status: 401 });
14 }
15
16 const { slug } = await context.params;
17 const prodRes = await query<{ id: string }>(
18 `SELECT id FROM products WHERE slug = $1 AND status = 'published' LIMIT 1`,
19 [slug]
20 );
21
22 const product = prodRes.rows[0];
23 if (!product) {
24 return NextResponse.json({ error: "Product not found." }, { status: 404 });
25 }
26
27 const payload = (await request.json().catch(() => null)) as {
28 body?: string;
29 website_hp?: string;
30 } | null;
31
32 if (!payload) {
33 return NextResponse.json({ error: "Invalid request body." }, { status: 400 });
34 }
35
36 // Honeypot
37 if (payload.website_hp && payload.website_hp.trim().length > 0) {
38 return NextResponse.json({ error: "Unable to post comment." }, { status: 400 });
39 }
40
41 const body = (payload.body ?? "").trim().replace(/\n{3,}/g, "\n\n");
42 if (body.length < 1 || body.length > LIMITS.commentBody) {
43 return NextResponse.json(
44 { error: `Comment must be between 1 and ${LIMITS.commentBody} characters.` },
45 { status: 400 }
46 );
47 }
48
49 // Anti-spam comment rate limit (admins exempt)
50 if (!user.is_admin) {
51 const recentRes = await query<{ last_30s: string; last_hour: string }>(
52 `SELECT
53 COUNT(*) FILTER (WHERE created_at > NOW() - INTERVAL '30 seconds')::int AS last_30s,
54 COUNT(*) FILTER (WHERE created_at > NOW() - INTERVAL '1 hour')::int AS last_hour
55 FROM comments
56 WHERE user_id = $1`,
57 [user.id]
58 );
59
60 const last30s = Number(recentRes.rows[0]?.last_30s ?? 0);
61 const lastHour = Number(recentRes.rows[0]?.last_hour ?? 0);
62
63 if (last30s > 0) {
64 return NextResponse.json(
65 { error: "Please wait 30 seconds before posting another comment." },
66 { status: 429 }
67 );
68 }
69 if (lastHour >= 10) {
70 return NextResponse.json(
71 { error: "Comment limit reached (10 per hour). Please try again later." },
72 { status: 429 }
73 );
74 }
75 }
76
77 const id = crypto.randomUUID();
78 const inserted = await query<{ id: string; created_at: string }>(
79 `INSERT INTO comments (id, product_id, user_id, body)
80 VALUES ($1, $2, $3, $4)
81 RETURNING id, created_at`,
82 [id, product.id, user.id, body]
83 );
84
85 return NextResponse.json(
86 {
87 comment: {
88 id: inserted.rows[0].id,
89 product_id: product.id,
90 user_id: user.id,
91 username: user.username,
92 body,
93 created_at: inserted.rows[0].created_at,
94 },
95 },
96 { status: 201 }
97 );
98}
99
cg / hackerproducts — hacker products