mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-19 01:32:19 +00:00
Merge origin/main: shipwrecks, news, transit fronts, signal-lost trails and uncredited photos on the renderer's ports
Upstream's layers and changes are carried onto the ports, with no Cesium outside src/render and no reach into the viewer: - Shipwrecks draw as one icon set, clicked through the input dispatcher. - News draws through draw sets, the camera port and the input dispatcher; its beacons pulse each frame through the frame port. - The scene occlusion probe moves into src/render/cesium and reaches detection through a new application hook, createOcclusionProbe; its private Cesium reads go through internals.js. - Flight and military trails, the stale and SIGNAL LOST cue, coasting and following the world clock run on the draw and frame ports. GPU icons follow the scene clock while the world clock is followed, and fade when stale. A report that holds is not planned again every tick. - Vessels are keyed by their record id. The stub boot now enables military, shipwrecks and news as well.
This commit is contained in:
commit
a755f63638
331 files changed
+40654
-1666
No files matched your search
@@ -0,0 +1,236 @@
|
||||
// Event cards on the globe: a picture, the headline in two lines, and "age ·
|
||||
// sources · sides", with the severity colour down the left edge and the
|
||||
// event's marker as a badge on the picture. Painted on a canvas and handed to
|
||||
// the world overlay as a thumbnail entry, so the overlay places them, keeps
|
||||
// them apart and draws their leader to the marker, as it does CCTV cards.
|
||||
//
|
||||
// The picture is the publisher's own (hotlinked, never copied, credited on
|
||||
// the card); until it loads, or without one, the card shows a tile in the
|
||||
// severity colour with the category icon.
|
||||
|
||||
import { WORLD_OVERLAY_STYLE } from '../../overlays/worldOverlayTokens.js';
|
||||
import { NEWS_SEVERITY } from './model.js';
|
||||
import { paintMarker } from './sprites.js';
|
||||
|
||||
/** Card size in CSS pixels. */
|
||||
export const CARD_W = 168;
|
||||
export const CARD_H = 136;
|
||||
const PAD = 4;
|
||||
const PIC_H = 84;
|
||||
const EDGE = 3;
|
||||
const SCALE = 2; // canvas pixels per CSS pixel
|
||||
const TITLE_FONT = '600 11px Inter, "Noto Sans Arabic", system-ui, sans-serif';
|
||||
const META_FONT = '9px "JetBrains Mono", "Noto Sans Arabic", monospace';
|
||||
const CREDIT_FONT = '8px Inter, "Noto Sans Arabic", system-ui, sans-serif';
|
||||
// The map shows about ten cards plus the clicked one; a few more stay ready
|
||||
// for a pan back. The least recently drawn card goes, and its picture with it.
|
||||
export const MAX_CARDS = 24;
|
||||
|
||||
const pictures = new Map(); // url → {image, state: 'loading'|'ok'|'failed', waiters}
|
||||
const cards = new Map(); // event id → {canvas, key, view, look, pic}, oldest first
|
||||
|
||||
/** Forget a picture no kept card shows any more. */
|
||||
function releasePicture(url) {
|
||||
if (!url || [...cards.values()].some((card) => card.view.image === url))
|
||||
return;
|
||||
const entry = pictures.get(url);
|
||||
if (!entry) return;
|
||||
entry.waiters.clear();
|
||||
entry.image.onload = entry.image.onerror = null;
|
||||
pictures.delete(url);
|
||||
}
|
||||
|
||||
function picture(url, onLoad) {
|
||||
let entry = pictures.get(url);
|
||||
if (!entry) {
|
||||
const image = new Image();
|
||||
// No referrer: the publisher sees a plain image request, as from a link preview.
|
||||
image.referrerPolicy = 'no-referrer';
|
||||
image.decoding = 'async';
|
||||
entry = { image, state: 'loading', waiters: new Set() };
|
||||
pictures.set(url, entry);
|
||||
image.onload = () => {
|
||||
entry.state = 'ok';
|
||||
for (const done of entry.waiters) done();
|
||||
entry.waiters.clear();
|
||||
};
|
||||
image.onerror = () => {
|
||||
entry.state = 'failed';
|
||||
entry.waiters.clear();
|
||||
};
|
||||
image.src = url;
|
||||
}
|
||||
if (entry.state === 'loading' && onLoad) entry.waiters.add(onLoad);
|
||||
return entry;
|
||||
}
|
||||
|
||||
function roundRect(ctx, x, y, w, h, r) {
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x + r, y);
|
||||
ctx.arcTo(x + w, y, x + w, y + h, r);
|
||||
ctx.arcTo(x + w, y + h, x, y + h, r);
|
||||
ctx.arcTo(x, y + h, x, y, r);
|
||||
ctx.arcTo(x, y, x + w, y, r);
|
||||
ctx.closePath();
|
||||
}
|
||||
|
||||
/** `text` in at most `lines` lines of `width`, the last cut with an ellipsis. */
|
||||
export function wrapLines(ctx, text, width, lines) {
|
||||
const words = String(text || '')
|
||||
.split(/\s+/)
|
||||
.filter(Boolean);
|
||||
const out = [];
|
||||
let line = '';
|
||||
for (let i = 0; i < words.length; i++) {
|
||||
const next = line ? `${line} ${words[i]}` : words[i];
|
||||
if (ctx.measureText(next).width <= width || !line) {
|
||||
line = next;
|
||||
continue;
|
||||
}
|
||||
out.push(line);
|
||||
line = words[i];
|
||||
if (out.length === lines - 1) {
|
||||
line = words.slice(i).join(' ');
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (line) out.push(line);
|
||||
const last = out.length - 1;
|
||||
if (last >= 0 && ctx.measureText(out[last]).width > width) {
|
||||
let cut = out[last];
|
||||
while (cut && ctx.measureText(`${cut}…`).width > width)
|
||||
cut = cut.slice(0, -1);
|
||||
out[last] = `${cut.trimEnd()}…`;
|
||||
}
|
||||
return out.slice(0, lines);
|
||||
}
|
||||
|
||||
/** Cover-fit an image into a box. */
|
||||
function drawCover(ctx, image, x, y, w, h) {
|
||||
const iw = image.naturalWidth || image.width;
|
||||
const ih = image.naturalHeight || image.height;
|
||||
if (!iw || !ih) return;
|
||||
const s = Math.max(w / iw, h / ih);
|
||||
const sw = w / s;
|
||||
const sh = h / s;
|
||||
ctx.drawImage(image, (iw - sw) / 2, (ih - sh) / 2, sw, sh, x, y, w, h);
|
||||
}
|
||||
|
||||
function paint(canvas, view, look, pic) {
|
||||
const ctx = canvas.getContext('2d');
|
||||
ctx.setTransform(SCALE, 0, 0, SCALE, 0, 0);
|
||||
ctx.clearRect(0, 0, CARD_W, CARD_H);
|
||||
const color = (NEWS_SEVERITY[look.severity] || NEWS_SEVERITY[0]).color;
|
||||
|
||||
roundRect(ctx, 0.5, 0.5, CARD_W - 1, CARD_H - 1, WORLD_OVERLAY_STYLE.radius);
|
||||
ctx.fillStyle = WORLD_OVERLAY_STYLE.selectedBackground;
|
||||
ctx.fill();
|
||||
ctx.strokeStyle = WORLD_OVERLAY_STYLE.border;
|
||||
ctx.lineWidth = 1;
|
||||
ctx.stroke();
|
||||
// Severity down the left edge.
|
||||
ctx.save();
|
||||
roundRect(ctx, 0.5, 0.5, CARD_W - 1, CARD_H - 1, WORLD_OVERLAY_STYLE.radius);
|
||||
ctx.clip();
|
||||
ctx.fillStyle = color;
|
||||
ctx.fillRect(0, 0, EDGE, CARD_H);
|
||||
ctx.restore();
|
||||
|
||||
const px = PAD + EDGE;
|
||||
const pw = CARD_W - px - PAD;
|
||||
ctx.save();
|
||||
roundRect(ctx, px, PAD, pw, PIC_H, 3);
|
||||
ctx.clip();
|
||||
if (pic?.state === 'ok') {
|
||||
try {
|
||||
drawCover(ctx, pic.image, px, PAD, pw, PIC_H);
|
||||
} catch {
|
||||
pic.state = 'failed';
|
||||
}
|
||||
}
|
||||
if (pic?.state !== 'ok') {
|
||||
// No picture: a plain glass tile with the event's marker.
|
||||
ctx.fillStyle = WORLD_OVERLAY_STYLE.background;
|
||||
ctx.fillRect(px, PAD, pw, PIC_H);
|
||||
ctx.fillStyle = 'rgba(255, 255, 255, 0.04)';
|
||||
ctx.fillRect(px, PAD, pw, PIC_H);
|
||||
paintMarker(ctx, look, px + pw / 2, PAD + PIC_H / 2, 34);
|
||||
}
|
||||
ctx.restore();
|
||||
// The event's marker as a badge, so the card reads with the map.
|
||||
if (pic?.state === 'ok') paintMarker(ctx, look, px + 13, PAD + 13, 18);
|
||||
// The picture's publisher, always named on it.
|
||||
if (pic?.state === 'ok' && view.credit) {
|
||||
ctx.font = CREDIT_FONT;
|
||||
const w = Math.min(pw - 30, ctx.measureText(view.credit).width + 8);
|
||||
ctx.fillStyle = 'rgba(0, 0, 0, 0.6)';
|
||||
ctx.fillRect(px + pw - w, PAD + PIC_H - 12, w, 12);
|
||||
ctx.fillStyle = WORLD_OVERLAY_STYLE.title;
|
||||
ctx.textBaseline = 'middle';
|
||||
ctx.fillText(
|
||||
wrapLines(ctx, view.credit, w - 8, 1)[0] || '',
|
||||
px + pw - w + 4,
|
||||
PAD + PIC_H - 6,
|
||||
);
|
||||
}
|
||||
|
||||
ctx.textBaseline = 'alphabetic';
|
||||
ctx.fillStyle = WORLD_OVERLAY_STYLE.title;
|
||||
ctx.font = TITLE_FONT;
|
||||
wrapLines(ctx, view.title, pw, 2).forEach((line, i) =>
|
||||
ctx.fillText(line, px, PAD + PIC_H + 14 + i * 13),
|
||||
);
|
||||
ctx.fillStyle = WORLD_OVERLAY_STYLE.detail;
|
||||
ctx.font = META_FONT;
|
||||
ctx.fillText(wrapLines(ctx, view.meta, pw, 1)[0] || '', px, CARD_H - PAD - 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* The canvas of an event's card, one per event: a change in what it shows
|
||||
* (the age in its meta line ticks by the minute) repaints the same canvas.
|
||||
* `onChange` runs when its picture arrives and the canvas has been repainted
|
||||
* (ask the scene for a frame).
|
||||
* @returns {HTMLCanvasElement|null} null where there is no canvas
|
||||
*/
|
||||
export function eventCard(id, view, look, onChange) {
|
||||
const key = `${look.severity}|${look.category}|${look.rim}|${view.image || ''}|${view.credit || ''}|${view.title}|${view.meta}`;
|
||||
let card = cards.get(id);
|
||||
if (card) {
|
||||
// Most recently drawn last.
|
||||
cards.delete(id);
|
||||
cards.set(id, card);
|
||||
if (card.key === key) return card.canvas;
|
||||
} else {
|
||||
const canvas = globalThis.document?.createElement?.('canvas');
|
||||
if (
|
||||
!canvas?.getContext?.('2d') ||
|
||||
typeof globalThis.Path2D !== 'function' ||
|
||||
typeof globalThis.Image !== 'function'
|
||||
)
|
||||
return null;
|
||||
canvas.width = CARD_W * SCALE;
|
||||
canvas.height = CARD_H * SCALE;
|
||||
card = { canvas, key: null, view: {}, look, pic: null };
|
||||
cards.set(id, card);
|
||||
while (cards.size > MAX_CARDS) {
|
||||
const [oldest, gone] = cards.entries().next().value;
|
||||
cards.delete(oldest);
|
||||
releasePicture(gone.view.image);
|
||||
}
|
||||
}
|
||||
const previous = card.view.image;
|
||||
card.key = key;
|
||||
card.view = view;
|
||||
card.look = look;
|
||||
const shown = card;
|
||||
card.pic = view.image
|
||||
? picture(view.image, () => {
|
||||
if (cards.get(id) !== shown || shown.view.image !== view.image) return;
|
||||
paint(shown.canvas, shown.view, shown.look, shown.pic);
|
||||
onChange?.();
|
||||
})
|
||||
: null;
|
||||
if (previous && previous !== view.image) releasePicture(previous);
|
||||
paint(card.canvas, view, look, card.pic);
|
||||
return card.canvas;
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { MAX_CARDS, eventCard } from './cards.js';
|
||||
|
||||
const look = { severity: 3, category: 'strike', rim: 'confirmed', size: 20 };
|
||||
const view = (extra = {}) => ({
|
||||
title: 'Strike on Kharkiv',
|
||||
meta: '10 min · 6 sources · UA, RU',
|
||||
image: 'https://a.test/p.jpg',
|
||||
credit: 'A',
|
||||
...extra,
|
||||
});
|
||||
|
||||
// Node has no canvas: a 2D context that accepts every call, and an Image that
|
||||
// records what was asked for.
|
||||
function fakeBrowser(t) {
|
||||
const saved = {
|
||||
document: globalThis.document,
|
||||
Path2D: globalThis.Path2D,
|
||||
Image: globalThis.Image,
|
||||
};
|
||||
const images = [];
|
||||
const ctx = new Proxy(
|
||||
{},
|
||||
{
|
||||
get: (_target, name) =>
|
||||
name === 'measureText'
|
||||
? (text) => ({ width: String(text).length * 5 })
|
||||
: name === 'createLinearGradient'
|
||||
? () => ({ addColorStop() {} })
|
||||
: () => {},
|
||||
set: () => true,
|
||||
},
|
||||
);
|
||||
let canvases = 0;
|
||||
globalThis.document = {
|
||||
createElement: () => {
|
||||
canvases++;
|
||||
return { getContext: () => ctx };
|
||||
},
|
||||
};
|
||||
globalThis.Path2D = class {};
|
||||
globalThis.Image = class {
|
||||
constructor() {
|
||||
images.push(this);
|
||||
}
|
||||
};
|
||||
t.after(() => {
|
||||
for (const [name, value] of Object.entries(saved))
|
||||
if (value === undefined) delete globalThis[name];
|
||||
else globalThis[name] = value;
|
||||
});
|
||||
return { images, canvases: () => canvases };
|
||||
}
|
||||
|
||||
test('a card keeps one canvas per event while its age ticks', (t) => {
|
||||
const browser = fakeBrowser(t);
|
||||
const first = eventCard('ev-a', view(), look);
|
||||
assert.ok(first);
|
||||
// The meta line's minute-level age changes: the same canvas, repainted.
|
||||
const later = eventCard('ev-a', view({ meta: '11 min · 6 sources' }), look);
|
||||
assert.equal(later, first);
|
||||
assert.equal(browser.canvases(), 1);
|
||||
// One picture request for the event, however often it is drawn.
|
||||
assert.equal(browser.images.length, 1);
|
||||
// Its picture arriving repaints and asks for a frame once; a card with no
|
||||
// picture never does.
|
||||
let frames = 0;
|
||||
const pictured = eventCard(
|
||||
'ev-b',
|
||||
view({ image: 'https://b.test/p.jpg' }),
|
||||
look,
|
||||
() => frames++,
|
||||
);
|
||||
assert.ok(pictured);
|
||||
browser.images.at(-1).onload();
|
||||
assert.equal(frames, 1);
|
||||
assert.ok(eventCard('ev-c', view({ image: null }), look));
|
||||
});
|
||||
|
||||
test('the card cache is bounded and evicts pictures with their cards', (t) => {
|
||||
const browser = fakeBrowser(t);
|
||||
const kept = eventCard('keep', view({ image: 'https://k.test/0.jpg' }), look);
|
||||
for (let i = 0; i < MAX_CARDS * 3; i++) {
|
||||
eventCard(`ev-${i}`, view({ image: `https://p.test/${i}.jpg` }), look);
|
||||
// A card drawn every frame stays: the least recently drawn goes first.
|
||||
assert.equal(
|
||||
eventCard('keep', view({ image: 'https://k.test/0.jpg' }), look),
|
||||
kept,
|
||||
);
|
||||
}
|
||||
const requested = browser.images.length;
|
||||
// The first event was evicted with its picture: drawing it again paints a
|
||||
// new canvas and asks for the picture anew.
|
||||
const again = eventCard(
|
||||
'ev-0',
|
||||
view({ image: 'https://p.test/0.jpg' }),
|
||||
look,
|
||||
);
|
||||
assert.ok(again);
|
||||
assert.equal(browser.images.length, requested + 1);
|
||||
// Evicted pictures are let go: their load handlers are cleared.
|
||||
assert.equal(browser.images[1].onload, null);
|
||||
// The recent ones are still cached.
|
||||
const recent = MAX_CARDS * 3 - 1;
|
||||
eventCard(
|
||||
`ev-${recent}`,
|
||||
view({ image: `https://p.test/${recent}.jpg` }),
|
||||
look,
|
||||
);
|
||||
assert.equal(browser.images.length, requested + 1);
|
||||
});
|
||||
|
||||
test('without a canvas no card is made and nothing is cached', (t) => {
|
||||
fakeBrowser(t);
|
||||
globalThis.document = { createElement: () => ({ getContext: () => null }) };
|
||||
assert.equal(eventCard('none', view(), look), null);
|
||||
globalThis.document = undefined;
|
||||
assert.equal(eventCard('none', view(), look), null);
|
||||
});
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,826 @@
|
||||
// Pure presentation model for the news layer: severity, category glyphs,
|
||||
// attestation, marker looks and placement, the feed's filters and paging, and
|
||||
// the event cards the News panel and the globe show. No renderer, no DOM.
|
||||
|
||||
import { getLocale, msg, t, tn } from '../../i18n/index.js';
|
||||
|
||||
export const NEWS_LAYER_ID = 'news';
|
||||
export const NEWS_OVERLAY_SOURCE_ID = 'news';
|
||||
export const NEWS_PICK_PREFIX = 'news:';
|
||||
export const NEWS_FEED_PAGE = 60;
|
||||
export const NEWS_MAP_LIMIT = 500;
|
||||
/** News is real time only: nothing older than a day is shown anywhere. */
|
||||
export const NEWS_WINDOW_MS = 24 * 3_600_000;
|
||||
|
||||
/**
|
||||
* The window a server serves, in ms: its `windowHours` (a whole number of
|
||||
* hours up to 24; a host may serve fewer), else the full day.
|
||||
*/
|
||||
export function newsWindowMs(hours) {
|
||||
return Number.isInteger(hours) && hours >= 1 && hours <= 24
|
||||
? hours * 3_600_000
|
||||
: NEWS_WINDOW_MS;
|
||||
}
|
||||
|
||||
/** The windows the News panel offers, longest first. */
|
||||
export const NEWS_WINDOW_CHOICES = Object.freeze([24, 12, 6, 3, 1]);
|
||||
|
||||
/**
|
||||
* The windows offered under a server's own window of `maxHours`: that window
|
||||
* itself, then each shorter choice.
|
||||
*/
|
||||
export function newsWindowChoices(maxHours) {
|
||||
const cap = newsWindowMs(maxHours) / 3_600_000;
|
||||
return [cap, ...NEWS_WINDOW_CHOICES.filter((hours) => hours < cap)];
|
||||
}
|
||||
// A publish time this far ahead of the clock is skew, not a future story
|
||||
// (as data/regionalNewsWindow.js).
|
||||
const NEWS_CLOCK_SKEW_MS = 15 * 60_000;
|
||||
/** At most this many reports are kept in the feed list, newest first. */
|
||||
export const NEWS_FEED_MAX = 600;
|
||||
// Above this camera height the layer shows one beacon per hotspot; below it
|
||||
// (or once a hotspot is chosen) the outlines and individual reports.
|
||||
export const NEWS_DETAIL_MAX_HEIGHT_M = 2_500_000;
|
||||
// "In view": reports near the view centre, only once the camera is low enough
|
||||
// for "here" to mean a region, read again when it settles in a new cell.
|
||||
// Near is inside the area the server names there (a country, a large
|
||||
// country's state, a disputed area), else, at sea, within the radius.
|
||||
export const NEWS_IN_VIEW_MAX_HEIGHT_M = 3_000_000;
|
||||
export const NEWS_IN_VIEW_RADIUS_KM = 500;
|
||||
export const NEWS_IN_VIEW_LIMIT = 12;
|
||||
/** The Local section lists at most this many reports near the view centre. */
|
||||
export const NEWS_LOCAL_LIMIT = 40;
|
||||
/**
|
||||
* The News panel's sections: Conflicts (the hotspots' significant news, on the
|
||||
* map whenever the layer is on) and Local (everything near the view centre,
|
||||
* on the map while the section is chosen).
|
||||
*/
|
||||
export const NEWS_SECTIONS = Object.freeze(['conflicts', 'local']);
|
||||
export const NEWS_SETTLE_MS = 800;
|
||||
export const NEWS_CELL_DEG = 0.5;
|
||||
/** A feed card's click flies the camera to this height over the event. */
|
||||
export const NEWS_FOCUS_HEIGHT_M = 150_000;
|
||||
// Minor news (a broad outlet's prices, sport, health) is left out unless asked for.
|
||||
export const NEWS_RELEVANT = 'core,related';
|
||||
|
||||
const SOURCE_LABEL = msg('Source');
|
||||
const DISPUTED = msg('Disputed: {name}');
|
||||
const VIA = msg('via {source}');
|
||||
const MEDIA_LABELS = { video: msg('Video'), photo: msg('Photos') };
|
||||
const CLAIMED_BY = msg('Claimed by {who}');
|
||||
const CLAIMED = msg('Claimed');
|
||||
const UNCONFIRMED = msg('Not yet confirmed by others');
|
||||
const REPORTED_BY_SIDE = msg('Reported by {side} sources');
|
||||
const REPORTED_BY_SIDES = msg('Reported by {sides}');
|
||||
const SIDE_SOURCES = msg('{side} sources');
|
||||
const OFFICIAL = msg('Official');
|
||||
const OFFICIAL_AFFILIATIONS = new Set(['military', 'state']);
|
||||
const ATTESTATION = {
|
||||
'both-sides': msg('Reported by both sides'),
|
||||
independent: msg('Independently reported'),
|
||||
single: msg('Single source'),
|
||||
};
|
||||
const NEUTRAL_SIDES = Object.freeze({
|
||||
intl: msg('International'),
|
||||
independent: msg('Independent'),
|
||||
unknown: msg('Unattributed'),
|
||||
external: msg('Outside party'),
|
||||
});
|
||||
|
||||
/**
|
||||
* Short chip names for the curated hotspots; the API's own English name is
|
||||
* the fallback for any other.
|
||||
*/
|
||||
export const NEWS_HOTSPOT_LABELS = Object.freeze({
|
||||
'ua-ru-war': msg('Ukraine–Russia'),
|
||||
'israel-gaza-lebanon': msg('Israel–Gaza–Lebanon'),
|
||||
'iran-hormuz': msg('Iran–Hormuz'),
|
||||
yemen: msg('Yemen–Red Sea'),
|
||||
'sudan-war': msg('Sudan'),
|
||||
sahel: msg('Sahel'),
|
||||
elsewhere: msg('Elsewhere'),
|
||||
// Core's own news (no news server): the world is one hotspot.
|
||||
worldwide: msg('Worldwide'),
|
||||
});
|
||||
|
||||
/** A hotspot's chip label in the interface language. */
|
||||
export function hotspotLabel(hotspot) {
|
||||
const key = NEWS_HOTSPOT_LABELS[hotspot?.id];
|
||||
if (key) return t(key);
|
||||
return hotspot?.catchAll ? t(NEWS_HOTSPOT_LABELS.elsewhere) : hotspot?.name;
|
||||
}
|
||||
|
||||
/** A country's name in the interface language, from its ISO code. */
|
||||
export function countryName(code) {
|
||||
if (!code) return '';
|
||||
try {
|
||||
return (
|
||||
new Intl.DisplayNames([getLocale(), 'en'], { type: 'region' }).of(code) ||
|
||||
code
|
||||
);
|
||||
} catch {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
const AREA_IN_COUNTRY = msg('{region}, {country}');
|
||||
|
||||
/**
|
||||
* The Local section's area in the interface language: a country by its code,
|
||||
* a state with its country, a disputed area by its own name only.
|
||||
*/
|
||||
export function areaLabel(area) {
|
||||
if (!area) return '';
|
||||
if (area.kind === 'disputed' || !area.country) return area.name;
|
||||
if (area.kind === 'country') return countryName(area.country);
|
||||
return t(AREA_IN_COUNTRY, {
|
||||
region: area.name,
|
||||
country: countryName(area.country),
|
||||
});
|
||||
}
|
||||
|
||||
/** Label for a disputed area on the map: its name, marked as disputed. */
|
||||
export const disputedLabel = (area) => t(DISPUTED, { name: area.name });
|
||||
|
||||
// A category is told by its icon (a 16-unit SVG path, filled or stroked). On
|
||||
// the map and in the feed colour is severity (NEWS_SEVERITY), never the type
|
||||
// of event or a side.
|
||||
export const NEWS_CATEGORIES = Object.freeze({
|
||||
strike: {
|
||||
label: msg('Strike'),
|
||||
icon: 'M8 0.8 L9.6 5.2 L14.2 3.6 L11.2 7.6 L15.2 10.6 L10.3 10.6 L8 15.2 L5.7 10.6 L0.8 10.6 L4.8 7.6 L1.8 3.6 L6.4 5.2 Z',
|
||||
solid: true,
|
||||
},
|
||||
// A threat on its way (a drone's course, an air-raid alert): nothing has
|
||||
// hit yet. Not a physical event, so never a picture card.
|
||||
alert: {
|
||||
label: msg('Alert'),
|
||||
icon: 'M1.5 8 H11.5 M8 4.5 L11.5 8 L8 11.5 M14.5 3.5 V12.5',
|
||||
},
|
||||
'air-defence': {
|
||||
label: msg('Air defence'),
|
||||
icon: 'M8 1.5 L13.5 3.5 V8 C13.5 11.3 10.8 13.4 8 14.6 C5.2 13.4 2.5 11.3 2.5 8 V3.5 Z M8 4.5 V11',
|
||||
},
|
||||
ground: {
|
||||
label: msg('Ground fighting'),
|
||||
icon: 'M3 13 L12 4 M8.5 3.5 H12.5 V7.5 M13 13 L4 4 M3.5 7.5 V3.5 H7.5',
|
||||
},
|
||||
casualties: {
|
||||
label: msg('Casualties'),
|
||||
icon: 'M6.2 2 H9.8 V6.2 H14 V9.8 H9.8 V14 H6.2 V9.8 H2 V6.2 H6.2 Z',
|
||||
solid: true,
|
||||
},
|
||||
humanitarian: {
|
||||
label: msg('Humanitarian'),
|
||||
icon: 'M8 2.5 L14.5 13.5 H1.5 Z M8 2.5 V13.5',
|
||||
},
|
||||
infrastructure: {
|
||||
label: msg('Infrastructure'),
|
||||
icon: 'M9.2 1 L3.2 9 H7.6 L6.6 15 L12.8 6.8 H8.4 Z',
|
||||
solid: true,
|
||||
},
|
||||
disaster: {
|
||||
label: msg('Disaster'),
|
||||
icon: 'M1.5 6.5 C3.5 4.5 5.5 4.5 7.5 6.5 S11.5 8.5 14.5 6.5 M1.5 10.5 C3.5 8.5 5.5 8.5 7.5 10.5 S11.5 12.5 14.5 10.5',
|
||||
},
|
||||
protest: {
|
||||
label: msg('Protest'),
|
||||
icon: 'M2.5 6.2 V9.8 H5 L12.5 13.2 V2.8 L5 6.2 Z',
|
||||
},
|
||||
political: {
|
||||
label: msg('Political'),
|
||||
icon: 'M2 14 H14 M2.5 5.5 H13.5 M8 1.8 L14 4.8 H2 Z M4.2 6.5 V12 M8 6.5 V12 M11.8 6.5 V12',
|
||||
},
|
||||
// Business, trade and industry: a national feed's news in Local.
|
||||
economy: {
|
||||
label: msg('Economy'),
|
||||
icon: 'M2 13.5 H14 M2.5 11 L6 7.5 L8.5 9.5 L13.5 4.5 M10.5 4.5 H13.5 V7.5',
|
||||
},
|
||||
other: {
|
||||
label: msg('Other'),
|
||||
icon: 'M8 4.5 A3.5 3.5 0 1 0 8.01 4.5 Z',
|
||||
solid: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Severity 0–3, in colours GEV already uses: its dim and primary text, the
|
||||
// military layers' amber and the Cockpit's emergency red. They are drawn on
|
||||
// a dark disc, so from minor up lightness falls as severity rises and the
|
||||
// ramp still reads under colour blindness and in greyscale.
|
||||
export const NEWS_SEVERITY = Object.freeze([
|
||||
{ label: msg('No physical event'), color: '#7c878d' },
|
||||
{ label: msg('Minor incident'), color: '#e8eaed' },
|
||||
{ label: msg('Significant'), color: '#ffb800' },
|
||||
{ label: msg('Major'), color: '#ff4444' },
|
||||
]);
|
||||
/** The feed's default floor: significant and major events. */
|
||||
export const NEWS_DEFAULT_MIN_SEVERITY = 2;
|
||||
const PHYSICAL = new Set([
|
||||
'strike',
|
||||
'air-defence',
|
||||
'ground',
|
||||
'casualties',
|
||||
'infrastructure',
|
||||
'disaster',
|
||||
]);
|
||||
|
||||
export const categoryStyle = (category) =>
|
||||
NEWS_CATEGORIES[category] || NEWS_CATEGORIES.other;
|
||||
export const categoryLabel = (category) => t(categoryStyle(category).label);
|
||||
/** Party codes (UA, RU, IL…) are shown as-is; neutral sides are translated. */
|
||||
export const sideLabel = (side) =>
|
||||
Object.hasOwn(NEUTRAL_SIDES, side) ? t(NEUTRAL_SIDES[side]) : side;
|
||||
|
||||
/**
|
||||
* A report's severity, 0–3: the server's (the local model's reading, else
|
||||
* its rules, raised to its event's), or a fallback from the category.
|
||||
*/
|
||||
export function reportSeverity(report) {
|
||||
const own = Number.isInteger(report?.severity) ? report.severity : null;
|
||||
const event = Number.isInteger(report?.event?.severity)
|
||||
? report.event.severity
|
||||
: null;
|
||||
if (own === null && event === null)
|
||||
return PHYSICAL.has(report?.category) ? 1 : 0;
|
||||
return Math.max(own ?? 0, event ?? 0);
|
||||
}
|
||||
export const severityStyle = (report) => NEWS_SEVERITY[reportSeverity(report)];
|
||||
export const severityLabel = (report) => t(severityStyle(report).label);
|
||||
|
||||
/** When a report (or its event) was last heard of, in ms; NaN when unknown. */
|
||||
const lastHeard = (report) =>
|
||||
Date.parse(report?.event?.lastAt || report?.publishedAt || '');
|
||||
|
||||
/**
|
||||
* Whether a time is inside the window (the last 24 hours unless the server
|
||||
* serves fewer; the only news shown); one more than 15 minutes ahead of the
|
||||
* clock is not.
|
||||
*/
|
||||
function inWindow(value, now, windowMs = NEWS_WINDOW_MS) {
|
||||
const at = Date.parse(value || '');
|
||||
return (
|
||||
Number.isFinite(at) &&
|
||||
now - at <= windowMs &&
|
||||
at - now <= NEWS_CLOCK_SKEW_MS
|
||||
);
|
||||
}
|
||||
|
||||
/** Whether a report is inside the window (the only news shown). */
|
||||
export function withinNewsWindow(
|
||||
report,
|
||||
now = Date.now(),
|
||||
windowMs = NEWS_WINDOW_MS,
|
||||
) {
|
||||
return inWindow(report?.publishedAt, now, windowMs);
|
||||
}
|
||||
|
||||
/** The event's one-report-per-side list, inside the last 24 hours only. */
|
||||
const recentSides = (report, now) =>
|
||||
(report?.event?.sides || []).filter((s) => inWindow(s.publishedAt, now));
|
||||
|
||||
/**
|
||||
* How a report or its event is attested, as a chip: `claimed` (someone's
|
||||
* claim, named where known), `confirmed` (both sides, or an independent
|
||||
* source), `side` (one party's own press), `single` (one report), or null.
|
||||
*/
|
||||
export function attestation(report) {
|
||||
const event = report?.event;
|
||||
const status = event?.status || (report?.claim ? 'claimed' : null);
|
||||
if (status === 'claimed') {
|
||||
const who = event?.claimedBy || report?.claim?.by;
|
||||
return {
|
||||
kind: 'claimed',
|
||||
label: who ? t(CLAIMED_BY, { who }) : t(CLAIMED),
|
||||
};
|
||||
}
|
||||
if (status === 'both-sides' || status === 'independent')
|
||||
return { kind: 'confirmed', label: t(ATTESTATION[status]) };
|
||||
if (status === 'side' && event.reportedSide)
|
||||
return {
|
||||
kind: 'side',
|
||||
label: t(REPORTED_BY_SIDE, { side: sideLabel(event.reportedSide) }),
|
||||
};
|
||||
if (status === 'single')
|
||||
return { kind: 'single', label: t(ATTESTATION.single) };
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The attestation chip under a report. An official channel's own post is
|
||||
* already marked Official: "Claimed by" itself would read as doubt about an
|
||||
* alert. It says only that nobody else has reported it yet, and nothing for
|
||||
* an alert, which is the channel's own notice.
|
||||
*/
|
||||
function ownClaimChip(report) {
|
||||
const chip = attestation(report);
|
||||
if (
|
||||
chip?.kind !== 'claimed' ||
|
||||
!OFFICIAL_AFFILIATIONS.has(report.affiliation)
|
||||
)
|
||||
return chip;
|
||||
const who = report.event?.claimedBy || report.claim?.by;
|
||||
if (who && who !== report.sourceName && who !== report.publisher) return chip;
|
||||
return report.category === 'alert'
|
||||
? null
|
||||
: { kind: 'claimed', label: t(UNCONFIRMED) };
|
||||
}
|
||||
|
||||
// The map card is a label, not a reader: long headlines are cut.
|
||||
export const NEWS_CARD_TITLE_MAX = 90;
|
||||
|
||||
/** A headline cut at a word to fit `max` characters. */
|
||||
export function shortTitle(title, max = NEWS_CARD_TITLE_MAX) {
|
||||
const text = String(title || '');
|
||||
if (text.length <= max) return text;
|
||||
const cut = text.slice(0, max - 1);
|
||||
const word = cut.lastIndexOf(' ');
|
||||
return `${word > max * 0.6 ? cut.slice(0, word) : cut}…`;
|
||||
}
|
||||
|
||||
/** The media a post carries ("Video", "Photos"), translated. */
|
||||
export function mediaLabels(report) {
|
||||
return (report?.media || [])
|
||||
.map((m) => (MEDIA_LABELS[m] ? t(MEDIA_LABELS[m]) : ''))
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
/**
|
||||
* How long ago, in few words: "now" under a minute, "N min" under an hour,
|
||||
* "N h" under a day. Older is never shown; it reads
|
||||
* "24 h".
|
||||
*/
|
||||
export function ageWords(at, now = Date.now()) {
|
||||
const minutes = Math.max(0, Math.floor((now - Date.parse(at)) / 60_000));
|
||||
if (!Number.isFinite(minutes)) return '';
|
||||
if (minutes < 1) return t('now', { context: 'age' });
|
||||
if (minutes < 60) return t('{minutes} min', { minutes });
|
||||
return t('{hours} h', { hours: Math.min(24, Math.floor(minutes / 60)) });
|
||||
}
|
||||
|
||||
/**
|
||||
* How a report's marker looks: colour = severity, icon = category, rim =
|
||||
* attestation (`confirmed` solid, `claimed` dashed, else thin), size =
|
||||
* sources, opacity = age (fading to 45 % over a day).
|
||||
*/
|
||||
export function markerStyle(report, now = Date.now()) {
|
||||
const kind = attestation(report)?.kind;
|
||||
const at = lastHeard(report);
|
||||
const hours = Number.isFinite(at) ? Math.max(0, (now - at) / 3_600_000) : 0;
|
||||
return {
|
||||
category: Object.hasOwn(NEWS_CATEGORIES, report?.category)
|
||||
? report.category
|
||||
: 'other',
|
||||
severity: reportSeverity(report),
|
||||
rim: kind === 'confirmed' || kind === 'claimed' ? kind : 'thin',
|
||||
size: eventPixelSize(report?.event?.size),
|
||||
alpha: Math.max(0.45, 1 - (0.55 * Math.min(hours, 24)) / 24),
|
||||
};
|
||||
}
|
||||
|
||||
function hash(text) {
|
||||
let h = 2166136261;
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
h ^= text.charCodeAt(i);
|
||||
h = Math.imul(h, 16777619);
|
||||
}
|
||||
return h >>> 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trend glyph for a hotspot: ▲/▼ when the last 24 h differ from the 24 h
|
||||
* before by more than a fifth, nothing when flat or without a prior day.
|
||||
*/
|
||||
export function hotspotTrend(last24h, prev24h) {
|
||||
if (!(prev24h > 0)) return '';
|
||||
if (last24h > prev24h * 1.2) return '▲';
|
||||
if (last24h < prev24h * 0.8) return '▼';
|
||||
return '';
|
||||
}
|
||||
|
||||
/** Beacon size in pixels: grows with the square root of daily activity. */
|
||||
export const beaconPixelSize = (last24h) =>
|
||||
Math.round(Math.min(26, 10 + Math.sqrt(Math.max(0, last24h)) * 1.4));
|
||||
|
||||
/** Marker size for an event: grows with the number of distinct sources. */
|
||||
export function eventPixelSize(sources = 1) {
|
||||
return Math.round(
|
||||
// No smaller than 24: the category icon is half the disc and unreadable
|
||||
// below that.
|
||||
Math.min(36, 24 + 3 * Math.log2(Math.max(1, Number(sources) || 1))),
|
||||
);
|
||||
}
|
||||
|
||||
// An event several sources report within the last few hours gets a halo.
|
||||
export const NEWS_PRESSING_SOURCES = 5;
|
||||
const PRESSING_MS = 3 * 3_600_000;
|
||||
|
||||
/** Whether an event is fresh and widely reported, or fresh and major. */
|
||||
export function isPressing(report, now = Date.now()) {
|
||||
const event = report?.event;
|
||||
const fresh = now - lastHeard(report) <= PRESSING_MS;
|
||||
return Boolean(
|
||||
fresh &&
|
||||
((event && event.size >= NEWS_PRESSING_SOURCES) ||
|
||||
reportSeverity(report) === 3),
|
||||
);
|
||||
}
|
||||
|
||||
// The big events stand as picture cards over their marker: several
|
||||
// sources, and major or pressing.
|
||||
export const NEWS_CARD_LIMIT = 10;
|
||||
export const NEWS_CARD_MIN_SOURCES = 2;
|
||||
|
||||
/** Whether an event gets a card on the map: physical events only, not statements. */
|
||||
export function isCardEvent(report, now = Date.now()) {
|
||||
return Boolean(
|
||||
report?.event?.size >= NEWS_CARD_MIN_SOURCES &&
|
||||
PHYSICAL.has(report.category) &&
|
||||
(reportSeverity(report) === 3 || isPressing(report, now)),
|
||||
);
|
||||
}
|
||||
|
||||
/** The card events among `reports`, most severe, most reported and newest first. */
|
||||
export function cardEvents(reports, now = Date.now(), limit = NEWS_CARD_LIMIT) {
|
||||
return reports
|
||||
.filter((r) => r.pins?.length && isCardEvent(r, now))
|
||||
.sort(
|
||||
(a, b) =>
|
||||
reportSeverity(b) - reportSeverity(a) ||
|
||||
b.event.size - a.event.size ||
|
||||
(b.event.lastAt || '').localeCompare(a.event.lastAt || ''),
|
||||
)
|
||||
.slice(0, limit);
|
||||
}
|
||||
|
||||
/** The distinct sides an event was reported by in the last day, lead first. */
|
||||
function eventSides(report, now) {
|
||||
const sides = recentSides(report, now).map((s) => s.side);
|
||||
return [...new Set(sides.length ? sides : [report?.side].filter(Boolean))];
|
||||
}
|
||||
|
||||
/**
|
||||
* "Reported by UA sources · RU sources", or '' when no side is known; an
|
||||
* unattributed side is left out.
|
||||
*/
|
||||
export function sidesLine(report, now = Date.now()) {
|
||||
const sides = eventSides(report, now).filter((side) => side !== 'unknown');
|
||||
if (!sides.length) return '';
|
||||
if (sides.length === 1)
|
||||
return t(REPORTED_BY_SIDE, { side: sideLabel(sides[0]) });
|
||||
return t(REPORTED_BY_SIDES, {
|
||||
sides: sides
|
||||
.map((side) => t(SIDE_SOURCES, { side: sideLabel(side) }))
|
||||
.join(' · '),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The event's picture, the publisher named, and its clip when it has one. The
|
||||
* panel shows it for any event; the globe's picture cards are for events of
|
||||
* two or more sources (isCardEvent).
|
||||
*/
|
||||
export function eventPicture(report) {
|
||||
const image = report?.event?.image;
|
||||
if (!image) return null;
|
||||
return {
|
||||
url: image.url,
|
||||
credit: image.publisher || '',
|
||||
...(image.video ? { video: image.video } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
/** What an event's picture card on the globe shows. */
|
||||
export function cardView(report, now = Date.now()) {
|
||||
const event = report.event;
|
||||
const sides = eventSides(report, now).map(sideLabel);
|
||||
// A globe card's picture needs two or more sources, as the card does.
|
||||
const picture =
|
||||
report?.event?.size >= NEWS_CARD_MIN_SOURCES ? eventPicture(report) : null;
|
||||
return {
|
||||
title: report.titleEn || report.title,
|
||||
meta: [
|
||||
ageWords(event?.lastAt || report.publishedAt, now),
|
||||
tn(event?.size || 1, '{count} source', '{count} sources'),
|
||||
sides.slice(0, 3).join(', ') + (sides.length > 3 ? '…' : ''),
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(' · '),
|
||||
image: picture?.url || null,
|
||||
credit: picture?.credit || '',
|
||||
};
|
||||
}
|
||||
|
||||
/** "Source: host ↗" line for a report's card; the card opens the article. */
|
||||
export function sourceLinkLabel(url) {
|
||||
let host = '';
|
||||
try {
|
||||
host = new URL(url).hostname.replace(/^www\./, '');
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
return `${t(SOURCE_LABEL)}: ${host} ↗`;
|
||||
}
|
||||
|
||||
/**
|
||||
* "via GDELT Project": the service a report was read through, which asks to
|
||||
* be credited where its reports show; '' when the server names none.
|
||||
*/
|
||||
export function attributionLabel(report) {
|
||||
return report?.attribution ? t(VIA, { source: report.attribution }) : '';
|
||||
}
|
||||
|
||||
/** The places a report is pinned at, joined, or null. */
|
||||
function placeLine(report) {
|
||||
return (
|
||||
(report.pins?.length ? report.pins : [report.geo])
|
||||
.map((p) => p?.place)
|
||||
.filter(Boolean)
|
||||
.join(' · ') || null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* One event card in the News panel: picture,
|
||||
* headline, a mono meta line (severity, category, place, sources, age, the
|
||||
* service it came through), the sides line, then attestation and media chips. Labels and colours are
|
||||
* resolved here so the panel imports no layer code.
|
||||
*/
|
||||
export function feedCardView(report, now = Date.now()) {
|
||||
const severity = reportSeverity(report);
|
||||
const style = categoryStyle(report.category);
|
||||
const size = report.event?.size || 1;
|
||||
const translated = Boolean(report.titleEn && report.titleEn !== report.title);
|
||||
return {
|
||||
id: report.id,
|
||||
url: report.url,
|
||||
title: translated ? report.titleEn : report.title,
|
||||
// The local model's English headline over a post in another language:
|
||||
// the original stays beside it.
|
||||
original: translated ? report.title : null,
|
||||
snippet: report.snippet,
|
||||
severity,
|
||||
severityLabel: t(NEWS_SEVERITY[severity].label),
|
||||
color: NEWS_SEVERITY[severity].color,
|
||||
icon: style.icon,
|
||||
solid: Boolean(style.solid),
|
||||
category: categoryLabel(report.category),
|
||||
place: placeLine(report),
|
||||
placedByModel: Boolean(report.geo?.byModel),
|
||||
mappable: Boolean(report.pins?.length),
|
||||
sources: tn(size, '{count} source', '{count} sources'),
|
||||
age: ageWords(report.event?.lastAt || report.publishedAt, now),
|
||||
publisher: report.publisher,
|
||||
via: attributionLabel(report),
|
||||
sides: sidesLine(report, now),
|
||||
sourceLink: sourceLinkLabel(report.url),
|
||||
picture: eventPicture(report),
|
||||
chips: [
|
||||
// An official channel speaking for itself (a ministry, the armed
|
||||
// forces, a press service), not an outlet that quotes one.
|
||||
OFFICIAL_AFFILIATIONS.has(report.affiliation)
|
||||
? { kind: 'official', label: t(OFFICIAL) }
|
||||
: null,
|
||||
ownClaimChip(report),
|
||||
...mediaLabels(report).map((label) => ({ kind: 'media', label })),
|
||||
].filter(Boolean),
|
||||
// The event's first report from each other side, under the lead.
|
||||
others: recentSides(report, now)
|
||||
.slice(1)
|
||||
.map((s) => ({
|
||||
side: sideLabel(s.side),
|
||||
publisher: s.publisher,
|
||||
title: s.title,
|
||||
url: s.url,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a report is placed no closer than a country: it has no spot of its
|
||||
* own on the map.
|
||||
*/
|
||||
export const countryOnly = (report) =>
|
||||
report.pins?.length > 0 &&
|
||||
report.pins.every((pin) => pin.precision === 'country');
|
||||
|
||||
/**
|
||||
* Country-only reports as one group per country, in the order met:
|
||||
* `{key, place, lat, lon, count}`. They are drawn as one badge with a count,
|
||||
* not as markers piled on the country's centre.
|
||||
*/
|
||||
export function countryGroups(reports) {
|
||||
const groups = new Map();
|
||||
for (const report of reports) {
|
||||
if (!countryOnly(report)) continue;
|
||||
const { lat, lon, place } = report.pins[0];
|
||||
const key = place || `${lat.toFixed(1)},${lon.toFixed(1)}`;
|
||||
const group = groups.get(key);
|
||||
if (group) group.count += 1;
|
||||
else groups.set(key, { key, place: place || '', lat, lon, count: 1 });
|
||||
}
|
||||
return [...groups.values()];
|
||||
}
|
||||
|
||||
/**
|
||||
* Marker position for a report. Many reports name the same city and would
|
||||
* stack on one centroid, so each is spread on a small deterministic ring
|
||||
* (about 1.5 km for a place, 6 km for a region, 28 km for a country) to stay
|
||||
* individually clickable. The spread is display-only; cards show the place.
|
||||
*/
|
||||
export function reportMarkerPosition(report, pin = 0) {
|
||||
const { lat, lon, precision } = report.pins?.[pin] || report.geo;
|
||||
const h = hash(pin ? `${report.id}~${pin}` : report.id);
|
||||
const angle = ((h % 3600) / 3600) * 2 * Math.PI;
|
||||
const radiusDeg =
|
||||
(precision === 'country' ? 0.25 : precision === 'region' ? 0.055 : 0.014) *
|
||||
(0.35 + ((h >>> 12) % 65) / 100);
|
||||
return {
|
||||
lat: lat + radiusDeg * Math.sin(angle),
|
||||
lon: lon + (radiusDeg * Math.cos(angle)) / Math.cos((lat * Math.PI) / 180),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge a page into the feed, newest first, one row per report id, keeping
|
||||
* only the window and at most `max` rows (a long session does not grow
|
||||
* the list). `mode` 'head' refreshes the top and keeps the older cursor;
|
||||
* 'append' adds an older page and takes its cursor; 'replace' starts over.
|
||||
* A head page that has older pages behind it and shares no row with the list
|
||||
* left a gap (more than a page arrived since), so it starts over too.
|
||||
*/
|
||||
export function mergeFeedPage(
|
||||
feed,
|
||||
page,
|
||||
mode,
|
||||
now = Date.now(),
|
||||
max = NEWS_FEED_MAX,
|
||||
windowMs = NEWS_WINDOW_MS,
|
||||
) {
|
||||
const byId = new Map();
|
||||
if (mode === 'head' && page.nextBefore && feed.items.length) {
|
||||
const held = new Set(feed.items.map((item) => item.id));
|
||||
if (!page.reports.some((item) => held.has(item.id))) mode = 'replace';
|
||||
}
|
||||
const items =
|
||||
mode === 'replace'
|
||||
? page.reports
|
||||
: mode === 'append'
|
||||
? [...feed.items, ...page.reports]
|
||||
: [...page.reports, ...feed.items];
|
||||
for (const item of items)
|
||||
if (!byId.has(item.id) && withinNewsWindow(item, now, windowMs))
|
||||
byId.set(item.id, item);
|
||||
return {
|
||||
items: [...byId.values()]
|
||||
.sort((a, b) => b.publishedAt.localeCompare(a.publishedAt))
|
||||
.slice(0, max),
|
||||
nextBefore:
|
||||
mode === 'head' && feed.items.length ? feed.nextBefore : page.nextBefore,
|
||||
};
|
||||
}
|
||||
|
||||
/** Normalised filters; anything unknown falls back to its default. */
|
||||
export function normalizeFilters(next = {}, hotspots = [], previous = {}) {
|
||||
const merged = { ...previous, ...next };
|
||||
const hotspot = hotspots.some((h) => h.id === merged.hotspot)
|
||||
? merged.hotspot
|
||||
: 'all';
|
||||
const category = Object.hasOwn(NEWS_CATEGORIES, merged.category)
|
||||
? merged.category
|
||||
: 'all';
|
||||
const minSeverity =
|
||||
Number.isInteger(merged.minSeverity) &&
|
||||
merged.minSeverity >= 0 &&
|
||||
merged.minSeverity <= 3
|
||||
? merged.minSeverity
|
||||
: NEWS_DEFAULT_MIN_SEVERITY;
|
||||
return {
|
||||
hotspot,
|
||||
category,
|
||||
minSeverity,
|
||||
section: NEWS_SECTIONS.includes(merged.section)
|
||||
? merged.section
|
||||
: 'conflicts',
|
||||
// A shorter window than the server's; null shows all it serves.
|
||||
hours: NEWS_WINDOW_CHOICES.includes(merged.hours) ? merged.hours : null,
|
||||
};
|
||||
}
|
||||
|
||||
/** Whether a report passes the filters the server does not apply. */
|
||||
export function passesFilters(
|
||||
report,
|
||||
filters,
|
||||
now = Date.now(),
|
||||
windowMs = NEWS_WINDOW_MS,
|
||||
) {
|
||||
return (
|
||||
withinNewsWindow(report, now, windowMs) &&
|
||||
reportSeverity(report) >= filters.minSeverity
|
||||
);
|
||||
}
|
||||
|
||||
/** Whether a report belongs on the map under the current filters. */
|
||||
export function visibleOnMap(
|
||||
report,
|
||||
filters,
|
||||
now = Date.now(),
|
||||
windowMs = NEWS_WINDOW_MS,
|
||||
) {
|
||||
return (
|
||||
(filters.hotspot === 'all' ||
|
||||
(report.hotspotIds || [report.hotspotId]).includes(filters.hotspot)) &&
|
||||
(filters.category === 'all' || report.category === filters.category) &&
|
||||
passesFilters(report, filters, now, windowMs)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The filters Local applies: every hotspot and every severity, near the view
|
||||
* centre; the category and the window still hold.
|
||||
*/
|
||||
export const localFilters = (filters) => ({
|
||||
...filters,
|
||||
hotspot: 'all',
|
||||
minSeverity: 0,
|
||||
});
|
||||
|
||||
/** API query for the Conflicts feed under the current filters: the window only. */
|
||||
export function feedQueryFor(filters, extra = {}) {
|
||||
return {
|
||||
events: true,
|
||||
today: true,
|
||||
relevance: NEWS_RELEVANT,
|
||||
hotspot: filters.hotspot,
|
||||
category: filters.category === 'all' ? '' : filters.category,
|
||||
limit: NEWS_FEED_PAGE,
|
||||
...extra,
|
||||
};
|
||||
}
|
||||
|
||||
/** The half-degree cell a point lies in, as a key. */
|
||||
export function viewCell({ lat, lon }) {
|
||||
return `${Math.floor(lat / NEWS_CELL_DEG)}:${Math.floor(lon / NEWS_CELL_DEG)}`;
|
||||
}
|
||||
|
||||
/** Great-circle distance in kilometres. */
|
||||
export function distanceKm(a, b) {
|
||||
const rad = Math.PI / 180;
|
||||
const dLat = (b.lat - a.lat) * rad;
|
||||
const dLon = (b.lon - a.lon) * rad;
|
||||
const s =
|
||||
Math.sin(dLat / 2) ** 2 +
|
||||
Math.cos(a.lat * rad) * Math.cos(b.lat * rad) * Math.sin(dLon / 2) ** 2;
|
||||
return 2 * 6371 * Math.asin(Math.min(1, Math.sqrt(s)));
|
||||
}
|
||||
|
||||
function inRing(ring, { lat, lon }) {
|
||||
let inside = false;
|
||||
for (let i = 0, j = ring.length - 1; i < ring.length; j = i++) {
|
||||
const [xi, yi] = ring[i];
|
||||
const [xj, yj] = ring[j];
|
||||
if (
|
||||
yi > lat !== yj > lat &&
|
||||
lon < ((xj - xi) * (lat - yi)) / (yj - yi) + xi
|
||||
)
|
||||
inside = !inside;
|
||||
}
|
||||
return inside;
|
||||
}
|
||||
|
||||
/** Whether a point lies in an outline's polygons (`{outer, holes}`). */
|
||||
export function inPolygons(polygons, point) {
|
||||
return polygons.some(
|
||||
({ outer, holes }) =>
|
||||
inRing(outer, point) && !holes.some((hole) => inRing(hole, point)),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The reports near `centre` that pass the filters, newest first: any pin
|
||||
* inside `area` (outline polygons), or without one within
|
||||
* NEWS_IN_VIEW_RADIUS_KM. Newest, not most severe: an area's own news often
|
||||
* carries no severity, and the area's fighting would otherwise crowd it out.
|
||||
*/
|
||||
export function reportsInView(
|
||||
reports,
|
||||
centre,
|
||||
filters,
|
||||
now = Date.now(),
|
||||
limit = NEWS_IN_VIEW_LIMIT,
|
||||
windowMs = NEWS_WINDOW_MS,
|
||||
area = null,
|
||||
) {
|
||||
if (!centre) return [];
|
||||
const near = area?.length
|
||||
? (pin) => inPolygons(area, pin)
|
||||
: (pin) => distanceKm(pin, centre) <= NEWS_IN_VIEW_RADIUS_KM;
|
||||
return reports
|
||||
.filter(
|
||||
(r) =>
|
||||
passesFilters(r, filters, now, windowMs) &&
|
||||
(filters.category === 'all' || r.category === filters.category) &&
|
||||
r.pins?.some(near),
|
||||
)
|
||||
.sort((a, b) => b.publishedAt.localeCompare(a.publishedAt))
|
||||
.slice(0, limit);
|
||||
}
|
||||
@@ -0,0 +1,765 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
normalizeFeedPayload,
|
||||
normalizeHotspotsPayload,
|
||||
normalizeOutlinePayload,
|
||||
} from './records.js';
|
||||
import {
|
||||
NEWS_IN_VIEW_RADIUS_KM,
|
||||
ageWords,
|
||||
areaLabel,
|
||||
beaconPixelSize,
|
||||
cardEvents,
|
||||
cardView,
|
||||
distanceKm,
|
||||
eventPixelSize,
|
||||
feedCardView,
|
||||
feedQueryFor,
|
||||
hotspotLabel,
|
||||
hotspotTrend,
|
||||
inPolygons,
|
||||
isPressing,
|
||||
markerStyle,
|
||||
mergeFeedPage,
|
||||
newsWindowChoices,
|
||||
newsWindowMs,
|
||||
normalizeFilters,
|
||||
passesFilters,
|
||||
reportMarkerPosition,
|
||||
reportSeverity,
|
||||
reportsInView,
|
||||
sidesLine,
|
||||
sourceLinkLabel,
|
||||
viewCell,
|
||||
visibleOnMap,
|
||||
withinNewsWindow,
|
||||
} from './model.js';
|
||||
import { newsFeedQuery } from './source.js';
|
||||
|
||||
const NOW = Date.parse('2026-10-28T10:00:00Z');
|
||||
const report = (id, publishedAt, extra = {}) => ({
|
||||
id,
|
||||
title: `Report ${id}`,
|
||||
url: `https://example.com/${id}`,
|
||||
publishedAt,
|
||||
publisher: 'Example',
|
||||
side: 'UA',
|
||||
category: 'strike',
|
||||
hotspotIds: ['ua'],
|
||||
geo: {
|
||||
lat: 50,
|
||||
lon: 36,
|
||||
place: 'Kharkiv',
|
||||
precision: 'city',
|
||||
mappable: true,
|
||||
},
|
||||
pins: [
|
||||
{ lat: 50, lon: 36, place: 'Kharkiv', precision: 'city', mappable: true },
|
||||
],
|
||||
...extra,
|
||||
});
|
||||
const event = (extra = {}) => ({
|
||||
status: 'both-sides',
|
||||
size: 6,
|
||||
severity: 3,
|
||||
lastAt: '2026-10-28T09:30:00Z',
|
||||
sides: [
|
||||
{
|
||||
side: 'UA',
|
||||
title: 'a',
|
||||
url: 'https://a.test/',
|
||||
publisher: 'A',
|
||||
publishedAt: '2026-10-28T09:00:00Z',
|
||||
},
|
||||
{
|
||||
side: 'RU',
|
||||
title: 'b',
|
||||
url: 'https://b.test/',
|
||||
publisher: 'B',
|
||||
publishedAt: '2026-10-28T09:20:00Z',
|
||||
},
|
||||
],
|
||||
image: { url: 'https://a.test/p.jpg', publisher: 'A' },
|
||||
...extra,
|
||||
});
|
||||
|
||||
test('payloads are validated record by record; empty and malformed payloads', () => {
|
||||
const hotspots = normalizeHotspotsPayload({
|
||||
hotspots: [
|
||||
{
|
||||
id: 'ua',
|
||||
name: 'UA',
|
||||
bbox: [22, 44, 42, 56],
|
||||
parties: ['UA', 'RU', '<x>'],
|
||||
counts: { last24h: 3 },
|
||||
},
|
||||
{ id: 'bad', name: 'Bad', bbox: [42, 44, 22, 56] },
|
||||
{ id: '../x', name: 'Bad id', bbox: [22, 44, 42, 56] },
|
||||
],
|
||||
ingest: { lastRunAt: '2026-10-28T10:00:00Z' },
|
||||
});
|
||||
assert.deepEqual(
|
||||
hotspots.hotspots.map((h) => [h.id, h.parties, h.last24h]),
|
||||
[['ua', ['UA', 'RU'], 3]],
|
||||
);
|
||||
assert.equal(hotspots.lastRunAt, '2026-10-28T10:00:00.000Z');
|
||||
assert.equal(normalizeHotspotsPayload({}), null);
|
||||
assert.equal(normalizeHotspotsPayload(null), null);
|
||||
assert.deepEqual(normalizeHotspotsPayload({ hotspots: [] }).hotspots, []);
|
||||
|
||||
const feed = normalizeFeedPayload({
|
||||
reports: [
|
||||
{
|
||||
id: 'r_1',
|
||||
title: ' Strike on Kharkiv ',
|
||||
url: 'https://x.test/a',
|
||||
publishedAt: '2026-10-28T09:00:00Z',
|
||||
geo: { lat: 50, lon: 36, mappable: true },
|
||||
media: ['video', 'video', 'audio'],
|
||||
event: { size: 3, image: { url: 'http://x.test/p.jpg' } },
|
||||
},
|
||||
{
|
||||
id: 'r_2',
|
||||
title: 'Bad link',
|
||||
url: 'javascript:alert(1)',
|
||||
publishedAt: '2026-10-28T09:00:00Z',
|
||||
},
|
||||
{
|
||||
id: 'r_3',
|
||||
title: 'Bad geo',
|
||||
url: 'https://user:[email protected]/c',
|
||||
publishedAt: '2026-10-28T09:00:00Z',
|
||||
},
|
||||
{
|
||||
id: 'r_4',
|
||||
title: 'Off the globe',
|
||||
url: 'https://x.test/d',
|
||||
publishedAt: '2026-10-28T09:00:00Z',
|
||||
geo: { lat: 200, lon: 36 },
|
||||
},
|
||||
],
|
||||
nextBefore: '2026-10-28T08:00:00Z',
|
||||
});
|
||||
assert.deepEqual(
|
||||
feed.reports.map((r) => [r.id, r.title, Boolean(r.geo), r.pins.length]),
|
||||
[
|
||||
['r_1', 'Strike on Kharkiv', true, 1],
|
||||
['r_4', 'Off the globe', false, 0],
|
||||
],
|
||||
);
|
||||
assert.deepEqual(feed.reports[0].media, ['video']);
|
||||
// A picture must be HTTPS on the publisher's site.
|
||||
assert.equal(feed.reports[0].event.image, null);
|
||||
assert.equal(feed.nextBefore, '2026-10-28T08:00:00.000Z');
|
||||
assert.equal(normalizeFeedPayload({ reports: 'x' }), null);
|
||||
assert.deepEqual(normalizeFeedPayload({ reports: [] }), {
|
||||
reports: [],
|
||||
nextBefore: null,
|
||||
});
|
||||
});
|
||||
|
||||
test("an event's clip is kept only as an HTTPS file a browser plays itself", () => {
|
||||
const clip = (video) =>
|
||||
normalizeFeedPayload({
|
||||
reports: [
|
||||
{
|
||||
id: 'r',
|
||||
title: 'T',
|
||||
url: 'https://x.test/r',
|
||||
publishedAt: '2026-10-28T09:00:00Z',
|
||||
event: {
|
||||
size: 1,
|
||||
image: { url: 'https://cdn.test/p.jpg', publisher: 'P', video },
|
||||
},
|
||||
},
|
||||
],
|
||||
}).reports[0].event.image.video;
|
||||
assert.equal(
|
||||
clip('https://cdn.test/v.mp4?token=a'),
|
||||
'https://cdn.test/v.mp4?token=a',
|
||||
);
|
||||
assert.equal(clip('https://cdn.test/v.webm'), 'https://cdn.test/v.webm');
|
||||
assert.equal(clip('http://cdn.test/v.mp4'), undefined);
|
||||
assert.equal(clip('https://cdn.test/v.m3u8'), undefined);
|
||||
assert.equal(clip(undefined), undefined);
|
||||
});
|
||||
|
||||
test('outlines keep valid rings, borders and named disputed areas', () => {
|
||||
const ring = [
|
||||
[30, 45],
|
||||
[31, 45],
|
||||
[31, 46],
|
||||
];
|
||||
const outline = normalizeOutlinePayload({
|
||||
polygons: [{ outer: ring, holes: [[[0, 0]]] }, { outer: [[1, 2]] }],
|
||||
borders: [ring, 'x'],
|
||||
disputed: [
|
||||
{
|
||||
name: 'Crimea',
|
||||
kind: 'held',
|
||||
heldBy: 'RU',
|
||||
claimedBy: 'UA',
|
||||
rings: [ring],
|
||||
},
|
||||
{ name: 'No rings', rings: [] },
|
||||
{ name: 'Odd kind', kind: 'zzz', heldBy: 'nope', rings: [ring] },
|
||||
],
|
||||
});
|
||||
assert.equal(outline.polygons.length, 1);
|
||||
assert.deepEqual(outline.polygons[0].holes, []);
|
||||
assert.equal(outline.borders.length, 1);
|
||||
assert.deepEqual(
|
||||
outline.disputed.map((d) => [d.name, d.kind, d.heldBy]),
|
||||
[
|
||||
['Crimea', 'held', 'RU'],
|
||||
['Odd kind', 'held', null],
|
||||
],
|
||||
);
|
||||
assert.deepEqual(normalizeOutlinePayload(null), {
|
||||
polygons: [],
|
||||
borders: [],
|
||||
disputed: [],
|
||||
area: null,
|
||||
});
|
||||
// Indonesia's 208 islands are all kept.
|
||||
const islands = Array.from({ length: 208 }, () => ({ outer: ring }));
|
||||
assert.equal(
|
||||
normalizeOutlinePayload({ polygons: islands }).polygons.length,
|
||||
208,
|
||||
);
|
||||
});
|
||||
|
||||
test('a point outline names its area, and Local keeps to it', () => {
|
||||
const box = [
|
||||
[35, 48],
|
||||
[37, 48],
|
||||
[37, 50.5],
|
||||
[35, 50.5],
|
||||
];
|
||||
const { area, polygons } = normalizeOutlinePayload({
|
||||
area: { key: 'US-TX', name: 'Texas', kind: 'region', country: 'US' },
|
||||
polygons: [{ outer: box, holes: [] }],
|
||||
});
|
||||
assert.deepEqual(area, {
|
||||
key: 'US-TX',
|
||||
name: 'Texas',
|
||||
kind: 'region',
|
||||
country: 'US',
|
||||
});
|
||||
assert.equal(areaLabel(area), 'Texas, United States');
|
||||
assert.equal(
|
||||
areaLabel({ ...area, kind: 'country', country: 'UA' }),
|
||||
'Ukraine',
|
||||
);
|
||||
// A disputed area is named as itself, never as either side's.
|
||||
assert.equal(
|
||||
areaLabel({ key: 'disputed:Crimea', name: 'Crimea', kind: 'disputed' }),
|
||||
'Crimea',
|
||||
);
|
||||
assert.equal(normalizeOutlinePayload({ area: { key: 'X' } }).area, null);
|
||||
const at = (id, lat, lon) =>
|
||||
report(id, '2026-10-28T09:00:00Z', {
|
||||
pins: [{ lat, lon, mappable: true }],
|
||||
});
|
||||
const reports = [at('inside', 50, 36), at('beyond', 49, 39)];
|
||||
const filters = normalizeFilters({ minSeverity: 0 });
|
||||
const centre = { lat: 49, lon: 36 };
|
||||
const now = Date.parse('2026-10-28T10:00:00Z');
|
||||
const ids = (area) =>
|
||||
reportsInView(reports, centre, filters, now, 10, undefined, area).map(
|
||||
(r) => r.id,
|
||||
);
|
||||
// Within 500 km both; inside the area only the one.
|
||||
assert.deepEqual(ids(null), ['inside', 'beyond']);
|
||||
assert.deepEqual(ids(polygons), ['inside']);
|
||||
assert.ok(inPolygons(polygons, { lat: 49, lon: 36 }));
|
||||
assert.ok(!inPolygons([{ outer: box, holes: [box] }], { lat: 49, lon: 36 }));
|
||||
});
|
||||
|
||||
test('feed pages merge newest first without duplicates', () => {
|
||||
const a = report('a', '2026-10-28T10:00:00Z');
|
||||
const b = report('b', '2026-10-28T09:00:00Z');
|
||||
const c = report('c', '2026-10-28T08:00:00Z');
|
||||
let feed = mergeFeedPage(
|
||||
{ items: [] },
|
||||
{ reports: [a, b], nextBefore: 'B' },
|
||||
'replace',
|
||||
NOW,
|
||||
);
|
||||
assert.deepEqual(
|
||||
[feed.items.map((r) => r.id), feed.nextBefore],
|
||||
[['a', 'b'], 'B'],
|
||||
);
|
||||
feed = mergeFeedPage(
|
||||
feed,
|
||||
{ reports: [b, c], nextBefore: null },
|
||||
'append',
|
||||
NOW,
|
||||
);
|
||||
assert.deepEqual(
|
||||
[feed.items.map((r) => r.id), feed.nextBefore],
|
||||
[['a', 'b', 'c'], null],
|
||||
);
|
||||
const newer = report('n', '2026-10-28T11:00:00Z');
|
||||
feed = mergeFeedPage(
|
||||
{ ...feed, nextBefore: 'C' },
|
||||
{ reports: [newer, a], nextBefore: 'X' },
|
||||
'head',
|
||||
NOW + 3_600_000,
|
||||
);
|
||||
assert.deepEqual(
|
||||
[feed.items.map((r) => r.id), feed.nextBefore],
|
||||
[['n', 'a', 'b', 'c'], 'C'],
|
||||
);
|
||||
assert.deepEqual(
|
||||
mergeFeedPage({ items: [] }, { reports: [], nextBefore: null }, 'replace'),
|
||||
{ items: [], nextBefore: null },
|
||||
);
|
||||
});
|
||||
|
||||
test('a head page that does not reach the rows held starts the list over', () => {
|
||||
const held = report('h', '2026-10-28T06:00:00Z');
|
||||
const fresh = report('n', '2026-10-28T09:30:00Z');
|
||||
// More than a page arrived since the last refresh (a suspended tab): the
|
||||
// reports between this page and the rows held are behind its cursor.
|
||||
const feed = mergeFeedPage(
|
||||
{ items: [held], nextBefore: 'OLD' },
|
||||
{ reports: [fresh], nextBefore: 'NEW' },
|
||||
'head',
|
||||
NOW,
|
||||
);
|
||||
assert.deepEqual(
|
||||
[feed.items.map((r) => r.id), feed.nextBefore],
|
||||
[['n'], 'NEW'],
|
||||
);
|
||||
});
|
||||
|
||||
test('the feed list keeps only the last 24 hours and at most its cap', () => {
|
||||
const fresh = report('f', '2026-10-28T09:00:00Z');
|
||||
const stale = report('s', '2026-10-27T09:00:00Z');
|
||||
const ahead = report('x', '2026-10-28T11:00:00Z');
|
||||
// A long session: yesterday's rows already held fall out on the next merge.
|
||||
const feed = mergeFeedPage(
|
||||
{ items: [stale], nextBefore: 'S' },
|
||||
{ reports: [fresh, ahead], nextBefore: 'F' },
|
||||
'head',
|
||||
NOW,
|
||||
);
|
||||
assert.deepEqual(
|
||||
feed.items.map((r) => r.id),
|
||||
['f'],
|
||||
);
|
||||
const many = Array.from({ length: 30 }, (_, i) =>
|
||||
report(`r${i}`, new Date(NOW - (i + 1) * 60_000).toISOString()),
|
||||
);
|
||||
const capped = mergeFeedPage(
|
||||
{ items: [] },
|
||||
{ reports: many, nextBefore: null },
|
||||
'replace',
|
||||
NOW,
|
||||
10,
|
||||
);
|
||||
assert.equal(capped.items.length, 10);
|
||||
assert.equal(capped.items[0].id, 'r0');
|
||||
assert.equal(capped.items.at(-1).id, 'r9');
|
||||
// Everything outside the window: an empty list, not an error.
|
||||
assert.deepEqual(
|
||||
mergeFeedPage(
|
||||
{ items: [] },
|
||||
{ reports: [stale], nextBefore: null },
|
||||
'replace',
|
||||
NOW,
|
||||
).items,
|
||||
[],
|
||||
);
|
||||
});
|
||||
|
||||
test('filters default to significant news and map to a last-24-hours query', () => {
|
||||
const hotspots = [{ id: 'ua' }, { id: 'me' }];
|
||||
const filters = normalizeFilters(
|
||||
{ hotspot: 'ua', category: 'nope', minSeverity: 9 },
|
||||
hotspots,
|
||||
);
|
||||
assert.deepEqual(filters, {
|
||||
hotspot: 'ua',
|
||||
category: 'all',
|
||||
minSeverity: 2,
|
||||
section: 'conflicts',
|
||||
hours: null,
|
||||
});
|
||||
assert.equal(
|
||||
normalizeFilters({ section: 'local' }, hotspots).section,
|
||||
'local',
|
||||
);
|
||||
assert.equal(
|
||||
normalizeFilters({ section: 'nope' }, hotspots).section,
|
||||
'conflicts',
|
||||
);
|
||||
assert.equal(normalizeFilters({ hours: 6 }, hotspots).hours, 6);
|
||||
assert.equal(normalizeFilters({ hours: 5 }, hotspots).hours, null);
|
||||
assert.equal(normalizeFilters({ minSeverity: 0 }, hotspots).minSeverity, 0);
|
||||
assert.equal(normalizeFilters({ hotspot: 'gone' }, hotspots).hotspot, 'all');
|
||||
assert.equal(
|
||||
newsFeedQuery(
|
||||
feedQueryFor(filters, { before: '2026-10-28T08:00:00.000Z' }),
|
||||
),
|
||||
'hotspot=ua&before=2026-10-28T08%3A00%3A00.000Z&limit=60&relevance=core%2Crelated&today=1&events=1',
|
||||
);
|
||||
// The Conflicts feed reads the same whichever section is chosen.
|
||||
assert.equal(
|
||||
newsFeedQuery(feedQueryFor({ ...filters, section: 'local' })),
|
||||
newsFeedQuery(feedQueryFor(filters)),
|
||||
);
|
||||
});
|
||||
|
||||
test('only the last 24 hours pass, and the severity floor applies', () => {
|
||||
const filters = normalizeFilters();
|
||||
const fresh = report('f', '2026-10-28T09:00:00Z', { severity: 2 });
|
||||
const minor = report('m', '2026-10-28T09:00:00Z', { severity: 1 });
|
||||
const old = report('o', '2026-10-27T09:00:00Z', { severity: 3 });
|
||||
assert.equal(withinNewsWindow(fresh, NOW), true);
|
||||
assert.equal(withinNewsWindow(old, NOW), false);
|
||||
assert.equal(withinNewsWindow({ publishedAt: 'x' }, NOW), false);
|
||||
// Clock skew up to 15 min ahead passes; further ahead is not news yet.
|
||||
const skew = report('k', '2026-10-28T10:14:00Z');
|
||||
const future = report('z', '2026-10-28T10:16:00Z');
|
||||
assert.equal(withinNewsWindow(skew, NOW), true);
|
||||
assert.equal(withinNewsWindow(future, NOW), false);
|
||||
assert.deepEqual(
|
||||
[fresh, minor, old]
|
||||
.filter((r) => passesFilters(r, filters, NOW))
|
||||
.map((r) => r.id),
|
||||
['f'],
|
||||
);
|
||||
assert.equal(visibleOnMap(minor, { ...filters, minSeverity: 0 }, NOW), true);
|
||||
assert.equal(visibleOnMap(fresh, { ...filters, hotspot: 'me' }, NOW), false);
|
||||
});
|
||||
|
||||
test('ageWords says now, minutes or hours', () => {
|
||||
assert.equal(ageWords('2026-10-28T09:59:30Z', NOW), 'now');
|
||||
assert.equal(ageWords('2026-10-28T09:55:00Z', NOW), '5 min');
|
||||
assert.equal(ageWords('2026-10-28T07:10:00Z', NOW), '2 h');
|
||||
assert.equal(ageWords('2026-10-26T07:10:00Z', NOW), '24 h');
|
||||
assert.equal(ageWords('not a date', NOW), '');
|
||||
});
|
||||
|
||||
test('markers spread deterministically near their place; source link line', () => {
|
||||
const r = report('r_spread', '2026-10-28T10:00:00Z');
|
||||
const p1 = reportMarkerPosition(r);
|
||||
assert.deepEqual(reportMarkerPosition(r), p1);
|
||||
assert.ok(Math.abs(p1.lat - 50) < 0.02 && Math.abs(p1.lon - 36) < 0.04);
|
||||
assert.notDeepEqual(
|
||||
reportMarkerPosition(report('r_other', r.publishedAt)),
|
||||
p1,
|
||||
);
|
||||
assert.equal(
|
||||
sourceLinkLabel('https://www.reliefweb.int/report/x'),
|
||||
'Source: reliefweb.int ↗',
|
||||
);
|
||||
assert.equal(sourceLinkLabel('not a url'), '');
|
||||
});
|
||||
|
||||
test('beacons carry a focus, a size by activity and a trend only against a prior day', () => {
|
||||
const { hotspots } = normalizeHotspotsPayload({
|
||||
hotspots: [
|
||||
{
|
||||
id: 'a',
|
||||
name: 'A',
|
||||
bbox: [20, 40, 40, 50],
|
||||
focus: { lat: 45, lon: 30 },
|
||||
counts: { last24h: 120, prev24h: 80 },
|
||||
},
|
||||
{
|
||||
id: 'b',
|
||||
name: 'B',
|
||||
bbox: [20, 40, 40, 50],
|
||||
focus: { lat: 80, lon: 30 },
|
||||
},
|
||||
],
|
||||
});
|
||||
assert.deepEqual(
|
||||
hotspots.map((h) => h.focus),
|
||||
[
|
||||
{ lat: 45, lon: 30 },
|
||||
{ lat: 45, lon: 30 },
|
||||
],
|
||||
);
|
||||
assert.equal(hotspotTrend(120, 80), '▲');
|
||||
assert.equal(hotspotTrend(50, 80), '▼');
|
||||
assert.equal(hotspotTrend(85, 80), '');
|
||||
assert.equal(hotspotTrend(40, 0), '');
|
||||
assert.equal(beaconPixelSize(0), 10);
|
||||
assert.equal(beaconPixelSize(10_000), 26);
|
||||
assert.equal(
|
||||
hotspotLabel({ id: 'ua-ru-war', name: 'Russia–Ukraine war' }),
|
||||
'Ukraine–Russia',
|
||||
);
|
||||
assert.equal(hotspotLabel({ id: 'new-one', name: 'New one' }), 'New one');
|
||||
assert.equal(
|
||||
hotspotLabel({ id: 'x', name: 'X', catchAll: true }),
|
||||
'Elsewhere',
|
||||
);
|
||||
assert.equal(
|
||||
hotspotLabel({ id: 'worldwide', name: 'Worldwide', catchAll: true }),
|
||||
'Worldwide',
|
||||
);
|
||||
});
|
||||
|
||||
test('marker style: severity fill, attestation rim, size by sources, fading with age', () => {
|
||||
const r = report('r_style', '2026-10-28T10:00:00Z', {
|
||||
severity: 2,
|
||||
event: event({
|
||||
status: 'claimed',
|
||||
size: 4,
|
||||
lastAt: '2026-10-28T10:00:00Z',
|
||||
}),
|
||||
});
|
||||
const fresh = markerStyle(r, NOW);
|
||||
assert.equal(fresh.severity, 3); // the event's, when higher than the report's
|
||||
assert.equal(fresh.rim, 'claimed');
|
||||
assert.equal(fresh.size, eventPixelSize(4));
|
||||
// The icon is half the disc: no marker is too small to read it.
|
||||
assert.equal(eventPixelSize(1), 24);
|
||||
assert.equal(eventPixelSize(1000), 36);
|
||||
assert.equal(fresh.alpha, 1);
|
||||
assert.equal(markerStyle(r, NOW + 48 * 3_600_000).alpha, 0.45);
|
||||
assert.equal(reportSeverity({ category: 'political' }), 0);
|
||||
assert.equal(reportSeverity({ category: 'strike' }), 1);
|
||||
});
|
||||
|
||||
test('picture cards: physical, two or more sources, major or pressing, at most ten', () => {
|
||||
const big = report('big', '2026-10-28T09:30:00Z', { event: event() });
|
||||
const statement = report('st', '2026-10-28T09:30:00Z', {
|
||||
category: 'political',
|
||||
event: event(),
|
||||
});
|
||||
const lone = report('lone', '2026-10-28T09:30:00Z', {
|
||||
event: event({ size: 1 }),
|
||||
});
|
||||
const stale = report('stale', '2026-10-28T01:00:00Z', {
|
||||
event: event({ severity: 2, lastAt: '2026-10-28T01:00:00Z' }),
|
||||
});
|
||||
assert.equal(isPressing(big, NOW), true);
|
||||
assert.deepEqual(
|
||||
cardEvents([statement, lone, stale, big], NOW).map((r) => r.id),
|
||||
['big'],
|
||||
);
|
||||
const many = Array.from({ length: 14 }, (_, i) =>
|
||||
report(`m${i}`, '2026-10-28T09:30:00Z', { event: event() }),
|
||||
);
|
||||
assert.equal(cardEvents(many, NOW).length, 10);
|
||||
const view = cardView(big, NOW);
|
||||
assert.equal(view.image, 'https://a.test/p.jpg');
|
||||
assert.equal(view.credit, 'A');
|
||||
assert.equal(view.meta, '30 min · 6 sources · UA, RU');
|
||||
assert.equal(cardView(lone, NOW).image, null);
|
||||
});
|
||||
|
||||
test('feed cards: headline, meta parts, sides and chips', () => {
|
||||
const r = report('fc', '2026-10-28T09:00:00Z', {
|
||||
title: 'Удар по Харкову',
|
||||
titleEn: 'Strike on Kharkiv',
|
||||
affiliation: 'military',
|
||||
media: ['video', 'photo'],
|
||||
geo: { lat: 50, lon: 36, place: 'Kharkiv', mappable: true, byModel: true },
|
||||
event: event({ lastAt: '2026-10-28T09:50:00Z' }),
|
||||
});
|
||||
const view = feedCardView(r, NOW);
|
||||
assert.equal(view.title, 'Strike on Kharkiv');
|
||||
assert.equal(view.original, 'Удар по Харкову');
|
||||
assert.equal(view.placedByModel, true);
|
||||
assert.equal(view.severityLabel, 'Major');
|
||||
assert.equal(view.color, '#ff4444');
|
||||
assert.equal(view.category, 'Strike');
|
||||
assert.equal(view.place, 'Kharkiv');
|
||||
assert.equal(view.sources, '6 sources');
|
||||
assert.equal(view.age, '10 min');
|
||||
assert.equal(view.sides, 'Reported by UA sources · RU sources');
|
||||
assert.deepEqual(view.picture, { url: 'https://a.test/p.jpg', credit: 'A' });
|
||||
assert.deepEqual(
|
||||
view.chips.map((c) => [c.kind, c.label]),
|
||||
[
|
||||
['official', 'Official'],
|
||||
['confirmed', 'Reported by both sides'],
|
||||
['media', 'Video'],
|
||||
['media', 'Photos'],
|
||||
],
|
||||
);
|
||||
assert.deepEqual(
|
||||
view.others.map((o) => o.side),
|
||||
['RU'],
|
||||
);
|
||||
// A lone report: its own side, no picture, one source.
|
||||
const single = feedCardView(report('s', '2026-10-28T09:00:00Z'), NOW);
|
||||
assert.equal(single.sides, 'Reported by UA sources');
|
||||
assert.equal(single.picture, null);
|
||||
// A one-source event (a Telegram post) shows its picture and its clip in
|
||||
// the panel, though the globe's picture cards need two sources.
|
||||
const post = report('p', '2026-10-28T09:00:00Z', {
|
||||
event: {
|
||||
size: 1,
|
||||
sides: [],
|
||||
image: {
|
||||
url: 'https://cdn4.telesco.pe/file/v.jpg',
|
||||
publisher: 'Channel',
|
||||
video: 'https://cdn4.telesco.pe/file/v.mp4?token=x',
|
||||
},
|
||||
},
|
||||
});
|
||||
assert.deepEqual(feedCardView(post, NOW).picture, {
|
||||
url: 'https://cdn4.telesco.pe/file/v.jpg',
|
||||
credit: 'Channel',
|
||||
video: 'https://cdn4.telesco.pe/file/v.mp4?token=x',
|
||||
});
|
||||
assert.equal(single.original, null);
|
||||
assert.equal(sidesLine({ side: 'unknown' }), '');
|
||||
});
|
||||
|
||||
test('event sides older than a day are left out of the card and its sides line', () => {
|
||||
const r = report('w', '2026-10-28T09:00:00Z', {
|
||||
event: event({
|
||||
sides: [
|
||||
{
|
||||
side: 'UA',
|
||||
title: 'a',
|
||||
url: 'https://a.test/',
|
||||
publisher: 'A',
|
||||
publishedAt: '2026-10-28T08:00:00Z',
|
||||
},
|
||||
{
|
||||
side: 'RU',
|
||||
title: 'old',
|
||||
url: 'https://b.test/',
|
||||
publisher: 'B',
|
||||
publishedAt: '2026-10-26T08:00:00Z',
|
||||
},
|
||||
{
|
||||
side: 'unknown',
|
||||
title: 'c',
|
||||
url: 'https://c.test/',
|
||||
publisher: 'C',
|
||||
publishedAt: '2026-10-28T09:10:00Z',
|
||||
},
|
||||
// No date: nothing shows it is recent.
|
||||
{ side: 'IL', title: 'd', url: 'https://d.test/', publisher: 'D' },
|
||||
],
|
||||
}),
|
||||
});
|
||||
const view = feedCardView(r, NOW);
|
||||
assert.deepEqual(
|
||||
view.others.map((o) => [o.side, o.title]),
|
||||
[['Unattributed', 'c']],
|
||||
);
|
||||
// The unattributed side is not named in the sides line.
|
||||
assert.equal(view.sides, 'Reported by UA sources');
|
||||
assert.equal(cardView(r, NOW).meta, '30 min · 6 sources · UA, Unattributed');
|
||||
// All sides outside the window: no others, the report's own side remains.
|
||||
const stale = report('v', '2026-10-28T09:00:00Z', {
|
||||
event: event({
|
||||
sides: [
|
||||
{
|
||||
side: 'RU',
|
||||
title: 'old',
|
||||
url: 'https://b.test/',
|
||||
publisher: 'B',
|
||||
publishedAt: '2026-10-26T08:00:00Z',
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
assert.deepEqual(feedCardView(stale, NOW).others, []);
|
||||
assert.equal(feedCardView(stale, NOW).sides, 'Reported by UA sources');
|
||||
});
|
||||
|
||||
test('in view: half-degree cells and reports within 500 km of the centre', () => {
|
||||
assert.equal(viewCell({ lat: 50.2, lon: 36.7 }), '100:73');
|
||||
assert.equal(viewCell({ lat: 50.4, lon: 36.9 }), '100:73');
|
||||
assert.notEqual(viewCell({ lat: 50.6, lon: 36.7 }), '100:73');
|
||||
assert.ok(
|
||||
Math.abs(distanceKm({ lat: 0, lon: 0 }, { lat: 0, lon: 1 }) - 111.2) < 0.5,
|
||||
);
|
||||
const filters = normalizeFilters({ minSeverity: 0 });
|
||||
const near = report('near', '2026-10-28T09:00:00Z', { severity: 1 });
|
||||
const major = report('major', '2026-10-28T08:00:00Z', { severity: 3 });
|
||||
const far = report('far', '2026-10-28T09:00:00Z', {
|
||||
pins: [{ lat: 31.5, lon: 34.4, place: 'Gaza', mappable: true }],
|
||||
});
|
||||
const old = report('old', '2026-10-27T08:00:00Z');
|
||||
const centre = { lat: 49, lon: 36 };
|
||||
assert.ok(distanceKm(centre, far.pins[0]) > NEWS_IN_VIEW_RADIUS_KM);
|
||||
assert.deepEqual(
|
||||
reportsInView([near, far, old, major], centre, filters, NOW).map(
|
||||
(r) => r.id,
|
||||
),
|
||||
['near', 'major'],
|
||||
);
|
||||
assert.deepEqual(reportsInView([near], null, filters, NOW), []);
|
||||
assert.deepEqual(reportsInView([], centre, filters, NOW), []);
|
||||
});
|
||||
|
||||
test('a server may serve fewer hours than the day, never more', () => {
|
||||
assert.equal(newsWindowMs(12), 12 * 3_600_000);
|
||||
assert.equal(newsWindowMs(24), 24 * 3_600_000);
|
||||
for (const bad of [0, 25, 12.5, '12', null, undefined])
|
||||
assert.equal(newsWindowMs(bad), 24 * 3_600_000);
|
||||
const page = {
|
||||
reports: [
|
||||
report('fresh', '2026-10-28T02:00:00Z'),
|
||||
report('old', '2026-10-27T20:00:00Z'),
|
||||
],
|
||||
nextBefore: null,
|
||||
};
|
||||
const empty = { items: [], nextBefore: null };
|
||||
const ids = (windowMs) =>
|
||||
mergeFeedPage(empty, page, 'replace', NOW, undefined, windowMs).items.map(
|
||||
(r) => r.id,
|
||||
);
|
||||
assert.deepEqual(ids(newsWindowMs(12)), ['fresh']);
|
||||
assert.deepEqual(ids(newsWindowMs(24)), ['fresh', 'old']);
|
||||
});
|
||||
|
||||
test('the hotspots answer carries the window it was counted over', () => {
|
||||
const base = {
|
||||
hotspots: [],
|
||||
ingest: { lastRunAt: '2026-10-28T09:00:00Z' },
|
||||
};
|
||||
assert.equal(
|
||||
normalizeHotspotsPayload({ ...base, windowHours: 12 }).windowHours,
|
||||
12,
|
||||
);
|
||||
assert.equal(normalizeHotspotsPayload(base).windowHours, null);
|
||||
assert.equal(
|
||||
normalizeHotspotsPayload({ ...base, windowHours: 48 }).windowHours,
|
||||
null,
|
||||
);
|
||||
const shorter = normalizeHotspotsPayload({
|
||||
...base,
|
||||
windowHours: 3,
|
||||
maxWindowHours: 12,
|
||||
});
|
||||
assert.deepEqual([shorter.windowHours, shorter.maxWindowHours], [3, 12]);
|
||||
assert.equal(normalizeHotspotsPayload(base).maxWindowHours, null);
|
||||
});
|
||||
|
||||
test('the window choices stop at the window the server serves', () => {
|
||||
assert.deepEqual(newsWindowChoices(24), [24, 12, 6, 3, 1]);
|
||||
assert.deepEqual(newsWindowChoices(12), [12, 6, 3, 1]);
|
||||
assert.deepEqual(newsWindowChoices(8), [8, 6, 3, 1]);
|
||||
assert.deepEqual(newsWindowChoices(1), [1]);
|
||||
assert.deepEqual(newsWindowChoices(null), [24, 12, 6, 3, 1]);
|
||||
});
|
||||
|
||||
test('a checked severity keeps saying who set it', () => {
|
||||
const page = normalizeFeedPayload({
|
||||
reports: ['label', 'model', 'guess'].map((by, i) => ({
|
||||
...report(`r${i}`, '2026-10-28T09:00:00Z'),
|
||||
severity: 2,
|
||||
severityBy: by,
|
||||
})),
|
||||
});
|
||||
assert.deepEqual(
|
||||
page.reports.map((r) => r.severityBy),
|
||||
['label', 'model', 'rules'],
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,366 @@
|
||||
// Validate /api/news payloads before they reach the scene or the DOM. Every
|
||||
// string is bounded, every URL is http(s), every position is finite; a record
|
||||
// that fails is dropped, a payload that fails is rejected whole. A portable
|
||||
// export (layers/news/source): no i18n here.
|
||||
|
||||
const CATEGORY = /^[a-z][a-z-]{1,31}$/;
|
||||
const CODE = /^[A-Za-z][A-Za-z0-9-]{0,31}$/;
|
||||
const ID = /^[a-z0-9][a-z0-9_-]{0,63}$/i;
|
||||
const EVENT_STATUSES = Object.freeze([
|
||||
'both-sides',
|
||||
'independent',
|
||||
'claimed',
|
||||
'reported',
|
||||
'side',
|
||||
'single',
|
||||
]);
|
||||
|
||||
const text = (value, max) =>
|
||||
typeof value === 'string' && value.trim()
|
||||
? value.replace(/\s+/g, ' ').trim().slice(0, max)
|
||||
: null;
|
||||
const finite = (value, min, max) =>
|
||||
Number.isFinite(value) && value >= min && value <= max ? value : null;
|
||||
const iso = (value) => {
|
||||
const ms = Date.parse(value);
|
||||
return Number.isFinite(ms) ? new Date(ms).toISOString() : null;
|
||||
};
|
||||
const code = (value) =>
|
||||
typeof value === 'string' && CODE.test(value) ? value : null;
|
||||
const severity = (value) =>
|
||||
Number.isInteger(value) && value >= 0 && value <= 3 ? value : null;
|
||||
|
||||
/** An http(s) URL without credentials, as its normalised href, or null. */
|
||||
export function httpUrl(value) {
|
||||
try {
|
||||
const url = new URL(value);
|
||||
return ['http:', 'https:'].includes(url.protocol) &&
|
||||
!url.username &&
|
||||
!url.password
|
||||
? url.href
|
||||
: null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeBbox(bbox) {
|
||||
if (!Array.isArray(bbox) || bbox.length !== 4) return null;
|
||||
const [west, south, east, north] = bbox;
|
||||
if (
|
||||
finite(west, -180, 180) === null ||
|
||||
finite(east, -180, 180) === null ||
|
||||
finite(south, -90, 90) === null ||
|
||||
finite(north, -90, 90) === null ||
|
||||
west >= east ||
|
||||
south >= north
|
||||
)
|
||||
return null;
|
||||
return [west, south, east, north];
|
||||
}
|
||||
|
||||
/** Beacon position: the curated focus point, else the bbox centre. */
|
||||
function normalizeFocus(focus, [west, south, east, north]) {
|
||||
const lat = finite(focus?.lat, south, north);
|
||||
const lon = finite(focus?.lon, west, east);
|
||||
return lat !== null && lon !== null
|
||||
? { lat, lon }
|
||||
: { lat: (south + north) / 2, lon: (west + east) / 2 };
|
||||
}
|
||||
|
||||
function normalizeDeveloping(items) {
|
||||
const out = [];
|
||||
for (const raw of Array.isArray(items) ? items.slice(0, 16) : []) {
|
||||
const lat = finite(raw?.focus?.lat, -90, 90);
|
||||
const lon = finite(raw?.focus?.lon, -180, 180);
|
||||
const country =
|
||||
typeof raw?.country === 'string' && /^[A-Z]{2}$/.test(raw.country)
|
||||
? raw.country
|
||||
: null;
|
||||
if (lat === null || lon === null || !country) continue;
|
||||
// A state where the catch-all flags states ("US-FL"), else the country.
|
||||
const area =
|
||||
typeof raw.area === 'string' && /^[A-Z]{2}-[A-Z0-9]{1,3}$/.test(raw.area)
|
||||
? raw.area
|
||||
: null;
|
||||
out.push({
|
||||
country,
|
||||
key: area || country,
|
||||
name: text(raw.name, 80) || country,
|
||||
events: Math.max(0, Math.floor(Number(raw.events) || 0)),
|
||||
reports: Math.max(0, Math.floor(Number(raw.reports) || 0)),
|
||||
focus: { lat, lon },
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
const hoursOrNull = (hours) =>
|
||||
Number.isInteger(hours) && hours >= 1 && hours <= 24 ? hours : null;
|
||||
|
||||
/**
|
||||
* `/api/news/hotspots` → `{hotspots, categories, lastRunAt, windowHours,
|
||||
* maxWindowHours}`, or null when the payload has no hotspot list.
|
||||
* `windowHours` is the window the counts cover and `maxWindowHours` the
|
||||
* longest the server serves this reader; each is null when the server names
|
||||
* none.
|
||||
*/
|
||||
export function normalizeHotspotsPayload(payload) {
|
||||
if (!Array.isArray(payload?.hotspots)) return null;
|
||||
const hotspots = [];
|
||||
for (const raw of payload.hotspots.slice(0, 64)) {
|
||||
const id = typeof raw?.id === 'string' && ID.test(raw.id) ? raw.id : null;
|
||||
const name = text(raw?.name, 80);
|
||||
const bbox = normalizeBbox(raw?.bbox);
|
||||
if (!id || !name || !bbox) continue;
|
||||
const counts = raw.counts || {};
|
||||
hotspots.push({
|
||||
id,
|
||||
name,
|
||||
kind: text(raw.kind, 16) || 'conflict',
|
||||
catchAll: raw.catchAll === true,
|
||||
developing: normalizeDeveloping(raw.developing),
|
||||
bbox,
|
||||
parties: Array.isArray(raw.parties)
|
||||
? raw.parties.filter((p) => code(p))
|
||||
: [],
|
||||
last24h: Math.max(0, Math.floor(Number(counts.last24h) || 0)),
|
||||
mapped24h: Math.max(0, Math.floor(Number(counts.mapped24h) || 0)),
|
||||
prev24h: Number.isFinite(counts.prev24h)
|
||||
? Math.max(0, Math.floor(counts.prev24h))
|
||||
: null,
|
||||
focus: normalizeFocus(raw.focus, bbox),
|
||||
latestAt: iso(raw.latestAt),
|
||||
});
|
||||
}
|
||||
return {
|
||||
hotspots,
|
||||
categories: Array.isArray(payload.categories)
|
||||
? payload.categories.filter(
|
||||
(c) => typeof c === 'string' && CATEGORY.test(c),
|
||||
)
|
||||
: [],
|
||||
lastRunAt: iso(payload.ingest?.lastRunAt),
|
||||
windowHours: hoursOrNull(payload.windowHours),
|
||||
maxWindowHours: hoursOrNull(payload.maxWindowHours),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeGeo(geo) {
|
||||
if (!geo || typeof geo !== 'object') return null;
|
||||
const lat = finite(geo.lat, -90, 90);
|
||||
const lon = finite(geo.lon, -180, 180);
|
||||
if (lat === null || lon === null) return null;
|
||||
return {
|
||||
lat,
|
||||
lon,
|
||||
place: text(geo.place, 80),
|
||||
precision: text(geo.precision, 16) || 'city',
|
||||
confidence: finite(geo.confidence, 0, 1) ?? 0,
|
||||
mappable: geo.mappable === true,
|
||||
byModel: geo.method === 'llm',
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The event's picture: an HTTPS URL on the publisher's site, and whose it is;
|
||||
* with the post's own clip when it has one a browser plays itself (an HTTPS
|
||||
* MP4 or WebM file), the picture then its poster.
|
||||
*/
|
||||
function eventImage(image) {
|
||||
const url = httpUrl(image?.url);
|
||||
if (!url?.startsWith('https://')) return null;
|
||||
const video = httpUrl(image.video);
|
||||
const playable =
|
||||
video?.startsWith('https://') &&
|
||||
/\.(?:mp4|m4v|webm)$/i.test(new URL(video).pathname);
|
||||
return {
|
||||
url,
|
||||
publisher: text(image.publisher, 80) || '',
|
||||
...(playable ? { video } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
/** An event summary from `/feed?events=1`: size and one report per side. */
|
||||
function normalizeEvent(event) {
|
||||
if (!event || typeof event !== 'object') return null;
|
||||
const count = (value) => Math.max(1, Math.floor(Number(value) || 1));
|
||||
const sides = [];
|
||||
for (const side of Array.isArray(event.sides)
|
||||
? event.sides.slice(0, 8)
|
||||
: []) {
|
||||
const url = httpUrl(side?.url);
|
||||
const title = text(side?.title, 300);
|
||||
if (!url || !title) continue;
|
||||
sides.push({
|
||||
side: code(side.side) || 'unknown',
|
||||
title,
|
||||
url,
|
||||
publisher: text(side.publisher, 80) || '',
|
||||
publishedAt: iso(side.publishedAt),
|
||||
});
|
||||
}
|
||||
return {
|
||||
id: typeof event.id === 'string' && ID.test(event.id) ? event.id : null,
|
||||
status: EVENT_STATUSES.includes(event.status) ? event.status : null,
|
||||
claimedBy: text(event.claimedBy, 80),
|
||||
reportedSide: code(event.reportedSide),
|
||||
size: count(event.size),
|
||||
reports: count(event.reports),
|
||||
severity: severity(event.severity),
|
||||
firstAt: iso(event.firstAt),
|
||||
lastAt: iso(event.lastAt),
|
||||
sides,
|
||||
image: eventImage(event.image),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeReport(raw) {
|
||||
const id = typeof raw?.id === 'string' && ID.test(raw.id) ? raw.id : null;
|
||||
const title = text(raw?.title, 300);
|
||||
const url = httpUrl(raw?.url);
|
||||
const publishedAt = iso(raw?.publishedAt);
|
||||
if (!id || !title || !url || !publishedAt) return null;
|
||||
const source = raw.source || {};
|
||||
const geo = normalizeGeo(raw.geo);
|
||||
// Map positions: the report's own place plus any other hotspot its title
|
||||
// names (a roundup is pinned in each).
|
||||
const pins = [];
|
||||
for (const place of [
|
||||
geo,
|
||||
...(Array.isArray(raw.locations)
|
||||
? raw.locations.slice(0, 8).map(normalizeGeo)
|
||||
: []),
|
||||
])
|
||||
if (
|
||||
place?.mappable &&
|
||||
!pins.some((p) => p.lat === place.lat && p.lon === place.lon)
|
||||
)
|
||||
pins.push(place);
|
||||
const hotspotId = typeof raw.hotspotId === 'string' ? raw.hotspotId : null;
|
||||
return {
|
||||
id,
|
||||
hotspotId,
|
||||
hotspotIds: Array.isArray(raw.hotspotIds)
|
||||
? raw.hotspotIds.filter((h) => typeof h === 'string').slice(0, 8)
|
||||
: [hotspotId].filter(Boolean),
|
||||
title,
|
||||
url,
|
||||
snippet: text(raw.snippet, 400),
|
||||
publisher: text(raw.publisher, 80) || text(source.name, 80) || '',
|
||||
sourceName: text(source.name, 80) || text(raw.publisher, 80) || '',
|
||||
kind: text(raw.kind, 16) || 'rss',
|
||||
side: code(raw.side) || 'unknown',
|
||||
affiliation: text(raw.affiliation, 32),
|
||||
attribution: text(source.attribution, 120),
|
||||
category:
|
||||
typeof raw.category === 'string' && CATEGORY.test(raw.category)
|
||||
? raw.category
|
||||
: 'other',
|
||||
publishedAt,
|
||||
geo,
|
||||
pins,
|
||||
event: normalizeEvent(raw.event),
|
||||
titleEn: text(raw.titleEn, 200),
|
||||
relevance: ['core', 'related', 'minor'].includes(raw.relevance)
|
||||
? raw.relevance
|
||||
: 'related',
|
||||
severity: severity(raw.severity),
|
||||
severityBy: ['model', 'label'].includes(raw.severityBy)
|
||||
? raw.severityBy
|
||||
: 'rules',
|
||||
claim:
|
||||
raw.claim && typeof raw.claim === 'object'
|
||||
? { by: text(raw.claim.by, 80) }
|
||||
: null,
|
||||
media: Array.isArray(raw.media)
|
||||
? [...new Set(raw.media.filter((m) => m === 'video' || m === 'photo'))]
|
||||
: [],
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeRing(ring) {
|
||||
if (!Array.isArray(ring) || ring.length < 3) return null;
|
||||
const points = [];
|
||||
for (const point of ring.slice(0, 20_000)) {
|
||||
const lon = finite(point?.[0], -180, 180);
|
||||
const lat = finite(point?.[1], -90, 90);
|
||||
if (lon !== null && lat !== null) points.push([lon, lat]);
|
||||
}
|
||||
return points.length >= 3 ? points : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* `/api/news/outline` → `{polygons, borders, disputed, area}`: the dissolved
|
||||
* extent (`{outer, holes}`), the national borders inside it (rings), its
|
||||
* disputed areas and, answering a point, the area named there (else null),
|
||||
* invalid parts dropped.
|
||||
*/
|
||||
export function normalizeOutlinePayload(payload) {
|
||||
const polygons = [];
|
||||
// Above every country's island count (Indonesia's is 208): an outline cut
|
||||
// short leaves islands outside the area, read again on each settle there.
|
||||
for (const raw of Array.isArray(payload?.polygons)
|
||||
? payload.polygons.slice(0, 300)
|
||||
: []) {
|
||||
const outer = normalizeRing(raw?.outer);
|
||||
if (!outer) continue;
|
||||
const holes = (Array.isArray(raw.holes) ? raw.holes.slice(0, 200) : [])
|
||||
.map(normalizeRing)
|
||||
.filter(Boolean);
|
||||
polygons.push({ outer, holes });
|
||||
}
|
||||
const borders = (
|
||||
Array.isArray(payload?.borders) ? payload.borders.slice(0, 50) : []
|
||||
)
|
||||
.map(normalizeRing)
|
||||
.filter(Boolean);
|
||||
const KINDS = ['held', 'self', 'un', 'unclaimed'];
|
||||
const country = (value) =>
|
||||
typeof value === 'string' && /^[A-Z]{2}$/.test(value) ? value : null;
|
||||
const disputed = [];
|
||||
for (const raw of Array.isArray(payload?.disputed)
|
||||
? payload.disputed.slice(0, 30)
|
||||
: []) {
|
||||
const rings = (Array.isArray(raw?.rings) ? raw.rings.slice(0, 20) : [])
|
||||
.map(normalizeRing)
|
||||
.filter(Boolean);
|
||||
const name = text(raw?.name, 60);
|
||||
if (!rings.length || !name) continue;
|
||||
disputed.push({
|
||||
name,
|
||||
kind: KINDS.includes(raw.kind) ? raw.kind : 'held',
|
||||
heldBy: country(raw.heldBy),
|
||||
claimedBy: country(raw.claimedBy),
|
||||
rings,
|
||||
});
|
||||
}
|
||||
return { polygons, borders, disputed, area: outlineArea(payload?.area) };
|
||||
}
|
||||
|
||||
const AREA_KINDS = ['country', 'region', 'disputed'];
|
||||
function outlineArea(raw) {
|
||||
const key = text(raw?.key, 80);
|
||||
const name = text(raw?.name, 80);
|
||||
if (!key || !name) return null;
|
||||
return {
|
||||
key,
|
||||
name,
|
||||
kind: AREA_KINDS.includes(raw.kind) ? raw.kind : 'country',
|
||||
country:
|
||||
typeof raw.country === 'string' && /^[A-Z]{2}$/.test(raw.country)
|
||||
? raw.country
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
/** `/api/news/feed` → `{reports, nextBefore}`, or null without a report list. */
|
||||
export function normalizeFeedPayload(payload) {
|
||||
if (!Array.isArray(payload?.reports)) return null;
|
||||
const reports = [];
|
||||
for (const raw of payload.reports.slice(0, 500)) {
|
||||
const report = normalizeReport(raw);
|
||||
if (report) reports.push(report);
|
||||
}
|
||||
return { reports, nextBefore: iso(payload.nextBefore) };
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
import { readResponseJsonCapped } from '../../sources/httpBody.js';
|
||||
import { isUnavailableCapability } from '../../sources/capability.js';
|
||||
import {
|
||||
normalizeFeedPayload,
|
||||
normalizeHotspotsPayload,
|
||||
normalizeOutlinePayload,
|
||||
} from './records.js';
|
||||
|
||||
// A portable export (package.json): no i18n here. Failures stay English data;
|
||||
// the layer shows them translated.
|
||||
const msg = (text) => text;
|
||||
|
||||
const MAX_BYTES = 4 * 1024 * 1024;
|
||||
const BASE_PATH = '/api/news';
|
||||
// The outline geometry version the client draws (disputed areas included).
|
||||
const OUTLINE_VERSION = '4';
|
||||
|
||||
/** The three read routes the news layer calls, for a host's path allowlist. */
|
||||
export const NEWS_API_PATHS = Object.freeze([
|
||||
`${BASE_PATH}/hotspots`,
|
||||
`${BASE_PATH}/feed`,
|
||||
`${BASE_PATH}/outline`,
|
||||
]);
|
||||
|
||||
const FEED_KEYS = Object.freeze([
|
||||
'hotspot',
|
||||
'before',
|
||||
'since',
|
||||
'limit',
|
||||
'category',
|
||||
'relevance',
|
||||
'at',
|
||||
]);
|
||||
const FLAG_KEYS = Object.freeze(['geo', 'today', 'events']);
|
||||
const ISO = /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d{1,3})?Z$/;
|
||||
const LIST = /^[a-z][a-z,-]{0,127}$/;
|
||||
// A point to two decimals (~1 km), as areaPoint writes it.
|
||||
const AT = /^-?\d{1,2}(?:\.\d{1,2})?,-?\d{1,3}(?:\.\d{1,2})?$/;
|
||||
// A coordinate to two decimals, without trailing zeros ("50.4", "-97.74").
|
||||
const areaPoint = (value) => String(Math.round(value * 100) / 100);
|
||||
const FEED_VALUE = {
|
||||
hotspot: (v) => /^[a-z0-9][a-z0-9_-]{0,63}$/i.test(v),
|
||||
before: (v) => ISO.test(v),
|
||||
since: (v) => ISO.test(v),
|
||||
limit: (v) => /^\d{1,3}$/.test(v) && Number(v) >= 1 && Number(v) <= 500,
|
||||
category: (v) => LIST.test(v),
|
||||
relevance: (v) => LIST.test(v),
|
||||
at: (v) => AT.test(v),
|
||||
};
|
||||
|
||||
/**
|
||||
* Feed query string from a plain object; unknown keys and empty values are
|
||||
* dropped. `at` is a point (`{lat, lon}`), the Local section's area there.
|
||||
*/
|
||||
export function newsFeedQuery(query = {}) {
|
||||
const params = new URLSearchParams();
|
||||
for (const key of FEED_KEYS) {
|
||||
const value =
|
||||
key === 'at' && query.at
|
||||
? `${areaPoint(query.at.lat)},${areaPoint(query.at.lon)}`
|
||||
: query[key];
|
||||
if (value !== undefined && value !== null && value !== '')
|
||||
params.set(key, String(value));
|
||||
}
|
||||
for (const key of FLAG_KEYS) if (query[key]) params.set(key, '1');
|
||||
return params.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a request URL is one this client sends: a known route, known keys
|
||||
* only, each value in its form. A host proxying the routes can admit exactly
|
||||
* these and refuse the rest.
|
||||
* @param {URL} url
|
||||
*/
|
||||
export function newsQueryAllowed(url) {
|
||||
const params = [...url.searchParams];
|
||||
if (new Set(params.map(([key]) => key)).size !== params.length) return false;
|
||||
if (url.pathname === `${BASE_PATH}/hotspots`)
|
||||
return (
|
||||
params.length === 0 ||
|
||||
(params.length === 1 &&
|
||||
params[0][0] === 'hours' &&
|
||||
/^\d{1,2}$/.test(params[0][1]) &&
|
||||
Number(params[0][1]) >= 1 &&
|
||||
Number(params[0][1]) <= 24)
|
||||
);
|
||||
if (url.pathname === `${BASE_PATH}/outline`) {
|
||||
const keys = params.map(([key]) => key).sort();
|
||||
const value = url.searchParams;
|
||||
return (
|
||||
params.length === 2 &&
|
||||
keys[1] === 'v' &&
|
||||
value.get('v') === OUTLINE_VERSION &&
|
||||
((keys[0] === 'hotspot' && FEED_VALUE.hotspot(value.get('hotspot'))) ||
|
||||
(keys[0] === 'country' &&
|
||||
/^[A-Z]{2}(?:-[A-Z0-9]{1,3})?$/.test(value.get('country'))) ||
|
||||
(keys[0] === 'at' && AT.test(value.get('at'))))
|
||||
);
|
||||
}
|
||||
if (url.pathname !== `${BASE_PATH}/feed`) return false;
|
||||
return params.every(([key, value]) =>
|
||||
FLAG_KEYS.includes(key)
|
||||
? value === '1'
|
||||
: Object.hasOwn(FEED_VALUE, key) && FEED_VALUE[key](value),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* A failed read: a server without the news routes is a permanent miss; one
|
||||
* that reads a section from a key it lacks (503 `no_key`, `key_rejected` or,
|
||||
* its allowance for the day spent, `key_quota`, naming the key registry's id)
|
||||
* says which, and is asked again later.
|
||||
*/
|
||||
async function responseError(response) {
|
||||
let body = null;
|
||||
try {
|
||||
body = await readResponseJsonCapped(response, 16 * 1024);
|
||||
} catch {
|
||||
await response.body?.cancel?.().catch?.(() => {});
|
||||
}
|
||||
if (
|
||||
response.status === 503 &&
|
||||
['no_key', 'key_rejected', 'key_quota'].includes(body?.error) &&
|
||||
typeof body.key === 'string' &&
|
||||
/^[a-z][a-z0-9-]{0,31}$/.test(body.key)
|
||||
)
|
||||
return Object.assign(new Error(msg('News key required')), {
|
||||
status: 503,
|
||||
code: body.error,
|
||||
keyRequired: true,
|
||||
keyRejected: body.error === 'key_rejected',
|
||||
keyQuota: body.error === 'key_quota',
|
||||
keyId: body.key,
|
||||
unavailable: false,
|
||||
retryable: true,
|
||||
});
|
||||
const unavailable =
|
||||
response.status === 404 ||
|
||||
(response.status === 503 && isUnavailableCapability(body));
|
||||
return Object.assign(
|
||||
new Error(
|
||||
unavailable
|
||||
? msg('News feed not configured on this server')
|
||||
: msg('News feed unavailable'),
|
||||
),
|
||||
{
|
||||
status: response.status,
|
||||
code: unavailable ? 'unavailable' : 'http',
|
||||
unavailable,
|
||||
retryable: !unavailable,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/** Read hotspots, reports and outlines from the server's /api/news routes. */
|
||||
export function createNewsSource({
|
||||
fetchImpl = (...args) => globalThis.fetch(...args),
|
||||
timeoutMs = 15_000,
|
||||
} = {}) {
|
||||
async function get(path, { signal } = {}) {
|
||||
signal?.throwIfAborted();
|
||||
const timeout = AbortSignal.timeout(timeoutMs);
|
||||
const request = signal ? AbortSignal.any([signal, timeout]) : timeout;
|
||||
try {
|
||||
const response = await fetchImpl(`${BASE_PATH}${path}`, {
|
||||
signal: request,
|
||||
cache: 'no-store',
|
||||
redirect: 'error',
|
||||
});
|
||||
if (!response.ok) throw await responseError(response);
|
||||
return await readResponseJsonCapped(response, MAX_BYTES, request);
|
||||
} catch (cause) {
|
||||
// Out of time the feed reads unavailable, not the browser's own words
|
||||
// ("signal timed out"); the caller's own abort stays an abort.
|
||||
if (timeout.aborted && !signal?.aborted)
|
||||
throw Object.assign(new Error(msg('News feed unavailable')), {
|
||||
code: 'timeout',
|
||||
unavailable: false,
|
||||
retryable: true,
|
||||
});
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
return {
|
||||
// `hours` asks for counts over a shorter window than the server's own.
|
||||
async getHotspots({ signal, hours } = {}) {
|
||||
const payload = normalizeHotspotsPayload(
|
||||
await get(hours ? `/hotspots?hours=${hours}` : '/hotspots', {
|
||||
signal,
|
||||
}),
|
||||
);
|
||||
if (!payload) throw new Error(msg('Malformed news hotspots response'));
|
||||
return payload;
|
||||
},
|
||||
async getOutline(hotspotId, { signal } = {}) {
|
||||
return normalizeOutlinePayload(
|
||||
await get(
|
||||
`/outline?hotspot=${encodeURIComponent(hotspotId)}&v=${OUTLINE_VERSION}`,
|
||||
{ signal },
|
||||
),
|
||||
);
|
||||
},
|
||||
async getCountryOutline(country, { signal } = {}) {
|
||||
return normalizeOutlinePayload(
|
||||
await get(
|
||||
`/outline?country=${encodeURIComponent(country)}&v=${OUTLINE_VERSION}`,
|
||||
{ signal },
|
||||
),
|
||||
);
|
||||
},
|
||||
// The area the Local section reads at a point, with its outline.
|
||||
async getAreaOutline({ lat, lon }, { signal } = {}) {
|
||||
return normalizeOutlinePayload(
|
||||
await get(
|
||||
`/outline?at=${areaPoint(lat)},${areaPoint(lon)}&v=${OUTLINE_VERSION}`,
|
||||
{ signal },
|
||||
),
|
||||
);
|
||||
},
|
||||
async getFeed(query = {}, { signal } = {}) {
|
||||
const payload = normalizeFeedPayload(
|
||||
await get(`/feed?${newsFeedQuery(query)}`, { signal }),
|
||||
);
|
||||
if (!payload) throw new Error(msg('Malformed news feed response'));
|
||||
return payload;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
NEWS_API_PATHS,
|
||||
createNewsSource,
|
||||
newsFeedQuery,
|
||||
newsQueryAllowed,
|
||||
} from './source.js';
|
||||
|
||||
const json = (body, status = 200) =>
|
||||
new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
});
|
||||
|
||||
function recorder(respond) {
|
||||
const calls = [];
|
||||
return {
|
||||
calls,
|
||||
fetchImpl: async (url, init) => {
|
||||
calls.push({ url, init });
|
||||
return respond(url, init);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
test('reads validated hotspots, feed and outlines from the same-origin routes', async () => {
|
||||
const { calls, fetchImpl } = recorder((url) => {
|
||||
if (url.startsWith('/api/news/hotspots'))
|
||||
return json({
|
||||
hotspots: [{ id: 'ua', name: 'UA', bbox: [22, 44, 42, 56] }],
|
||||
});
|
||||
if (url.startsWith('/api/news/feed'))
|
||||
return json({
|
||||
reports: [
|
||||
{
|
||||
id: 'r1',
|
||||
title: 'T',
|
||||
url: 'https://x.test/',
|
||||
publishedAt: '2026-10-28T09:00:00Z',
|
||||
},
|
||||
],
|
||||
nextBefore: null,
|
||||
});
|
||||
return json({ polygons: [], borders: [], disputed: [] });
|
||||
});
|
||||
const source = createNewsSource({ fetchImpl });
|
||||
assert.equal((await source.getHotspots()).hotspots[0].id, 'ua');
|
||||
assert.equal(
|
||||
(await source.getFeed({ hotspot: 'all', today: true, limit: 60 }))
|
||||
.reports[0].id,
|
||||
'r1',
|
||||
);
|
||||
assert.deepEqual((await source.getOutline('ua')).polygons, []);
|
||||
await source.getCountryOutline('SD');
|
||||
await source.getHotspots({ hours: 6 });
|
||||
await source.getAreaOutline({ lat: 30.26666, lon: -97.7 });
|
||||
await source.getFeed({ hotspot: 'all', at: { lat: 30.26666, lon: -97.7 } });
|
||||
assert.deepEqual(
|
||||
calls.map((c) => c.url),
|
||||
[
|
||||
'/api/news/hotspots',
|
||||
'/api/news/feed?hotspot=all&limit=60&today=1',
|
||||
'/api/news/outline?hotspot=ua&v=4',
|
||||
'/api/news/outline?country=SD&v=4',
|
||||
'/api/news/hotspots?hours=6',
|
||||
'/api/news/outline?at=30.27,-97.7&v=4',
|
||||
'/api/news/feed?hotspot=all&at=30.27%2C-97.7',
|
||||
],
|
||||
);
|
||||
for (const { url, init } of calls) {
|
||||
assert.equal(init.redirect, 'error');
|
||||
assert.equal(init.cache, 'no-store');
|
||||
assert.ok(init.signal instanceof AbortSignal);
|
||||
// Every URL the client sends is one a host's allowlist admits.
|
||||
assert.equal(newsQueryAllowed(new URL(url, 'http://h')), true, url);
|
||||
}
|
||||
});
|
||||
|
||||
test('malformed, failed and unconfigured reads reject; a server without news is a permanent miss', async () => {
|
||||
const malformed = createNewsSource({ fetchImpl: async () => json({}) });
|
||||
await assert.rejects(malformed.getHotspots(), /Malformed news hotspots/);
|
||||
await assert.rejects(malformed.getFeed(), /Malformed news feed/);
|
||||
|
||||
const down = createNewsSource({
|
||||
fetchImpl: async () => json({ error: 'upstream' }, 502),
|
||||
});
|
||||
await assert.rejects(down.getHotspots(), (error) => {
|
||||
assert.equal(error.message, 'News feed unavailable');
|
||||
assert.equal(error.retryable, true);
|
||||
assert.equal(error.unavailable, false);
|
||||
return true;
|
||||
});
|
||||
|
||||
const unconfigured = createNewsSource({
|
||||
fetchImpl: async () =>
|
||||
json(
|
||||
{ error: 'news_not_configured', unavailable: true, retryable: false },
|
||||
503,
|
||||
),
|
||||
});
|
||||
await assert.rejects(unconfigured.getHotspots(), (error) => {
|
||||
assert.equal(error.message, 'News feed not configured on this server');
|
||||
assert.equal(error.code, 'unavailable');
|
||||
assert.equal(error.unavailable, true);
|
||||
assert.equal(error.retryable, false);
|
||||
return true;
|
||||
});
|
||||
const missing = createNewsSource({
|
||||
fetchImpl: async () => new Response('Not found', { status: 404 }),
|
||||
});
|
||||
await assert.rejects(missing.getFeed(), { unavailable: true });
|
||||
const broken = createNewsSource({
|
||||
fetchImpl: async () => new Response('{', { status: 200 }),
|
||||
});
|
||||
await assert.rejects(broken.getHotspots(), SyntaxError);
|
||||
});
|
||||
|
||||
test('a section read from a key the server lacks names the key and is asked again', async () => {
|
||||
const answer = (body) =>
|
||||
createNewsSource({
|
||||
fetchImpl: async () =>
|
||||
new Response(JSON.stringify(body), {
|
||||
status: 503,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
}),
|
||||
});
|
||||
await assert.rejects(
|
||||
answer({ error: 'no_key', key: 'liveuamap' }).getHotspots(),
|
||||
(error) => {
|
||||
assert.equal(error.keyRequired, true);
|
||||
assert.equal(error.keyRejected, false);
|
||||
assert.equal(error.keyId, 'liveuamap');
|
||||
assert.equal(error.unavailable, false);
|
||||
assert.equal(error.retryable, true);
|
||||
return true;
|
||||
},
|
||||
);
|
||||
await assert.rejects(
|
||||
answer({ error: 'key_rejected', key: 'worldnewsapi' }).getFeed({
|
||||
at: { lat: 51.13, lon: 71.43 },
|
||||
}),
|
||||
{ keyRequired: true, keyRejected: true, keyId: 'worldnewsapi' },
|
||||
);
|
||||
// The day's allowance spent: still the key's matter, back tomorrow.
|
||||
await assert.rejects(
|
||||
answer({ error: 'key_quota', key: 'worldnewsapi' }).getFeed(),
|
||||
{
|
||||
keyRequired: true,
|
||||
keyRejected: false,
|
||||
keyQuota: true,
|
||||
keyId: 'worldnewsapi',
|
||||
unavailable: false,
|
||||
},
|
||||
);
|
||||
// A key name not in the registry's form is an ordinary failure.
|
||||
await assert.rejects(
|
||||
answer({ error: 'no_key', key: '<script>' }).getHotspots(),
|
||||
(error) => error.keyRequired === undefined && error.code === 'http',
|
||||
);
|
||||
});
|
||||
|
||||
test('reads time out and follow the caller signal', async () => {
|
||||
// AbortSignal.timeout does not hold the event loop open: the hanging fetch
|
||||
// does, until its signal aborts, so the run cannot end before it.
|
||||
const hang = (_url, { signal }) =>
|
||||
new Promise((_, reject) => {
|
||||
const alive = setTimeout(() => {}, 60_000);
|
||||
signal.addEventListener(
|
||||
'abort',
|
||||
() => {
|
||||
clearTimeout(alive);
|
||||
reject(signal.reason);
|
||||
},
|
||||
{ once: true },
|
||||
);
|
||||
});
|
||||
const slow = createNewsSource({ fetchImpl: hang, timeoutMs: 20 });
|
||||
// Out of time it reads unavailable, in the app's words, as retryable.
|
||||
await assert.rejects(slow.getHotspots(), {
|
||||
message: 'News feed unavailable',
|
||||
code: 'timeout',
|
||||
retryable: true,
|
||||
});
|
||||
// The caller's abort stays its own, even once the time is up too.
|
||||
const late = (_url, { signal }) =>
|
||||
new Promise((_, reject) => {
|
||||
setTimeout(() => reject(signal.reason), 40);
|
||||
});
|
||||
const leaving = new AbortController();
|
||||
const gone = createNewsSource({ fetchImpl: late, timeoutMs: 20 }).getFeed(
|
||||
{},
|
||||
{ signal: leaving.signal },
|
||||
);
|
||||
leaving.abort(new Error('gone'));
|
||||
await assert.rejects(gone, /gone/);
|
||||
const caller = new AbortController();
|
||||
const pending = createNewsSource({ fetchImpl: hang }).getFeed(
|
||||
{},
|
||||
{ signal: caller.signal },
|
||||
);
|
||||
caller.abort(new Error('gone'));
|
||||
await assert.rejects(pending, /gone/);
|
||||
await assert.rejects(
|
||||
createNewsSource({ fetchImpl: hang }).getHotspots({
|
||||
signal: AbortSignal.abort(new Error('already')),
|
||||
}),
|
||||
/already/,
|
||||
);
|
||||
});
|
||||
|
||||
test('the host allowlist admits the client routes and refuses anything else', () => {
|
||||
const allowed = (path) => newsQueryAllowed(new URL(path, 'http://h'));
|
||||
assert.deepEqual(NEWS_API_PATHS, [
|
||||
'/api/news/hotspots',
|
||||
'/api/news/feed',
|
||||
'/api/news/outline',
|
||||
]);
|
||||
assert.equal(
|
||||
allowed(
|
||||
`/api/news/feed?${newsFeedQuery({ hotspot: 'ua-ru-war', before: '2026-10-28T08:00:00.000Z', limit: 500, category: 'strike', relevance: 'core,related', geo: true, today: true, events: true })}`,
|
||||
),
|
||||
true,
|
||||
);
|
||||
assert.equal(allowed('/api/news/hotspots?hours=6'), true);
|
||||
for (const path of [
|
||||
'/api/news/hotspots?hours=0',
|
||||
'/api/news/hotspots?hours=25',
|
||||
'/api/news/hotspots?hours=6&hours=3',
|
||||
'/api/news/hotspots?hours=6&x=1',
|
||||
'/api/news/hotspots?x=1',
|
||||
'/api/news/feed?limit=501',
|
||||
'/api/news/feed?limit=0',
|
||||
'/api/news/feed?hotspot=all&hotspot=ua',
|
||||
'/api/news/feed?since=yesterday',
|
||||
'/api/news/feed?geo=yes',
|
||||
'/api/news/feed?side=UA',
|
||||
'/api/news/air-alerts',
|
||||
'/api/news/outline?hotspot=ua',
|
||||
'/api/news/outline?hotspot=ua&v=2',
|
||||
'/api/news/outline?country=ukraine&v=4',
|
||||
'/api/news/outline?at=30.267,-97.7&v=4',
|
||||
'/api/news/outline?at=30.27&v=4',
|
||||
'/api/news/outline?at=30.27,-97.7&country=US&v=4',
|
||||
'/api/news/feed?at=30.267,-97.7',
|
||||
])
|
||||
assert.equal(allowed(path), false, path);
|
||||
});
|
||||
@@ -0,0 +1,131 @@
|
||||
// Marker sprites for news reports: a dark disc with the category's icon and
|
||||
// a rim in the severity colour, the rim's form telling attestation. Drawn once per look
|
||||
// on a canvas and handed to the renderer as a data URL, so its texture atlas keeps
|
||||
// one copy per look however often the layer re-renders.
|
||||
|
||||
import { NEWS_CATEGORIES, NEWS_SEVERITY } from './model.js';
|
||||
|
||||
const CANVAS = 64;
|
||||
const BODY = 52; // disc diameter inside the canvas; the rest is rim and ring
|
||||
const INK = 'rgba(10, 10, 15, 0.92)';
|
||||
const SELECTED = '#00d4ff';
|
||||
const COUNT_INK = '#e8eaed';
|
||||
|
||||
/** Canvas pixels per marker pixel: the billboard is drawn at this ratio. */
|
||||
export const SPRITE_SCALE = CANVAS / BODY;
|
||||
|
||||
const cache = new Map();
|
||||
|
||||
/**
|
||||
* Paint one marker look centred at (cx, cy) with a disc of `body` pixels: the
|
||||
* sprites and the event cards' badges draw the same thing.
|
||||
*/
|
||||
export function paintMarker(ctx, { category, severity, rim }, cx, cy, body) {
|
||||
const k = body / BODY; // the strokes below are tuned for the 52 px body
|
||||
const r = body / 2;
|
||||
// A dark edge keeps the lightest fills apart from bright ground.
|
||||
ctx.beginPath();
|
||||
ctx.arc(cx, cy, r + 1.5 * k, 0, 2 * Math.PI);
|
||||
ctx.fillStyle = 'rgba(0, 0, 0, 0.85)';
|
||||
ctx.fill();
|
||||
// GEV's dark glass disc, as its HUD buttons are; severity colours the rim
|
||||
// and the icon rather than filling the disc.
|
||||
ctx.beginPath();
|
||||
ctx.arc(cx, cy, r, 0, 2 * Math.PI);
|
||||
ctx.fillStyle = INK;
|
||||
ctx.fill();
|
||||
const color = (NEWS_SEVERITY[severity] || NEWS_SEVERITY[0]).color;
|
||||
|
||||
// Attestation is the rim's form, never its colour.
|
||||
ctx.beginPath();
|
||||
ctx.arc(cx, cy, r - 3 * k, 0, 2 * Math.PI);
|
||||
ctx.setLineDash(rim === 'claimed' ? [7 * k, 5 * k] : []);
|
||||
ctx.lineWidth = (rim === 'thin' ? 2.5 : 6) * k;
|
||||
ctx.strokeStyle = color;
|
||||
ctx.stroke();
|
||||
ctx.setLineDash([]);
|
||||
|
||||
const style = NEWS_CATEGORIES[category] || NEWS_CATEGORIES.other;
|
||||
const icon = body * 0.5;
|
||||
ctx.save();
|
||||
ctx.translate(cx - icon / 2, cy - icon / 2);
|
||||
ctx.scale(icon / 16, icon / 16);
|
||||
const path = new Path2D(style.icon);
|
||||
ctx.lineCap = 'round';
|
||||
ctx.lineJoin = 'round';
|
||||
ctx.lineWidth = 1.7;
|
||||
ctx.strokeStyle = color;
|
||||
if (style.solid) {
|
||||
ctx.fillStyle = color;
|
||||
ctx.fill(path);
|
||||
} else ctx.stroke(path);
|
||||
ctx.restore();
|
||||
}
|
||||
|
||||
/**
|
||||
* Data URL of the sprite for a look, or null where there is no canvas (the
|
||||
* layer then falls back to a plain point).
|
||||
* @param {{category: string, severity: number, rim: 'confirmed'|'claimed'|'thin', selected?: boolean}} look
|
||||
*/
|
||||
export function markerSprite({ category, severity, rim, selected = false }) {
|
||||
const key = `${category}|${severity}|${rim}|${selected ? 1 : 0}`;
|
||||
if (cache.has(key)) return cache.get(key);
|
||||
const canvas = globalThis.document?.createElement?.('canvas');
|
||||
const ctx = canvas?.getContext?.('2d');
|
||||
if (!ctx || typeof globalThis.Path2D !== 'function') return null;
|
||||
canvas.width = canvas.height = CANVAS;
|
||||
const c = CANVAS / 2;
|
||||
paintMarker(ctx, { category, severity, rim }, c, c, BODY);
|
||||
if (selected) {
|
||||
ctx.beginPath();
|
||||
ctx.arc(c, c, CANVAS / 2 - 2, 0, 2 * Math.PI);
|
||||
ctx.lineWidth = 3.5;
|
||||
ctx.strokeStyle = SELECTED;
|
||||
ctx.stroke();
|
||||
}
|
||||
const url = canvas.toDataURL('image/png');
|
||||
cache.set(key, url);
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Data URL of a country badge: the dark disc with a light rim and the number
|
||||
* of reports it stands for, drawn at the pixels it is shown at so the digits
|
||||
* stay sharp. Null where there is no canvas.
|
||||
* @param {number} count Reports the badge stands for.
|
||||
* @param {number} pixels The disc's diameter in drawn pixels.
|
||||
*/
|
||||
export function countSprite(count, pixels) {
|
||||
const body = Math.max(1, Math.round(pixels));
|
||||
const text = count > 99 ? '99+' : String(count);
|
||||
const key = `count|${text}|${body}`;
|
||||
if (cache.has(key)) return cache.get(key);
|
||||
const canvas = globalThis.document?.createElement?.('canvas');
|
||||
const ctx = canvas?.getContext?.('2d');
|
||||
if (!ctx) return null;
|
||||
const k = body / BODY;
|
||||
canvas.width = canvas.height = Math.round(CANVAS * k);
|
||||
const c = canvas.width / 2;
|
||||
const r = body / 2;
|
||||
ctx.beginPath();
|
||||
ctx.arc(c, c, r + 1.5 * k, 0, 2 * Math.PI);
|
||||
ctx.fillStyle = 'rgba(0, 0, 0, 0.85)';
|
||||
ctx.fill();
|
||||
ctx.beginPath();
|
||||
ctx.arc(c, c, r, 0, 2 * Math.PI);
|
||||
ctx.fillStyle = INK;
|
||||
ctx.fill();
|
||||
ctx.beginPath();
|
||||
ctx.arc(c, c, r - 2 * k, 0, 2 * Math.PI);
|
||||
ctx.lineWidth = 3 * k;
|
||||
ctx.strokeStyle = COUNT_INK;
|
||||
ctx.stroke();
|
||||
ctx.fillStyle = COUNT_INK;
|
||||
ctx.font = `600 ${Math.round(body * (text.length > 2 ? 0.34 : 0.44))}px ui-monospace, Menlo, monospace`;
|
||||
ctx.textAlign = 'center';
|
||||
ctx.textBaseline = 'middle';
|
||||
ctx.fillText(text, c, c + k);
|
||||
const url = canvas.toDataURL('image/png');
|
||||
cache.set(key, url);
|
||||
return url;
|
||||
}
|
||||
Reference in new issue
Block a user