Improve chat roster parsing, message read state and loading feedback

This commit is contained in:
alberto committed 2026-10-18 14:35:41 +02:00
1 parent c980ba10de
commit c8941dc199
16 files changed
+183 -20

No files matched your search

+9 -4
View File
@@ -1,3 +1,5 @@
import {MessageReadState} from './message-read-state.mjs';
import {ForwardingScheduler} from './forwarding-scheduler.mjs';
import {deleteNativeMessage} from './delete-message.mjs';
import {readSubmission,outgoingForm,verifySavedAttachments,uploadLimits} from './mail-upload.mjs';
import {SyncOperations} from './sync-operations.mjs';
@@ -34,6 +36,7 @@ import {parseAttachments,safeName,b2Body,attachmentSummary} from './attachments.
const dir=path.dirname(fileURLToPath(import.meta.url));
const config=await loadConfig();
const root=config.root;
const messageReadState=new MessageReadState(path.join(dir,'message-read-state.json'));await messageReadState.ready;
const radioControl=new RadioControl(config);await radioControl.snapshot();
setInterval(()=>radioControl.snapshot().catch(()=>{}),2000).unref();
const journal=new Journal(config,dir);
@@ -93,6 +96,7 @@ const winlinkRadio=new WinlinkRadio(config,dir,forwarding,gatewayDirectory);
await winlinkRadio.init;
const manualForwarding=new ManualForwarding(config,dir,forwarding);
await manualForwarding.init;
const forwardingScheduler=new ForwardingScheduler(config,dir,manualForwarding);await forwardingScheduler.init;
const syncOperations=new SyncOperations(forwarding);
async function syncNow(peer=config.forwardPeer){if(!config.features.bbs)throw new Error('BBS non configurata');return forwarding.start(peer);}
const messageMetadata=new ProgressiveMetadata(async item=>{const fields={...await messageTimes(item.id)};if(item.type==='P'&&(item.to==='RMS'||item.needsMetadata)){const f=await messageFields(item.id);Object.assign(fields,{from:f.From||item.from,to:f.To||item.to,subject:f.Title||item.subject,via:f.VIA||item.via});}return fields;});
@@ -175,7 +179,7 @@ const server=http.createServer(async(req,res)=>{
else if(data.kind==='winlink-internet'&&config.features.winlink){const peer=config.cmsPeer||'RMS';spec={kind:data.kind,peer,label:'Winlink CMS · Internet',run:signal=>forwarding.start(peer,{signal})};}
else throw Error('Tipo di sincronizzazione non disponibile');json(res,202,syncOperations.start(spec));return;}
const syncJob=url.pathname.match(/^\/api\/sync\/jobs\/([a-f0-9-]{36})(\/abort)?$/);if(syncJob&&req.method==='GET'&&!syncJob[2]){json(res,200,syncOperations.get(syncJob[1]));return;}if(syncJob&&req.method==='POST'&&syncJob[2]){json(res,200,await syncOperations.abort(syncJob[1]));return;}
if(url.pathname==='/api/forwarding'&&req.method==='GET'){const data=await forwarding.snapshot();json(res,200,{...data,manualSession:manualForwarding.snapshot(),profiles:manualForwarding.displayProfiles(data.profiles)});return;}
if(url.pathname==='/api/forwarding'&&req.method==='GET'){const data=await forwarding.snapshot();json(res,200,{...data,manualSession:manualForwarding.snapshot(),schedules:forwardingScheduler.snapshot(),profiles:manualForwarding.displayProfiles(data.profiles).map(p=>{const s=forwardingScheduler.snapshot().find(s=>s.route===p.call&&s.enabled);return s?{...p,scheduled:true,manualOnly:false,enabled:true,interval:s.intervalSeconds,nextEstimated:s.nextAt}:p;})});return;}
if(url.pathname==='/api/forwarding/manual/restore'&&req.method==='POST'){json(res,200,await manualForwarding.restore());return;}
const fwdStart=url.pathname.match(/^\/api\/forwarding\/([A-Z0-9-]{1,32})\/sync$/);if(fwdStart&&req.method==='POST'){const peer=fwdStart[1];json(res,200,manualForwarding.routes.some(r=>r.id===peer)?await manualForwarding.startRoute(peer):await forwarding.start(peer));return;}
if(url.pathname==='/api/dashboard'){json(res,200,await dashboard());return;}
@@ -188,15 +192,16 @@ const server=http.createServer(async(req,res)=>{
const radioPortUpdate=url.pathname.match(/^\/api\/radio\/ports\/(\d+)$/);if(radioPortUpdate&&req.method==='POST'){json(res,200,await radioPorts.update(Number(radioPortUpdate[1]),await body(req)));return;}
if(url.pathname==='/api/radio/control'&&req.method==='POST'){json(res,200,await radioControl.update(await body(req)));return;}
if(url.pathname==='/api/telemetry'&&req.method==='GET'){json(res,200,telemetry);return;}
if(url.pathname==='/api/messages'&&req.method==='GET'){json(res,200,await messages());return;}
if(url.pathname==='/api/messages'&&req.method==='GET'){json(res,200,messageReadState.decorate(await messages()));return;}
const attachmentRoute=url.pathname.match(/^\/api\/messages\/(\d+)\/attachments(?:\/(\d+))?$/);
if(attachmentRoute&&req.method==='GET'){const id=Number(attachmentRoute[1]),message=await attachedMessage(id);if(attachmentRoute[2]===undefined){json(res,200,{files:message.files.map(({data,...f})=>f),encoded:message.encoded});return;}const file=message.files[Number(attachmentRoute[2])];if(!file)throw new Error('Allegato non disponibile');if(url.searchParams.get('text')==='1'){if(file.mime!=='text/plain')throw new Error('Questo file non ha anteprima testuale');const bytes=file.data.subarray(0,128*1024);let text;try{text=new TextDecoder('utf8',{fatal:true}).decode(bytes);}catch{text=bytes.toString('latin1');}json(res,200,{text,truncated:file.size>bytes.length});return;}const preview=url.searchParams.get('preview')==='1'&&(file.mime.startsWith('image/')||file.mime==='application/pdf');res.setHeader('Content-Security-Policy',"default-src 'none'; sandbox; frame-ancestors 'self'");res.setHeader('X-Frame-Options','SAMEORIGIN');res.writeHead(200,{'Content-Type':preview?file.mime:'application/octet-stream','Content-Length':file.size,'Content-Disposition':`${preview?'inline':'attachment'}; filename="${file.name.replace(/[^A-Za-z0-9._ -]/g,'_')}"; filename*=UTF-8''${encodeURIComponent(file.name)}`,'Cache-Control':'no-store'});res.end(file.data);return;}
const sourceRoute=url.pathname.match(/^\/api\/messages\/(\d+)\/source$/);if(sourceRoute&&req.method==='GET'){const m=await attachedMessage(Number(sourceRoute[1]));res.writeHead(200,{'Content-Type':'application/octet-stream','Content-Length':m.bytes.length,'Content-Disposition':`attachment; filename="messaggio-${m.item.id}.txt"`,'Cache-Control':'no-store'});res.end(m.bytes);return;}
if(url.pathname==='/api/winlink/gateways'&&req.method==='GET'){json(res,200,await gatewayDirectory.snapshot(url.searchParams.get('refresh')==='1'));return;}
if(url.pathname==='/api/winlink/radio/restore'&&req.method==='POST'){json(res,200,await winlinkRadio.restore());return;}
if(url.pathname==='/api/winlink/radio/sync'&&req.method==='POST'){const data=await body(req);json(res,200,await winlinkRadio.start(String(data.channelId||'')));return;}
if(url.pathname==='/api/winlink'&&req.method==='GET'){json(res,200,await winlink());return;}
if(url.pathname==='/api/winlink'&&req.method==='GET'){const data=await winlink();data.messages=messageReadState.decorate(data.messages);json(res,200,data);return;}
if(url.pathname==='/api/winlink/sync'&&req.method==='POST'){if(Date.now()-lastCmsRequest<10000)throw new Error('Sincronizzazione già richiesta. Attendi qualche secondo.');await syncNow(config.cmsPeer||'RMS');lastCmsRequest=Date.now();json(res,200,{ok:true});return;}
const readRoute=url.pathname.match(/^\/api\/messages\/(\d+)\/read$/);if(readRoute&&req.method==='POST'){const item=(await messages()).find(m=>m.id===Number(readRoute[1]));if(!item)throw new Error('Messaggio non disponibile');json(res,200,await messageReadState.mark(item));return;}
if(/^\/api\/messages\/\d+\/delete$/.test(url.pathname)&&req.method==='POST'){json(res,200,await deleteMessage(Number(url.pathname.split('/')[3])));return;}
if(/^\/api\/messages\/\d+$/.test(url.pathname)&&req.method==='GET'){const id=url.pathname.split('/').pop();const {key}=await mailContext();const msg=parseMessage(await bpq('/WebMail/WM?'+key+'&'+id));if(!msg)throw new Error('Messaggio non disponibile');try{const raw=await attachedMessage(Number(id));const text=b2Body(raw.bytes);if(text!==null)msg.text=text.replace(/\r\n|\r/g,'\n');}catch{}json(res,200,{id:Number(id),...msg,...await messageTimes(Number(id))});return;}
if(url.pathname==='/api/messages'&&req.method==='POST'){json(res,201,await sendMessage(await readSubmission(req)));return;}
@@ -205,7 +210,7 @@ const server=http.createServer(async(req,res)=>{
}
if(req.method!=='GET'){json(res,405,{error:'Metodo non disponibile'});return;}
const vendor=url.pathname.match(/^\/vendor\/pdfjs\/(pdf\.min\.mjs|pdf\.worker\.min\.mjs|(?:standard_fonts|cmaps)\/[A-Za-z0-9_.-]+)$/);if(vendor&&!vendor[1].includes('..')){const bytes=await readFile(path.join(dir,'public','vendor','pdfjs',vendor[1]));res.writeHead(200,{'Content-Type':vendor[1].endsWith('.mjs')?'text/javascript':'application/octet-stream','Cache-Control':'public, max-age=86400'});res.end(bytes);return;}
const files={'/vendor/leaflet/leaflet.js':'vendor/leaflet/leaflet.js','/vendor/leaflet/leaflet.css':'vendor/leaflet/leaflet.css','/aprs-activity.js':'aprs-activity.js','/aprs-activity.css':'aprs-activity.css','/aprs.js':'aprs.js','/aprs.css':'aprs.css','/radio-ports.js':'radio-ports.js','/radio-ports.css':'radio-ports.css','/compose-attachments.js':'compose-attachments.js','/compose-attachments.css':'compose-attachments.css','/i18n.js':'i18n.js','/i18n.css':'i18n.css','/sync-state.js':'sync-state.js','/sync-progress.js':'sync-progress.js','/sync-progress.css':'sync-progress.css','/radio-control.js':'radio-control.js','/radio-control.css':'radio-control.css','/terminal.js':'terminal.js','/terminal.css':'terminal.css','/winlink-radio.js':'winlink-radio.js','/winlink-radio.css':'winlink-radio.css','/forwarding.js':'forwarding.js','/forwarding.css':'forwarding.css','/journal.js':'journal.js','/journal.css':'journal.css','/world-land.json':'world-land.json','/monitor.js':'monitor.js','/monitor.css':'monitor.css','/':'index.html','/app.js':'app.js','/enhancements.js':'enhancements.js','/notifications.js':'notifications.js','/winlink.js':'winlink.js','/mission.js':'mission.js','/attachments.js':'attachments.js','/mission.css':'mission.css','/style.css':'style.css','/dark.css':'dark.css'};const file=files[url.pathname];if(!file){json(res,404,{error:'Pagina non trovata'});return;}const bytes=await readFile(path.join(dir,'public',file));res.writeHead(200,{'Content-Type':file.endsWith('.json')?'application/json':file.endsWith('.css')?'text/css; charset=utf-8':file.endsWith('.js')?'text/javascript; charset=utf-8':'text/html; charset=utf-8','Cache-Control':'no-cache'});res.end(bytes);
const files={'/busy.js':'busy.js','/busy.css':'busy.css','/vendor/leaflet/leaflet.js':'vendor/leaflet/leaflet.js','/vendor/leaflet/leaflet.css':'vendor/leaflet/leaflet.css','/aprs-activity.js':'aprs-activity.js','/aprs-activity.css':'aprs-activity.css','/aprs.js':'aprs.js','/aprs.css':'aprs.css','/radio-ports.js':'radio-ports.js','/radio-ports.css':'radio-ports.css','/compose-attachments.js':'compose-attachments.js','/compose-attachments.css':'compose-attachments.css','/i18n.js':'i18n.js','/i18n.css':'i18n.css','/sync-state.js':'sync-state.js','/sync-progress.js':'sync-progress.js','/sync-progress.css':'sync-progress.css','/radio-control.js':'radio-control.js','/radio-control.css':'radio-control.css','/terminal.js':'terminal.js','/terminal.css':'terminal.css','/winlink-radio.js':'winlink-radio.js','/winlink-radio.css':'winlink-radio.css','/forwarding.js':'forwarding.js','/forwarding.css':'forwarding.css','/journal.js':'journal.js','/journal.css':'journal.css','/world-land.json':'world-land.json','/monitor.js':'monitor.js','/monitor.css':'monitor.css','/':'index.html','/app.js':'app.js','/enhancements.js':'enhancements.js','/notifications.js':'notifications.js','/winlink.js':'winlink.js','/mission.js':'mission.js','/attachments.js':'attachments.js','/mission.css':'mission.css','/style.css':'style.css','/dark.css':'dark.css'};const file=files[url.pathname];if(!file){json(res,404,{error:'Pagina non trovata'});return;}const bytes=await readFile(path.join(dir,'public',file));res.writeHead(200,{'Content-Type':file.endsWith('.json')?'application/json':file.endsWith('.css')?'text/css; charset=utf-8':file.endsWith('.js')?'text/javascript; charset=utf-8':'text/html; charset=utf-8','Cache-Control':'no-cache'});res.end(bytes);
}catch(e){json(res,502,{error:e.message||'BPQ temporaneamente non disponibile'});}
});
server.listen(port,'0.0.0.0',()=>console.log(`BPQ Console: http://127.0.0.1:${port}`));