MC/website/node/jarvis-gazette.mjs
2026-11-27 23:43:05 +00:00

36 lines
3.6 KiB
JavaScript

import {createHash} from 'node:crypto';
import {STAFF_OFFICE_ACCOUNT_ID} from './organisation-keys.mjs';
import {OFFICIAL_ACCOUNT} from './official-account.mjs';
export const GAZETTE_NAME='J.A.R.V.I.S. Gazette';
// Domain adapters publish confirmed public facts, never arbitrary audit rows.
export const GAZETTE_EVENT_TYPES=Object.freeze([
'project-approved','election-announced','election-result','war-declared','peace-agreed',
'clan-ownership','company-ownership','server-event','progression-milestone'
]);
export function gazetteEventEligible(event){
return !!event&&GAZETTE_EVENT_TYPES.includes(event.type)&&event.visibility==='public'&&event.confirmed===true&&typeof event.sourceId==='string'&&event.sourceId.length>0;
}
export const GAZETTE_SCHEMA=`CREATE TABLE IF NOT EXISTS jarvis_gazette_records(id CHAR(36) PRIMARY KEY,key_id CHAR(36) NOT NULL UNIQUE,event_type VARCHAR(32) NOT NULL,source_id VARCHAR(160) NOT NULL,post_id CHAR(36) NOT NULL UNIQUE,payload_json TEXT NOT NULL,payload_hash CHAR(64) NOT NULL,created_at BIGINT NOT NULL,UNIQUE KEY gazette_source(event_type,source_id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4`;
const hash=value=>createHash('sha256').update(value).digest('hex');
const recordId=(type,source)=>{const h=hash(JSON.stringify([type,source]));return h.slice(0,8)+'-'+h.slice(8,12)+'-5'+h.slice(13,16)+'-8'+h.slice(17,20)+'-'+h.slice(20,32);};
// The trusted domain adapter supplies a public projection of confirmed facts.
// Existing editorial workers handle delivery; this creates no second outbox.
export function createGazettePublisher(db,keys,{authorize,resolve,clock=Date.now}){
if(typeof authorize!=='function'||typeof resolve!=='function')throw TypeError('Gazette domain adapters required');
return Object.freeze({async publish(actor,event){
if(!gazetteEventEligible(event)||event.sourceId.length>160)throw TypeError('Confirmed public Gazette event required');
const id=recordId(event.type,event.sourceId);
const grant=await keys.issue({accountId:STAFF_OFFICE_ACCOUNT_ID,keyType:'gazette-news',scopeId:'news:'+id+':v1',authorize:async(tx,{keyId})=>{
if(!await authorize(actor,event,tx))throw Error('Gazette publication authority required');
const existing=await tx.first('SELECT payload_json,payload_hash FROM jarvis_gazette_records WHERE id=? FOR UPDATE',[id]);
if(existing){if(hash(existing.payload_json)!==existing.payload_hash)throw Error('Gazette record integrity check failed');return;}
const fact=await resolve(event,tx),now=clock();
if(!fact||typeof fact.title!=='string'||!fact.title.trim()||fact.title.length>120||typeof fact.body!=='string'||!fact.body.trim()||fact.body.length>12000||!['news','world','events','community'].includes(fact.category)||!Number.isSafeInteger(fact.occurredAt)||fact.occurredAt<0||fact.occurredAt>now)throw Error('Gazette source is not a confirmed public fact');
const payload={type:event.type,sourceId:event.sourceId,title:fact.title,body:fact.body,category:fact.category,occurredAt:fact.occurredAt,publisher:GAZETTE_NAME};
const json=JSON.stringify(payload);
await tx.query('INSERT INTO website_posts(id,source_key,category,title,body,status,author_id,created_at,updated_at,published_at,version) VALUES(?,?,?,?,?,?,?,?,?,?,1)',[id,'gazette:'+id,fact.category,fact.title,fact.body,'published',OFFICIAL_ACCOUNT.id,now,now,now]);
await tx.query('INSERT INTO jarvis_gazette_records(id,key_id,event_type,source_id,post_id,payload_json,payload_hash,created_at) VALUES(?,?,?,?,?,?,?,?)',[id,keyId,event.type,event.sourceId,id,json,hash(json),now]);
}});
return {postId:id,keyId:grant.id,created:grant.created,sealed:true};
}});
}