|
| 1 | +import type { APIRoute } from 'astro'; |
| 2 | +import { env } from 'cloudflare:workers'; |
| 3 | +import { bearerUser, currentUser } from '../../../../lib/auth'; |
| 4 | +import { json, readJson, requestError } from '../../../../lib/http'; |
| 5 | +import { PROOF_REACTIONS, type ProofReaction } from '../../../../lib/types'; |
| 6 | + |
| 7 | +const writableReactions = ['not_my_problem', 'skip'] as const satisfies readonly ProofReaction[]; |
| 8 | + |
| 9 | +async function reactionCounts(problemId: number) { |
| 10 | + return env.DB.prepare(` |
| 11 | + SELECT |
| 12 | + (SELECT COUNT(*) FROM confirmations WHERE problem_id = ?) AS confirmations, |
| 13 | + (SELECT COUNT(*) FROM proof_reactions WHERE problem_id = ? AND reaction_type = 'not_my_problem') AS not_my_problem, |
| 14 | + (SELECT COUNT(*) FROM proof_reactions WHERE problem_id = ? AND reaction_type = 'skip') AS skips |
| 15 | + `).bind(problemId, problemId, problemId).first<{ |
| 16 | + confirmations: number; |
| 17 | + not_my_problem: number; |
| 18 | + skips: number; |
| 19 | + }>(); |
| 20 | +} |
| 21 | + |
| 22 | +export const POST: APIRoute = async ({ params, request }) => { |
| 23 | + try { |
| 24 | + const user = await currentUser(request, env.DB, env) ?? await bearerUser(request, env.DB, env); |
| 25 | + if (!user) return json({ error: 'Bitte melde dich mit GitHub an.' }, { status: 401 }); |
| 26 | + |
| 27 | + const problemId = Number(params.id); |
| 28 | + if (!Number.isInteger(problemId) || problemId < 1) { |
| 29 | + return json({ error: 'Ungültige Problem-Reaktion.' }, { status: 422 }); |
| 30 | + } |
| 31 | + |
| 32 | + const body = (await readJson(request)) as Record<string, unknown>; |
| 33 | + const reaction = typeof body.reaction === 'string' ? body.reaction : ''; |
| 34 | + if (!PROOF_REACTIONS.includes(reaction as ProofReaction) || !writableReactions.includes(reaction as (typeof writableReactions)[number])) { |
| 35 | + return json({ error: 'Ungültige Problem-Reaktion.' }, { status: 422 }); |
| 36 | + } |
| 37 | + |
| 38 | + const exists = await env.DB.prepare('SELECT 1 FROM problems WHERE id = ?').bind(problemId).first(); |
| 39 | + if (!exists) return json({ error: 'Problem nicht gefunden.' }, { status: 404 }); |
| 40 | + |
| 41 | + const confirmed = await env.DB.prepare('SELECT 1 FROM confirmations WHERE problem_id = ? AND user_id = ?') |
| 42 | + .bind(problemId, user.id) |
| 43 | + .first(); |
| 44 | + if (confirmed) { |
| 45 | + await env.DB.prepare(` |
| 46 | + INSERT OR IGNORE INTO proof_reactions (problem_id, user_id, reaction_type, source) |
| 47 | + VALUES (?, ?, 'yes', 'confirmation') |
| 48 | + `).bind(problemId, user.id).run(); |
| 49 | + const counts = await reactionCounts(problemId); |
| 50 | + return json({ |
| 51 | + reaction: 'yes', |
| 52 | + ignored: true, |
| 53 | + confirmations: counts?.confirmations ?? 0, |
| 54 | + notMyProblem: counts?.not_my_problem ?? 0, |
| 55 | + skips: counts?.skips ?? 0, |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + const source = typeof body.source === 'string' && body.source.trim() |
| 60 | + ? body.source.trim().slice(0, 40) |
| 61 | + : 'proof-feed'; |
| 62 | + await env.DB.prepare(` |
| 63 | + INSERT INTO proof_reactions (problem_id, user_id, reaction_type, source) |
| 64 | + VALUES (?, ?, ?, ?) |
| 65 | + ON CONFLICT(problem_id, user_id) DO UPDATE SET |
| 66 | + reaction_type = excluded.reaction_type, |
| 67 | + source = excluded.source, |
| 68 | + updated_at = CURRENT_TIMESTAMP |
| 69 | + `).bind(problemId, user.id, reaction, source).run(); |
| 70 | + |
| 71 | + const counts = await reactionCounts(problemId); |
| 72 | + return json({ |
| 73 | + reaction, |
| 74 | + confirmations: counts?.confirmations ?? 0, |
| 75 | + notMyProblem: counts?.not_my_problem ?? 0, |
| 76 | + skips: counts?.skips ?? 0, |
| 77 | + }); |
| 78 | + } catch (error) { |
| 79 | + console.error('record proof reaction failed', error); |
| 80 | + return requestError(error); |
| 81 | + } |
| 82 | +}; |
0 commit comments