From e2411aeec0137ef26d4cf6beef3c2f332b817aeb Mon Sep 17 00:00:00 2001 From: Flummi Date: Thu, 30 Dec 2021 03:39:16 +0100 Subject: [PATCH] random overhaul --- src/inc/routes/inc/f0cklib.mjs | 45 ++++++++++++++++++++++++++++++ src/inc/routes/random.mjs | 51 ++++++++++++++++++---------------- views/snippets/navbar.html | 4 +-- 3 files changed, 74 insertions(+), 26 deletions(-) diff --git a/src/inc/routes/inc/f0cklib.mjs b/src/inc/routes/inc/f0cklib.mjs index e67d78b..15c88a7 100644 --- a/src/inc/routes/inc/f0cklib.mjs +++ b/src/inc/routes/inc/f0cklib.mjs @@ -137,6 +137,7 @@ export default { } let items = sql("items").select("*").orderBy("items.id", "desc").whereRaw(modequery); + if(tag) { items = items.innerJoin( sql("tags") @@ -208,5 +209,49 @@ export default { tmp: tmp }; return data; + }, + getRandom: async (o = ({ user, tag, mime, mode })) => { + 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 modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(o.mode ?? 0); + + let item = sql("items").select("*").orderByRaw("rand()").whereRaw(modequery); + + if(tag) { + item = item.innerJoin( + sql("tags") + .select("tags_assign.item_id", "tags.tag") + .leftJoin("tags_assign", "tags_assign.tag_id", "tags.id") + .where("tags.tag", "like", "%"+tag+"%") + .groupBy("tags_assign.item_id") + .as("st"), + "st.item_id", "items.id" + ); + } + if(user) + item = item.andWhere("username", "like", "%" + user + "%"); + if(mime) + item = item.andWhere("mime", "like", mime + "/%"); + + item = await item; + + if(item.length === 0) { + return { + success: false, + message: "no f0cks found :(" + }; + } + + const link = lib.genLink({ user, tag, mime }); + + return { + success: true, + link: link, + itemid: item[0].id + }; } }; diff --git a/src/inc/routes/random.mjs b/src/inc/routes/random.mjs index ed82d80..62a23c8 100644 --- a/src/inc/routes/random.mjs +++ b/src/inc/routes/random.mjs @@ -1,33 +1,36 @@ -import sql from "../sql.mjs"; -import lib from "../lib.mjs"; - -const allowedMimes = [ "audio", "image", "video" ]; +import f0cklib from "./inc/f0cklib.mjs"; export default (router, tpl) => { - router.get(/^\/random\/?(?:user\/(?[a-zA-Z0-9\[\]\-\_\{\}\\`\|]+))?(?:\/(?image|audio|video))?$/, async (req, res) => { - const user = req.params.user ?? null; - const mime = (req.params.mime ?? ""); - const smime = allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%"; + router.get(/^\/random$/, async (req, res) => { + let referer = req.headers.referer; + let opts = {}; - const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(req.session.mode ?? 0); + if(referer.match(/f0ck\.me/)) { // parse referer + referer = referer.split("f0ck.me")[1]; + const tmp = referer.match(/^\/?(?:\/tag\/(?.+?))?(?:\/user\/(?.+?)\/(?:f0cks|favs))?(?:\/(?image|audio|video))?(?:\/p\/(?\d+))?(?:\/(?\d+))?$/); + if(tmp) + opts = tmp.groups; + } - if(user && (await sql("items").where("username", "like", user).count("* as total"))[0].total === 0) - return res.end("200 - user not found lol"); + const data = await f0cklib.getRandom({ + user: opts.user, + tag: opts.tag, + mime: opts.mime, + page: opts.page, + mode: req.session.mode + }); - const rows = await sql("items") - .select("id") - .whereRaw(modequery) - .andWhere("mime", "like", smime) - .andWhere("username", "like", user ? user : "%") - .orderByRaw("rand()") - .limit(1); - - if(rows.length === 0) - return res.end("nope"); + if(!data.success) { + return res.reply({ + code: 404, + body: tpl.render('error', { + message: data.message, + tmp: null + }, req) + }); + } - const link = lib.genLink({ user, mime }); - - res.redirect(`/${link}${link.length != 0 ? "/": ""}${rows[0].id}`); + res.redirect(`/${data.link}${data.link.length != 0 ? "/" : ""}${data.itemid}`); }); return router; }; diff --git a/views/snippets/navbar.html b/views/snippets/navbar.html index 6283fbc..86b6bba 100644 --- a/views/snippets/navbar.html +++ b/views/snippets/navbar.html @@ -9,7 +9,7 @@