Changeset dd1028a in Klonkt
- Timestamp:
- 06/24/2026 10:48:18 AM (3 months ago)
- Branches:
- main
- Children:
- 065452a
- Parents:
- 5bf63b7
- File:
-
- 1 edited
-
src/server.js (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/server.js
r5bf63b7 rdd1028a 65 65 import ogRoutes from './routes/og.js'; 66 66 import apRoutes from './routes/activitypub.js'; 67 import { apWants } from './services/ActivityPubService.js'; 67 68 68 69 // SESSION_SECRET: use the env var if set. Otherwise auto-generate a strong one … … 235 236 app.use(loadTheme); 236 237 238 // ActivityPub content negotiation on the human URLs: an AP request (Accept: 239 // application/activity+json) to a profile/post URL is redirected to its /ap/* 240 // representation — same URL serves HTML to browsers, AP-JSON to servers (this is 241 // how Mastodon resolves a pasted profile/post URL). Gated on apWants() so normal 242 // browser requests pay nothing. 243 app.use((req, res, next) => { 244 if (req.method !== 'GET' || !apWants(req)) return next(); 245 const site = res.locals.site; 246 if (!site || !site.slug) return next(); 247 const seg = req.path.replace(/^\/+|\/+$/g, ''); 248 if (seg === '') return res.redirect(302, `/ap/users/${encodeURIComponent(site.slug)}`); 249 if (!seg.includes('/')) { 250 try { 251 const post = db.prepare( 252 "SELECT id FROM posts WHERE site_id = ? AND slug = ? AND status = 'published' AND (fan_only IS NULL OR fan_only = 0)" 253 ).get(site.id, seg); 254 if (post) return res.redirect(302, `/ap/notes/${post.id}`); 255 } catch { /* fall through to normal HTML handling */ } 256 } 257 return next(); 258 }); 259 237 260 // Lightweight CSRF defense: reject cross-origin state-mutating requests. 238 261 // Same-origin forms + HTMX send a matching Origin; missing Origin is allowed
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)