backend stuff lol

This commit is contained in:
Flummi 2021-12-18 19:52:38 +01:00
parent adb4f18a7f
commit d5eada1814
5 changed files with 87 additions and 31 deletions

View File

@ -40,16 +40,16 @@ export default new class {
let tmp;
switch(mode) {
case 1: // nsfw
tmp = "id in (select item_id from tags_assign where tag_id = 2 group by item_id)";
tmp = "items.id in (select item_id from tags_assign where tag_id = 2 group by item_id)";
break;
case 2: // untagged
tmp = "id not in (select item_id from tags_assign group by item_id)";
tmp = "items.id not in (select item_id from tags_assign group by item_id)";
break;
case 3: // all
tmp = "";
break;
default: // sfw
tmp = "id in (select item_id from tags_assign where tag_id = 1 group by item_id)";
tmp = "items.id in (select item_id from tags_assign where tag_id = 1 group by item_id)";
break;
}
return tmp;
@ -60,10 +60,15 @@ export default new class {
genLink(env) {
const link = [];
if(env.user) link.push("user", env.user);
if(env.tag) link.push("tag", env.tag);
if(env.mime.length > 2) link.push(env.mime);
if(env.page) link.push("p", env.page);
return link.join("/");
};
parseTag(tag) {
return tag
.replace(/%20/g, " ");
}
// async funcs
async countf0cks() {

View File

@ -19,7 +19,7 @@ export default (router, tpl) => {
if(req.cookies.session)
return res.reply({ body: "du bist schon eingeloggt lol" });
res.reply({
body: tpl.render("login")
body: tpl.render("login", { theme: req.cookies.theme ?? "f0ck" })
});
});
@ -103,7 +103,7 @@ export default (router, tpl) => {
.select("items.id", "items.username", "tags.tag")
.leftJoin("tags_assign", "tags_assign.tag_id", "tags.id")
.leftJoin("items", "items.id", "tags_assign.item_id")
.where("tags.tag", "regexp", tag);
.where("tags.tag", "like", '%'+tag+'%');
ret = search(rows, tag);
}

View File

@ -12,15 +12,20 @@ const auth = async (req, res, next) => {
};
export default (router, tpl) => {
router.get(/^\/?(?:user\/(?<user>[a-zA-Z0-9\[\]\-\_\{\}\\`\|]+))?(?:\/(?<mime>image|audio|video))?(?:\/p\/(?<page>\d+))?(?:\/(?<itemid>\d+))?$/, async (req, res) => {
//router.get(/^\/?(?:user\/(?<user>[a-zA-Z0-9\[\]\-\_\{\}\\`\|]+))?(?:\/(?<mime>image|audio|video))?(?:\/p\/(?<page>\d+))?(?:\/(?<itemid>\d+))?$/, async (req, res) => {
router.get(/^\/?(?:tag\/(?<tag>.+?))?(?:user\/(?<user>.+?))?(?:\/(?<mime>image|audio|video))?(?:\/p\/(?<page>\d+))?(?:\/(?<itemid>\d+))?$/, async (req, res) => {
const user = req.params.user ?? null;
let tag = req.params.tag ?? null;
const mime = (req.params.mime ?? "");
const smime = allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
const page = +(req.params.page ?? 1);
const itemid = +(req.params.itemid ?? null);
if(tag)
tag = lib.parseTag(tag);
const output = {
user, mime, smime, page, itemid
user, tag, mime, smime, page, itemid
};
const mode = itemid > 0 ? "item" : "index";
@ -34,15 +39,24 @@ export default (router, tpl) => {
if(itemid == 404)
return res.end("404 - lol");
const query = (await sql("items")
let query = (await sql("items")
.whereRaw(modequery)
.andWhere("id", itemid)
.andWhere("mime", "like", smime)
.andWhere("username", "like", user ? user : "%")
.limit(1))[0];
if(!query?.id) {
return res.end("Sorry, this f0ck either doesn't exist (yet) or hasn't been tagged yet, please remain patient, a support assistant will be shortly with you.");
if(!query?.id) { // same query with sfw-mode
query = (await sql("items")
.whereRaw(lib.getMode(0))
.andWhere("id", itemid)
.andWhere("mime", "like", smime)
.andWhere("username", "like", user ? user : "%")
.limit(1)
)[0];
if(!query?.id)
return res.end("Sorry, this f0ck either doesn't exist (yet) or hasn't been tagged yet, please remain patient, a support assistant will be shortly with you.");
}
const tags = await sql("tags_assign")
@ -129,26 +143,59 @@ export default (router, tpl) => {
};
}
else { // page!
const total = (await sql("items")
.whereRaw(modequery)
.andWhere("items.mime", "like", smime)
.andWhere("items.username", "like", user ? user : "%")
.count("* as total")
)[0].total;
let total, rows;
if(tag) {
total = (await sql("tags")
.leftJoin("tags_assign", "tags_assign.tag_id", "tags.id")
.leftJoin("items", "items.id", "tags_assign.item_id")
.whereRaw(modequery)
.andWhere("items.mime", "like", smime)
.andWhere("tags.tag", "like", '%'+tag+'%')
.groupBy("items.id")
.count("* as total")
)[0]?.total;
}
else {
total = (await sql("items")
.whereRaw(modequery)
.andWhere("items.mime", "like", smime)
.andWhere("items.username", "like", user ? user : "%")
.count("* as total")
)[0]?.total;
}
if(!total || total.length === 0)
return res.end("no f0cks given lol");
const pages = +Math.ceil(total / cfg.websrv.eps);
const act_page = Math.min(pages, page || 1);
const offset = Math.max(0, (act_page - 1) * cfg.websrv.eps);
const rows = await sql("items")
.select("items.id", "items.mime", "tags_assign.tag_id")
.joinRaw("left join tags_assign on tags_assign.item_id = items.id and (tags_assign.tag_id = 1 or tags_assign.tag_id = 2)")
.whereRaw(modequery)
.andWhere("items.mime", "like", smime)
.andWhere("items.username", "like", user ? user : "%")
.orderBy("items.id", "desc")
.offset(offset)
.limit(cfg.websrv.eps);
if(tag) {
rows = await sql("tags")
.select("items.id", "items.mime", "tags_assign.tag_id")
.leftJoin("tags_assign", "tags_assign.tag_id", "tags.id")
.leftJoin("items", "items.id", "tags_assign.item_id")
.whereRaw(modequery)
.andWhere("items.mime", "like", smime)
.andWhere("tags.tag", "like", '%'+tag+'%')
.orderBy("items.id", "desc")
.groupBy("items.id")
.offset(offset)
.limit(cfg.websrv.eps);
}
else {
rows = await sql("items")
.select("items.id", "items.mime", "tags_assign.tag_id")
.joinRaw("left join tags_assign on tags_assign.item_id = items.id and (tags_assign.tag_id = 1 or tags_assign.tag_id = 2)")
.whereRaw(modequery)
.andWhere("items.mime", "like", smime)
.andWhere("items.username", "like", user ? user : "%")
.orderBy("items.id", "desc")
.offset(offset)
.limit(cfg.websrv.eps);
}
if(rows.length === 0)
return res.end("oops");
@ -162,7 +209,7 @@ export default (router, tpl) => {
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, mime, act_page });
const link = lib.genLink({ user, tag, mime, act_page });
data = {
items: rows,
@ -193,10 +240,11 @@ export default (router, tpl) => {
router.get(/^\/mode\/(\d)/, auth, async (req, res) => {
const mode = +req.url.split[1];
let referertmp = req.headers.referer?.split("/");
let referertmp = req.headers.referer;
let referer = "";
if(referertmp.length && ['image','audio','video'].includes(referertmp[3]))
referer = referertmp[3];
if(referertmp.match(/f0ck\.me/))
referer = referertmp.split("/").slice(3).join("/");
if(cfg.allowedModes[mode]) {
await sql("user_options")

View File

@ -5,7 +5,10 @@ import sql from "../sql.mjs";
import lib from "../lib.mjs";
const cleanTags = async () => {
const tags = await sql("tags").leftJoin("tags_assign", "tags_assign.tag_id", "tags.id").whereNull("tags_assign.item_id");
const tags = await sql("tags")
.leftJoin("tags_assign", "tags_assign.tag_id", "tags.id")
.whereNull("tags_assign.item_id");
if(tags.length === 0)
return 0;

View File

@ -59,7 +59,7 @@ import flummpress from "flummpress";
app.use(async (req, res) => {
// sessionhandler
req.session = false;
if(req.url.pathname.match(/^\/(s|b|t|ca)/))
if(req.url.pathname.match(/^\/(s|b|t|ca)\//))
return;
req.theme = req.cookies.theme ?? 'f0ck';