From 9ef3207cd4da9a350e0d9e04298ef7d8d40518f2 Mon Sep 17 00:00:00 2001 From: Kibi Kelburton Date: Thu, 21 May 2026 19:51:57 +0200 Subject: [PATCH] fixing random api endpoint --- src/inc/routes/apiv2/index.mjs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/inc/routes/apiv2/index.mjs b/src/inc/routes/apiv2/index.mjs index c3471da..1dfe1e3 100644 --- a/src/inc/routes/apiv2/index.mjs +++ b/src/inc/routes/apiv2/index.mjs @@ -519,17 +519,36 @@ export default router => { }); } - // API expects { success: true, items: { id: ... } } (based on f0ck.js usage) - // The old query returned full item row. f0cklib.getRandom returns { itemid: ... } or { itemid: ... } (actually it returns { itemid: ... } on success) + const rows = await db` + 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? - // Looking at f0ck.js: - // if (data.success && data.items && data.items.id) { loadItemAjax(`/${data.items.id}`, true); } - // So it only really needs the ID. + if (!item) { + return res.json({ + success: false, + 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({ success: true, - items: { id: data.itemid } + items: { + ...safeItem, + id: item.id, + dest: relativeDest, + url: directUrl, + direct_url: directUrl + } }); });