From 4d0d5edec33b0260c8ac86048138762b63796c49 Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Tue, 11 Aug 2026 21:18:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Discourse-style=20reply-to=20indicator?= =?UTF-8?q?=20(=E2=86=A9=20@username)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- app/api/posts/[id]/replies/route.js | 12 +++++++----- app/globals.css | 22 ++++++++++++++++++++++ components/ReplyList.js | 15 +++++++++++++-- lib/api.js | 4 ++-- schema.sql | 23 ++++++++++++----------- 5 files changed, 56 insertions(+), 20 deletions(-) diff --git a/app/api/posts/[id]/replies/route.js b/app/api/posts/[id]/replies/route.js index 248ed13..77c7e5a 100644 --- a/app/api/posts/[id]/replies/route.js +++ b/app/api/posts/[id]/replies/route.js @@ -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]); diff --git a/app/globals.css b/app/globals.css index 12f29b4..f92a365 100644 --- a/app/globals.css +++ b/app/globals.css @@ -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; +} diff --git a/components/ReplyList.js b/components/ReplyList.js index e91a2bb..a028afc 100644 --- a/components/ReplyList.js +++ b/components/ReplyList.js @@ -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 ( -
+
+ {r.reply_to_username && ( +
{ + const target = document.querySelector(`.reply-item[data-username="${r.reply_to_username}"]`); + if (target) target.scrollIntoView({ behavior: 'smooth', block: 'center' }); + }} + > + 正在回复 @{r.reply_to_username} +
+ )}
); }) diff --git a/lib/api.js b/lib/api.js index c2d44f8..fe5056d 100644 --- a/lib/api.js +++ b/lib/api.js @@ -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' }); }, diff --git a/schema.sql b/schema.sql index a2b4e11..f833902 100644 --- a/schema.sql +++ b/schema.sql @@ -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);