diff --git a/app/api/conversations/[id]/messages/route.js b/app/api/conversations/[id]/messages/route.js new file mode 100644 index 0000000..c0b1c16 --- /dev/null +++ b/app/api/conversations/[id]/messages/route.js @@ -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 }); + } +} diff --git a/app/api/conversations/route.js b/app/api/conversations/route.js new file mode 100644 index 0000000..e738893 --- /dev/null +++ b/app/api/conversations/route.js @@ -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 }); + } +} diff --git a/app/api/messages/[id]/read/route.js b/app/api/messages/[id]/read/route.js new file mode 100644 index 0000000..58e3377 --- /dev/null +++ b/app/api/messages/[id]/read/route.js @@ -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 }); + } +} diff --git a/app/page.js b/app/page.js index f991022..24fc35d 100644 --- a/app/page.js +++ b/app/page.js @@ -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 ( <> - +
+ +
+ +
+
diff --git a/components/Navbar.js b/components/Navbar.js index f20c51f..302c231 100644 --- a/components/Navbar.js +++ b/components/Navbar.js @@ -22,6 +22,13 @@ export default function Navbar({ onOpenModal }) { navigate(page); }; + const openMessages = (e) => { + e.preventDefault(); + setMenuOpen(false); + // 打开私信列表(ChatManager 监听) + window.dispatchEvent(new CustomEvent('forumlify-open-messages')); + }; + return (