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:
1 parent
e6ba5507ae
commit
5ef8d4b1ff
9 files changed
+166
-17
No files matched your search
+12
-4
@@ -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
@@ -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 });
|
||||
|
||||
+3
-1
@@ -10,10 +10,11 @@ import PostDetail from '@/components/PostDetail';
|
||||
import NewPost from '@/components/NewPost';
|
||||
import AdminPage from '@/components/AdminPage';
|
||||
import SettingsPage from '@/components/SettingsPage';
|
||||
import UserProfile from '@/components/UserProfile';
|
||||
import Modals from '@/components/Modals';
|
||||
|
||||
function HomeInner() {
|
||||
const { view, currentPostId } = useApp();
|
||||
const { view, currentPostId, currentUsername } = useApp();
|
||||
const [modal, setModal] = useState(null); // null | login | register | report
|
||||
const [reportPostId, setReportPostId] = useState(null);
|
||||
|
||||
@@ -32,6 +33,7 @@ function HomeInner() {
|
||||
{/* 独立页面 */}
|
||||
<div style={{ display: view !== 'feed' ? 'block' : 'none' }}>
|
||||
{view === 'post' && currentPostId && <PostDetail postId={currentPostId} />}
|
||||
{view === 'user' && currentUsername && <UserProfile username={currentUsername} />}
|
||||
{view === 'new' && <NewPost />}
|
||||
{view === 'admin' && <AdminPage />}
|
||||
{view === 'settings' && <SettingsPage />}
|
||||
|
||||
Reference in new issue
Block a user