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; let tmp;
switch(mode) { switch(mode) {
case 1: // nsfw 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; break;
case 2: // untagged 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; break;
case 3: // all case 3: // all
tmp = ""; tmp = "";
break; break;
default: // sfw 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; break;
} }
return tmp; return tmp;
@ -60,10 +60,15 @@ export default new class {
genLink(env) { genLink(env) {
const link = []; const link = [];
if(env.user) link.push("user", env.user); 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.mime.length > 2) link.push(env.mime);
if(env.page) link.push("p", env.page); if(env.page) link.push("p", env.page);
return link.join("/"); return link.join("/");
}; };
parseTag(tag) {
return tag
.replace(/%20/g, " ");
}
// async funcs // async funcs
async countf0cks() { async countf0cks() {

View File

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

View File

@ -12,15 +12,20 @@ const auth = async (req, res, next) => {
}; };
export default (router, tpl) => { 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; const user = req.params.user ?? null;
let tag = req.params.tag ?? null;
const mime = (req.params.mime ?? ""); const mime = (req.params.mime ?? "");
const smime = allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%"; const smime = allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
const page = +(req.params.page ?? 1); const page = +(req.params.page ?? 1);
const itemid = +(req.params.itemid ?? null); const itemid = +(req.params.itemid ?? null);
if(tag)
tag = lib.parseTag(tag);
const output = { const output = {
user, mime, smime, page, itemid user, tag, mime, smime, page, itemid
}; };
const mode = itemid > 0 ? "item" : "index"; const mode = itemid > 0 ? "item" : "index";
@ -34,15 +39,24 @@ export default (router, tpl) => {
if(itemid == 404) if(itemid == 404)
return res.end("404 - lol"); return res.end("404 - lol");
const query = (await sql("items") let query = (await sql("items")
.whereRaw(modequery) .whereRaw(modequery)
.andWhere("id", itemid) .andWhere("id", itemid)
.andWhere("mime", "like", smime) .andWhere("mime", "like", smime)
.andWhere("username", "like", user ? user : "%") .andWhere("username", "like", user ? user : "%")
.limit(1))[0]; .limit(1))[0];
if(!query?.id) { if(!query?.id) { // same query with sfw-mode
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."); 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") const tags = await sql("tags_assign")
@ -129,26 +143,59 @@ export default (router, tpl) => {
}; };
} }
else { // page! else { // page!
const total = (await sql("items") let total, rows;
.whereRaw(modequery)
.andWhere("items.mime", "like", smime) if(tag) {
.andWhere("items.username", "like", user ? user : "%") total = (await sql("tags")
.count("* as total") .leftJoin("tags_assign", "tags_assign.tag_id", "tags.id")
)[0].total; .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 pages = +Math.ceil(total / cfg.websrv.eps);
const act_page = Math.min(pages, page || 1); const act_page = Math.min(pages, page || 1);
const offset = Math.max(0, (act_page - 1) * cfg.websrv.eps); const offset = Math.max(0, (act_page - 1) * cfg.websrv.eps);
const rows = await sql("items") if(tag) {
.select("items.id", "items.mime", "tags_assign.tag_id") rows = await sql("tags")
.joinRaw("left join tags_assign on tags_assign.item_id = items.id and (tags_assign.tag_id = 1 or tags_assign.tag_id = 2)") .select("items.id", "items.mime", "tags_assign.tag_id")
.whereRaw(modequery) .leftJoin("tags_assign", "tags_assign.tag_id", "tags.id")
.andWhere("items.mime", "like", smime) .leftJoin("items", "items.id", "tags_assign.item_id")
.andWhere("items.username", "like", user ? user : "%") .whereRaw(modequery)
.orderBy("items.id", "desc") .andWhere("items.mime", "like", smime)
.offset(offset) .andWhere("tags.tag", "like", '%'+tag+'%')
.limit(cfg.websrv.eps); .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) if(rows.length === 0)
return res.end("oops"); 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++) 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, mime, act_page }); const link = lib.genLink({ user, tag, mime, act_page });
data = { data = {
items: rows, items: rows,
@ -193,10 +240,11 @@ export default (router, tpl) => {
router.get(/^\/mode\/(\d)/, auth, async (req, res) => { router.get(/^\/mode\/(\d)/, auth, async (req, res) => {
const mode = +req.url.split[1]; const mode = +req.url.split[1];
let referertmp = req.headers.referer?.split("/"); let referertmp = req.headers.referer;
let 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]) { if(cfg.allowedModes[mode]) {
await sql("user_options") await sql("user_options")

View File

@ -5,7 +5,10 @@ import sql from "../sql.mjs";
import lib from "../lib.mjs"; import lib from "../lib.mjs";
const cleanTags = async () => { 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) if(tags.length === 0)
return 0; return 0;

View File

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