fixing random api endpoint

This commit is contained in:
2026-05-21 19:51:57 +02:00
parent 72b8140f77
commit 9ef3207cd4

View File

@@ -519,17 +519,36 @@ export default router => {
}); });
} }
// API expects { success: true, items: { id: ... } } (based on f0ck.js usage) const rows = await db`
// The old query returned full item row. f0cklib.getRandom returns { itemid: ... } or { itemid: ... } (actually it returns { itemid: ... } on success) SELECT *
FROM "items"
WHERE id = ${data.itemid} AND active = true
LIMIT 1
`;
const item = rows[0];
// We need to fetch the item details if the frontend expects them? if (!item) {
// Looking at f0ck.js: return res.json({
// if (data.success && data.items && data.items.id) { loadItemAjax(`/${data.items.id}`, true); } success: false,
// So it only really needs the ID. items: []
});
}
const isYouTube = item.mime === 'video/youtube';
const relativeDest = isYouTube ? item.dest : `${cfg.websrv.paths.images}/${item.dest}`;
const directUrl = isYouTube ? item.dest : `${cfg.main.url.full}${cfg.websrv.paths.images}/${item.dest}`;
const { username, src, xd_score, ...safeItem } = item;
return res.json({ return res.json({
success: true, success: true,
items: { id: data.itemid } items: {
...safeItem,
id: item.id,
dest: relativeDest,
url: directUrl,
direct_url: directUrl
}
}); });
}); });