querybuilder intensifies

This commit is contained in:
Flummi
2021-05-16 13:24:31 +02:00
parent 3bc0a74932
commit 04d4787232
16 changed files with 244 additions and 410 deletions

View File

@@ -1,80 +1,32 @@
import router from "../router.mjs";
import sql from "../sql.mjs";
import { parse } from "url";
import cfg from "../../../config.json";
import { mimes, queries } from "./inc/apiv1.mjs";
const allowedMimes = [ "audio", "image", "video", "%" ];
router.get("/api/v1", (req, res) => {
res.end("api lol");
});
router.get(/^\/api\/v1\/random(\/user\/.+|\/image|\/video|\/audio)?$/, async (req, res) => {
const args = [];
let q = queries.random.main;
if(req.url.split[3] === "user") {
q += queries.random.where("username like ?");
args.push(req.url.split[4] || "flummi");
}
else
q += queries.random.where(mimes[req.url.split[3]] ? mimes[req.url.split[3]].map(mime => `mime = "${mime}"`).join(" or ") : null);
const user = req.url.split[3] === "user" ? req.url.split[4] : "%";
const mime = (allowedMimes.filter(n => req.url.split[3]?.startsWith(n))[0] ? req.url.split[3] : "") + "%";
try {
const rows = await sql.query(q, args);
res
.writeHead(200, { "Content-Type": "application/json" })
.end(JSON.stringify(rows.length > 0 ? rows[0] : []), "utf-8");
} catch(err) {
res
.writeHead(500)
.end(JSON.stringify(err), "utf-8");
}
});
const rows = await sql("items").orderByRaw("rand()").limit(1).where("mime", "like", mime).andWhere("username", "like", user);
router.get("/api/v1/p", async (req, res) => {
let id = parseInt(req.url.qs.id) || 99999999;
const eps = Math.min(parseInt(req.url.qs.eps) || 100, 200);
let [ order, trend ] = req.url.qs.order === "asc" ? [ "asc", ">" ] : [ "desc", "<" ];
try {
const tmp = (await sql.query("select min(id) as min, max(id) as max from items limit 1"))[0];
if((id - 1 + eps) > tmp.max) {
id = tmp.max;
[ order, trend ] = [ "desc", "<=" ];
}
if((id + 1 - eps) < tmp.min) {
id = tmp.min;
[ order, trend ] = [ "asc", ">=" ];
}
const rows = await sql.query(queries.p(trend, order), [ id, eps ]);
const items = {
items: rows,
first: rows[0].id,
last: rows[rows.length - 1].id,
newest: tmp.max,
oldest: tmp.min
};
res
.writeHead(200, { "Content-Type": "application/json" })
.end(JSON.stringify(items), "utf-8");
} catch(err) {
console.error(err);
res
.writeHead(500)
.end(JSON.stringify(err), "utf-8");
}
res
.writeHead(200, { "Content-Type": "application/json" })
.end(JSON.stringify(rows.length > 0 ? rows[0] : []), "utf-8");
});
router.get(/^\/api\/v1\/p\/([0-9]+)/, async (req, res) => { // legacy
let eps = 100;
let id = +req.url.split[3];
const query = await sql.query("select * from items where id < ? order by id desc limit ?", [ id, eps ]);
const rows = await sql("items").where("id", "<", id).orderBy("id", "desc").limit(eps);
const items = {
items: query,
last: query[query.length - 1].id
items: rows,
last: rows[rows.length - 1].id
};
res.writeHead(200, { "Content-Type": "application/json" });
@@ -82,33 +34,40 @@ router.get(/^\/api\/v1\/p\/([0-9]+)/, async (req, res) => { // legacy
});
router.get(/^\/api\/v1\/item\/[0-9]+$/, async (req, res) => {
try {
const rows = await sql.query(queries.item, Array(3).fill(req.url.split[3]));
res.reply({
type: "application/json",
body: JSON.stringify(rows?.shift() || [])
});
} catch(err) {
res.reply({
code: 500,
body: JSON.stringify(err)
});
}
const id = +req.url.split[3];
const item = await sql("items").where("id", id).limit(1);
const next = await sql("items").select("id").where("id", ">", id).orderBy("id").limit(1);
const prev = await sql("items").select("id").where("id", "<", id).orderBy("id", "desc").limit(1);
if(item.length === 0)
return "nope";
const rows = {
...item[0],
...{
next: next[0]?.id ?? null,
prev: prev[0]?.id ?? null
}
};
res.reply({
type: "application/json",
body: JSON.stringify(rows)
});
});
router.get(/^\/api\/v1\/user\/.*(\/[0-9]+)?$/, async (req, res) => { // auf qs umstellen
const user = req.url.split[3];
const eps = Math.min(req.url.split[4] || 50, 50);
try {
const rows = await sql.query(queries.user, [ user, eps ]);
res.reply({
type: "application/json",
body: JSON.stringify(rows.length > 0 ? rows : [])
});
} catch(err) {
res.reply({
code: 500,
body: JSON.stringify(err)
});
}
const eps = +req.url.split[4] || 50;
const rows = await sql("items")
.select("id", "mime", "size", "src", "stamp", "userchannel", "username", "usernetwork")
.where("username", user)
.orderBy("stamp", "desc")
.limit(eps);
res.reply({
type: "application/json",
body: JSON.stringify(rows.length > 0 ? rows : [])
});
});