Reuse political elections and appointments for the New Faith, with Church-only artwork and theme. Deny faith-office conquest, enforce repeatable reward cooldowns, and open quest and decision details in accessible in-page dialogs. Compact item listings while preserving their lore previews.
24 lines
5.4 KiB
JavaScript
24 lines
5.4 KiB
JavaScript
import {faithPOL} from '../control-panel/public/faith-pol-policy.mjs';
|
|
import {naibSeats} from './naib-membership.mjs';
|
|
import {passiveOrderRecords} from './political-passive-orders.mjs';
|
|
import {activePoliticalAppointments} from './political-appointments.mjs';
|
|
import {politicalOfficeId} from './political-office-charters.mjs';
|
|
import {entityId} from './postbank-authority.mjs';
|
|
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,claimOrganisationId:politicalOfficeId(o.key),name:o.scope,tradition:faithPOL(o.key)?.tradition||null,theme:faithPOL(o.key)?.theme||null,roleTitles:faithPOL(o.key)?.roles||null,description:faithPOL(o.key)?faithPOL(o.key).tradition+' · '+o.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',mailScope:o.office==='emperor'?'imperial':['house_leader','reverent_mother','doci','guild_leader'].includes(o.office)?'faction':null,bankEntityId:o.office==='emperor'?entityId('office','emperor'):['house_leader','reverent_mother','doci'].includes(o.office)?entityId('office','leader:'+o.faction):null,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}));
|
|
const council=organisations.find(o=>o.id==='arrakis');if(council){const seats=await naibSeats(db);for(const seat of seats)if(!council.members.some(m=>m.username===seat.username))council.members.push({username:seat.username,name:seat.display_name||seat.username,role:'naib',title:'Naib of '+seat.clan_name});const own=seats.find(s=>s.user_id===user.id);if(own&&!council.membership){council.role='naib';council.membership={source:'current-sietch-deed',clanId:own.clan_id};}}
|
|
if(db.politicalAppointmentsEnabled){const appointments=await activePoliticalAppointments(db,{clock});for(const organisation of organisations){const rows=appointments.filter(a=>a.office_key===organisation.id);for(const row of rows)if(!organisation.members.some(m=>m.username===row.username))organisation.members.push({username:row.username,name:row.display_name||row.username,role:row.role_key,title:row.title,appointmentId:row.id,expiresAt:Number(row.expires_at)});const own=rows.find(a=>a.user_id===user.id);if(!organisation.membership&&own){organisation.role=own.role_key;organisation.membership={source:'appointment-deed',id:own.id,startsAt:Number(own.starts_at),expiresAt:Number(own.expires_at)};}}}
|
|
for(const order of await passiveOrderRecords(db,{actor:user,clock}))organisations.push({...order,bankEntityId:entityId('company',order.bankLegacyId),boardScope:{kind:'company',id:order.legacyBoardId}});
|
|
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);}
|
|
};}
|

Staff