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); let items = sql("items").select("*").orderBy("items.id", "desc").whereRaw(modequery);
if(tag) { if(tag) {
items = items.innerJoin( items = items.innerJoin(
sql("tags") sql("tags")
@ -208,5 +209,49 @@ export default {
tmp: tmp tmp: tmp
}; };
return data; 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 f0cklib from "./inc/f0cklib.mjs";
import lib from "../lib.mjs";
const allowedMimes = [ "audio", "image", "video" ];
export default (router, tpl) => { export default (router, tpl) => {
router.get(/^\/random\/?(?:user\/(?<user>[a-zA-Z0-9\[\]\-\_\{\}\\`\|]+))?(?:\/(?<mime>image|audio|video))?$/, async (req, res) => { router.get(/^\/random$/, async (req, res) => {
const user = req.params.user ?? null; let referer = req.headers.referer;
const mime = (req.params.mime ?? ""); let opts = {};
const smime = allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
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) const data = await f0cklib.getRandom({
return res.end("200 - user not found lol"); user: opts.user,
tag: opts.tag,
mime: opts.mime,
page: opts.page,
mode: req.session.mode
});
const rows = await sql("items") if(!data.success) {
.select("id") return res.reply({
.whereRaw(modequery) code: 404,
.andWhere("mime", "like", smime) body: tpl.render('error', {
.andWhere("username", "like", user ? user : "%") message: data.message,
.orderByRaw("rand()") tmp: null
.limit(1); }, req)
});
}
if(rows.length === 0) res.redirect(`/${data.link}${data.link.length != 0 ? "/" : ""}${data.itemid}`);
return res.end("nope");
const link = lib.genLink({ user, mime });
res.redirect(`/${link}${link.length != 0 ? "/": ""}${rows[0].id}`);
}); });
return router; return router;
}; };

View File

@ -9,7 +9,7 @@
</a> </a>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li><a href="/admin">adminpanel</a></li> <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="/settings">settings</a></li>
<li><a href="/about">About</a></li> <li><a href="/about">About</a></li>
<li><a href="/ranking">Ranking</a></li> <li><a href="/ranking">Ranking</a></li>
@ -51,7 +51,7 @@
</li> </li>
@endif @endif
<li class="nav-item"> <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> <span class="nav-link-identifier">Random</span>
</a> </a>
</li> </li>