feat: Discourse-style reply-to indicator (↩ @username)

- replies table: add reply_to_username column
- POST /api/replies: accept reply_to_username param
- GET /api/replies: LEFT JOIN users to return reply_to_username
- ReplyList: submit replyTo as reply_to_username on create
- UI: each reply shows '↩ 正在回复 @username' indicator (clickable,
  scrolls to target reply)
- data-username attribute on .reply-item for scroll targeting
This commit is contained in:
lezi-fun committed 2026-08-11 21:18:18 +08:00
1 parent 7bf9e37036
commit 4d0d5edec3
5 files changed
+56 -20

No files matched your search

+7 -5
View File
@@ -7,9 +7,11 @@ import { verifyCaptcha } from '@/lib/captcha';
export async function GET(req, { params }) {
try {
const r = await pool.query(
`SELECT r.*, u.username, u.avatar_url
`SELECT r.*, u.username, u.avatar_url,
rt.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
WHERE r.post_id = $1
ORDER BY r.created_at ASC`,
[(await params).id]
@@ -25,7 +27,7 @@ export async function POST(req, { params }) {
if (!user) {
return Response.json({ error: '请先登录' }, { status: 401 });
}
const { content, captcha_id, captcha_answer, captcha_sig } = await req.json();
const { content, captcha_id, captcha_answer, captcha_sig, reply_to_username } = await req.json();
if (!content || content.trim().length === 0) {
return Response.json({ error: '请填写回复内容' }, { status: 400 });
}
@@ -35,10 +37,10 @@ export async function POST(req, { params }) {
}
try {
const r = await pool.query(
`INSERT INTO replies (post_id, user_id, content)
VALUES ($1, $2, $3)
`INSERT INTO replies (post_id, user_id, content, reply_to_username)
VALUES ($1, $2, $3, $4)
RETURNING *`,
[(await params).id, user.id, content]
[(await params).id, user.id, content, reply_to_username || null]
);
await pool.query('UPDATE posts SET updated_at = NOW() WHERE id = $1', [(await params).id]);
return Response.json(r.rows[0]);
+22
View File
@@ -3098,3 +3098,25 @@ a.btn-secondary {
@media (hover: none) {
.reply-btn { opacity: 0.8; }
}
/* ===== 回复指示器(Discourse 风格:正在回复 @xxx) ===== */
.reply-to-indicator {
display: inline-flex;
align-items: center;
gap: 4px;
margin-top: 4px;
font-size: 12px;
color: var(--text-light);
cursor: pointer;
transition: color 0.15s;
}
.reply-to-indicator .icon {
stroke: var(--text-light);
}
.reply-to-indicator:hover {
color: var(--primary);
}
.reply-to-indicator strong {
color: var(--primary);
font-weight: 600;
}
+13 -2
View File
@@ -51,7 +51,7 @@ export default function ReplyList({ postId, onRefresh }) {
if (!captcha || !captchaInput.trim()) { toast('请填写验证码', 'warning'); return; }
try {
// 答案由服务端 HMAC 校验
await API.createReply(postId, content.trim(), { id: captcha.id, answer: captchaInput.trim(), sig: captcha.sig });
await API.createReply(postId, content.trim(), { id: captcha.id, answer: captchaInput.trim(), sig: captcha.sig }, replyTo);
API.logEvent('create_reply').catch(() => {});
setContent('');
setCaptchaInput('');
@@ -88,7 +88,7 @@ export default function ReplyList({ postId, onRefresh }) {
replies.map((r) => {
const rTime = r.created_at ? new Date(r.created_at).toLocaleString('zh-CN') : '';
return (
<div key={r.id} className="reply-item">
<div key={r.id} className="reply-item" data-username={r.username}>
<div className="reply-header">
<img src={r.avatar_url || avatar(r.username)} className="reply-avatar" alt="" />
<span
@@ -109,6 +109,17 @@ export default function ReplyList({ postId, onRefresh }) {
)}
</div>
<div className="reply-content" dangerouslySetInnerHTML={{ __html: renderMarkdown(r.content) }} />
{r.reply_to_username && (
<div
className="reply-to-indicator"
onClick={() => {
const target = document.querySelector(`.reply-item[data-username="${r.reply_to_username}"]`);
if (target) target.scrollIntoView({ behavior: 'smooth', block: 'center' });
}}
>
<Icon name="reply" size={11} /> 正在回复 <strong>@{r.reply_to_username}</strong>
</div>
)}
</div>
);
})
+2 -2
View File
@@ -77,10 +77,10 @@ export const API = {
// 回复
getReplies(postId) { return apiFetch('/posts/' + postId + '/replies'); },
createReply(postId, content, captcha) {
createReply(postId, content, captcha, replyToUsername) {
return apiFetch('/posts/' + postId + '/replies', {
method: 'POST',
body: JSON.stringify({ content, captcha_id: captcha?.id, captcha_answer: captcha?.answer, captcha_sig: captcha?.sig }),
body: JSON.stringify({ content, captcha_id: captcha?.id, captcha_answer: captcha?.answer, captcha_sig: captcha?.sig, reply_to_username: replyToUsername || null }),
});
},
deleteReply(replyId) { return apiFetch('/replies/' + replyId, { method: 'DELETE' }); },
+12 -11
View File
@@ -35,6 +35,7 @@ CREATE TABLE IF NOT EXISTS replies (
post_id UUID NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
content TEXT NOT NULL,
reply_to_id UUID REFERENCES replies(id) ON DELETE SET NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
@@ -227,14 +228,14 @@ CREATE TABLE IF NOT EXISTS recovery_codes (
);
CREATE INDEX IF NOT EXISTS idx_recovery_codes_user ON recovery_codes(user_id);
-- ============================================================
-- 超级管理员自动设立(数据库兼容,幂等)
-- 若没有任何 admin,将最早注册的用户提升为超级管理员。
-- 不依赖自增 uid(users 主键是 UUID),任何现有数据库都可安全执行。
-- ============================================================
UPDATE users SET role = 'admin'
WHERE role <> 'admin'
AND NOT EXISTS (SELECT 1 FROM users WHERE role = 'admin')
AND id = (SELECT id FROM users ORDER BY created_at ASC, id ASC LIMIT 1);
-- ============================================================
-- 超级管理员自动设立(数据库兼容,幂等)
-- 若没有任何 admin,将最早注册的用户提升为超级管理员。
-- 不依赖自增 uid(users 主键是 UUID),任何现有数据库都可安全执行。
-- ============================================================
UPDATE users SET role = 'admin'
WHERE role <> 'admin'
AND NOT EXISTS (SELECT 1 FROM users WHERE role = 'admin')
AND id = (SELECT id FROM users ORDER BY created_at ASC, id ASC LIMIT 1);