Files
forumlify/app/api/messages/[id]/read/route.js
T
lezi-fun 300ad5959c 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)
2026-08-09 21:01:55 +08:00

25 lines
758 B
JavaScript

// 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 });
}
}