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
@@ -0,0 +1,22 @@
|
||||
import pool from '@/lib/db';
|
||||
import { getUser } from '@/lib/auth';
|
||||
import { ensureUserBlocksSchema, UUID_RE } from '@/lib/user-blocks';
|
||||
|
||||
export async function GET(req, { params }) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
const targetId = (await params).id;
|
||||
if (!UUID_RE.test(targetId)) return Response.json({ error: '用户不存在' }, { status: 404 });
|
||||
if (targetId === user.id) return Response.json({ blocked_by_me: false, blocked_me: false, self: true });
|
||||
try {
|
||||
await ensureUserBlocksSchema();
|
||||
const result = await pool.query(`
|
||||
SELECT
|
||||
EXISTS(SELECT 1 FROM user_blocks WHERE blocker_id = $1 AND blocked_id = $2) AS blocked_by_me,
|
||||
EXISTS(SELECT 1 FROM user_blocks WHERE blocker_id = $2 AND blocked_id = $1) AS blocked_me
|
||||
`, [user.id, targetId]);
|
||||
return Response.json(result.rows[0], { headers: { 'Cache-Control': 'private, no-store' } });
|
||||
} catch {
|
||||
return Response.json({ error: '服务器错误' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import pool from '@/lib/db';
|
||||
import { getUser } from '@/lib/auth';
|
||||
import { ensureUserBlocksSchema, UUID_RE } from '@/lib/user-blocks';
|
||||
|
||||
export async function POST(req, { params }) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
const targetId = (await params).id;
|
||||
if (!UUID_RE.test(targetId) || targetId === user.id) {
|
||||
return Response.json({ error: '不能屏蔽该用户' }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
await ensureUserBlocksSchema();
|
||||
const result = await pool.query(`
|
||||
INSERT INTO user_blocks (blocker_id, blocked_id)
|
||||
SELECT $1, id FROM users WHERE id = $2
|
||||
ON CONFLICT (blocker_id, blocked_id) DO NOTHING RETURNING id
|
||||
`, [user.id, targetId]);
|
||||
if (!result.rowCount) {
|
||||
const target = await pool.query('SELECT id FROM users WHERE id = $1', [targetId]);
|
||||
if (!target.rowCount) return Response.json({ error: '用户不存在' }, { status: 404 });
|
||||
}
|
||||
return Response.json({ success: true, blocked: true });
|
||||
} catch {
|
||||
return Response.json({ error: '操作失败,请稍后重试' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(req, { params }) {
|
||||
const user = getUser(req);
|
||||
if (!user) return Response.json({ error: '请先登录' }, { status: 401 });
|
||||
const targetId = (await params).id;
|
||||
if (!UUID_RE.test(targetId)) return Response.json({ error: '用户不存在' }, { status: 404 });
|
||||
try {
|
||||
await ensureUserBlocksSchema();
|
||||
await pool.query('DELETE FROM user_blocks WHERE blocker_id = $1 AND blocked_id = $2', [user.id, targetId]);
|
||||
return Response.json({ success: true, blocked: false });
|
||||
} catch {
|
||||
return Response.json({ error: '操作失败,请稍后重试' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import pool from '@/lib/db';
|
||||
import { getUser } from '@/lib/auth';
|
||||
import { ensureUserBlocksSchema } from '@/lib/user-blocks';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
export async function GET(req) {
|
||||
const url = new URL(req.url);
|
||||
const keyword = (url.searchParams.get('q') || '').trim();
|
||||
if (!keyword) return Response.json({ error: '请输入搜索关键词' }, { status: 400 });
|
||||
if (keyword.length > 50) return Response.json({ error: '搜索关键词过长(最多 50 字)' }, { status: 400 });
|
||||
|
||||
const limit = Math.min(20, Math.max(1, parseInt(url.searchParams.get('limit'), 10) || 10));
|
||||
const user = getUser(req);
|
||||
try {
|
||||
const values = [`%${keyword.replace(/[\\%_]/g, (character) => `\\${character}`)}%`,
|
||||
`${keyword.replace(/[\\%_]/g, (character) => `\\${character}`)}%`];
|
||||
let filter = '';
|
||||
if (user) {
|
||||
await ensureUserBlocksSchema();
|
||||
values.push(user.id);
|
||||
filter = ` AND u.id <> $3 AND NOT EXISTS (
|
||||
SELECT 1 FROM user_blocks b WHERE b.blocker_id = $3 AND b.blocked_id = u.id
|
||||
)`;
|
||||
}
|
||||
values.push(limit);
|
||||
const result = await pool.query(`
|
||||
SELECT u.id, u.username, u.avatar_url, u.role, u.signature
|
||||
FROM users u WHERE u.username ILIKE $1${filter}
|
||||
ORDER BY CASE WHEN u.username ILIKE $2 THEN 0 ELSE 1 END,
|
||||
LENGTH(u.username), u.username
|
||||
LIMIT $${values.length}
|
||||
`, values);
|
||||
return Response.json({ data: result.rows, query: keyword }, { headers: { 'Cache-Control': 'private, no-store' } });
|
||||
} catch {
|
||||
return Response.json({ error: '服务器错误' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user