Name the Local area, say what each News section needs, and credit its sources

Local reads the area at the view centre (`/outline?at=`) and asks the feed
for its reports (`at`): the area is outlined faintly on the globe and named
at the top of the section ("News from Kazakhstan"; within 500 km at sea).
A disputed area is named as itself. A read that a newer look replaced, or
one left behind by a switch to Conflicts, is dropped, and a server without
the area route is asked once.

A section read from a key the server lacks says which key it needs
(503 `no_key`), that the key was refused (`key_rejected`) or that its
daily allowance is spent (`key_quota`); the other section keeps reading.
Liveuamap and World News API join the key registry.

Cards name the service a report came through ("via GDELT Project"), and
Liveuamap and Natural Earth are credited once their data shows. Economy is
a category of its own. Disputed-area labels are smaller and show the name
only; outlines keep up to 300 parts.
This commit is contained in:
Denis Groshev committed 2026-11-06 00:45:31 +01:00
1 parent fa736a7cd5
commit edb5837ae5
27 files changed
+1343 -154

No files matched your search

+1 -1
View File
@@ -128,7 +128,7 @@ function paint(canvas, view, look, pic) {
ctx.strokeStyle = WORLD_OVERLAY_STYLE.border;
ctx.lineWidth = 1;
ctx.stroke();
// Severity down the left edge, where Breaking marks a Live report.
// 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();
+253 -39
View File
@@ -13,6 +13,11 @@ import {
removeEntityContextsForLayer,
selectEntityContext,
} from '../../data/contextStore.js';
import {
LIVEUAMAP_CREDIT,
NATURAL_EARTH_CREDIT,
registerDynamicCredit,
} from '../../data/dataCredits.js';
import { msg, t, tn } from '../../i18n/index.js';
import { createHorizonCull, viewCentre } from '../streetLevel/view.js';
import {
@@ -22,6 +27,7 @@ import {
NEWS_FEED_PAGE,
NEWS_FOCUS_HEIGHT_M,
NEWS_IN_VIEW_MAX_HEIGHT_M,
NEWS_IN_VIEW_RADIUS_KM,
NEWS_LOCAL_LIMIT,
NEWS_LAYER_ID,
NEWS_MAP_LIMIT,
@@ -32,7 +38,9 @@ import {
NEWS_SEVERITY,
NEWS_WINDOW_MS,
ageWords,
areaLabel,
attestation,
attributionLabel,
beaconPixelSize,
cardEvents,
cardView,
@@ -42,6 +50,7 @@ import {
feedQueryFor,
hotspotLabel,
hotspotTrend,
inPolygons,
isPressing,
markerStyle,
mediaLabels,
@@ -68,15 +77,54 @@ export * from './model.js';
export { createNewsSource } from './source.js';
const FEED_UNAVAILABLE = msg('News feed unavailable');
// The providers a server without a news pipeline reads each section from,
// by the key registry's ids (keySetupCore.mjs): name and variable. A copy, so
// the layer pulls in no registry; a test holds the two together.
export const NEWS_KEY_PROVIDERS = Object.freeze({
liveuamap: { name: 'Liveuamap', envVar: 'LIVEUAMAP_API_KEY' }, // i18n-ignore: provider product name
worldnewsapi: { name: 'World News API', envVar: 'WORLDNEWSAPI_KEY' }, // i18n-ignore: provider product name
});
// A read refused for want of a key, or its day's allowance spent:
// `{id, rejected, quota}`, else null.
const keyNeed = (cause) =>
cause?.keyRequired && Object.hasOwn(NEWS_KEY_PROVIDERS, cause.keyId)
? {
id: cause.keyId,
rejected: cause.keyRejected === true,
quota: cause.keyQuota === true,
}
: null;
/** What the News panel says of a section's missing, refused or spent key. */
function keyNotice(need) {
if (!need) return null;
const { name: provider, envVar: envVars } = NEWS_KEY_PROVIDERS[need.id];
if (need.quota)
return {
title: t('{provider} limit reached for today', { provider }),
hint: t('News resumes tomorrow, or with a paid plan'),
};
return need.rejected
? {
title: t('{provider} refused its key', { provider }),
hint: t('Check {envVars} in Provider Settings', { envVars }),
}
: {
title: t('{provider} key required', { provider }),
hint: t('Needs {envVars} — add it in Provider Settings', { envVars }),
};
}
const REPORT_PREFIX = `${NEWS_PICK_PREFIX}report:`;
const HOTSPOT_PREFIX = `${NEWS_PICK_PREFIX}hotspot:`;
const DEVELOPING_PREFIX = `${NEWS_PICK_PREFIX}developing:`;
const LOCAL_PREFIX = `${NEWS_PICK_PREFIX}local:`;
// Outlines in GEV's own colours, apart from the severity ramp: hotspots in
// the accent, developing places in its lighter tint, disputed areas dotted
// in the primary text colour.
// in the primary text colour, the Local section's area a faint line of it.
const DEVELOPING_COLOR = '#9defff';
const DISPUTED_COLOR = '#e8eaed';
const HOTSPOT_COLOR = '#00d4ff';
// Disputed areas' names are context, not news.
const DISPUTED_LABEL_SCALE = 0.75;
// A filtered feed page that shows almost nothing reads the next page by
// itself, a few times, so a strict severity floor still fills the list.
const AUTO_PAGES = 3;
@@ -88,8 +136,11 @@ const emptyFeed = () => ({
error: null,
});
// Local: `active` while the camera is low enough to have a "near here";
// `minor` holds the day's reports with minor news included, read once while
// the Local section is chosen.
// `minor` holds the day's reports for the area, minor news included, read
// once per area while the Local section is chosen (and with each refresh);
// `area` is the area named at the view centre (`{key, name, kind, country}`,
// null at sea), `areaPolygons` its outline and `areaAt` the point it was
// named at; `readKey` the area `minor` is of.
const emptyInView = () => ({
active: false,
cell: null,
@@ -97,15 +148,20 @@ const emptyInView = () => ({
minor: null,
loading: false,
error: null,
area: null,
areaPolygons: null,
areaAt: null,
areaLoading: false,
readKey: null,
});
/**
* News from curated hotspots, real time only (the last 24 hours). Zoomed out:
* one beacon per hotspot with its daily count and trend, and a breathing
* beacon per developing situation. Close up or with a hotspot chosen:
* outlines (disputed areas dotted) and the geocoded reports as markers, the
* big events as picture cards. The News panel renders the feed this layer
* publishes through getRowControls().feed.
* News from the hotspots a news server names, real time only (the last 24
* hours). Zoomed out: one beacon per hotspot with its daily count and trend,
* and a breathing beacon per developing situation. Close up or with a hotspot
* chosen: outlines (disputed areas dotted) and the geocoded reports as
* markers, the big events as picture cards. The News panel renders the feed
* this layer publishes through getRowControls().feed.
*/
export function createNewsLayer({
source,
@@ -116,6 +172,7 @@ export function createNewsLayer({
hitTestOverlay = hitTestWorldOverlay,
openUrl = (url) => globalThis.open?.(url, '_blank', 'noopener,noreferrer'),
getViewCentre = viewCentre,
registerCredit = registerDynamicCredit,
} = {}) {
if (
typeof source?.getHotspots !== 'function' ||
@@ -133,6 +190,7 @@ export function createNewsLayer({
request = null,
feedRequest = null,
inViewRequest = null,
areaRequest = null,
settleTimer = null,
pulseTimer = null,
removeCameraListener = null;
@@ -143,8 +201,13 @@ export function createNewsLayer({
error = null,
lastUpdate = null,
lastRunAt = null,
autoPages = 0;
autoPages = 0,
// A server without the area route (/outline?at=) answers so once; Local
// then keeps to the radius until the layer is turned off.
noAreaRoute = false;
let hotspots = [];
// Each section's missing or refused key (keyNeed), while its reads say so.
let needs = { conflicts: null, local: null };
// The window the server serves (newsWindowMs): a host may serve
// fewer hours than the day. `windowMs` is the one shown: that window, or a
// shorter one chosen in the panel (filters.hours).
@@ -159,6 +222,19 @@ export function createNewsLayer({
let shownDetail = null;
let cullTargets = [];
const contextEntities = new Map();
// Credits are registered once what they credit first shows; the registry
// keeps them for the session (as TomTom's) and has no removal.
const credited = new Set();
function credit(entry, shown) {
if (shown && !credited.has(entry.key) && registerCredit(viewer, entry))
credited.add(entry.key);
}
// Liveuamap asks to be credited wherever its mapped reports show.
const creditReports = (reports) =>
credit(
LIVEUAMAP_CREDIT,
reports.some((r) => r?.kind === 'liveuamap'),
);
const notify = () => listener?.();
const requestRender = () => viewer?.scene?.requestRender?.();
const reducedMotion = () =>
@@ -189,6 +265,7 @@ export function createNewsLayer({
now(),
limit,
windowMs,
inView.areaPolygons,
);
const shownWindowMs = () =>
Math.min(maxWindowMs, (filters.hours ?? Infinity) * 3_600_000);
@@ -389,20 +466,21 @@ export function createNewsLayer({
priority: 10_000 + hotspot.last24h,
}),
);
// Each disputed area is named, with who holds it and who claims it.
// Each disputed area is named, smaller than the news labels and below
// them in priority. Its title only: a label paints no detail line, yet
// the overlay sizes the label for one, which left a wide empty box.
for (const area of outlineDisputed.get(hotspot.id) || []) {
const ring = area.rings[0];
const lon = ring.reduce((sum, p) => sum + p[0], 0) / ring.length;
const lat = ring.reduce((sum, p) => sum + p[1], 0) / ring.length;
const label = disputedLabel(area);
labels.push(
ambientLabel({
id: `disputed:${hotspot.id}:${area.name}`,
position: cesium.Cartesian3.fromDegrees(lon, lat),
title: label.title,
details: [label.detail],
title: disputedLabel(area),
accent: DISPUTED_COLOR,
priority: 8_000,
presentationScale: DISPUTED_LABEL_SCALE,
}),
);
}
@@ -538,8 +616,9 @@ export function createNewsLayer({
}
}
// The selected report's card: what, where, how attested, and the link to
// the original article ("Source: host ↗"), which a click on it opens.
// The selected report's card: what, where, how attested, the link to the
// original article ("Source: host ↗"), which a click on it opens, and the
// service it came through.
function selectedCard(report, labels) {
if (!report?.pins.length) return;
const pin = report.pins[selectedPin] ? selectedPin : 0;
@@ -567,6 +646,7 @@ export function createNewsLayer({
attestation(report)?.label || '',
mediaLabels(report).join(' · '),
sourceLinkLabel(report.url),
attributionLabel(report),
].filter(Boolean),
accent: severityStyle(report).color,
selected: true,
@@ -612,6 +692,7 @@ export function createNewsLayer({
? [...shown, current]
: shown;
renderMarkers(entities, drawn);
if (enabled) creditReports(drawn.filter((r) => r.pins.length && onMap(r)));
entities.resumeEvents();
showOutlines(detail);
const at = cesium.JulianDate.now();
@@ -724,7 +805,7 @@ export function createNewsLayer({
function addOutline(
key,
{ polygons, borders, disputed = [] },
{ color, fillAlpha, pickId },
{ color, fillAlpha, pickId, lineAlpha = 0.85, width = 2 },
) {
if (!outlineSource || outlineParts.has(key)) return;
const css = cesium.Color.fromCssColorString(color);
@@ -761,8 +842,8 @@ export function createNewsLayer({
line(
`${pickId}~line${index}.${k}`,
closed(points),
2,
css.withAlpha(0.85),
width,
css.withAlpha(lineAlpha),
);
}
// National borders inside a multi-country hotspot: thin and dashed.
@@ -829,10 +910,21 @@ export function createNewsLayer({
const developing = new Set(
hotspots.flatMap((h) => (h.developing || []).map((d) => d.key)),
);
const localKey =
localMode() && inView.area ? `local:${inView.area.key}` : null;
// The built-in server's area outlines are Natural Earth's.
credit(NATURAL_EARTH_CREDIT, enabled && outlineParts.has(localKey));
for (const [key, parts] of outlineParts) {
const country = key.startsWith('developing:') ? key.slice(11) : null;
const visible = enabled && (country ? developing.has(country) : detail);
const fill = visible && !country && filters.hotspot !== key;
const isLocal = key.startsWith('local:');
const visible =
enabled &&
(isLocal
? key === localKey
: country
? developing.has(country)
: detail);
const fill = visible && !country && !isLocal && filters.hotspot !== key;
for (const entity of parts.fills)
if (entity.show !== fill) entity.show = fill;
for (const entity of parts.borders)
@@ -862,6 +954,9 @@ export function createNewsLayer({
);
if (controller.signal.aborted || feedRequest !== controller || !enabled)
return;
// Any news server may refuse a section for a key; a read that passes
// clears it, not only the next refresh.
needs = { ...needs, conflicts: null };
feed = {
...mergeFeedPage(feed, page, mode, now(), undefined, windowMs),
loading: false,
@@ -869,10 +964,12 @@ export function createNewsLayer({
};
} catch (cause) {
if (controller.signal.aborted || feedRequest !== controller) return;
const need = keyNeed(cause);
if (need) needs = { ...needs, conflicts: need };
feed = {
...feed,
loading: false,
error: cause?.message || FEED_UNAVAILABLE,
error: need ? null : cause?.message || FEED_UNAVAILABLE,
};
} finally {
if (feedRequest === controller) feedRequest = null;
@@ -890,11 +987,11 @@ export function createNewsLayer({
}
}
// "In view": below 3,000 km, the reports within 500 km of the view centre,
// placed again once the camera has settled for 0.8 s in another half-degree
// cell. The feed has no area query: the layer already holds the last day's
// geocoded reports (the map's), kept here by distance. Only "Include minor
// news" needs its own read, once per refresh, never per pan.
// "In view" (the Local section): below 3,000 km, the reports in the area
// named at the view centre (else, at sea, within 500 km of it), placed
// again once the camera has settled for 0.8 s in another half-degree cell.
// Each area is read once (its reports, minor news included, by the `at`
// query) and again with each refresh; a pan inside it reads nothing.
function scheduleInView() {
clearTimeout(settleTimer);
settleTimer = setTimeout(() => {
@@ -908,7 +1005,8 @@ export function createNewsLayer({
if (!(height < NEWS_IN_VIEW_MAX_HEIGHT_M)) {
if (inView.active) {
inViewRequest?.abort();
inViewRequest = null;
areaRequest?.abort();
inViewRequest = areaRequest = null;
inView = emptyInView();
if (local()) render();
notify();
@@ -921,13 +1019,31 @@ export function createNewsLayer({
if (!force && inView.active && cell === inView.cell) return;
inView = { ...inView, active: true, cell, centre };
if (!local()) {
// Conflicts reads neither the area nor its reports.
inViewRequest?.abort();
inViewRequest = null;
inView = { ...inView, minor: null, loading: false, error: null };
areaRequest?.abort();
inViewRequest = areaRequest = null;
inView = {
...inView,
minor: null,
loading: false,
error: null,
areaLoading: false,
};
notify();
return;
}
if (!force && (inView.minor || inViewRequest)) {
// The area first: the read asks for its reports (and wakes its
// country's national feeds on the server).
if ((await refreshArea(centre)) === 'superseded') return;
// The section changed, or the layer went off, while the area was read.
if (!local() || !enabled) return;
const readKey = inView.area?.key ?? '';
if (
!force &&
inView.readKey === readKey &&
(inView.minor || inViewRequest)
) {
render();
notify();
return;
@@ -946,12 +1062,15 @@ export function createNewsLayer({
events: true,
relevance: '',
limit: NEWS_MAP_LIMIT,
at: inView.area ? inView.areaAt : undefined,
},
{ signal: controller.signal },
);
if (controller.signal.aborted || inViewRequest !== controller) return;
needs = { ...needs, local: null };
inView = {
...inView,
readKey,
minor: page.reports.filter((r) =>
withinNewsWindow(r, now(), maxWindowMs),
),
@@ -960,10 +1079,15 @@ export function createNewsLayer({
};
} catch (cause) {
if (controller.signal.aborted || inViewRequest !== controller) return;
const need = keyNeed(cause);
needs = { ...needs, local: need };
inView = {
...inView,
// A missing key is read again with the next refresh, not each pan.
readKey: need ? readKey : inView.readKey,
minor: need ? [] : inView.minor,
loading: false,
error: cause?.message || FEED_UNAVAILABLE,
error: need ? null : cause?.message || FEED_UNAVAILABLE,
};
} finally {
if (inViewRequest === controller) {
@@ -974,6 +1098,67 @@ export function createNewsLayer({
}
}
// The area named at the view centre, read again only once the centre has
// left the one shown. Without one (at sea, or a server that names none)
// Local keeps to the radius. 'superseded' when a later look took over.
async function refreshArea(centre) {
if (!source.getAreaOutline || noAreaRoute) return 'none';
if (inView.areaPolygons && inPolygons(inView.areaPolygons, centre)) {
// Back inside the area shown: a read still out for where the camera
// was must not land over it.
if (areaRequest) {
areaRequest.abort();
areaRequest = null;
inView = { ...inView, areaLoading: false };
}
return 'same';
}
areaRequest?.abort();
const controller = new AbortController();
areaRequest = controller;
inView = { ...inView, areaLoading: true };
notify();
let found = null;
try {
found = await source.getAreaOutline(centre, {
signal: controller.signal,
});
} catch (cause) {
// No area: the radius stands in for it. A server without the route
// (or one that refuses the query) is not asked again.
if (cause?.unavailable === true || cause?.status === 400)
noAreaRoute = true;
}
if (controller.signal.aborted || areaRequest !== controller)
return 'superseded';
areaRequest = null;
// The camera moved on while this was read, and no later read replaced it.
if (inView.centre !== centre) {
inView = { ...inView, areaLoading: false };
notify();
return 'superseded';
}
const area = found?.area && found.polygons.length ? found.area : null;
if (area)
addOutline(`local:${area.key}`, found, {
color: DISPUTED_COLOR,
fillAlpha: 0,
lineAlpha: 0.45,
width: 1.5,
pickId: `${LOCAL_PREFIX}${area.key}`,
});
inView = {
...inView,
area,
areaPolygons: area ? found.polygons : null,
areaAt: area ? centre : null,
areaLoading: false,
};
render();
notify();
return 'read';
}
function installSelection() {
if (clickHandler || !viewer?.scene?.canvas) return;
clickHandler = new cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
@@ -1037,6 +1222,11 @@ export function createNewsLayer({
const at = now();
const report = selected();
const total = hotspots.reduce((sum, h) => sum + h.last24h, 0);
const listed = feed.items.filter((r) =>
passesFilters(r, filters, at, windowMs),
);
const nearby = localReports(NEWS_LOCAL_LIMIT);
if (enabled) creditReports([...(local() ? nearby : listed), report]);
return {
filters,
hotspots: [
@@ -1056,14 +1246,19 @@ export function createNewsLayer({
label: t(level.label),
color: level.color,
})),
items: feed.items
.filter((r) => passesFilters(r, filters, at, windowMs))
.map((r) => feedCardView(r, at)),
items: listed.map((r) => feedCardView(r, at)),
inView: {
active: inView.active,
loading: inView.loading,
loading: inView.loading || inView.areaLoading,
// What Local reads: the named area, else the radius at sea.
area: inView.area ? areaLabel(inView.area) : null,
// Until the first area is named, the panel names neither it nor the radius.
areaLoading: inView.areaLoading,
radiusKm: NEWS_IN_VIEW_RADIUS_KM,
error: inView.error ? t(FEED_UNAVAILABLE) : null,
items: localReports(NEWS_LOCAL_LIMIT).map((r) => feedCardView(r, at)),
// Local's key, when the server reads it from one and has none.
key: keyNotice(needs.local),
items: nearby.map((r) => feedCardView(r, at)),
},
selected:
report && withinNewsWindow(report, at, windowMs)
@@ -1074,6 +1269,8 @@ export function createNewsLayer({
loading: feed.loading || loading,
// Read failures are English data; the panel shows them translated.
error: error || feed.error ? t(error || feed.error) : null,
// Conflicts' key, likewise.
key: keyNotice(needs.conflicts),
lastRunAt,
windowHours: windowMs / 3_600_000,
windowChoices: newsWindowChoices(maxWindowMs / 3_600_000),
@@ -1125,11 +1322,13 @@ export function createNewsLayer({
disable() {
enabled = false;
unavailable = false;
noAreaRoute = false;
error = null;
request?.abort();
feedRequest?.abort();
inViewRequest?.abort();
request = feedRequest = inViewRequest = null;
areaRequest?.abort();
request = feedRequest = inViewRequest = areaRequest = null;
clearTimeout(settleTimer);
settleTimer = null;
loading = false;
@@ -1148,6 +1347,7 @@ export function createNewsLayer({
contextEntities.clear();
feed = emptyFeed();
inView = emptyInView();
needs = { conflicts: null, local: null };
mapReports = [];
if (dataSource) {
dataSource.entities.removeAll();
@@ -1190,6 +1390,7 @@ export function createNewsLayer({
if (!enabled || controller.signal.aborted || request !== controller)
return false;
hotspots = index.hotspots;
needs = { ...needs, conflicts: null };
loadOutlines();
lastRunAt = index.lastRunAt;
// A server that names no limit serves the window its counts cover
@@ -1212,6 +1413,19 @@ export function createNewsLayer({
return true;
} catch (cause) {
if (controller.signal.aborted || request !== controller) return false;
const need = keyNeed(cause);
if (need) {
// Conflicts has no key: nothing of its own, and Local still reads.
needs = { ...needs, conflicts: need };
hotspots = [];
mapReports = [];
feed = emptyFeed();
filters = normalizeFilters({}, hotspots, filters);
error = null;
render();
void refreshInView({ force: inView.active });
return true;
}
unavailable = cause?.unavailable === true;
error = cause?.message || FEED_UNAVAILABLE;
return true;
@@ -1272,7 +1486,7 @@ export function createNewsLayer({
chips: [],
legend: [],
info: t(
'Last 24 h from curated hotspots. Each report links to its publisher; places and categories are read from the text automatically and can be wrong. Side labels describe the source, not whether a report is true.',
'Last 24 h of news. Each report links to its publisher; places and categories are read from the text and data automatically and can be wrong. Side labels, where shown, describe the source, not whether a report is true.',
),
// A ready-to-render view for the News panel (src/ui/newsPanel.js),
// which takes no layer imports: labels and colours are resolved here.
+449 -1
View File
@@ -1,7 +1,8 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import { createNewsLayer } from './index.js';
import { KEY_SETUP_KEYS } from '../../keySetupCore.mjs';
import { NEWS_KEY_PROVIDERS, createNewsLayer } from './index.js';
const NOW = Date.parse('2026-10-28T10:00:00Z');
const hotspotsPayload = {
@@ -43,12 +44,14 @@ function fixture(
height = 20_000_000,
reduced = false,
centre = () => ({ lat: 49, lon: 36 }),
credits = [],
} = {},
) {
const calls = { feed: [], hotspots: 0, outline: 0 };
const reports = [
report('major', {
severity: 3,
attribution: 'GDELT Project',
event: {
size: 3,
severity: 3,
@@ -110,6 +113,10 @@ function fixture(
now: () => NOW,
matchMedia: () => ({ matches: reduced }),
getViewCentre: () => centre(),
registerCredit: (_viewer, credit) => {
credits.push(credit.key);
return true;
},
});
// The context store announces selections on `window`, which Node lacks.
const originalWindow = globalThis.window;
@@ -125,6 +132,34 @@ function fixture(
}
const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
const flush = async () => {
for (let i = 0; i < 6; i++) await Promise.resolve();
};
const keyError = (key, extra = {}) =>
Object.assign(new Error('News key required'), {
keyRequired: true,
keyRejected: false,
keyId: key,
retryable: true,
...extra,
});
const ukraine = {
area: { key: 'UA', name: 'Ukraine', kind: 'country', country: 'UA' },
polygons: [
{
outer: [
[35, 48],
[37, 48],
[37, 50.5],
[35, 50.5],
],
holes: [],
},
],
borders: [],
disputed: [],
};
const noArea = { area: null, polygons: [], borders: [], disputed: [] };
test('the feed shows the last 24 h at significant and above, beacons when zoomed out', async (t) => {
const { layer, calls, entries } = fixture(t);
@@ -165,6 +200,9 @@ test('choosing a card selects it and flies straight down from 150 km; reduced mo
assert.ok(Math.abs(Cesium.Math.toDegrees(at.latitude) - 50) < 1e-6);
const card = entries.at(-1).find((e) => e.id === 'selected-report:major');
assert.ok(card.details.includes('Source: example.com ↗'));
// The service the report came through is credited where it shows.
assert.ok(card.details.includes('via GDELT Project'));
assert.equal(layer.getRowControls().feed.selected.via, 'via GDELT Project');
assert.equal(layer.getRowControls().feed.selected.id, 'major');
layer.setParams({ clear: true });
assert.equal(layer.getRowControls().feed.selected, null);
@@ -273,6 +311,106 @@ test('Local reads every report once, lists those near the view and takes the map
assert.deepEqual(view.inView.items, []);
});
test('Local reads the area at the view centre, lists its reports and outlines it faintly', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
let centre = { lat: 49, lon: 36 };
const areaReads = [];
const feedReads = [];
const box = [
[35, 48],
[37, 48],
[37, 50.5],
[35, 50.5],
];
const { layer, camera, viewer } = fixture(t, {
height: 1_000_000,
centre: () => centre,
source: {
getFeed: async (query) => {
feedReads.push(query);
return {
reports: [
report('inside', { severity: 1, relevance: 'minor' }),
report('beyond', {
severity: 2,
pins: [{ lat: 49, lon: 39, place: 'Luhansk', mappable: true }],
}),
],
nextBefore: null,
};
},
getAreaOutline: async (point) => {
areaReads.push(point);
return point.lat > 45
? {
area: {
key: 'UA',
name: 'Ukraine',
kind: 'country',
country: 'UA',
},
polygons: [{ outer: box, holes: [] }],
borders: [],
disputed: [],
}
: { area: null, polygons: [], borders: [], disputed: [] };
},
},
});
const outline = () =>
viewer.added[0].entities.values.filter((e) =>
String(e.id).includes('local:UA'),
);
layer.enable();
await layer.update();
await flush();
// Conflicts reads no area.
assert.equal(areaReads.length, 0);
layer.setParams({ filters: { section: 'local' } });
await flush();
assert.equal(areaReads.length, 1);
// The read asks the server for the area's reports, at the point named.
const localRead = () => feedReads.filter((q) => q.relevance === '').at(-1);
assert.deepEqual(localRead().at, { lat: 49, lon: 36 });
let view = layer.getRowControls().feed;
assert.equal(view.inView.area, 'Ukraine');
// Luhansk is within 500 km, outside the area.
assert.deepEqual(
view.inView.items.map((item) => item.id),
['inside'],
);
assert.ok(outline().length > 0);
assert.ok(
outline().every((e) => e.show && e.polyline.width.getValue() === 1.5),
);
// A pan inside the area reads it again only once the centre leaves it.
centre = { lat: 50, lon: 36.5 };
camera.moveEnd.raiseEvent();
t.mock.timers.tick(800);
await flush();
assert.equal(areaReads.length, 1);
// At sea: no area, the radius, and the outline hidden.
centre = { lat: 43, lon: 34 };
camera.moveEnd.raiseEvent();
t.mock.timers.tick(800);
await flush();
assert.equal(areaReads.length, 2);
assert.equal(localRead().at, undefined);
view = layer.getRowControls().feed;
assert.equal(view.inView.area, null);
assert.equal(view.inView.radiusKm, 500);
assert.ok(outline().every((e) => !e.show));
// Conflicts hides the area's outline too.
centre = { lat: 49, lon: 36 };
camera.moveEnd.raiseEvent();
t.mock.timers.tick(800);
await flush();
assert.ok(outline().every((e) => e.show));
layer.setParams({ filters: { section: 'conflicts' } });
await flush();
assert.ok(outline().every((e) => !e.show));
});
test('a shorter window narrows the feed and reads the hotspot counts for it', async (t) => {
const asked = [];
const { layer } = fixture(t, {
@@ -351,6 +489,316 @@ test('a server without news is unavailable once and is not asked again', async (
assert.equal(reads, 2);
});
test('a section read from a key the server lacks says which key, and the other still reads', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
let liveuamap = keyError('liveuamap');
let worldNews = keyError('worldnewsapi');
let hotspotReads = 0;
const { layer } = fixture(t, {
height: 1_000_000,
source: {
getHotspots: async () => {
hotspotReads++;
if (liveuamap) throw liveuamap;
return hotspotsPayload;
},
getFeed: async (query) => {
if (query.at && worldNews) throw worldNews;
if (!query.at && liveuamap) throw liveuamap;
return {
reports: [report('inside', { relevance: 'minor' })],
nextBefore: null,
};
},
getAreaOutline: async () => ukraine,
},
});
layer.enable();
assert.equal(await layer.update(), true);
await flush();
let view = layer.getRowControls().feed;
assert.equal(view.key.title, 'Liveuamap key required');
assert.match(view.key.hint, /LIVEUAMAP_API_KEY/);
assert.equal(view.error, null, 'a missing key is no failure');
assert.equal(layer.getStats().sourceUnavailable, undefined);
layer.setParams({ filters: { section: 'local' } });
await flush();
view = layer.getRowControls().feed;
assert.equal(view.inView.key.title, 'World News API key required');
assert.match(view.inView.key.hint, /WORLDNEWSAPI_KEY/);
assert.equal(view.inView.error, null);
// A key set meanwhile is picked up by the next refresh.
worldNews = null;
await layer.update();
await flush();
view = layer.getRowControls().feed;
assert.equal(view.inView.key, null);
assert.deepEqual(
view.inView.items.map((item) => item.id),
['inside'],
);
assert.equal(view.key.title, 'Liveuamap key required');
liveuamap = keyError('liveuamap', { keyRejected: true });
await layer.update();
view = layer.getRowControls().feed;
assert.equal(view.key.title, 'Liveuamap refused its key');
assert.equal(view.key.hint, 'Check LIVEUAMAP_API_KEY in Provider Settings');
liveuamap = null;
await layer.update();
await flush();
view = layer.getRowControls().feed;
assert.equal(view.key, null);
assert.equal(hotspotReads, 4, 'a missing key is asked again each refresh');
});
test("Local says when its provider's allowance for the day is spent, until a read passes", async (t) => {
let quota = true;
const { layer } = fixture(t, {
height: 1_000_000,
source: {
getFeed: async (query) => {
if (query.at && quota)
throw keyError('worldnewsapi', { keyQuota: true });
return {
reports: [report('inside', { relevance: 'minor' })],
nextBefore: null,
};
},
getAreaOutline: async () => ukraine,
},
});
layer.enable();
await layer.update();
await flush();
layer.setParams({ filters: { section: 'local' } });
await flush();
let view = layer.getRowControls().feed;
assert.deepEqual(view.inView.key, {
title: 'World News API limit reached for today',
hint: 'News resumes tomorrow, or with a paid plan',
});
assert.equal(view.inView.error, null, 'a spent allowance is no failure');
quota = false;
await layer.update();
await flush();
view = layer.getRowControls().feed;
assert.equal(view.inView.key, null);
assert.deepEqual(
view.inView.items.map((item) => item.id),
['inside'],
);
});
test('a Conflicts feed read that passes clears its key notice', async (t) => {
let refuse = true;
const { layer } = fixture(t, {
source: {
getFeed: async (query) => {
// The panel's feed page, not the map's read.
if (query.limit === 60 && refuse) throw keyError('liveuamap');
return {
reports: [report('major', { severity: 3 })],
nextBefore: null,
};
},
},
});
layer.enable();
await layer.update();
await settle();
assert.equal(layer.getRowControls().feed.key.title, 'Liveuamap key required');
refuse = false;
layer.setParams({ filters: { category: 'strike' } });
await settle();
const view = layer.getRowControls().feed;
assert.equal(view.key, null);
assert.deepEqual(
view.items.map((item) => item.id),
['major'],
);
});
test('the key providers the layer names match the key registry', () => {
for (const [id, { envVar }] of Object.entries(NEWS_KEY_PROVIDERS)) {
const entry = KEY_SETUP_KEYS.find((key) => key.id === id);
assert.ok(entry, `${id} is in the key registry`);
assert.deepEqual([...entry.envVars], [envVar]);
}
});
test('a slower area read for where the camera was never lands over the area it came back to', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
let centre = { lat: 49, lon: 36 };
const pending = [];
const { layer, camera } = fixture(t, {
height: 1_000_000,
centre: () => centre,
source: {
getAreaOutline: (point) =>
point.lat > 45
? Promise.resolve(ukraine)
: new Promise((resolve) => pending.push(resolve)),
},
});
const look = async (at) => {
centre = at;
camera.moveEnd.raiseEvent();
t.mock.timers.tick(800);
await flush();
};
layer.enable();
await layer.update();
await flush();
layer.setParams({ filters: { section: 'local' } });
await flush();
assert.equal(layer.getRowControls().feed.inView.area, 'Ukraine');
// Out to sea, where the area read is slow, and back before it answers.
await look({ lat: 43, lon: 34 });
assert.equal(pending.length, 1);
assert.equal(layer.getRowControls().feed.inView.areaLoading, true);
await look({ lat: 49.6, lon: 36.2 });
pending[0](noArea);
await flush();
const view = layer.getRowControls().feed;
assert.equal(view.inView.area, 'Ukraine');
assert.equal(view.inView.areaLoading, false);
assert.equal(view.inView.loading, false);
});
test('leaving Local while its area is read sends no Local read', async (t) => {
let answer = null;
const localReads = [];
// The camera stays put: only the section changes.
const here = { lat: 49, lon: 36 };
const { layer } = fixture(t, {
height: 1_000_000,
centre: () => here,
source: {
getFeed: async (query) => {
if (query.relevance === '') localReads.push(query);
return { reports: [], nextBefore: null };
},
getAreaOutline: () =>
new Promise((resolve) => {
answer = resolve;
}),
},
});
layer.enable();
await layer.update();
await flush();
layer.setParams({ filters: { section: 'local' } });
await flush();
assert.equal(layer.getRowControls().feed.inView.loading, true);
layer.setParams({ filters: { section: 'conflicts' } });
await flush();
assert.equal(layer.getRowControls().feed.inView.loading, false);
answer(ukraine);
await flush();
assert.equal(localReads.length, 0);
assert.equal(layer.getRowControls().feed.inView.loading, false);
});
test('a server without the area route keeps Local to the radius and is asked once', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout'] });
let centre = { lat: 49, lon: 36 };
let areaReads = 0;
const localReads = [];
const { layer, camera } = fixture(t, {
height: 1_000_000,
centre: () => centre,
source: {
getFeed: async (query) => {
if (query.relevance === '') localReads.push(query);
return { reports: [report('near')], nextBefore: null };
},
getAreaOutline: async () => {
areaReads++;
throw Object.assign(
new Error('News feed not configured on this server'),
{ status: 404, code: 'unavailable', unavailable: true },
);
},
},
});
layer.enable();
await layer.update();
await flush();
layer.setParams({ filters: { section: 'local' } });
await flush();
assert.equal(areaReads, 1);
assert.equal(localReads.length, 1);
assert.equal(localReads[0].at, undefined);
let view = layer.getRowControls().feed;
assert.equal(view.inView.area, null);
assert.equal(view.inView.areaLoading, false);
assert.deepEqual(
view.inView.items.map((item) => item.id),
['near'],
);
// The settles that follow do not ask for an area again.
centre = { lat: 47, lon: 31 };
camera.moveEnd.raiseEvent();
t.mock.timers.tick(800);
await flush();
assert.equal(areaReads, 1);
// Turning the layer off and on asks again.
layer.disable();
layer.enable();
await layer.update();
await flush();
assert.equal(areaReads, 2);
view = layer.getRowControls().feed;
assert.equal(view.inView.error, null);
});
test('Liveuamap and Natural Earth are credited once what they credit shows', async (t) => {
const credits = [];
const { layer } = fixture(t, {
height: 1_000_000,
credits,
source: {
getFeed: async (query) => ({
reports:
query.relevance === ''
? [report('near', { kind: 'rss' })]
: [report('mapped', { kind: 'liveuamap', severity: 3 })],
nextBefore: null,
}),
getAreaOutline: async () => ukraine,
},
});
layer.enable();
await layer.update();
await flush();
layer.getRowControls();
assert.deepEqual(credits, ['liveuamap']);
// The Local area's outline (the built-in server's are Natural Earth's).
layer.setParams({ filters: { section: 'local' } });
await flush();
assert.deepEqual(credits, ['liveuamap', 'natural-earth']);
// Shown again: registered once.
layer.setParams({ filters: { section: 'conflicts' } });
await layer.update();
await flush();
layer.getRowControls();
assert.deepEqual(credits, ['liveuamap', 'natural-earth']);
});
test('without Liveuamap reports or a Local area, nothing is credited', async (t) => {
const credits = [];
const { layer } = fixture(t, { height: 1_000_000, credits });
layer.enable();
await layer.update();
await flush();
layer.getRowControls();
assert.deepEqual(credits, []);
});
test('a failed read keeps the layer on with an error; disable clears the overlay', async (t) => {
const { layer, entries } = fixture(t, {
source: { getFeed: async () => Promise.reject(new Error('boom')) },
+68 -22
View File
@@ -43,6 +43,8 @@ export const NEWS_FEED_MAX = 600;
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;
@@ -63,12 +65,7 @@ export const NEWS_RELEVANT = 'core,related';
const SOURCE_LABEL = msg('Source');
const DISPUTED = msg('Disputed: {name}');
const DISPUTE_TEXT = {
held: msg('Administered by {holder} · claimed by {claimant}'),
self: msg('Self-administered · claimed by {claimant}'),
un: msg('UN-patrolled zone'),
unclaimed: msg('Claimed by neither neighbour'),
};
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');
@@ -102,6 +99,8 @@ export const NEWS_HOTSPOT_LABELS = Object.freeze({
'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. */
@@ -124,17 +123,25 @@ export function countryName(code) {
}
}
/** Label for a disputed area: its name, who administers it, who claims it. */
export function disputedLabel(area) {
return {
title: t(DISPUTED, { name: area.name }),
detail: t(DISPUTE_TEXT[area.kind] || DISPUTE_TEXT.held, {
holder: countryName(area.heldBy),
claimant: countryName(area.claimedBy),
}),
};
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.
@@ -184,6 +191,11 @@ export const NEWS_CATEGORIES = Object.freeze({
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',
@@ -507,6 +519,14 @@ export function sourceLinkLabel(url) {
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 (
@@ -519,8 +539,8 @@ function placeLine(report) {
/**
* One event card in the News panel: picture,
* headline, a mono meta line (severity, category, place, sources, age), the
* sides line, then attestation and media chips. Labels and colours are
* 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()) {
@@ -548,6 +568,7 @@ export function feedCardView(report, now = Date.now()) {
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),
@@ -718,9 +739,32 @@ export function distanceKm(a, b) {
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` (any pin within NEWS_IN_VIEW_RADIUS_KM) that pass
* the filters, most severe and newest first.
* The reports near `centre` that pass the filters, most severe and newest
* first: any pin inside `area` (outline polygons), or without one within
* NEWS_IN_VIEW_RADIUS_KM.
*/
export function reportsInView(
reports,
@@ -729,16 +773,18 @@ export function reportsInView(
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(
(pin) => distanceKm(pin, centre) <= NEWS_IN_VIEW_RADIUS_KM,
),
r.pins?.some(near),
)
.sort(
(a, b) =>
+61 -1
View File
@@ -8,6 +8,7 @@ import {
import {
NEWS_IN_VIEW_RADIUS_KM,
ageWords,
areaLabel,
beaconPixelSize,
cardEvents,
cardView,
@@ -17,6 +18,7 @@ import {
feedQueryFor,
hotspotLabel,
hotspotTrend,
inPolygons,
isPressing,
markerStyle,
mergeFeedPage,
@@ -218,7 +220,61 @@ test('outlines keep valid rings, borders and named disputed areas', () => {
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', () => {
@@ -427,6 +483,10 @@ test('beacons carry a focus, a size by activity and a trend only against a prior
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', () => {
@@ -476,7 +536,7 @@ test('picture cards: physical, two or more sources, major or pressing, at most t
assert.equal(cardView(lone, NOW).image, null);
});
test('feed cards read like Breaking: headline, meta parts, sides and chips', () => {
test('feed cards: headline, meta parts, sides and chips', () => {
const r = report('fc', '2026-10-28T09:00:00Z', {
title: 'Удар по Харкову',
titleEn: 'Strike on Kharkiv',
+24 -5
View File
@@ -291,14 +291,17 @@ function normalizeRing(ring) {
}
/**
* `/api/news/outline` → `{polygons, borders, disputed}`: the dissolved extent
* (`{outer, holes}`), the national borders inside it (rings) and its disputed
* areas, invalid parts dropped.
* `/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, 200)
? payload.polygons.slice(0, 300)
: []) {
const outer = normalizeRing(raw?.outer);
if (!outer) continue;
@@ -332,7 +335,23 @@ export function normalizeOutlinePayload(payload) {
rings,
});
}
return { polygons, borders, disputed };
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. */
+47 -4
View File
@@ -29,10 +29,15 @@ const FEED_KEYS = Object.freeze([
'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),
@@ -40,13 +45,20 @@ const FEED_VALUE = {
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. */
/**
* 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 = query[key];
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));
}
@@ -81,7 +93,8 @@ export function newsQueryAllowed(url) {
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'))))
/^[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;
@@ -92,7 +105,12 @@ export function newsQueryAllowed(url) {
);
}
/** A failed read: a server without the news routes is a permanent miss. */
/**
* 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 {
@@ -100,6 +118,22 @@ async function responseError(response) {
} 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));
@@ -162,6 +196,15 @@ export function createNewsSource({
),
);
},
// 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 }),
+52
View File
@@ -54,6 +54,8 @@ test('reads validated hotspots, feed and outlines from the same-origin routes',
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),
[
@@ -62,6 +64,8 @@ test('reads validated hotspots, feed and outlines from the same-origin routes',
'/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) {
@@ -112,6 +116,50 @@ test('malformed, failed and unconfigured reads reject; a server without news is
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.
@@ -174,6 +222,10 @@ test('the host allowlist admits the client routes and refuses anything else', ()
'/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);
});