web/website/control-panel/native-appeal-decision.mjs
unimatrixgit 77cdf19faa Checkpoint Alpha 1.0.0.3 graphical menus and moderation evidence
Preserve the current Alpha source and artwork snapshot. See handoff/alpha-1.0.0.3 for actual deployed hashes, checks, staged follow-ups and the production pause. This checkpoint does not claim every source feature is deployed.
2026-11-02 00:05:34 +01:00

35 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {randomUUID} from 'node:crypto';
const json=(data,status=200)=>Response.json(data,{status,headers:{'Cache-Control':'no-store'}});
export async function decideNativeAppeal(db,settings,{user,access,ban,body,fetcher=fetch}){
if(!['web.bans.manage','web.tickets.staff','web.tickets.appeals'].every(node=>access.nodes.includes(node)))return json({message:'Appeal decision permission required.'},403);
if(!['approve','deny','reopen'].includes(body.action)||!Number.isSafeInteger(Number(body.ticketId))||Number(body.ticketId)<1||typeof body.reason!=='string'||body.reason.trim().length<5||body.reason.length>500||/[\x00-\x1f]/.test(body.reason))return json({message:'Choose an action and enter a single-line reason of 5–500 characters.'},400);
const appeal=await db.prepare('SELECT * FROM native_ban_appeals WHERE ban_id=? AND ticket_id=?').bind(ban.id,Number(body.ticketId)).first();
if(!appeal)return json({message:'The ticket is not linked to this ban.'},404);
const now=Date.now(),reason=body.reason.trim(),demo=ban.source_key.startsWith('demo:');
const audit=(action)=>db.prepare('INSERT INTO moderation_events(id,ban_id,actor_id,action,reason,snapshot,created_at) VALUES(?,?,?,?,?,?,?)').bind(randomUUID(),ban.id,user.id,action,reason,JSON.stringify({ticketId:Number(body.ticketId),demo}),now);
if(body.action==='reopen'){
if(appeal.status==='approving')return json({message:'Approval is in progress; retry that decision first.'},409);
await db.batch([db.prepare("UPDATE native_ban_appeals SET status='pending',decision_reason=NULL,decided_by=NULL,decided_at=NULL WHERE id=? AND status<>'approving'").bind(appeal.id),audit('appeal-reopened')]);return json({ok:true,status:'pending'});
}
const target=body.action==='approve'?'approved':'denied';
if(appeal.status===target)return json({ok:true,status:target,replayed:true});
if(!['pending','closed',...(body.action==='approve'?['approving']:[])].includes(appeal.status))return json({message:'This appeal already has a different decision. Reopen it before deciding again.'},409);
if(body.action==='approve'){
if(appeal.status!=='approving'){
const claimed=await db.prepare("UPDATE native_ban_appeals SET status='approving' WHERE id=? AND status IN ('pending','closed')").bind(appeal.id).run();
if(!claimed.meta.changes)return json({message:'Another staff action changed this appeal. Refresh first.'},409);
}
if(!demo){
try{
const response=await fetcher('http://127.0.0.1:15433/bans/action',{method:'POST',headers:{Authorization:'Bearer '+settings.SERVER_CONTROL_TOKEN,'Content-Type':'application/json'},body:JSON.stringify({key:ban.native_key,action:'remove',created:ban.created_text,actor:user.username,reason}),signal:AbortSignal.timeout(18000)});
const confirmed=await response.json();if(!response.ok||confirmed.confirmed!==true||confirmed.absent!==true||confirmed.key!==ban.native_key)throw Error('Removal unconfirmed');
}catch{
await db.prepare("UPDATE native_ban_appeals SET status='pending' WHERE id=? AND status='approving'").bind(appeal.id).run();await audit('appeal-approval-unconfirmed').run();return json({message:'The game host did not confirm ban removal. The appeal remains pending; no approval was recorded.'},503);
}
}
}
const expected=body.action==='approve'?'approving':appeal.status;
const result=await db.batch([db.prepare('UPDATE native_ban_appeals SET status=?,decision_reason=?,decided_by=?,decided_at=? WHERE id=? AND status=?').bind(target,reason,user.username,now,appeal.id,expected),...(body.action==='approve'&&!demo?[db.prepare('UPDATE moderation_bans SET active=0,updated_at=? WHERE id=?').bind(now,ban.id)]:[]),audit('appeal-'+target+(demo?'-demo':''))]);
if(!result[0].meta.changes)return json({message:'The appeal changed during this decision. Refresh to review its status.'},409);
return json({ok:true,status:target,demo,banRemoved:body.action==='approve'&&!demo});
}