|
| 1 | +import { NextRequest, NextResponse } from 'next/server'; |
| 2 | +import { createClient } from '@supabase/supabase-js'; |
| 3 | +import { computeReputation } from '@/lib/reputation/attestation-engine'; |
| 4 | +import { isValidDid } from '@/lib/reputation/crypto'; |
| 5 | + |
| 6 | +const supabase = createClient( |
| 7 | + process.env.NEXT_PUBLIC_SUPABASE_URL!, |
| 8 | + process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! |
| 9 | +); |
| 10 | + |
| 11 | +function generateBadgeSvg(taskCount: number, acceptedRate: number, flagged: boolean): string { |
| 12 | + const label = 'CPR Score'; |
| 13 | + const scoreText = taskCount === 0 |
| 14 | + ? 'no data' |
| 15 | + : `${(acceptedRate * 100).toFixed(0)}% · ${taskCount} tasks`; |
| 16 | + |
| 17 | + const labelColor = '#555'; |
| 18 | + const valueColor = flagged |
| 19 | + ? '#e05d44' |
| 20 | + : acceptedRate >= 0.9 |
| 21 | + ? '#4c1' |
| 22 | + : acceptedRate >= 0.7 |
| 23 | + ? '#dfb317' |
| 24 | + : '#e05d44'; |
| 25 | + |
| 26 | + const labelWidth = label.length * 7 + 12; |
| 27 | + const valueWidth = scoreText.length * 6.5 + 12; |
| 28 | + const totalWidth = labelWidth + valueWidth; |
| 29 | + |
| 30 | + return `<svg xmlns="http://www.w3.org/2000/svg" width="${totalWidth}" height="20" role="img" aria-label="${label}: ${scoreText}"> |
| 31 | + <title>${label}: ${scoreText}</title> |
| 32 | + <linearGradient id="s" x2="0" y2="100%"> |
| 33 | + <stop offset="0" stop-color="#bbb" stop-opacity=".1"/> |
| 34 | + <stop offset="1" stop-opacity=".1"/> |
| 35 | + </linearGradient> |
| 36 | + <clipPath id="r"> |
| 37 | + <rect width="${totalWidth}" height="20" rx="3" fill="#fff"/> |
| 38 | + </clipPath> |
| 39 | + <g clip-path="url(#r)"> |
| 40 | + <rect width="${labelWidth}" height="20" fill="${labelColor}"/> |
| 41 | + <rect x="${labelWidth}" width="${valueWidth}" height="20" fill="${valueColor}"/> |
| 42 | + <rect width="${totalWidth}" height="20" fill="url(#s)"/> |
| 43 | + </g> |
| 44 | + <g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="11"> |
| 45 | + <text aria-hidden="true" x="${labelWidth / 2}" y="15" fill="#010101" fill-opacity=".3">${label}</text> |
| 46 | + <text x="${labelWidth / 2}" y="14">${label}</text> |
| 47 | + <text aria-hidden="true" x="${labelWidth + valueWidth / 2}" y="15" fill="#010101" fill-opacity=".3">${scoreText}</text> |
| 48 | + <text x="${labelWidth + valueWidth / 2}" y="14">${scoreText}</text> |
| 49 | + </g> |
| 50 | +</svg>`; |
| 51 | +} |
| 52 | + |
| 53 | +export async function GET( |
| 54 | + request: NextRequest, |
| 55 | + { params }: { params: Promise<{ did: string }> } |
| 56 | +) { |
| 57 | + try { |
| 58 | + const { did } = await params; |
| 59 | + const agentDid = decodeURIComponent(did); |
| 60 | + |
| 61 | + if (!isValidDid(agentDid)) { |
| 62 | + const svg = generateBadgeSvg(0, 0, false); |
| 63 | + return new NextResponse(svg, { |
| 64 | + headers: { |
| 65 | + 'Content-Type': 'image/svg+xml', |
| 66 | + 'Cache-Control': 'public, max-age=300', |
| 67 | + }, |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + const reputation = await computeReputation(supabase, agentDid); |
| 72 | + const allTime = reputation.windows.all_time; |
| 73 | + const svg = generateBadgeSvg( |
| 74 | + allTime.task_count, |
| 75 | + allTime.accepted_rate, |
| 76 | + reputation.anti_gaming.flagged |
| 77 | + ); |
| 78 | + |
| 79 | + return new NextResponse(svg, { |
| 80 | + headers: { |
| 81 | + 'Content-Type': 'image/svg+xml', |
| 82 | + 'Cache-Control': 'public, max-age=300', |
| 83 | + }, |
| 84 | + }); |
| 85 | + } catch (error) { |
| 86 | + console.error('Badge generation error:', error); |
| 87 | + const svg = generateBadgeSvg(0, 0, false); |
| 88 | + return new NextResponse(svg, { |
| 89 | + headers: { 'Content-Type': 'image/svg+xml' }, |
| 90 | + status: 500, |
| 91 | + }); |
| 92 | + } |
| 93 | +} |
0 commit comments