Files
jarvis/api/endpoints/news.php
T

38 lines
1.2 KiB
PHP

<?php
// News endpoint — serves from api_cache + custom pinned news from admin kb_facts
$cached = JarvisDB::query(
'SELECT data, UNIX_TIMESTAMP(updated_at) as ts FROM api_cache WHERE cache_key=? LIMIT 1',
['news']
);
if ($cached && !empty($cached[0]['data'])) {
$out = json_decode($cached[0]['data'], true);
$out['cache_age_s'] = (int)(time() - (int)$cached[0]['ts']);
} else {
$out = [
'categories' => [],
'total' => 0,
'cache_age_s' => -1,
'message' => 'News feed warming up — available within 5 minutes.',
];
}
// Prepend custom/pinned news items added via admin portal
$custom = JarvisDB::query(
"SELECT fact_key as title, fact_value as url, updated_at FROM kb_facts WHERE category='custom_news' ORDER BY id DESC"
);
if (!empty($custom)) {
$pinned = array_map(fn($r) => [
'title' => $r['title'],
'url' => $r['url'] ?: null,
'source' => 'JARVIS',
'published' => $r['updated_at'],
'pinned' => true,
], $custom);
// Insert pinned as first category
$out['categories'] = array_merge(['pinned' => $pinned], $out['categories'] ?? []);
}
echo json_encode($out);