|
| 1 | +import { error } from '@sveltejs/kit'; |
| 2 | +import type { PageServerLoad } from './$types'; |
| 3 | +import { getCurrentUser } from '$lib/server/auth'; |
| 4 | + |
| 5 | +export const load: PageServerLoad = async ({ params, platform, request, cookies }) => { |
| 6 | + const { username, slug } = params; |
| 7 | + const env = platform?.env; |
| 8 | + if (!env) throw error(500, 'Platform env not available'); |
| 9 | + |
| 10 | + // 1. Find user |
| 11 | + const targetUser = await env.DB.prepare('SELECT id, username FROM users WHERE username = ?') |
| 12 | + .bind(username) |
| 13 | + .first<{ id: string; username: string }>(); |
| 14 | + |
| 15 | + if (!targetUser) throw error(404, 'User not found'); |
| 16 | + |
| 17 | + // 2. Find config |
| 18 | + const config = await env.DB.prepare( |
| 19 | + 'SELECT * FROM configs WHERE user_id = ? AND slug = ?' |
| 20 | + ) |
| 21 | + .bind(targetUser.id, slug) |
| 22 | + .first<any>(); |
| 23 | + |
| 24 | + if (!config) throw error(404, 'Configuration not found'); |
| 25 | + |
| 26 | + // 3. Check visibility |
| 27 | + if (!config.is_public) { |
| 28 | + const currentUser = await getCurrentUser(request, cookies, env.DB, env.JWT_SECRET); |
| 29 | + if (!currentUser || currentUser.id !== targetUser.id) { |
| 30 | + throw error(404, 'Configuration not found'); // Hide private configs |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // Parse JSON fields |
| 35 | + try { |
| 36 | + config.snapshot = JSON.parse(config.snapshot); |
| 37 | + config.packages = JSON.parse(config.packages); |
| 38 | + } catch (e) { |
| 39 | + console.error('Failed to parse config JSON', e); |
| 40 | + } |
| 41 | + |
| 42 | + return { |
| 43 | + configUser: targetUser, |
| 44 | + config |
| 45 | + }; |
| 46 | +}; |
0 commit comments