feat: port user profile feature from upstream

- GET /api/users?username= filter (admin)
- GET /api/posts?user_id= filter
- Add UserProfile page (profile card + user's posts), reachable via ?user=<username>
- Clickable usernames on feed, post detail, replies
This commit is contained in:
lezi-fun committed 2026-08-09 18:30:49 +08:00
1 parent e6ba5507ae
commit 5ef8d4b1ff
9 files changed
+166 -17

No files matched your search

+12 -4
View File
@@ -1,12 +1,14 @@
// GET /api/posts, POST /api/posts
// GET /api/posts, POST /api/posts — 支持 ?sort= & ?user_id= 筛选
import pool from '@/lib/db';
import { getUser } from '@/lib/auth';
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');
try {
const r = await pool.query(`
let query = `
SELECT
p.*,
u.username,
@@ -14,8 +16,14 @@ export async function GET(req) {
(SELECT COUNT(*) FROM replies WHERE post_id = p.id) as reply_count
FROM posts p
JOIN users u ON p.user_id = u.id
ORDER BY ${sort} DESC
`);
`;
const params = [];
if (userId) {
query += ' WHERE p.user_id = $1';
params.push(userId);
}
query += ` ORDER BY ${sort} DESC`;
const r = await pool.query(query, params);
return Response.json(r.rows);
} catch {
return Response.json({ error: '服务器错误' }, { status: 500 });
+13 -4
View File
@@ -1,4 +1,4 @@
// GET /api/users (admin)
// GET /api/users (admin) — 支持 ?username= 按用户名查询
import pool from '@/lib/db';
import { getUser, requireAdmin } from '@/lib/auth';
@@ -6,10 +6,19 @@ export async function GET(req) {
const user = getUser(req);
const forbidden = await requireAdmin(user);
if (forbidden) return forbidden;
const url = new URL(req.url);
const username = url.searchParams.get('username');
try {
const r = await pool.query(
'SELECT id, username, avatar_url, bio, role, created_at FROM users ORDER BY created_at DESC'
);
let query = 'SELECT id, username, avatar_url, bio, role, created_at FROM users';
const params = [];
if (username) {
query += ' WHERE username = $1';
params.push(username);
}
query += ' ORDER BY created_at DESC';
const r = await pool.query(query, params);
return Response.json(r.rows);
} catch {
return Response.json({ error: '服务器错误' }, { status: 500 });