From f7313d864c9ff237eaa0a4ee82bab1423afab985 Mon Sep 17 00:00:00 2001 From: Flummi Date: Mon, 6 Dec 2021 10:21:11 +0100 Subject: [PATCH] routes and stuff --- public/s/js/admin.js | 2 +- src/inc/lib.mjs | 7 + src/inc/routes/index.mjs | 293 ++++++++++++++++++------------- src/inc/routes/random.mjs | 30 +++- views/index.html | 2 +- views/item.html | 8 +- views/partials/pagination.html | 12 +- views/snippets/navbar.html | 12 +- views/snippets/navbar_admin.html | 1 - 9 files changed, 216 insertions(+), 151 deletions(-) diff --git a/public/s/js/admin.js b/public/s/js/admin.js index 41e48a8..5a57af2 100644 --- a/public/s/js/admin.js +++ b/public/s/js/admin.js @@ -39,7 +39,7 @@ const flash = ({ type, msg }) => { (async () => { if(_addtag = document.querySelector("a#a_addtag")) { const postid = +document.querySelector("a.id-link").innerText; - const poster = document.querySelector("span#a_username").innerText; + const poster = document.querySelector("a#a_username").innerText; let tags = [...document.querySelectorAll("#tags > .badge")].map(t => t.innerText.slice(0, -2)); const deleteEvent = async e => { diff --git a/src/inc/lib.mjs b/src/inc/lib.mjs index ed1817c..ffdb912 100644 --- a/src/inc/lib.mjs +++ b/src/inc/lib.mjs @@ -57,6 +57,13 @@ export default new class { createID() { return crypto.randomBytes(16).toString("hex") + Date.now().toString(24); }; + genLink(env) { + const link = []; + if(env.user) link.push("user", env.user); + if(env.mime.length > 2) link.push(env.mime); + if(env.page) link.push("p", env.page); + return link.join("/"); + }; // async funcs async countf0cks() { diff --git a/src/inc/routes/index.mjs b/src/inc/routes/index.mjs index 4e50499..b52c838 100644 --- a/src/inc/routes/index.mjs +++ b/src/inc/routes/index.mjs @@ -1,10 +1,10 @@ -import cfg from "../../../config.json" assert { type: "json" }; +import cfg from "../config.mjs"; import sql from "../sql.mjs"; import lib from "../lib.mjs"; import fs from "fs"; import url from "url"; -const allowedMimes = [ "audio", "image", "video", "%" ]; +const allowedMimes = [ "audio", "image", "video" ]; const auth = async (req, res, next) => { if(!req.session) return res.redirect("/login"); @@ -12,136 +12,177 @@ const auth = async (req, res, next) => { }; export default (router, tpl) => { - router.get(/^\/(audio\/?|image\/?|video\/?)?(p\/\d+)?$/, async (req, res) => { - const tmpmime = (allowedMimes.filter(n => req.url.split[0].startsWith(n))[0] ? req.url.split[0] : ""); - const mime = tmpmime + "%"; - const tmp = tmpmime == "audio" ? lib.getMode(0) : lib.getMode(req.session.mode ?? 0); + router.get(/^\/?(?:user\/(?[a-zA-Z0-9\[\]\-\_\{\}\\`\|]+))?(?:\/(?image|audio|video))?(?:\/p\/(?\d+))?(?:\/(?\d+))?$/, async (req, res) => { + const user = req.params.user ?? 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); - const total = ( - await sql("items") - .whereRaw(tmp) - .andWhere("items.mime", "like", mime) - .count("* as total") - )[0].total; - - const pages = +Math.ceil(total / cfg.websrv.eps); - const page = Math.min(pages, +req.url.split[tmpmime.length > 0 ? 2 : 1] || 1); - const offset = (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(tmp) - .andWhere("items.mime", "like", mime) - .orderBy("items.id", "desc") - .offset(offset) - .limit(cfg.websrv.eps); - - let cheat = []; - for(let i = Math.max(1, page - 3); i <= Math.min(page + 3, pages); i++) - cheat.push(i); - - rows.forEach(e => { - if(!fs.existsSync(`public/t/${e.id}.png`)) - fs.copyFileSync("public/s/img/broken.png", `public/t/${e.id}.png`); - }); - - const data = { - items: rows, - pagination: { - start: 1, - end: pages, - prev: (page > 1) ? page - 1 : null, - next: (page < pages) ? page + 1 : null, - page: page, - cheat: cheat, - link: `/${tmpmime ? tmpmime + "/" : ""}p/` - }, - last: rows[rows.length - 1].id, - filter: tmpmime ? tmpmime : undefined + const output = { + user, mime, smime, page, itemid }; - - res.reply({ body: tpl.render("index", data, req) }); - }); - - router.get(/^\/((audio\/|video\/|image\/)?[0-9]+)$/, async (req, res) => { - let id = false; - let mime = ""; - let tmpmime = false; + + const mode = itemid > 0 ? "item" : "index"; + const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(req.session.mode ?? 0); + let data; + + if(user && (await sql("items").where("username", "like", user).count("* as total"))[0].total === 0) + return res.end("200 - user not found lol"); + + if(mode === "item") { // item! + if(itemid == 404) + return res.end("404 - lol"); - if(allowedMimes.filter(n => req.url.split[0].startsWith(n))[0] ? req.url.split[0] : "") { - mime = tmpmime = req.url.split[0]; - id = +req.url.split[1]; - } - else { - mime = "%"; - id = +req.url.split[0]; - } - mime += "/%"; + const query = (await sql("items") + .whereRaw(modequery) + .andWhere("id", itemid) + .andWhere("mime", "like", smime) + .andWhere("username", "like", user ? user : "%") + .limit(1))[0]; - let tmp = tmpmime == "audio" ? lib.getMode(0) : lib.getMode(req.session.mode ?? 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") + .leftJoin("tags", "tags.id", "tags_assign.tag_id") + .where("tags_assign.item_id", itemid); + + const qmin = await sql("items") + .select("id") + .whereRaw(modequery) + .andWhere("mime", "like", smime) + .andWhere("username", "like", user ? user : "%") + .orderBy("id") + .limit(1); - if(id == 404) - tmp = ""; - - const query = (await sql("items") - .whereRaw(tmp) - .andWhere("id", id) - .andWhere("mime", "like", mime) - .limit(1)) - ?.shift(); - - if(!query?.id) - return res.redirect("/404"); - - const tags = await sql("tags_assign").leftJoin("tags", "tags.id", "tags_assign.tag_id").where("tags_assign.item_id", id); - - const qmin = await sql("items").select("id").whereRaw(tmp).andWhere("mime", "like", mime).orderBy("id").limit(1); - const qmax = await sql("items").select("id").whereRaw(tmp).andWhere("mime", "like", mime).orderBy("id", "desc").limit(1); - - const qnext = (await sql("items").select("id").whereRaw(tmp).andWhere("id", ">", id).andWhere("mime", "like", mime).orderBy("id").limit(3)).reverse(); - const qprev = await sql("items").select("id").whereRaw(tmp).andWhere("id", "<", id).andWhere("mime", "like", mime).orderBy("id", "desc").limit(3); - - const cheat = qnext.concat([{ id: id }].concat(qprev)).map(e => +e.id); - const next = qnext[qnext.length - 1] ? qnext[qnext.length - 1].id : false; - const prev = qprev[0] ? qprev[0].id : false; - - for(let t = 0; t < tags.length; t++) - tags[t].tag = tags[t].tag.replace(/[\u00A0-\u9999<>\&]/g, i => '&#'+i.charCodeAt(0)+';'); - - const data = { - user: { - name: query.username, - channel: query.userchannel, - network: query.usernetwork - }, - item: { - id: query.id, - src: { - long: query.src, - short: url.parse(query.src).hostname, + const qmax = await sql("items") + .select("id") + .whereRaw(modequery) + .andWhere("mime", "like", smime) + .andWhere("username", "like", user ? user : "%") + .orderBy("id", "desc") + .limit(1); + + const qnext = (await sql("items") + .select("id") + .whereRaw(modequery) + .andWhere("id", ">", itemid) + .andWhere("mime", "like", smime) + .andWhere("username", "like", user ? user : "%") + .orderBy("id") + .limit(3) + ).reverse(); + + const qprev = await sql("items") + .select("id") + .whereRaw(modequery) + .andWhere("id", "<", itemid) + .andWhere("mime", "like", smime) + .andWhere("username", "like", user ? user : "%") + .orderBy("id", "desc") + .limit(3); + + const cheat = qnext.concat([{ id: itemid }].concat(qprev)).map(e => +e.id); + const next = qnext[qnext.length - 1] ? qnext[qnext.length - 1].id : false; + const prev = qprev[0] ? qprev[0].id : false; + + const link = lib.genLink({ user, mime, act_page: query.id }); + + for(let t = 0; t < tags.length; t++) + tags[t].tag = tags[t].tag.replace(/[\u00A0-\u9999<>\&]/g, i => '&#'+i.charCodeAt(0)+';'); + + data = { + user: { + name: query.username, + channel: query.userchannel, + network: query.usernetwork }, - thumbnail: `${cfg.websrv.paths.thumbnails}/${query.id}.png`, - coverart: `${cfg.websrv.paths.coverarts}/${query.id}.png`, - dest: `${cfg.websrv.paths.images}/${query.dest}`, - mime: query.mime, - size: lib.formatSize(query.size), - timestamp: lib.timeAgo(new Date(query.stamp * 1e3).toISOString()), - tags: tags - }, - title: `${query.id} - f0ck.me`, - pagination: { - start: qmax[0].id, - end: qmin[0].id, - prev: next, - next: prev, - page: query.id, - cheat: cheat, - link: `/${tmpmime ? tmpmime + "/" : ""}` - }, - filter: tmpmime ? tmpmime : undefined - }; - res.reply({ body: tpl.render("item", data, req) }); + item: { + id: query.id, + src: { + long: query.src, + short: url.parse(query.src).hostname, + }, + thumbnail: `${cfg.websrv.paths.thumbnails}/${query.id}.png`, + coverart: `${cfg.websrv.paths.coverarts}/${query.id}.png`, + dest: `${cfg.websrv.paths.images}/${query.dest}`, + mime: query.mime, + size: lib.formatSize(query.size), + timestamp: lib.timeAgo(new Date(query.stamp * 1e3).toISOString()), + tags: tags + }, + title: `${query.id} - f0ck.me`, + pagination: { + start: qmax[0].id, + end: qmin[0].id, + prev: next, + next: prev, + page: query.id, + cheat: cheat, + link: link, + uff: true + }, + link: link, + tmp: output + }; + } + 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; + + 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(rows.length === 0) + return res.end("oops"); + + rows.forEach(e => { + if(!fs.existsSync(`public/t/${e.id}.png`)) + fs.copyFileSync("public/s/img/broken.png", `public/t/${e.id}.png`); + }); + + const cheat = []; + 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 }); + + data = { + items: rows, + pagination: { + start: 1, + end: pages, + prev: (act_page > 1) ? act_page - 1 : null, + next: (act_page < pages) ? act_page + 1 : null, + page: act_page, + cheat: cheat, + link: link, + uff: false + }, + last: rows[rows.length - 1].id, + link: link, + tmp: output + }; + } + + res.reply({ body: tpl.render(mode, data, req) }); }); router.get(/^\/(about)$/, (req, res) => { diff --git a/src/inc/routes/random.mjs b/src/inc/routes/random.mjs index 53ece1b..ed82d80 100644 --- a/src/inc/routes/random.mjs +++ b/src/inc/routes/random.mjs @@ -1,15 +1,33 @@ import sql from "../sql.mjs"; import lib from "../lib.mjs"; -const allowedMimes = [ "audio", "image", "video", "%" ]; +const allowedMimes = [ "audio", "image", "video" ]; export default (router, tpl) => { - router.get(/^\/random(\/image|\/video|\/audio)?$/, async (req, res) => { - const tmp = lib.getMode(req.session.mode ?? 0); - const mime = (allowedMimes.filter(n => req.url.split[1]?.startsWith(n))[0] ? req.url.split[1] : "") + "%"; - const rows = await sql("items").select("id").whereRaw(tmp).andWhere("mime", "like", mime).orderByRaw("rand()").limit(1); + 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 === "" ? "%" : "%"; + + const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(req.session.mode ?? 0); + + if(user && (await sql("items").where("username", "like", user).count("* as total"))[0].total === 0) + return res.end("200 - user not found lol"); + + 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"); + + const link = lib.genLink({ user, mime }); - res.redirect(`/${req.url.split[1] ? req.url.split[1] + "/" : ""}${rows[0].id}`); + res.redirect(`/${link}${link.length != 0 ? "/": ""}${rows[0].id}`); }); return router; }; diff --git a/views/index.html b/views/index.html index 1ed9346..5adb458 100644 --- a/views/index.html +++ b/views/index.html @@ -2,7 +2,7 @@
@each(items as item) -

+

@endeach
diff --git a/views/item.html b/views/item.html index 2160fdc..33d889d 100644 --- a/views/item.html +++ b/views/item.html @@ -4,7 +4,7 @@