diff --git a/src/inc/routeinc/f0cklib.mjs b/src/inc/routeinc/f0cklib.mjs index a7786f9..c512a6f 100644 --- a/src/inc/routeinc/f0cklib.mjs +++ b/src/inc/routeinc/f0cklib.mjs @@ -8,13 +8,13 @@ const globalfilter = cfg.nsfp.map(n => `tag_id = ${n}`).join(' or '); export default { getf0cks: async (o = { user, tag, mime, page, mode, fav, session, limit }) => { - const user = o.user ? decodeURI(o.user) : null; - const tag = lib.parseTag(o.tag ?? null); - const mime = o.mime ?? null; - const page = +(o.page ?? 1); - const smime = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%"; - const eps = o.limit ?? cfg.websrv.eps; - + const user = o.user ? decodeURI(o.user) : null; + const tag = lib.parseTag(o.tag ?? null); + const mime = o.mime ?? null; + const page = +(o.page ?? 1); + const smime = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%"; + const eps = o.limit ?? cfg.websrv.eps; + const tmp = { user, tag, mime, smime, page, mode: o.mode }; const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0); @@ -27,17 +27,17 @@ export default { left join favorites on favorites.item_id = items.id left join "user" on "user".id = favorites.user_id where - ${ db.unsafe(modequery) } + ${db.unsafe(modequery)} and items.active = 'true' - ${ tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db`` } - ${ o.fav ? db`and "user".user ilike ${'%'+user+'%'}` : db`` } - ${ !o.fav && user ? db`and items.username ilike ${'%'+user+'%'}` : db`` } - ${ mime ? db`and items.mime ilike ${smime}` : db`` } - ${ !o.session && globalfilter ? db`and items.id not in (select item_id from tags_assign where item_id = items.id and (${db.unsafe(globalfilter)}))` : db`` } + ${tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db``} + ${o.fav ? db`and "user".user ilike ${'%' + user + '%'}` : db``} + ${!o.fav && user ? db`and items.username ilike ${'%' + user + '%'}` : db``} + ${mime ? db`and items.mime ilike ${smime}` : db``} + ${!o.session && globalfilter ? db`and items.id not in (select item_id from tags_assign where item_id = items.id and (${db.unsafe(globalfilter)}))` : db``} group by items.id, tags.tag `)?.length || 0; - if(!total || total === 0) { + if (!total || total === 0) { return { success: false, message: "404 - no f0cks given" @@ -61,13 +61,13 @@ export default { left join "user" on "user".id = favorites.user_id left join tags_assign ta on ta.item_id = items.id and (ta.tag_id = 1 or ta.tag_id = 2) where - ${ db.unsafe(modequery) } + ${db.unsafe(modequery)} and items.active = 'true' - ${ tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db`` } - ${ o.fav ? db`and "user".user ilike ${'%'+user+'%'}` : db`` } - ${ !o.fav && user ? db`and items.username ilike ${'%'+user+'%'}` : db`` } - ${ mime ? db`and items.mime ilike ${smime}` : db`` } - ${ !o.session && globalfilter ? db`and items.id not in (select item_id from tags_assign where item_id = items.id and (${db.unsafe(globalfilter)}))` : db`` } + ${tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db``} + ${o.fav ? db`and "user".user ilike ${'%' + user + '%'}` : db``} + ${!o.fav && user ? db`and items.username ilike ${'%' + user + '%'}` : db``} + ${mime ? db`and items.mime ilike ${smime}` : db``} + ${!o.session && globalfilter ? db`and items.id not in (select item_id from tags_assign where item_id = items.id and (${db.unsafe(globalfilter)}))` : db``} group by items.id, tags.tag, ta.tag_id order by items.id desc offset ${offset} @@ -75,11 +75,11 @@ export default { `; const cheat = []; - for(let i = Math.max(1, act_page - 3); i <= Math.min(act_page + 3, pages); i++) + for (let i = Math.max(1, act_page - 3); i <= Math.min(act_page + 3, pages); i++) cheat.push(i); - + const link = lib.genLink({ user, tag, mime, type: o.fav ? 'favs' : 'f0cks', path: 'p/' }); - + return { success: true, items: rows, @@ -96,54 +96,61 @@ export default { }; }, getf0ck: async (o = ({ user, tag, mime, itemid, mode, session })) => { - const user = o.user ? decodeURI(o.user) : null; - const tag = lib.parseTag(o.tag ?? null); - const mime = (o.mime ?? ""); - const itemid = +(o.itemid ?? 404); - const smime = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%"; - + const user = o.user ? decodeURI(o.user) : null; + const tag = lib.parseTag(o.tag ?? null); + const mime = (o.mime ?? ""); + const itemid = +(o.itemid ?? 404); + const smime = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%"; + const tmp = { user, tag, mime, smime, itemid }; - + const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0); - - if(itemid === 404) { + + if (itemid === 404) { return { success: false, message: "404 - f0ck not found" }; } - + const items = await db` select distinct on (items.id) items.* from items left join tags_assign on tags_assign.item_id = items.id left join tags on tags.id = tags_assign.tag_id - left join favorites on favorites.item_id = items.id - left join "user" on "user".id = favorites.user_id + ${o.fav + ? db`inner join favorites on favorites.item_id = items.id inner join "user" on "user".id = favorites.user_id` + : db`left join favorites on favorites.item_id = items.id left join "user" on "user".id = favorites.user_id` + } left join tags_assign ta on ta.item_id = items.id and (ta.tag_id = 1 or ta.tag_id = 2) where - ${ db.unsafe(modequery) } + ${db.unsafe(modequery)} and items.active = 'true' - ${ tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db`` } - ${ o.fav ? db`and "user".user ilike ${'%'+user+'%'}` : db`` } - ${ !o.fav && user ? db`and items.username ilike ${'%'+user+'%'}` : db`` } - ${ mime ? db`and items.mime ilike ${smime}` : db`` } - ${ !o.session && globalfilter ? db`and items.id not in (select item_id from tags_assign where item_id = items.id and (${db.unsafe(globalfilter)}))` : db`` } + ${tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db``} + ${o.fav ? db`and "user"."user" = ${user}` : db``} + ${!o.fav && user ? db`and items.username ilike ${'%' + user + '%'}` : db``} + ${mime ? db`and items.mime ilike ${smime}` : db``} + ${!o.session && globalfilter ? db`and items.id not in (select item_id from tags_assign where item_id = items.id and (${db.unsafe(globalfilter)}))` : db``} group by items.id, tags.tag, ta.tag_id order by items.id desc `; + console.log('[GETF0CK DEBUG] Query params:', { user, itemid, fav: o.fav }); + console.log('[GETF0CK DEBUG] Items found:', items.length, 'Item IDs:', items.slice(0, 10).map(i => i.id)); + const item = items.findIndex(i => i.id === itemid); const actitem = items[item]; - - if(!actitem) { // sfw-check! + + console.log('[GETF0CK DEBUG] findIndex result:', item, 'actitem exists:', !!actitem); + + if (!actitem) { // sfw-check! return { success: false, message: "Sorry, this post is currently not visible." }; } - + const tags = await lib.getTags(itemid); const cheat = [...new Set(items.slice(Math.max(0, item - 3), item + 4).map(i => i.id))]; const link = lib.genLink({ user, tag, mime, type: o.fav ? 'favs' : 'f0cks', path: '' }); @@ -154,14 +161,14 @@ export default { left join "user_options" on "user_options".user_id = "favorites".user_id where "favorites".item_id = ${itemid} `; - + let coverart = true; try { await fs.promises.access(`./public${cfg.websrv.paths.coverarts}/${actitem.id}.webp`); - } catch(err) { + } catch (err) { coverart = false; } - + const data = { success: true, user: { @@ -201,16 +208,16 @@ export default { tmp }; return data; - },getRandom: async (o = ({ user, tag, mime, mode, fav, session })) => { + }, getRandom: async (o = ({ user, tag, mime, mode, fav, session })) => { const user = o.user ? decodeURI(o.user) : null; const tag = lib.parseTag(o.tag ?? null); const mime = (o.mime ?? ""); const smime = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%"; - + const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0); - + let item; - + if (o.fav && user) { // Special case: random from user's favorites item = await db` @@ -246,20 +253,20 @@ export default { limit 1 `; } - + if (item.length === 0) { return { success: false, message: "no f0cks found :(" }; } - + const link = lib.genLink({ user, tag, mime, type: o.fav ? 'favs' : 'f0cks' }); - + return { success: true, link: link, itemid: item[0].id }; } - }; +}; diff --git a/src/inc/routes/ajax.mjs b/src/inc/routes/ajax.mjs index 87c904c..f1f781a 100644 --- a/src/inc/routes/ajax.mjs +++ b/src/inc/routes/ajax.mjs @@ -20,6 +20,8 @@ export default (router, tpl) => { : `/user/${query.user}/${req.params.itemid}`; } + console.log('[AJAX DEBUG] Params:', { itemid: req.params.itemid, user: query.user, fav: query.fav, contextUrl }); + const data = await f0cklib.getf0ck({ itemid: req.params.itemid, mode: req.session.mode, @@ -31,6 +33,8 @@ export default (router, tpl) => { fav: query.fav === 'true' }); + console.log('[AJAX DEBUG] getf0ck result:', { success: data.success, message: data.message }); + if (!data.success) { return res.reply({ code: 404, diff --git a/src/inc/routes/apiv2/index.mjs b/src/inc/routes/apiv2/index.mjs index ff10be6..c302a16 100644 --- a/src/inc/routes/apiv2/index.mjs +++ b/src/inc/routes/apiv2/index.mjs @@ -32,7 +32,7 @@ export default router => { where mime ilike ${mime} and active = 'true' - ${isFav ? db`and fu."user" ilike ${user}` : db`and items.username ilike ${user}`} + ${isFav ? db`and fu."user" = ${user}` : db`and items.username ilike ${user}`} ${tag ? db`and tags.normalized ilike ${'%' + tag + '%'}` : db``} order by random() limit 1