random overhaul

This commit is contained in:
Flummi 2021-12-30 03:39:16 +01:00
parent 29ad6c5773
commit e2411aeec0
3 changed files with 74 additions and 26 deletions

View File

@ -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
};
}
};

View File

@ -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\/(?<user>[a-zA-Z0-9\[\]\-\_\{\}\\`\|]+))?(?:\/(?<mime>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\/(?<tag>.+?))?(?:\/user\/(?<user>.+?)\/(?:f0cks|favs))?(?:\/(?<mime>image|audio|video))?(?:\/p\/(?<page>\d+))?(?:\/(?<itemid>\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(!data.success) {
return res.reply({
code: 404,
body: tpl.render('error', {
message: data.message,
tmp: null
}, req)
});
}
if(rows.length === 0)
return res.end("nope");
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;
};

View File

@ -9,7 +9,7 @@
</a>
<ul class="dropdown-menu">
<li><a href="/admin">adminpanel</a></li>
<li><a href="/user/{{ session.user.toLowerCase() }}">my f0cks</a></li>
<li><a href="/user/{{ session.user.toLowerCase() }}/f0cks">my f0cks</a></li>
<li><a href="/settings">settings</a></li>
<li><a href="/about">About</a></li>
<li><a href="/ranking">Ranking</a></li>
@ -51,7 +51,7 @@
</li>
@endif
<li class="nav-item">
<a id="random" class="nav-link" href="/random@if(tmp?.user)/user/{{ tmp?.user }}@endif@if(tmp?.mime)/{{ tmp?.mime }}@endif">
<a id="random" class="nav-link" href="/random">
<span class="nav-link-identifier">Random</span>
</a>
</li>