feat: follow lite forum updates
This commit is contained in:
1 parent
f59739e36f
commit
b42944b687
38 files changed
+1433
-71
No files matched your search
@@ -1,6 +1,7 @@
|
||||
// GET /api/conversations/[id]/messages, POST /api/conversations/[id]/messages
|
||||
import pool from '@/lib/db';
|
||||
import { getUser } from '@/lib/auth';
|
||||
import { ensureUserBlocksSchema } from '@/lib/user-blocks';
|
||||
|
||||
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||||
|
||||
@@ -49,30 +50,60 @@ export async function POST(req, { params }) {
|
||||
return Response.json({ error: '无权限访问此会话' }, { status: 403 });
|
||||
}
|
||||
const { content } = await req.json();
|
||||
if (!content || content.trim().length === 0) {
|
||||
return Response.json({ error: '请填写消息内容' }, { status: 400 });
|
||||
if (typeof content !== 'string' || !content.trim() || content.length > 10000) {
|
||||
return Response.json({ error: '消息长度应为1到10000个字符' }, { status: 400 });
|
||||
}
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
const check = await pool.query(`
|
||||
SELECT id FROM conversations
|
||||
await ensureUserBlocksSchema();
|
||||
await client.query('BEGIN');
|
||||
const check = await client.query(`
|
||||
SELECT id, user1_id, user2_id FROM conversations
|
||||
WHERE id = $1 AND (user1_id = $2 OR user2_id = $2)
|
||||
FOR UPDATE
|
||||
`, [(await params).id, user.id]);
|
||||
if (check.rows.length === 0) {
|
||||
await client.query('ROLLBACK');
|
||||
return Response.json({ error: '无权限访问此会话' }, { status: 403 });
|
||||
}
|
||||
const r = await pool.query(`
|
||||
const conversation = check.rows[0];
|
||||
const otherId = conversation.user1_id === user.id ? conversation.user2_id : conversation.user1_id;
|
||||
const blocked = await client.query(`
|
||||
SELECT 1 FROM user_blocks
|
||||
WHERE (blocker_id = $1 AND blocked_id = $2)
|
||||
OR (blocker_id = $2 AND blocked_id = $1)
|
||||
LIMIT 1
|
||||
`, [otherId, user.id]);
|
||||
if (blocked.rowCount) {
|
||||
await client.query('ROLLBACK');
|
||||
return Response.json({ error: '无法向该用户发送消息' }, { status: 403 });
|
||||
}
|
||||
const stats = await client.query(`
|
||||
SELECT COUNT(*) FILTER (WHERE sender_id = $2) AS mine,
|
||||
COUNT(*) FILTER (WHERE sender_id = $3) AS theirs
|
||||
FROM messages WHERE conversation_id = $1
|
||||
`, [(await params).id, user.id, otherId]);
|
||||
if (Number(stats.rows[0].theirs) === 0 && Number(stats.rows[0].mine) >= 3) {
|
||||
await client.query('ROLLBACK');
|
||||
return Response.json({ error: '对方回复前最多发送 3 条消息,请等待对方回复', limit_reached: true, limit: 3 }, { status: 429 });
|
||||
}
|
||||
const r = await client.query(`
|
||||
INSERT INTO messages (conversation_id, sender_id, content)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING *
|
||||
`, [(await params).id, user.id, content]);
|
||||
await pool.query('UPDATE conversations SET last_message_at = NOW() WHERE id = $1', [(await params).id]);
|
||||
const userInfo = await pool.query('SELECT username, avatar_url FROM users WHERE id = $1', [user.id]);
|
||||
`, [(await params).id, user.id, content.trim()]);
|
||||
await client.query('UPDATE conversations SET last_message_at = NOW() WHERE id = $1', [(await params).id]);
|
||||
const userInfo = await client.query('SELECT username, avatar_url FROM users WHERE id = $1', [user.id]);
|
||||
await client.query('COMMIT');
|
||||
return Response.json({
|
||||
...r.rows[0],
|
||||
sender_username: userInfo.rows[0].username,
|
||||
sender_avatar_url: userInfo.rows[0].avatar_url,
|
||||
});
|
||||
} catch {
|
||||
await client.query('ROLLBACK').catch(() => {});
|
||||
return Response.json({ error: '发送失败,请稍后重试' }, { status: 500 });
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
// GET /api/conversations, POST /api/conversations
|
||||
import pool from '@/lib/db';
|
||||
import { getUser } from '@/lib/auth';
|
||||
import { ensureUserBlocksSchema, hasBlocked, UUID_RE } from '@/lib/user-blocks';
|
||||
|
||||
// 避免 GET 被静态优化导致写方法 405(动态接口,不能缓存)
|
||||
export const dynamic = 'force-dynamic';
|
||||
@@ -42,17 +43,21 @@ export async function POST(req) {
|
||||
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 (!UUID_RE.test(other_user_id || '')) {
|
||||
return Response.json({ error: '对方用户ID无效' }, { status: 400 });
|
||||
}
|
||||
if (other_user_id === user.id) {
|
||||
return Response.json({ error: '不能与自己私信' }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
await ensureUserBlocksSchema();
|
||||
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 });
|
||||
}
|
||||
if (await hasBlocked(other_user_id, user.id) || await hasBlocked(user.id, other_user_id)) {
|
||||
return Response.json({ error: '无法向该用户发起私信' }, { status: 403 });
|
||||
}
|
||||
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)
|
||||
@@ -62,7 +67,9 @@ export async function POST(req) {
|
||||
}
|
||||
const r = await pool.query(`
|
||||
INSERT INTO conversations (user1_id, user2_id)
|
||||
VALUES ($1, $2)
|
||||
VALUES (LEAST($1::uuid, $2::uuid), GREATEST($1::uuid, $2::uuid))
|
||||
ON CONFLICT (user1_id, user2_id)
|
||||
DO UPDATE SET user1_id = EXCLUDED.user1_id
|
||||
RETURNING id
|
||||
`, [user.id, other_user_id]);
|
||||
return Response.json({ id: r.rows[0].id });
|
||||
|
||||
Reference in new issue
Block a user