feat: port private messaging (私信) from upstream

- Add conversations/messages tables to schema.sql (upstream missed them)
- API: GET/POST /api/conversations, GET/POST /api/conversations/[id]/messages, PUT /api/messages/[id]/read
- Chat UI: conversation list modal, chat window with 3s polling, unread badge
- Navbar message entry opens DM list; user profile has 发私信 button
- Access control: only conversation participants (403 otherwise)
This commit is contained in:
lezi-fun committed 2026-08-09 21:01:55 +08:00
1 parent 5ef8d4b1ff
commit 300ad5959c
11 files changed
+522 -3

No files matched your search

@@ -0,0 +1,78 @@
// GET /api/conversations/[id]/messages, POST /api/conversations/[id]/messages
import pool from '@/lib/db';
import { getUser } from '@/lib/auth';
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
export async function GET(req, { params }) {
const user = getUser(req);
if (!user) {
return Response.json({ error: '请先登录' }, { status: 401 });
}
if (!UUID_RE.test(params.id)) {
return Response.json({ error: '无权限访问此会话' }, { status: 403 });
}
try {
const check = await pool.query(`
SELECT id FROM conversations
WHERE id = $1 AND (user1_id = $2 OR user2_id = $2)
`, [params.id, user.id]);
if (check.rows.length === 0) {
return Response.json({ error: '无权限访问此会话' }, { status: 403 });
}
const r = await pool.query(`
SELECT
m.*,
u.username as sender_username,
u.avatar_url as sender_avatar_url
FROM messages m
JOIN users u ON m.sender_id = u.id
WHERE m.conversation_id = $1
ORDER BY m.created_at ASC
`, [params.id]);
await pool.query(`
UPDATE messages SET is_read = true
WHERE conversation_id = $1 AND sender_id != $2 AND is_read = false
`, [params.id, user.id]);
return Response.json(r.rows);
} catch {
return Response.json({ error: '服务器错误' }, { status: 500 });
}
}
export async function POST(req, { params }) {
const user = getUser(req);
if (!user) {
return Response.json({ error: '请先登录' }, { status: 401 });
}
if (!UUID_RE.test(params.id)) {
return Response.json({ error: '无权限访问此会话' }, { status: 403 });
}
const { content } = await req.json();
if (!content || content.trim().length === 0) {
return Response.json({ error: '请填写消息内容' }, { status: 400 });
}
try {
const check = await pool.query(`
SELECT id FROM conversations
WHERE id = $1 AND (user1_id = $2 OR user2_id = $2)
`, [params.id, user.id]);
if (check.rows.length === 0) {
return Response.json({ error: '无权限访问此会话' }, { status: 403 });
}
const r = await pool.query(`
INSERT INTO messages (conversation_id, sender_id, content)
VALUES ($1, $2, $3)
RETURNING *
`, [params.id, user.id, content]);
await pool.query('UPDATE conversations SET last_message_at = NOW() WHERE id = $1', [params.id]);
const userInfo = await pool.query('SELECT username, avatar_url FROM users WHERE id = $1', [user.id]);
return Response.json({
...r.rows[0],
sender_username: userInfo.rows[0].username,
sender_avatar_url: userInfo.rows[0].avatar_url,
});
} catch {
return Response.json({ error: '发送失败,请稍后重试' }, { status: 500 });
}
}
+68
View File
@@ -0,0 +1,68 @@
// GET /api/conversations, POST /api/conversations
import pool from '@/lib/db';
import { getUser } from '@/lib/auth';
export async function GET(req) {
const user = getUser(req);
if (!user) {
return Response.json({ error: '请先登录' }, { status: 401 });
}
try {
const r = await pool.query(`
SELECT
c.id,
c.user1_id,
c.user2_id,
c.last_message_at,
c.created_at,
u.id as other_user_id,
u.username as other_username,
u.avatar_url as other_avatar_url,
(SELECT COUNT(*) FROM messages WHERE conversation_id = c.id AND sender_id != $1 AND is_read = false) as unread_count,
(SELECT content FROM messages WHERE conversation_id = c.id ORDER BY created_at DESC LIMIT 1) as last_message,
(SELECT created_at FROM messages WHERE conversation_id = c.id ORDER BY created_at DESC LIMIT 1) as last_message_time
FROM conversations c
JOIN users u ON (u.id = c.user1_id OR u.id = c.user2_id) AND u.id != $1
WHERE c.user1_id = $1 OR c.user2_id = $1
ORDER BY c.last_message_at DESC
`, [user.id]);
return Response.json(r.rows);
} catch {
return Response.json({ error: '服务器错误' }, { status: 500 });
}
}
export async function POST(req) {
const user = getUser(req);
if (!user) {
return Response.json({ error: '请先登录' }, { status: 401 });
}
const { other_user_id } = await req.json();
if (!other_user_id) {
return Response.json({ error: '缺少对方用户ID' }, { status: 400 });
}
if (other_user_id === user.id) {
return Response.json({ error: '不能与自己私信' }, { status: 400 });
}
try {
const userCheck = await pool.query('SELECT id FROM users WHERE id = $1', [other_user_id]);
if (userCheck.rows.length === 0) {
return Response.json({ error: '用户不存在' }, { status: 404 });
}
const existing = await pool.query(`
SELECT id FROM conversations
WHERE (user1_id = $1 AND user2_id = $2) OR (user1_id = $2 AND user2_id = $1)
`, [user.id, other_user_id]);
if (existing.rows.length > 0) {
return Response.json({ id: existing.rows[0].id });
}
const r = await pool.query(`
INSERT INTO conversations (user1_id, user2_id)
VALUES ($1, $2)
RETURNING id
`, [user.id, other_user_id]);
return Response.json({ id: r.rows[0].id });
} catch {
return Response.json({ error: '服务器错误' }, { status: 500 });
}
}
+24
View File
@@ -0,0 +1,24 @@
// PUT /api/messages/[id]/read — 标记消息已读
import pool from '@/lib/db';
import { getUser } from '@/lib/auth';
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
export async function PUT(req, { params }) {
const user = getUser(req);
if (!user) {
return Response.json({ error: '请先登录' }, { status: 401 });
}
if (!UUID_RE.test(params.id)) {
return Response.json({ error: '服务器错误' }, { status: 500 });
}
try {
await pool.query(`
UPDATE messages SET is_read = true
WHERE id = $1 AND sender_id != $2
`, [params.id, user.id]);
return Response.json({ success: true });
} catch {
return Response.json({ error: '服务器错误' }, { status: 500 });
}
}
+7 -1
View File
@@ -11,6 +11,7 @@ import NewPost from '@/components/NewPost';
import AdminPage from '@/components/AdminPage';
import SettingsPage from '@/components/SettingsPage';
import UserProfile from '@/components/UserProfile';
import ChatManager from '@/components/chat/ChatManager';
import Modals from '@/components/Modals';
function HomeInner() {
@@ -23,7 +24,12 @@ function HomeInner() {
return (
<>
<Navbar onOpenModal={openModal} />
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
<Navbar onOpenModal={openModal} style={{ flex: 1 }} />
<div style={{ paddingRight: 16 }}>
<ChatManager />
</div>
</div>
<div id="app" style={{ display: view === 'feed' ? 'flex' : 'none' }}>
<Sidebar />