possible fix for random fav behaviour

This commit is contained in:
x
2026-01-23 21:52:34 +01:00
parent 8af49b6ec1
commit 6692f32c4b
3 changed files with 67 additions and 56 deletions

View File

@@ -8,13 +8,13 @@ const globalfilter = cfg.nsfp.map(n => `tag_id = ${n}`).join(' or ');
export default { export default {
getf0cks: async (o = { user, tag, mime, page, mode, fav, session, limit }) => { getf0cks: async (o = { user, tag, mime, page, mode, fav, session, limit }) => {
const user = o.user ? decodeURI(o.user) : null; const user = o.user ? decodeURI(o.user) : null;
const tag = lib.parseTag(o.tag ?? null); const tag = lib.parseTag(o.tag ?? null);
const mime = o.mime ?? null; const mime = o.mime ?? null;
const page = +(o.page ?? 1); const page = +(o.page ?? 1);
const smime = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%"; const smime = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
const eps = o.limit ?? cfg.websrv.eps; const eps = o.limit ?? cfg.websrv.eps;
const tmp = { user, tag, mime, smime, page, mode: o.mode }; const tmp = { user, tag, mime, smime, page, mode: o.mode };
const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0); 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 favorites on favorites.item_id = items.id
left join "user" on "user".id = favorites.user_id left join "user" on "user".id = favorites.user_id
where where
${ db.unsafe(modequery) } ${db.unsafe(modequery)}
and items.active = 'true' and items.active = 'true'
${ tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db`` } ${tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db``}
${ o.fav ? db`and "user".user ilike ${'%'+user+'%'}` : db`` } ${o.fav ? db`and "user".user ilike ${'%' + user + '%'}` : db``}
${ !o.fav && user ? db`and items.username ilike ${'%'+user+'%'}` : db`` } ${!o.fav && user ? db`and items.username ilike ${'%' + user + '%'}` : db``}
${ mime ? db`and items.mime ilike ${smime}` : 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`` } ${!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 group by items.id, tags.tag
`)?.length || 0; `)?.length || 0;
if(!total || total === 0) { if (!total || total === 0) {
return { return {
success: false, success: false,
message: "404 - no f0cks given" message: "404 - no f0cks given"
@@ -61,13 +61,13 @@ export default {
left join "user" on "user".id = favorites.user_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) left join tags_assign ta on ta.item_id = items.id and (ta.tag_id = 1 or ta.tag_id = 2)
where where
${ db.unsafe(modequery) } ${db.unsafe(modequery)}
and items.active = 'true' and items.active = 'true'
${ tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db`` } ${tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db``}
${ o.fav ? db`and "user".user ilike ${'%'+user+'%'}` : db`` } ${o.fav ? db`and "user".user ilike ${'%' + user + '%'}` : db``}
${ !o.fav && user ? db`and items.username ilike ${'%'+user+'%'}` : db`` } ${!o.fav && user ? db`and items.username ilike ${'%' + user + '%'}` : db``}
${ mime ? db`and items.mime ilike ${smime}` : 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`` } ${!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 group by items.id, tags.tag, ta.tag_id
order by items.id desc order by items.id desc
offset ${offset} offset ${offset}
@@ -75,11 +75,11 @@ export default {
`; `;
const cheat = []; 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); cheat.push(i);
const link = lib.genLink({ user, tag, mime, type: o.fav ? 'favs' : 'f0cks', path: 'p/' }); const link = lib.genLink({ user, tag, mime, type: o.fav ? 'favs' : 'f0cks', path: 'p/' });
return { return {
success: true, success: true,
items: rows, items: rows,
@@ -96,54 +96,61 @@ export default {
}; };
}, },
getf0ck: async (o = ({ user, tag, mime, itemid, mode, session })) => { getf0ck: async (o = ({ user, tag, mime, itemid, mode, session })) => {
const user = o.user ? decodeURI(o.user) : null; const user = o.user ? decodeURI(o.user) : null;
const tag = lib.parseTag(o.tag ?? null); const tag = lib.parseTag(o.tag ?? null);
const mime = (o.mime ?? ""); const mime = (o.mime ?? "");
const itemid = +(o.itemid ?? 404); const itemid = +(o.itemid ?? 404);
const smime = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%"; const smime = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
const tmp = { user, tag, mime, smime, itemid }; const tmp = { user, tag, mime, smime, itemid };
const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0); const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0);
if(itemid === 404) { if (itemid === 404) {
return { return {
success: false, success: false,
message: "404 - f0ck not found" message: "404 - f0ck not found"
}; };
} }
const items = await db` const items = await db`
select distinct on (items.id) select distinct on (items.id)
items.* items.*
from items from items
left join tags_assign on tags_assign.item_id = items.id left join tags_assign on tags_assign.item_id = items.id
left join tags on tags.id = tags_assign.tag_id left join tags on tags.id = tags_assign.tag_id
left join favorites on favorites.item_id = items.id ${o.fav
left join "user" on "user".id = favorites.user_id ? 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) left join tags_assign ta on ta.item_id = items.id and (ta.tag_id = 1 or ta.tag_id = 2)
where where
${ db.unsafe(modequery) } ${db.unsafe(modequery)}
and items.active = 'true' and items.active = 'true'
${ tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db`` } ${tag ? db`and tags.normalized ilike '%' || slugify(${tag}) || '%'` : db``}
${ o.fav ? db`and "user".user ilike ${'%'+user+'%'}` : db`` } ${o.fav ? db`and "user"."user" = ${user}` : db``}
${ !o.fav && user ? db`and items.username ilike ${'%'+user+'%'}` : db`` } ${!o.fav && user ? db`and items.username ilike ${'%' + user + '%'}` : db``}
${ mime ? db`and items.mime ilike ${smime}` : 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`` } ${!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 group by items.id, tags.tag, ta.tag_id
order by items.id desc 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 item = items.findIndex(i => i.id === itemid);
const actitem = items[item]; const actitem = items[item];
if(!actitem) { // sfw-check! console.log('[GETF0CK DEBUG] findIndex result:', item, 'actitem exists:', !!actitem);
if (!actitem) { // sfw-check!
return { return {
success: false, success: false,
message: "Sorry, this post is currently not visible." message: "Sorry, this post is currently not visible."
}; };
} }
const tags = await lib.getTags(itemid); const tags = await lib.getTags(itemid);
const cheat = [...new Set(items.slice(Math.max(0, item - 3), item + 4).map(i => i.id))]; 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: '' }); 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 left join "user_options" on "user_options".user_id = "favorites".user_id
where "favorites".item_id = ${itemid} where "favorites".item_id = ${itemid}
`; `;
let coverart = true; let coverart = true;
try { try {
await fs.promises.access(`./public${cfg.websrv.paths.coverarts}/${actitem.id}.webp`); await fs.promises.access(`./public${cfg.websrv.paths.coverarts}/${actitem.id}.webp`);
} catch(err) { } catch (err) {
coverart = false; coverart = false;
} }
const data = { const data = {
success: true, success: true,
user: { user: {
@@ -201,16 +208,16 @@ export default {
tmp tmp
}; };
return data; 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 user = o.user ? decodeURI(o.user) : null;
const tag = lib.parseTag(o.tag ?? null); const tag = lib.parseTag(o.tag ?? null);
const mime = (o.mime ?? ""); const mime = (o.mime ?? "");
const smime = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%"; const smime = cfg.allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0); const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0);
let item; let item;
if (o.fav && user) { if (o.fav && user) {
// Special case: random from user's favorites // Special case: random from user's favorites
item = await db` item = await db`
@@ -246,20 +253,20 @@ export default {
limit 1 limit 1
`; `;
} }
if (item.length === 0) { if (item.length === 0) {
return { return {
success: false, success: false,
message: "no f0cks found :(" message: "no f0cks found :("
}; };
} }
const link = lib.genLink({ user, tag, mime, type: o.fav ? 'favs' : 'f0cks' }); const link = lib.genLink({ user, tag, mime, type: o.fav ? 'favs' : 'f0cks' });
return { return {
success: true, success: true,
link: link, link: link,
itemid: item[0].id itemid: item[0].id
}; };
} }
}; };

View File

@@ -20,6 +20,8 @@ export default (router, tpl) => {
: `/user/${query.user}/${req.params.itemid}`; : `/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({ const data = await f0cklib.getf0ck({
itemid: req.params.itemid, itemid: req.params.itemid,
mode: req.session.mode, mode: req.session.mode,
@@ -31,6 +33,8 @@ export default (router, tpl) => {
fav: query.fav === 'true' fav: query.fav === 'true'
}); });
console.log('[AJAX DEBUG] getf0ck result:', { success: data.success, message: data.message });
if (!data.success) { if (!data.success) {
return res.reply({ return res.reply({
code: 404, code: 404,

View File

@@ -32,7 +32,7 @@ export default router => {
where where
mime ilike ${mime} and mime ilike ${mime} and
active = 'true' 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``} ${tag ? db`and tags.normalized ilike ${'%' + tag + '%'}` : db``}
order by random() order by random()
limit 1 limit 1