feat: notifications system, rate limiting, fix recipe visibility 404, follow race
Part of the social-feature backlog (follow, comments, reactions, ratings, feed, threading) audited earlier — see conversation. - notifications table: follow/comment/reply/reaction/rating events, replaces the fully-dead feed_items table (feed_item_type enum existed but had zero references anywhere in the codebase). - Bell UI in the nav with unread badge, mark-all-read, 30s poll. - Rate limiting on comment posting (20/min), follow/unfollow (30/min), and comment reactions (60/min) — previously unthrottled. - /recipes/[id] queried by (id, authorId=session.user) only, so any recipe not owned by the viewer 404'd regardless of visibility. Widen the query to include public/unlisted recipes and gate the owner-only actions (edit, delete, version history, translate, AI content generation) behind an isOwner check. - user_follows had no primary key/unique constraint, so the follow route's onConflictDoNothing() was a silent no-op — concurrent follow clicks could insert duplicate rows and inflate follower counts. Add a composite primary key on (follower_id, following_id). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,8 @@ import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { db, comments, commentReactions, eq, and, count } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { applyRateLimit } from "@/lib/rate-limit";
|
||||
import { createNotification } from "@/lib/notifications";
|
||||
|
||||
const ReactionSchema = z.object({
|
||||
type: z.enum(["like", "love", "laugh", "wow", "sad", "fire"]),
|
||||
@@ -49,6 +51,10 @@ export async function GET(req: NextRequest, { params }: Params) {
|
||||
export async function POST(req: NextRequest, { params }: Params) {
|
||||
const { session, response } = await requireSession();
|
||||
if (response) return response;
|
||||
|
||||
const limited = await applyRateLimit(`rl:reaction:${session!.user.id}`, 60, 60);
|
||||
if (limited) return limited;
|
||||
|
||||
const { commentId } = await params;
|
||||
|
||||
// Verify comment exists
|
||||
@@ -85,6 +91,7 @@ export async function POST(req: NextRequest, { params }: Params) {
|
||||
type,
|
||||
});
|
||||
added = true;
|
||||
void createNotification({ userId: comment.userId, type: "reaction", actorId: userId, recipeId: comment.recipeId, commentId });
|
||||
}
|
||||
|
||||
// Return updated counts
|
||||
|
||||
@@ -2,8 +2,10 @@ import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { db, recipes, comments, users, eq, and } from "@epicure/db";
|
||||
import { requireSession } from "@/lib/api-auth";
|
||||
import { applyRateLimit } from "@/lib/rate-limit";
|
||||
import { dispatchWebhook } from "@/lib/webhooks";
|
||||
import { sendPushNotification } from "@/lib/push";
|
||||
import { createNotification } from "@/lib/notifications";
|
||||
|
||||
const Schema = z.object({
|
||||
content: z.string().min(1).max(5000),
|
||||
@@ -44,6 +46,9 @@ export async function POST(req: NextRequest, { params }: Params) {
|
||||
if (response) return response;
|
||||
const { id } = await params;
|
||||
|
||||
const limited = await applyRateLimit(`rl:comment:${session!.user.id}`, 20, 60);
|
||||
if (limited) return limited;
|
||||
|
||||
const recipe = await db.query.recipes.findFirst({ where: eq(recipes.id, id) });
|
||||
if (!recipe || (recipe.visibility === "private" && recipe.authorId !== session!.user.id)) {
|
||||
return NextResponse.json({ error: "Not found" }, { status: 404 });
|
||||
@@ -53,8 +58,9 @@ export async function POST(req: NextRequest, { params }: Params) {
|
||||
const parsed = Schema.safeParse(body);
|
||||
if (!parsed.success) return NextResponse.json({ error: "Validation error" }, { status: 400 });
|
||||
|
||||
let parent: { id: string; userId: string } | undefined;
|
||||
if (parsed.data.parentId) {
|
||||
const parent = await db.query.comments.findFirst({
|
||||
parent = await db.query.comments.findFirst({
|
||||
where: and(eq(comments.id, parsed.data.parentId), eq(comments.recipeId, id)),
|
||||
});
|
||||
if (!parent) return NextResponse.json({ error: "Parent comment not found" }, { status: 404 });
|
||||
@@ -78,5 +84,13 @@ export async function POST(req: NextRequest, { params }: Params) {
|
||||
url: `/recipes/${id}`,
|
||||
});
|
||||
}
|
||||
|
||||
if (parent && parent.userId !== session!.user.id) {
|
||||
void createNotification({ userId: parent.userId, type: "reply", actorId: session!.user.id, recipeId: id, commentId });
|
||||
}
|
||||
if (recipe.authorId !== parent?.userId) {
|
||||
void createNotification({ userId: recipe.authorId, type: "comment", actorId: session!.user.id, recipeId: id, commentId });
|
||||
}
|
||||
|
||||
return NextResponse.json({ id: commentId }, { status: 201 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user