routes and stuff

This commit is contained in:
Flummi 2021-12-06 10:21:11 +01:00
parent 43665884f6
commit f7313d864c
9 changed files with 216 additions and 151 deletions

View File

@ -39,7 +39,7 @@ const flash = ({ type, msg }) => {
(async () => { (async () => {
if(_addtag = document.querySelector("a#a_addtag")) { if(_addtag = document.querySelector("a#a_addtag")) {
const postid = +document.querySelector("a.id-link").innerText; 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)); let tags = [...document.querySelectorAll("#tags > .badge")].map(t => t.innerText.slice(0, -2));
const deleteEvent = async e => { const deleteEvent = async e => {

View File

@ -57,6 +57,13 @@ export default new class {
createID() { createID() {
return crypto.randomBytes(16).toString("hex") + Date.now().toString(24); 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 funcs
async countf0cks() { async countf0cks() {

View File

@ -1,10 +1,10 @@
import cfg from "../../../config.json" assert { type: "json" }; import cfg from "../config.mjs";
import sql from "../sql.mjs"; import sql from "../sql.mjs";
import lib from "../lib.mjs"; import lib from "../lib.mjs";
import fs from "fs"; import fs from "fs";
import url from "url"; import url from "url";
const allowedMimes = [ "audio", "image", "video", "%" ]; const allowedMimes = [ "audio", "image", "video" ];
const auth = async (req, res, next) => { const auth = async (req, res, next) => {
if(!req.session) if(!req.session)
return res.redirect("/login"); return res.redirect("/login");
@ -12,104 +12,88 @@ const auth = async (req, res, next) => {
}; };
export default (router, tpl) => { export default (router, tpl) => {
router.get(/^\/(audio\/?|image\/?|video\/?)?(p\/\d+)?$/, async (req, res) => { router.get(/^\/?(?:user\/(?<user>[a-zA-Z0-9\[\]\-\_\{\}\\`\|]+))?(?:\/(?<mime>image|audio|video))?(?:\/p\/(?<page>\d+))?(?:\/(?<itemid>\d+))?$/, async (req, res) => {
const tmpmime = (allowedMimes.filter(n => req.url.split[0].startsWith(n))[0] ? req.url.split[0] : ""); const user = req.params.user ?? null;
const mime = tmpmime + "%"; const mime = (req.params.mime ?? "");
const tmp = tmpmime == "audio" ? lib.getMode(0) : lib.getMode(req.session.mode ?? 0); const smime = allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
const page = +(req.params.page ?? 1);
const itemid = +(req.params.itemid ?? null);
const total = ( const output = {
await sql("items") user, mime, smime, page, itemid
.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
}; };
res.reply({ body: tpl.render("index", data, req) }); const mode = itemid > 0 ? "item" : "index";
}); const modequery = mime == "audio" ? lib.getMode(0) : lib.getMode(req.session.mode ?? 0);
let data;
router.get(/^\/((audio\/|video\/|image\/)?[0-9]+)$/, async (req, res) => { if(user && (await sql("items").where("username", "like", user).count("* as total"))[0].total === 0)
let id = false; return res.end("200 - user not found lol");
let mime = "";
let tmpmime = false;
if(allowedMimes.filter(n => req.url.split[0].startsWith(n))[0] ? req.url.split[0] : "") { if(mode === "item") { // item!
mime = tmpmime = req.url.split[0]; if(itemid == 404)
id = +req.url.split[1]; return res.end("404 - lol");
}
else {
mime = "%";
id = +req.url.split[0];
}
mime += "/%";
let tmp = tmpmime == "audio" ? lib.getMode(0) : lib.getMode(req.session.mode ?? 0);
if(id == 404)
tmp = "";
const query = (await sql("items") const query = (await sql("items")
.whereRaw(tmp) .whereRaw(modequery)
.andWhere("id", id) .andWhere("id", itemid)
.andWhere("mime", "like", mime) .andWhere("mime", "like", smime)
.limit(1)) .andWhere("username", "like", user ? user : "%")
?.shift(); .limit(1))[0];
if(!query?.id) if(!query?.id) {
return res.redirect("/404"); 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", id); 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(tmp).andWhere("mime", "like", mime).orderBy("id").limit(1); const qmin = await sql("items")
const qmax = await sql("items").select("id").whereRaw(tmp).andWhere("mime", "like", mime).orderBy("id", "desc").limit(1); .select("id")
.whereRaw(modequery)
.andWhere("mime", "like", smime)
.andWhere("username", "like", user ? user : "%")
.orderBy("id")
.limit(1);
const qnext = (await sql("items").select("id").whereRaw(tmp).andWhere("id", ">", id).andWhere("mime", "like", mime).orderBy("id").limit(3)).reverse(); const qmax = await sql("items")
const qprev = await sql("items").select("id").whereRaw(tmp).andWhere("id", "<", id).andWhere("mime", "like", mime).orderBy("id", "desc").limit(3); .select("id")
.whereRaw(modequery)
.andWhere("mime", "like", smime)
.andWhere("username", "like", user ? user : "%")
.orderBy("id", "desc")
.limit(1);
const cheat = qnext.concat([{ id: id }].concat(qprev)).map(e => +e.id); 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 next = qnext[qnext.length - 1] ? qnext[qnext.length - 1].id : false;
const prev = qprev[0] ? qprev[0].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++) for(let t = 0; t < tags.length; t++)
tags[t].tag = tags[t].tag.replace(/[\u00A0-\u9999<>\&]/g, i => '&#'+i.charCodeAt(0)+';'); tags[t].tag = tags[t].tag.replace(/[\u00A0-\u9999<>\&]/g, i => '&#'+i.charCodeAt(0)+';');
const data = { data = {
user: { user: {
name: query.username, name: query.username,
channel: query.userchannel, channel: query.userchannel,
@ -137,11 +121,68 @@ export default (router, tpl) => {
next: prev, next: prev,
page: query.id, page: query.id,
cheat: cheat, cheat: cheat,
link: `/${tmpmime ? tmpmime + "/" : ""}` link: link,
uff: true
}, },
filter: tmpmime ? tmpmime : undefined link: link,
tmp: output
}; };
res.reply({ body: tpl.render("item", data, req) }); }
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) => { router.get(/^\/(about)$/, (req, res) => {

View File

@ -1,15 +1,33 @@
import sql from "../sql.mjs"; import sql from "../sql.mjs";
import lib from "../lib.mjs"; import lib from "../lib.mjs";
const allowedMimes = [ "audio", "image", "video", "%" ]; const allowedMimes = [ "audio", "image", "video" ];
export default (router, tpl) => { export default (router, tpl) => {
router.get(/^\/random(\/image|\/video|\/audio)?$/, async (req, res) => { router.get(/^\/random\/?(?:user\/(?<user>[a-zA-Z0-9\[\]\-\_\{\}\\`\|]+))?(?:\/(?<mime>image|audio|video))?$/, async (req, res) => {
const tmp = lib.getMode(req.session.mode ?? 0); const user = req.params.user ?? null;
const mime = (allowedMimes.filter(n => req.url.split[1]?.startsWith(n))[0] ? req.url.split[1] : "") + "%"; const mime = (req.params.mime ?? "");
const rows = await sql("items").select("id").whereRaw(tmp).andWhere("mime", "like", mime).orderByRaw("rand()").limit(1); const smime = allowedMimes.includes(mime) ? mime + "/%" : mime === "" ? "%" : "%";
res.redirect(`/${req.url.split[1] ? req.url.split[1] + "/" : ""}${rows[0].id}`); 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(`/${link}${link.length != 0 ? "/": ""}${rows[0].id}`);
}); });
return router; return router;
}; };

View File

@ -2,7 +2,7 @@
<div class="index-container"> <div class="index-container">
<div id="posts"> <div id="posts">
@each(items as item) @each(items as item)
<a href="/@if(typeof filter !== "undefined"){{ filter }}/@endif{{ item.id }}" data-mime="{{ item.mime }}" data-mode="{{ item.tag_id ? ['','sfw','nsfw'][item.tag_id] : 'null' }}" style="background-image: url('/t/{{ item.id }}.png')"><p></p></a> <a href="/{{ link }}@if(link.length != 0)/@endif{{ item.id }}" data-mime="{{ item.mime }}" data-mode="{{ item.tag_id ? ['','sfw','nsfw'][item.tag_id] : 'null' }}" style="background-image: url('/t/{{ item.id }}.png')"><p></p></a>
@endeach @endeach
</div> </div>
</div> </div>

View File

@ -4,7 +4,7 @@
<div class="next-post"> <div class="next-post">
@if(pagination.prev) @if(pagination.prev)
<div class="arrow-next"> <div class="arrow-next">
<a id="next" href="{{ pagination.link }}{{ pagination.prev }}"></a> <a id="next" href="/{{ link }}@if(link.length != 0)/@endif{{ pagination.prev }}"></a>
</div> </div>
@else @else
<div class="arrow-next"> <div class="arrow-next">
@ -20,7 +20,7 @@
preload="auto" autoplay controls loop playsinline></video> preload="auto" autoplay controls loop playsinline></video>
</div> </div>
@elseif(item.mime.startsWith("audio")) @elseif(item.mime.startsWith("audio"))
<div class="embed-responsive embed-responsive-16by9" style="background: url('@if(item.coverart)//f0ck.me{{ item.coverart }}@else/s/img/200.gif@endif') no-repeat center / contain black;"></div> <div class="embed-responsive embed-responsive-16by9" style="background: url('@if(item.coverart)//f0ck.me{{ item.coverart }}@else/s/img/200.gif@endif') no-repeat center / contain black;">
<audio id="my-video" class="embed-responsive-item" autoplay controls loop src="{{ item.dest }}" data-setup="{}" poster="@if(item.coverart){{ item.coverart }}@else/s/img/200.gif@endif" type="{{ item.mime }}"></audio> <audio id="my-video" class="embed-responsive-item" autoplay controls loop src="{{ item.dest }}" data-setup="{}" poster="@if(item.coverart){{ item.coverart }}@else/s/img/200.gif@endif" type="{{ item.mime }}"></audio>
</div> </div>
@elseif(item.mime.startsWith("image")) @elseif(item.mime.startsWith("image"))
@ -36,7 +36,7 @@
<div class="previous-post"> <div class="previous-post">
@if(pagination.next) @if(pagination.next)
<div class="arrow-prev"> <div class="arrow-prev">
<a id="prev" href="{{ pagination.link }}{{ pagination.next }}"></a> <a id="prev" href="/{{ link }}@if(link.length != 0)/@endif{{ pagination.next }}"></a>
</div> </div>
@else @else
<div class="arrow-prev"> <div class="arrow-prev">
@ -47,7 +47,7 @@
</div> </div>
<div class="metadata"> <div class="metadata">
<span class="badge badge-dark"> <span class="badge badge-dark">
<a href="/{{ item.id }}" style="--hover-image: url('/t/{{ item.id }}.png');" class="id-link">{{ item.id }}</a>@if(session) (<span id="a_username">{{ user.name }}</span>)@endif <a href="/{{ item.id }}" style="--hover-image: url('/t/{{ item.id }}.png');" class="id-link">{{ item.id }}</a>@if(session) (<a id="a_username" href="/user/{{ user.name.toLowerCase() }}@if(tmp.mime)/{{ tmp.mime }}@endif">{{ user.name }}</a>)@endif
</span> </span>
<span class="badge badge-dark">{{ user.network }} / {{ user.channel }}</span> <span class="badge badge-dark">{{ user.network }} / {{ user.channel }}</span>
<span class="badge badge-dark image-source"><a class="post_source" title="{{ item.src.long }}" href="{{ item.src.long }}" target="_blank">{{ item.src.short }}</a></span> <span class="badge badge-dark image-source"><a class="post_source" title="{{ item.src.long }}" href="{{ item.src.long }}" target="_blank">{{ item.src.short }}</a></span>

View File

@ -1,15 +1,15 @@
@if(typeof pagination !== "undefined") @if(pagination)
<nav class="pagination"> <nav class="pagination">
<a href="{{ pagination.link }}{{ pagination.start }}" class="page-item-1 btn start@if(!pagination.prev) disabled@endif">&laquo;</a> <a href="/{{ pagination.link }}@if(pagination.link.length != 0)/@endif@if(!pagination.uff)p/@endif{{ pagination.start }}" class="page-item-1 btn start@if(!pagination.prev) disabled@endif">&laquo;</a>
<a href="{{ pagination.link }}{{ pagination.prev }}" class="page-item-2 btn prev@if(!pagination.prev) disabled@endif">&lsaquo;</a> <a href="/{{ pagination.link }}@if(pagination.link.length != 0)/@endif@if(!pagination.uff)p/@endif{{ pagination.prev }}" class="page-item-2 btn prev@if(!pagination.prev) disabled@endif">&lsaquo;</a>
@each(pagination.cheat as i) @each(pagination.cheat as i)
@if(i == pagination.page) @if(i == pagination.page)
<span class="btn disabled">{{ i }}</span> <span class="btn disabled">{{ i }}</span>
@else @else
<a href="{{ pagination.link }}{{ i }}" class="pagination-int-item btn">{{ i }}</a> <a href="/{{ pagination.link }}@if(pagination.link.length != 0)/@endif@if(!pagination.uff)p/@endif{{ i }}" class="pagination-int-item btn">{{ i }}</a>
@endif @endif
@endeach @endeach
<a href="{{ pagination.link }}{{ pagination.next }}" class="page-item-3 btn next@if(!pagination.next) disabled@endif">&rsaquo;</a> <a href="/{{ pagination.link }}@if(pagination.link.length != 0)/@endif@if(!pagination.uff)p/@endif{{ pagination.next }}" class="page-item-3 btn next@if(!pagination.next) disabled@endif">&rsaquo;</a>
<a href="{{ pagination.link }}{{ pagination.end }}" class="page-item-4 btn start@if(!pagination.next) disabled@endif">&raquo;</a> <a href="/{{ pagination.link }}@if(pagination.link.length != 0)/@endif@if(!pagination.uff)p/@endif{{ pagination.end }}" class="page-item-4 btn start@if(!pagination.next) disabled@endif">&raquo;</a>
</nav> </nav>
@endif @endif

View File

@ -29,12 +29,12 @@
</li> </li>
<span class="placeholder">&nbsp;</span> <span class="placeholder">&nbsp;</span>
<li class="nav-item dropdown"> <li class="nav-item dropdown">
<a class="nav-link ddcontent" href="#"@if(typeof filter !== "undefined") content="{{ filter }}" data-toggle="dropdown"@endif>Filter@if(typeof filter === "undefined")&nbsp;&#9660;@endif</a> <a class="nav-link ddcontent" href="#"@if(tmp.mime) content="{{ tmp.mime }}" data-toggle="dropdown"@endif>Filter@if(!tmp.mime)&nbsp;&#9660;@endif</a>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li><a class="dropdown-item" href="/">All</a></li> <li><a class="dropdown-item" href="/@if(tmp.user)user/{{ tmp.user }}/@endif">All</a></li>
<li><a class="dropdown-item" href="/audio">Audio</a></li> <li><a class="dropdown-item" href="/@if(tmp.user)user/{{ tmp.user }}/@endifaudio">Audio</a></li>
<li><a class="dropdown-item" href="/video">Video</a></li> <li><a class="dropdown-item" href="/@if(tmp.user)user/{{ tmp.user }}/@endifvideo">Video</a></li>
<li><a class="dropdown-item" href="/image">Image</a></li> <li><a class="dropdown-item" href="/@if(tmp.user)user/{{ tmp.user }}/@endifimage">Image</a></li>
</ul> </ul>
</li> </li>
@if(session) @if(session)
@ -48,7 +48,7 @@
</li> </li>
@endif @endif
<li class="nav-item"> <li class="nav-item">
<a id="random" class="nav-link" href="/random@if(typeof filter !== "undefined")/{{ filter }}@endif"> <a id="random" class="nav-link" href="/random@if(tmp.user)/user/{{ tmp.user }}@endif@if(tmp.mime)/{{ tmp.mime }}@endif">
<span class="nav-link-identifier">Random</span> <span class="nav-link-identifier">Random</span>
</a> </a>
</li> </li>

View File

@ -36,7 +36,6 @@
<div class="pagination-container-fluid"> <div class="pagination-container-fluid">
<div class="pagination-wrapper"> <div class="pagination-wrapper">
Henlo, {{ session.user }}&nbsp;&nbsp;<a href="/logout">Logout</a> Henlo, {{ session.user }}&nbsp;&nbsp;<a href="/logout">Logout</a>
@include(partials/pagination)
</div> </div>
</div> </div>
</div> </div>