import router from "../router.mjs"; import sql from "../sql.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 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] : "") + "%"; const rows = await sql("items").orderByRaw("rand()").limit(1).where("mime", "like", mime).andWhere("username", "like", user); 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 rows = await sql("items").where("id", "<", id).orderBy("id", "desc").limit(eps); const items = { items: rows, last: rows[rows.length - 1].id }; res.writeHead(200, { "Content-Type": "application/json" }); res.end(JSON.stringify(items), "utf-8"); }); router.get(/^\/api\/v1\/item\/[0-9]+$/, async (req, res) => { 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 = +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 : []) }); });