fix: reply chain uses reply_to_id (UUID) instead of reply_to_username

- reply_to_id UUID FK: precise parent tracking (username was ambiguous
  when same user wrote multiple replies in a chain)
- GET /api/replies: JOIN replies + users to return reply_to_username
- POST /api/replies: accept reply_to_id (UUID) instead of reply_to_username
- Frontend depth calc: follow reply_to_id chain (not username)
- Preview bar: resolve username from local replies by ID
- Indicator scroll: target by data-reply-id
This commit is contained in:
lezi-fun committed 2026-08-11 21:38:45 +08:00
1 parent 59d5fb89c0
commit 6c100ff3a1
3 files changed
+22 -18

No files matched your search

+6 -5
View File
@@ -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]);
+14 -11
View File
@@ -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 (
<>
<div className="post-reply-count" style={{ marginTop: 20, fontSize: 14, color: '#64748b' }}>
@@ -104,7 +107,7 @@ export default function ReplyList({ postId, onRefresh }) {
const rTime = r.created_at ? new Date(r.created_at).toLocaleString('zh-CN') : '';
const nestDepth = Math.min(replyDepths[r.id] || 0, 5);
return (
<div key={r.id} className="reply-item" data-username={r.username} style={{ marginLeft: nestDepth ? nestDepth * 18 : 0 }}>
<div key={r.id} className="reply-item" data-username={r.username} data-reply-id={r.id} style={{ marginLeft: nestDepth ? nestDepth * 18 : 0 }}>
<div className="reply-header">
<img src={r.avatar_url || avatar(r.username)} className="reply-avatar" alt="" />
<span
@@ -115,7 +118,7 @@ export default function ReplyList({ postId, onRefresh }) {
{r.username || '匿名用户'}
</span>
<span className="reply-time">{rTime}</span>
<button className="reply-btn" onClick={() => startReply(r.username)} title="回复">
<button className="reply-btn" onClick={() => startReply(r.id)} title="回复">
<Icon name="reply" size={13} />
</button>
{currentUser && (currentUser.id === r.user_id || currentUser.role === 'admin') && (
@@ -129,7 +132,7 @@ export default function ReplyList({ postId, onRefresh }) {
<div
className="reply-to-indicator"
onClick={() => {
const target = document.querySelector(`.reply-item[data-username="${r.reply_to_username}"]`);
const target = document.querySelector(`.reply-item[data-reply-id="${r.reply_to_id}"]`);
if (target) target.scrollIntoView({ behavior: 'smooth', block: 'center' });
}}
>
@@ -144,11 +147,11 @@ export default function ReplyList({ postId, onRefresh }) {
<div id="replyArea" ref={replyAreaRef} style={{ marginTop: 24, borderTop: '1px solid var(--border)', paddingTop: 20 }}>
<h3 style={{ fontSize: 16, marginBottom: 12 }}><Icon name="message" size={16} /> 发表回复</h3>
{replyTo && (
{replyTo && replyToObj && (
<div className="reply-preview-bar">
<Icon name="reply" size={13} />
<span>回复</span>
<strong>@{replyTo}</strong>
<strong>@{replyToObj.username}</strong>
<button className="reply-preview-cancel" onClick={() => setReplyTo(null)}>取消</button>
</div>
)}
+2 -2
View File
@@ -77,10 +77,10 @@ export const API = {
// 回复
getReplies(postId) { return apiFetch('/posts/' + postId + '/replies'); },
createReply(postId, content, captcha, replyToUsername) {
createReply(postId, content, captcha, replyToId) {
return apiFetch('/posts/' + postId + '/replies', {
method: 'POST',
body: JSON.stringify({ content, captcha_id: captcha?.id, captcha_answer: captcha?.answer, captcha_sig: captcha?.sig, reply_to_username: replyToUsername || null }),
body: JSON.stringify({ content, captcha_id: captcha?.id, captcha_answer: captcha?.answer, captcha_sig: captcha?.sig, reply_to_id: replyToId || null }),
});
},
deleteReply(replyId) { return apiFetch('/replies/' + replyId, { method: 'DELETE' }); },