| 0b4b582 | | | 1 | import { NextRequest, NextResponse } from "next/server"; |
| 0b4b582 | | | 2 | import { extractToken, verifyToken } from "@/server/collab-auth"; |
| 0b4b582 | | | 3 | import { canAccessRepo, loadRoom, safeId } from "@/server/collab-rooms"; |
| 0b4b582 | | | 4 | |
| 0b4b582 | | | 5 | export const dynamic = "force-dynamic"; |
| 0b4b582 | | | 6 | |
| 0b4b582 | | | 7 | export async function GET( |
| 0b4b582 | | | 8 | req: NextRequest, |
| 0b4b582 | | | 9 | { params }: { params: Promise<{ owner: string; repo: string }> } |
| 0b4b582 | | | 10 | ) { |
| 0b4b582 | | | 11 | const { owner, repo } = await params; |
| 0b4b582 | | | 12 | const token = extractToken(req); |
| 0b4b582 | | | 13 | |
| 0b4b582 | | | 14 | if (!token) { |
| 0b4b582 | | | 15 | return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); |
| 0b4b582 | | | 16 | } |
| 0b4b582 | | | 17 | |
| 0b4b582 | | | 18 | try { |
| 0b4b582 | | | 19 | verifyToken(token); |
| 0b4b582 | | | 20 | } catch { |
| 0b4b582 | | | 21 | return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); |
| 0b4b582 | | | 22 | } |
| 0b4b582 | | | 23 | |
| 0b4b582 | | | 24 | if (!(await canAccessRepo(owner, repo, token))) { |
| 0b4b582 | | | 25 | return NextResponse.json({ error: "Not found" }, { status: 404 }); |
| 0b4b582 | | | 26 | } |
| 0b4b582 | | | 27 | |
| 0b4b582 | | | 28 | const notes = loadRoom(owner, repo); |
| 0b4b582 | | | 29 | const timestamp = new Date().toISOString(); |
| 0b4b582 | | | 30 | let output = `# Diagram Review Notes\n# Repo: ${owner}/${repo}\n# Exported: ${timestamp}\n\n`; |
| 0b4b582 | | | 31 | |
| 0b4b582 | | | 32 | for (const [diagramId, diagramNotes] of Object.entries(notes)) { |
| 0b4b582 | | | 33 | if (diagramNotes.length === 0) continue; |
| 0b4b582 | | | 34 | const diagramTitle = diagramNotes[0]?.diagramTitle || diagramId; |
| 0b4b582 | | | 35 | output += `## ${diagramTitle} (${diagramId})\n\n`; |
| 0b4b582 | | | 36 | for (const note of diagramNotes) { |
| 0b4b582 | | | 37 | const location = note.targetNode |
| 0b4b582 | | | 38 | ? ` [on: ${note.targetNode}]` |
| 0b4b582 | | | 39 | : note.x != null |
| 0b4b582 | | | 40 | ? ` [pinned at (${Math.round(note.x!)}, ${Math.round(note.y!)})]` |
| 0b4b582 | | | 41 | : ""; |
| 0b4b582 | | | 42 | output += `- **${note.author}** (${note.timestamp})${location}: ${note.text}\n`; |
| 0b4b582 | | | 43 | } |
| 0b4b582 | | | 44 | output += "\n"; |
| 0b4b582 | | | 45 | } |
| 0b4b582 | | | 46 | |
| 0b4b582 | | | 47 | const filename = `collab-notes-${safeId(owner)}-${safeId(repo)}-${timestamp.slice(0, 10)}.md`; |
| 0b4b582 | | | 48 | |
| 0b4b582 | | | 49 | return new NextResponse(output, { |
| 0b4b582 | | | 50 | headers: { |
| 0b4b582 | | | 51 | "Content-Type": "text/markdown", |
| 0b4b582 | | | 52 | "Content-Disposition": `attachment; filename="${filename}"`, |
| 0b4b582 | | | 53 | }, |
| 0b4b582 | | | 54 | }); |
| 0b4b582 | | | 55 | } |