|
| 1 | +/** |
| 2 | + * POST /api/v1/votes — cast / change / clear a vote via PAT. |
| 3 | + * |
| 4 | + * Body: { submissionId: uuid, value: 1 | -1 | 0 } |
| 5 | + * |
| 6 | + * value = 1 → upvote |
| 7 | + * value = -1 → downvote (gated on karma >= 100, like the web UI) |
| 8 | + * value = 0 → clear the existing vote |
| 9 | + * |
| 10 | + * The DB trigger handles score deltas; the action upserts on |
| 11 | + * (user_id, submission_id) so flipping is one row, not two. |
| 12 | + */ |
| 13 | + |
| 14 | +import { authenticate, requireScope } from "@/lib/api/auth"; |
| 15 | +import { checkAndIncrement } from "@/lib/api/rate-limit"; |
| 16 | +import { |
| 17 | + forbidden, |
| 18 | + notFound, |
| 19 | + rateLimited, |
| 20 | + validation, |
| 21 | +} from "@/lib/api/errors"; |
| 22 | +import { ok, preflight, problemResponse } from "@/lib/api/response"; |
| 23 | +import { castVote, voteInputSchema } from "@/lib/votes"; |
| 24 | + |
| 25 | +export async function OPTIONS(): Promise<Response> { |
| 26 | + return preflight(); |
| 27 | +} |
| 28 | + |
| 29 | +export async function POST(req: Request): Promise<Response> { |
| 30 | + const auth = await authenticate(req); |
| 31 | + if (!auth.ok) return problemResponse(auth.problem); |
| 32 | + |
| 33 | + const denied = requireScope(auth.token, "vote:write"); |
| 34 | + if (denied) return problemResponse(denied.problem); |
| 35 | + |
| 36 | + let body: unknown; |
| 37 | + try { |
| 38 | + body = await req.json(); |
| 39 | + } catch { |
| 40 | + return problemResponse(validation("Request body must be valid JSON.")); |
| 41 | + } |
| 42 | + |
| 43 | + const parsed = voteInputSchema.safeParse(body); |
| 44 | + if (!parsed.success) { |
| 45 | + return problemResponse( |
| 46 | + validation( |
| 47 | + "Vote validation failed.", |
| 48 | + parsed.error.issues.map((i) => ({ |
| 49 | + field: i.path.join("."), |
| 50 | + message: i.message, |
| 51 | + })), |
| 52 | + ), |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + const limit = await checkAndIncrement(auth.token.id, "votes"); |
| 57 | + if (!limit.ok) { |
| 58 | + return problemResponse( |
| 59 | + rateLimited( |
| 60 | + `Daily vote limit (${limit.limit}) exceeded for this token.`, |
| 61 | + limit.resetAt, |
| 62 | + ), |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + const result = await castVote(auth.user.id, parsed.data); |
| 67 | + |
| 68 | + if (!result.ok) { |
| 69 | + if (result.reason === "missing_user") { |
| 70 | + // Token references a deleted user — already handled in |
| 71 | + // authenticate(), but the core may still detect this if the user |
| 72 | + // was deleted between auth and the vote. Surface as 401. |
| 73 | + return problemResponse({ |
| 74 | + type: "https://claudepot.com/api/errors/unauthorized", |
| 75 | + title: "Unauthorized", |
| 76 | + status: 401, |
| 77 | + detail: "Token references a deleted user.", |
| 78 | + }); |
| 79 | + } |
| 80 | + if (result.reason === "locked") { |
| 81 | + return problemResponse(forbidden("Account is locked.")); |
| 82 | + } |
| 83 | + if (result.reason === "karma_gate") { |
| 84 | + return problemResponse( |
| 85 | + forbidden( |
| 86 | + "Downvotes require at least 100 karma. Your account is below the threshold.", |
| 87 | + ), |
| 88 | + ); |
| 89 | + } |
| 90 | + return problemResponse( |
| 91 | + notFound("Submission not found, or not in a votable state."), |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return ok({ submissionId: parsed.data.submissionId, value: result.value }); |
| 96 | +} |
0 commit comments