| 1 | /**
|
|---|
| 2 | * GET /users/:username
|
|---|
| 3 | *
|
|---|
| 4 | * Author profile page: shows the user + their published posts on the
|
|---|
| 5 | * current site. Read-only. Honours v9-style URL pattern.
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | import express from 'express';
|
|---|
| 9 | import db from '../config/database.js';
|
|---|
| 10 | import { renderPage } from '../middleware/render.js';
|
|---|
| 11 |
|
|---|
| 12 | const router = express.Router();
|
|---|
| 13 |
|
|---|
| 14 | router.get('/:username', (req, res) => {
|
|---|
| 15 | const site = res.locals.site;
|
|---|
| 16 | if (!site) return res.status(404).send('No site');
|
|---|
| 17 |
|
|---|
| 18 | const username = (req.params.username || '').trim();
|
|---|
| 19 | if (!username) return res.status(404).send('Not found');
|
|---|
| 20 |
|
|---|
| 21 | const author = db.prepare(`
|
|---|
| 22 | SELECT id, username, bio, avatar_url, role, palette, theme, created_at
|
|---|
| 23 | FROM users WHERE username = ?
|
|---|
| 24 | `).get(username);
|
|---|
| 25 |
|
|---|
| 26 | if (!author) return res.status(404).send('User not found');
|
|---|
| 27 |
|
|---|
| 28 | const posts = db.prepare(`
|
|---|
| 29 | SELECT p.id, p.slug, p.title, p.excerpt, p.cover_image_url, p.published_at
|
|---|
| 30 | FROM posts p
|
|---|
| 31 | WHERE p.author_id = ? AND p.site_id = ? AND p.status = 'published'
|
|---|
| 32 | ORDER BY p.published_at DESC
|
|---|
| 33 | LIMIT 100
|
|---|
| 34 | `).all(author.id, site.id);
|
|---|
| 35 |
|
|---|
| 36 | // Total across all sites — useful context, doesn't leak content
|
|---|
| 37 | const totalPosts = db.prepare(`
|
|---|
| 38 | SELECT COUNT(*) AS c FROM posts WHERE author_id = ? AND status = 'published'
|
|---|
| 39 | `).get(author.id).c;
|
|---|
| 40 |
|
|---|
| 41 | renderPage(req, res, 'pages/user', {
|
|---|
| 42 | pageTitle: `${author.username} — ${site.title}`,
|
|---|
| 43 | bodyClass: 'on-special',
|
|---|
| 44 | author,
|
|---|
| 45 | posts,
|
|---|
| 46 | totalPosts,
|
|---|
| 47 | });
|
|---|
| 48 | });
|
|---|
| 49 |
|
|---|
| 50 | export default router;
|
|---|