import useDatabase from '~/composables/useDatabase'; import { explorerContentTable } from '~/db/schema'; import type { Navigation } from '~/types/api'; export default defineEventHandler(async (e) => { const { user } = await getUserSession(e); /*if(!user) { throw createError({ statusCode: 401, statusText: 'Unauthorized' }); }*/ const db = useDatabase(); const content = db.select({ path: explorerContentTable.path, title: explorerContentTable.title, type: explorerContentTable.type, private: explorerContentTable.private, navigable: explorerContentTable.navigable, owner: explorerContentTable.owner }).from(explorerContentTable).prepare().all() as (Navigation & { owner: number, navigable: boolean })[]; if(content.length > 0) { const navigation: Navigation[] = []; for(const idx in content) { const item = content[idx]; if(!!item.private && (user?.id ?? -1) !== item.owner || !item.navigable) { delete content[idx]; continue; } const parent = item.path.includes('/') ? item.path.substring(0, item.path.lastIndexOf('/')) : undefined; if(parent && !content.find(e => e && e.path === parent)) { delete content[idx]; continue; } } for(const item of content.filter(e => !!e)) { addChild(navigation, item); } return navigation; } setResponseStatus(e, 404); }); function addChild(arr: Navigation[], e: Navigation): void { const parent = arr.find(f => e.path.startsWith(f.path)); if(parent) { if(!parent.children) parent.children = []; addChild(parent.children, e); } else { arr.push({ title: e.title, path: e.path, type: e.type, private: e.private }); } }