fixed issues with the random button and hotkeys

This commit is contained in:
eins
2026-01-23 14:07:52 +00:00
parent b72fcaa426
commit 6799ec1567
5 changed files with 85 additions and 76 deletions

View File

@@ -3,7 +3,7 @@ import db from '../../sql.mjs';
import lib from '../../lib.mjs';
import search from '../../routeinc/search.mjs';
const allowedMimes = [ "audio", "image", "video", "%" ];
const allowedMimes = ["audio", "image", "video", "%"];
export default router => {
router.group(/^\/api\/v2/, group => {
group.get(/$/, (req, res) => {
@@ -13,24 +13,28 @@ export default router => {
group.get(/\/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 tag = req.url.qs.tag || null;
const rows = await db`
select *
select "items".*
from "items"
left join tags_assign on tags_assign.item_id = items.id
left join tags on tags.id = tags_assign.tag_id
where
mime ilike ${mime} and
username ilike ${user} and
active = 'true'
${tag ? db`and tags.normalized ilike ${'%' + tag + '%'}` : db``}
order by random()
limit 1
`;
return res.json({
success: rows.length > 0,
items: rows.length > 0 ? rows[0] : []
});
});
group.get(/\/items\/get/, async (req, res) => {
let eps = 150;
@@ -51,17 +55,15 @@ export default router => {
where
${db.unsafe(modequery)} and
active = 'true'
${
opt.older
? db`and id <= ${opt.older}`
: opt.newer
? db`and id >= ${opt.newer}`
: db``
}
order by id ${
opt.newer
? db`asc`
: db`desc`
${opt.older
? db`and id <= ${opt.older}`
: opt.newer
? db`and id >= ${opt.newer}`
: db``
}
order by id ${opt.newer
? db`asc`
: db`desc`
}
limit ${eps}
`).sort((a, b) => b.id - a.id);
@@ -73,10 +75,10 @@ export default router => {
items: rows
}, 200);
});
group.get(/\/item\/[0-9]+$/, async (req, res) => {
const id = +req.url.split[3];
const item = await db`
select *
from "items"
@@ -97,14 +99,14 @@ export default router => {
order by id desc
limit 1
`;
if(item.length === 0) {
if (item.length === 0) {
return res.json({
success: false,
msg: 'no items found'
});
}
const rows = {
...item[0],
...{
@@ -118,11 +120,11 @@ export default router => {
rows
});
});
group.get(/\/user\/.*(\/\d+)?$/, async (req, res) => {
const user = req.url.split[3];
const eps = +req.url.split[4] || 50;
const rows = db`
select id, mime, size, src, stamp, userchannel, username, usernetwork
from "items"
@@ -130,7 +132,7 @@ export default router => {
order by stamp desc
limit ${+eps}
`;
return res.json({
success: rows.length > 0,
items: rows.length > 0 ? rows : []
@@ -140,7 +142,7 @@ export default router => {
// tags lol
group.put(/\/admin\/tags\/(?<tagname>.*)/, lib.loggedin, async (req, res) => {
if(!req.params.tagname || !req.post.newtag) {
if (!req.params.tagname || !req.post.newtag) {
return res.json({
success: false,
msg: 'missing tagname or newtag',
@@ -154,7 +156,7 @@ export default router => {
const tagname = decodeURIComponent(req.params.tagname);
const newtag = req.post.newtag;
if(['sfw', 'nsfw'].includes(tagname) || ['sfw', 'nsfw'].includes(newtag)) {
if (['sfw', 'nsfw'].includes(tagname) || ['sfw', 'nsfw'].includes(newtag)) {
return res.json({
msg: 'f0ck you'
}, 405); // method not allowed
@@ -166,8 +168,8 @@ export default router => {
where tag = ${tagname}
limit 1
`)[0];
if(!tmptag) {
if (!tmptag) {
return res.json({
success: false,
msg: 'no tag found'
@@ -175,10 +177,9 @@ export default router => {
}
const q = (await db`
update "tags" set ${
db({
tag: newtag
}, 'tag')
update "tags" set ${db({
tag: newtag
}, 'tag')
}
where tag = ${tagname}
returning *
@@ -195,7 +196,7 @@ export default router => {
const searchString = req.url.qs.q;
if(searchString?.length <= 1) {
if (searchString?.length <= 1) {
reply.error = 'too short lol';
return res.json(reply);
}
@@ -212,7 +213,7 @@ export default router => {
`;
reply.success = true;
reply.suggestions = search(q, searchString);
} catch(err) {
} catch (err) {
reply.error = err.msg;
}
@@ -220,7 +221,7 @@ export default router => {
});
group.post(/\/admin\/deletepost$/, lib.auth, async (req, res) => {
if(!req.post.postid) {
if (!req.post.postid) {
return res.json({
success: false,
msg: 'no postid'
@@ -228,7 +229,7 @@ export default router => {
}
const id = +req.post.postid;
if(id <= 1) {
if (id <= 1) {
return res.json({
success: false
});
@@ -243,7 +244,7 @@ export default router => {
limit 1
`;
if(f0ck.length === 0) {
if (f0ck.length === 0) {
return res.json({
success: false,
msg: `f0ck ${id}: f0ck not found`
@@ -251,15 +252,15 @@ export default router => {
}
await db`update "items" set active = 'false' where id = ${id}`;
await fs.copyFile(`./public/b/${f0ck[0].dest}`, `./deleted/b/${f0ck[0].dest}`).catch(_=>{});
await fs.copyFile(`./public/t/${id}.webp`, `./deleted/t/${id}.webp`).catch(_=>{});
await fs.unlink(`./public/b/${f0ck[0].dest}`).catch(_=>{});
await fs.unlink(`./public/t/${id}.webp`).catch(_=>{});
if(f0ck[0].mime.startsWith('audio')) {
await fs.copyFile(`./public/ca/${id}.webp`, `./deleted/ca/${id}.webp`).catch(_=>{});
await fs.unlink(`./public/ca/${id}.webp`).catch(_=>{});
await fs.copyFile(`./public/b/${f0ck[0].dest}`, `./deleted/b/${f0ck[0].dest}`).catch(_ => { });
await fs.copyFile(`./public/t/${id}.webp`, `./deleted/t/${id}.webp`).catch(_ => { });
await fs.unlink(`./public/b/${f0ck[0].dest}`).catch(_ => { });
await fs.unlink(`./public/t/${id}.webp`).catch(_ => { });
if (f0ck[0].mime.startsWith('audio')) {
await fs.copyFile(`./public/ca/${id}.webp`, `./deleted/ca/${id}.webp`).catch(_ => { });
await fs.unlink(`./public/ca/${id}.webp`).catch(_ => { });
}
res.json({
@@ -269,14 +270,14 @@ export default router => {
group.post(/\/admin\/togglefav$/, lib.loggedin, async (req, res) => {
const postid = +req.post.postid;
let favs = await db`
select user_id
from "favorites"
where item_id = ${+postid}
`;
if(Object.values(favs).filter(u => u.user_id === req.session.id)[0]) {
if (Object.values(favs).filter(u => u.user_id === req.session.id)[0]) {
// del fav
await db`
delete from "favorites"
@@ -287,11 +288,10 @@ export default router => {
else {
// add fav
await db`
insert into "favorites" ${
db({
item_id: +postid,
user_id: +req.session.id
}, 'item_id', 'user_id')
insert into "favorites" ${db({
item_id: +postid,
user_id: +req.session.id
}, 'item_id', 'user_id')
}
`;
}
@@ -310,7 +310,7 @@ export default router => {
favs
});
});
});
return router;