feat: @mentions in comments
Parse @username in comment content, notify mentioned users (dedup against recipe-author/parent-author who are already notified via comment/reply), and render @username as a link to their profile. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { db, recipes, comments, users, userBlocks, eq, and } from "@epicure/db";
|
||||
import { db, recipes, comments, users, userBlocks, eq, and, inArray } 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";
|
||||
import { isBlockedEitherWay } from "@/lib/blocks";
|
||||
import { extractMentionedUsernames } from "@/lib/mentions";
|
||||
|
||||
const Schema = z.object({
|
||||
content: z.string().min(1).max(5000),
|
||||
@@ -107,5 +108,18 @@ export async function POST(req: NextRequest, { params }: Params) {
|
||||
void createNotification({ userId: recipe.authorId, type: "comment", actorId: session!.user.id, recipeId: id, commentId });
|
||||
}
|
||||
|
||||
const mentionedUsernames = extractMentionedUsernames(parsed.data.content);
|
||||
if (mentionedUsernames.length > 0) {
|
||||
const alreadyNotified = new Set([recipe.authorId, parent?.userId].filter(Boolean));
|
||||
const mentionedUsers = await db
|
||||
.select({ id: users.id, username: users.username })
|
||||
.from(users)
|
||||
.where(inArray(users.username, mentionedUsernames));
|
||||
for (const mentioned of mentionedUsers) {
|
||||
if (alreadyNotified.has(mentioned.id)) continue;
|
||||
void createNotification({ userId: mentioned.id, type: "mention", actorId: session!.user.id, recipeId: id, commentId });
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ id: commentId }, { status: 201 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user