web/website/node/politics-workspace.mjs

15 lines
3.1 KiB
JavaScript

import {identity} from './auth.mjs';
import {accessFor} from './permissions.mjs';
import {readGovernmentOffices} from './public-government.mjs';
// Protected political organisations project active office deeds; they never replace personal membership.
export function createPoliticsWorkspace(db,{authenticate=r=>identity(r,db),access=u=>accessFor(db,u),clock=Date.now,keys=null}={}){return async request=>{
const reply=(v,status=200)=>Response.json(v,{status,headers:{'Cache-Control':'private, no-store',Vary:'Cookie'}});
if(request.method!=='GET')return reply({message:'Read-only political workspace.'},405);
const user=await authenticate(request);if(!user)return reply({message:'Sign in to open Politics CP.'},401);
if(!(await access(user)).nodes.includes('web.menu.game'))return reply({message:'Game account access required.'},403);
const params=new URL(request.url).searchParams,selected=params.get('office');if([...params.keys()].some(k=>k!=='office')||params.getAll('office').length>1||selected!==null&&!/^[a-z_]{1,32}$/.test(selected))return reply({message:'Choose a valid political organisation.'},400);
try{const account=await db.prepare("SELECT username FROM site_accounts WHERE id=? AND password_hash NOT LIKE '!%'").bind(user.id).first();if(!account)return reply({message:'Account access changed.'},403);const offices=await readGovernmentOffices(db,clock());const grants=new Map();if(keys){const now=clock();const {results:terms}=await db.prepare("SELECT id,faction,office,expires_at FROM political_office_terms WHERE user_id=? AND outcome='winner' AND starts_at<=? AND expires_at>?").bind(user.id,now,now).all();for(const office of offices.filter(o=>o.holder?.username.toLowerCase()===String(account.username).toLowerCase())){const matches=terms.filter(t=>t.faction===office.faction&&t.office===office.office);if(matches.length!==1)continue;const grant=await keys.issue({accountId:user.id,keyType:'political-clan',scopeId:matches[0].id,expiresAt:Number(matches[0].expires_at)});grants.set(office.key,grant.id);}}const usable=keys?new Set((await keys.list(user.id)).filter(k=>['available','used'].includes(k.status)).map(k=>k.id)):null;const belongs=o=>o.holder?.username.toLowerCase()===String(account.username).toLowerCase()&&(!usable||usable.has(grants.get(o.key)));const organisations=offices.map(o=>({id:o.key,name:o.scope,description:o.description,tag:o.faction.toUpperCase(),emblem:'https://www.dunerealms.com/assets/seals/'+o.art,protected:true,kind:'politics',charterKind:'royal',memberLimit:50,charterSource:'staff-key',role:belongs(o)?'officeholder':'visitor',members:o.holder?[{username:o.holder.username,role:'officeholder',name:o.holder.name}]:[],membership:belongs(o)?{source:'active-office-deed',keyId:grants.get(o.key)||null,startsAt:o.term.startsAt,expiresAt:o.term.expiresAt}:null}));
if(selected&&!organisations.some(o=>o.id===selected))return reply({message:'Political organisation not found.'},404);
return reply({organisations,selected:selected||organisations.find(o=>o.membership)?.id||organisations[0]?.id||null,membershipSource:'active-office-deed',decisionKeysSeparate:true});
}catch{return reply({message:'Political deed membership could not be verified.'},503);}
};}