import {createHash,randomUUID} from 'node:crypto'; // Public server events reuse the existing listing/publication queue. Personal // and organisation calendars never acquire public promotion through this adapter. export async function mirrorPublicServerEvents(bank,{edition,clock=Date.now}={}){ const now=clock();let cursorTime=-1,cursorId='',changed=0; for(let page=0;page<5;page++){ const rows=await bank.query("SELECT e.* FROM jarvis_events e WHERE e.scope_kind='server' AND e.scope_id='realm' AND ((e.state='scheduled' AND e.ends_at>? AND NOT EXISTS(SELECT 1 FROM jarvis_listings l WHERE l.id=e.id)) OR EXISTS(SELECT 1 FROM jarvis_listings l WHERE l.id=e.id AND l.definition_id='events' AND l.organisation_kind='server' AND (l.revision<>e.revision OR (l.state='open' AND (e.state='cancelled' OR e.ends_at<=?))))) AND (e.updated_at>? OR (e.updated_at=? AND e.id>?)) ORDER BY e.updated_at,e.id LIMIT 100",[now,now,cursorTime,cursorTime,cursorId]); if(!rows.length)break; for(const row of rows){ const state=row.state==='cancelled'?'cancelled':Number(row.ends_at)<=now?'filled':'open',message=row.description+'\n\n'+new Date(Number(row.starts_at)).toISOString()+' — '+new Date(Number(row.ends_at)).toISOString(); const fingerprint=createHash('sha256').update(JSON.stringify([row.id,row.title,message,state,Number(row.revision)])).digest('hex'); const prior=await bank.first('SELECT definition_id,organisation_kind,fingerprint FROM jarvis_listings WHERE id=?',[row.id]); if(prior&&(prior.definition_id!=='events'||prior.organisation_kind!=='server'))throw Error('Event publication reference collision'); if(prior?.fingerprint===fingerprint)continue; const origin=prior?null:await edition(); await bank.transaction(async tx=>{ const current=await tx.first('SELECT definition_id,organisation_kind,fingerprint FROM jarvis_listings WHERE id=? FOR UPDATE',[row.id]); if(current&&(current.definition_id!=='events'||current.organisation_kind!=='server'))throw Error('Event publication reference collision'); if(current?.fingerprint===fingerprint)return; if(!current){ if(state!=='open')return; await tx.query("INSERT INTO jarvis_listings(id,request_id,actor_id,organisation_kind,organisation_id,definition_id,title,message,organisation_name,world_name,fee_minor,release_stage,release_version,season_id,rarity,state,revision,fingerprint,created_at,updated_at) VALUES(?,?,?,'server','realm','events',?,?,'DuneRealms server events','Realm',0,?,?,?,'common',?,?,?,?,?)",[row.id,row.id,row.actor_id,row.title,message,origin.stage,origin.version,origin.season,state,Number(row.revision),fingerprint,Number(row.created_at),now]); await tx.query("INSERT INTO jarvis_promotion_jobs(id,listing_id,channel,state,next_attempt,created_at) VALUES(?,?,'discord','queued',?,?)",[randomUUID(),row.id,now,now]); }else{ await tx.query('UPDATE jarvis_listings SET title=?,message=?,state=?,revision=?,fingerprint=?,updated_at=? WHERE id=?',[row.title,message,state,Number(row.revision),fingerprint,now,row.id]); await tx.query("UPDATE jarvis_promotion_jobs SET next_attempt=? WHERE listing_id=? AND channel='discord' AND state='sent'",[now,row.id]); } changed++; }); } cursorTime=Number(rows.at(-1).updated_at);cursorId=rows.at(-1).id; if(rows.length<100)break; } return {changed}; }