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
+19
-6
@@ -5,6 +5,7 @@ import { jsonWithEtag } from '@/lib/http-cache';
|
||||
import { verifyCaptcha } from '@/lib/captcha';
|
||||
import { logAudit } from '@/lib/audit';
|
||||
import { ensurePostNumberSchema } from '@/lib/post-reference';
|
||||
import { ensureUserBlocksSchema } from '@/lib/user-blocks';
|
||||
|
||||
// 避免 GET 被静态优化导致写方法 405(动态接口,不能缓存)
|
||||
export const dynamic = 'force-dynamic';
|
||||
@@ -14,6 +15,7 @@ export async function GET(req) {
|
||||
const url = new URL(req.url);
|
||||
const sort = url.searchParams.get('sort') === 'hot' ? 'updated_at' : 'created_at';
|
||||
const userId = url.searchParams.get('user_id');
|
||||
const viewer = getUser(req);
|
||||
const page = Math.max(1, parseInt(url.searchParams.get('page')) || 1);
|
||||
// 分页边界:limit 封顶 50,page 封顶 10000(防深分页 DoS)
|
||||
const limit = Math.min(50, Math.max(1, parseInt(url.searchParams.get('limit')) || 20));
|
||||
@@ -24,11 +26,19 @@ export async function GET(req) {
|
||||
try {
|
||||
await ensurePostNumberSchema();
|
||||
const params = [];
|
||||
let where = '';
|
||||
const conditions = [];
|
||||
if (userId) {
|
||||
where = ' WHERE p.user_id = $1';
|
||||
(await params).push(userId);
|
||||
params.push(userId);
|
||||
conditions.push(`p.user_id = $${params.length}`);
|
||||
}
|
||||
if (viewer) {
|
||||
await ensureUserBlocksSchema();
|
||||
params.push(viewer.id);
|
||||
conditions.push(`NOT EXISTS (
|
||||
SELECT 1 FROM user_blocks b WHERE b.blocker_id = $${params.length} AND b.blocked_id = p.user_id
|
||||
)`);
|
||||
}
|
||||
const where = conditions.length ? ` WHERE ${conditions.join(' AND ')}` : '';
|
||||
|
||||
const countResult = await pool.query(`SELECT COUNT(*) as total FROM posts p${where}`, params);
|
||||
const total = parseInt(countResult.rows[0]?.total || 0);
|
||||
@@ -44,13 +54,16 @@ export async function GET(req) {
|
||||
JOIN users u ON p.user_id = u.id
|
||||
${where}
|
||||
ORDER BY is_pinned DESC, pinned_at DESC NULLS LAST, ${sort} DESC
|
||||
LIMIT $${(await params).length + 1} OFFSET $${(await params).length + 2}
|
||||
LIMIT $${params.length + 1} OFFSET $${params.length + 2}
|
||||
`, [...params, limit, offset]);
|
||||
|
||||
return jsonWithEtag(req, {
|
||||
const payload = {
|
||||
data: r.rows,
|
||||
pagination: { page, limit, total, totalPages: Math.ceil(total / limit) },
|
||||
});
|
||||
};
|
||||
return viewer
|
||||
? Response.json(payload, { headers: { 'Cache-Control': 'private, no-store' } })
|
||||
: jsonWithEtag(req, payload);
|
||||
} catch {
|
||||
return Response.json({ error: '服务器错误' }, { status: 500 });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user