diff --git a/app/api/posts/[id]/replies/route.js b/app/api/posts/[id]/replies/route.js index 77c7e5a..5f13b29 100644 --- a/app/api/posts/[id]/replies/route.js +++ b/app/api/posts/[id]/replies/route.js @@ -8,10 +8,11 @@ export async function GET(req, { params }) { try { const r = await pool.query( `SELECT r.*, u.username, u.avatar_url, - rt.username AS reply_to_username + ru.username AS reply_to_username FROM replies r JOIN users u ON r.user_id = u.id - LEFT JOIN users rt ON r.reply_to_username = rt.username + LEFT JOIN replies rt ON r.reply_to_id = rt.id + LEFT JOIN users ru ON rt.user_id = ru.id WHERE r.post_id = $1 ORDER BY r.created_at ASC`, [(await params).id] @@ -27,7 +28,7 @@ export async function POST(req, { params }) { if (!user) { return Response.json({ error: '请先登录' }, { status: 401 }); } - const { content, captcha_id, captcha_answer, captcha_sig, reply_to_username } = await req.json(); + const { content, captcha_id, captcha_answer, captcha_sig, reply_to_id } = await req.json(); if (!content || content.trim().length === 0) { return Response.json({ error: '请填写回复内容' }, { status: 400 }); } @@ -37,10 +38,10 @@ export async function POST(req, { params }) { } try { const r = await pool.query( - `INSERT INTO replies (post_id, user_id, content, reply_to_username) + `INSERT INTO replies (post_id, user_id, content, reply_to_id) VALUES ($1, $2, $3, $4) RETURNING *`, - [(await params).id, user.id, content, reply_to_username || null] + [(await params).id, user.id, content, reply_to_id || null] ); await pool.query('UPDATE posts SET updated_at = NOW() WHERE id = $1', [(await params).id]); return Response.json(r.rows[0]); diff --git a/components/ReplyList.js b/components/ReplyList.js index 6046e76..2071ad9 100644 --- a/components/ReplyList.js +++ b/components/ReplyList.js @@ -26,9 +26,9 @@ export default function ReplyList({ postId, onRefresh }) { const replyAreaRef = useRef(null); const replyInputRef = useRef(null); - // 点击"回复":记录目标用户,滚动到回复框并聚焦 - const startReply = (username) => { - setReplyTo(username); + // 点击"回复":记录目标回复 ID,滚动到回复框并聚焦 + const startReply = (replyId) => { + setReplyTo(replyId); setTimeout(() => { replyAreaRef.current?.scrollIntoView({ behavior: 'smooth', block: 'center' }); replyInputRef.current?.focus(); @@ -76,13 +76,13 @@ export default function ReplyList({ postId, onRefresh }) { } }; - // 计算每条回复的嵌套深度(按 reply_to_username 追踪回复链) + // 计算每条回复的嵌套深度(按 reply_to_id 精确追踪回复链) const replyDepths = (() => { const map = {}; const getDepth = (r) => { if (map[r.id] !== undefined) return map[r.id]; - if (!r.reply_to_username) { map[r.id] = 0; return 0; } - const parent = replies.find((x) => x.username === r.reply_to_username); + if (!r.reply_to_id) { map[r.id] = 0; return 0; } + const parent = replies.find((x) => x.id === r.reply_to_id); const d = parent ? getDepth(parent) + 1 : 0; map[r.id] = d; return d; @@ -91,6 +91,9 @@ export default function ReplyList({ postId, onRefresh }) { return map; })(); + // 正在回复的目标回复对象(用于显示 @用户名) + const replyToObj = replyTo ? replies.find((x) => x.id === replyTo) : null; + return ( <>