diff --git a/public/s/js/admin.js b/public/s/js/admin.js index 1d61cd3..7817b70 100644 --- a/public/s/js/admin.js +++ b/public/s/js/admin.js @@ -206,16 +206,22 @@ confirmBtn.onclick = async () => { confirmBtn.textContent = 'Deleting...'; confirmBtn.disabled = true; - const res = await post("/api/v2/admin/deletepost", { - postid: postid - }); - if (!res.success) { - alert(res.msg); + try { + const res = await post("/api/v2/admin/deletepost", { + postid: postid + }); + if (!res.success) { + alert(res.msg); + confirmBtn.textContent = 'Delete'; + confirmBtn.disabled = false; + } else { + closeModal(); + window.location.href = '/'; + } + } catch (e) { + alert('Error: ' + e); // Or e.message confirmBtn.textContent = 'Delete'; confirmBtn.disabled = false; - } else { - closeModal(); - window.location.href = '/'; } }; } diff --git a/src/inc/routes/admin.mjs b/src/inc/routes/admin.mjs index a9c797b..7fc4923 100644 --- a/src/inc/routes/admin.mjs +++ b/src/inc/routes/admin.mjs @@ -212,57 +212,59 @@ export default (router, tpl) => { posts, page, pages, + stats: { total: posts.length }, tmp: null }, req) }); }); + const deleteItem = async (id) => { + const f0ck = await db` + select dest, mime + from "items" + where + id = ${id} + limit 1 + `; + + if (f0ck.length > 0) { + console.log(`[ADMIN DENY] Found item, deleting files: ${f0ck[0].dest}`); + // Delete files + await fs.unlink(`./public/b/${f0ck[0].dest}`).catch(e => console.log('File error pub/b:', e.message)); + await fs.unlink(`./public/t/${id}.webp`).catch(e => console.log('File error pub/t:', e.message)); + await fs.unlink(`./deleted/b/${f0ck[0].dest}`).catch(e => console.log('File error del/b:', e.message)); + await fs.unlink(`./deleted/t/${id}.webp`).catch(e => console.log('File error del/t:', e.message)); + + if (f0ck[0].mime.startsWith('audio')) { + await fs.unlink(`./public/ca/${id}.webp`).catch(() => { }); + await fs.unlink(`./deleted/ca/${id}.webp`).catch(() => { }); + } + + // Delete DB entries + console.log('[ADMIN DENY] Deleting DB entries...'); + try { + await db`delete from "tags_assign" where item_id = ${id}`; + await db`delete from "favorites" where item_id = ${id}`; + await db`delete from "comments" where item_id = ${id}`.catch(() => { }); + await db`delete from "items" where id = ${id}`; + console.log('[ADMIN DENY] Deleted successfully'); + return true; + } catch (dbErr) { + console.error('[ADMIN DENY DB ERROR]', dbErr); + return false; + } + } else { + console.log('[ADMIN DENY] Item not found in DB'); + return false; + } + }; + router.get(/^\/admin\/deny\/?/, lib.auth, async (req, res) => { console.log('[ADMIN DENY] Logs initiated'); if (req.url.qs?.id) { const id = +req.url.qs.id; console.log(`[ADMIN DENY] Denying ID: ${id}`); - - try { - const f0ck = await db` - select dest, mime - from "items" - where - id = ${id} - limit 1 - `; - - if (f0ck.length > 0) { - console.log(`[ADMIN DENY] Found item, deleting files: ${f0ck[0].dest}`); - // Delete files - await fs.unlink(`./public/b/${f0ck[0].dest}`).catch(e => console.log('File error pub/b:', e.message)); - await fs.unlink(`./public/t/${id}.webp`).catch(e => console.log('File error pub/t:', e.message)); - await fs.unlink(`./deleted/b/${f0ck[0].dest}`).catch(e => console.log('File error del/b:', e.message)); - await fs.unlink(`./deleted/t/${id}.webp`).catch(e => console.log('File error del/t:', e.message)); - - if (f0ck[0].mime.startsWith('audio')) { - await fs.unlink(`./public/ca/${id}.webp`).catch(() => { }); - await fs.unlink(`./deleted/ca/${id}.webp`).catch(() => { }); - } - - // Delete DB entries - console.log('[ADMIN DENY] Deleting DB entries...'); - try { - await db`delete from "tags_assign" where item_id = ${id}`; - await db`delete from "favorites" where item_id = ${id}`; - await db`delete from "comments" where item_id = ${id}`.catch(() => { }); - await db`delete from "items" where id = ${id}`; - console.log('[ADMIN DENY] Deleted successfully'); - } catch (dbErr) { - console.error('[ADMIN DENY DB ERROR]', dbErr); - } - } else { - console.log('[ADMIN DENY] Item not found in DB'); - } - } catch (err) { - console.error('[ADMIN DENY ERROR]', err); - } - + await deleteItem(id); return res.writeHead(302, { "Location": `/admin/approve` }).end(); @@ -272,5 +274,22 @@ export default (router, tpl) => { return res.writeHead(302, { "Location": "/admin/approve" }).end(); }); + router.post(/^\/admin\/deny-multi\/?/, lib.auth, async (req, res) => { + try { + const ids = req.post.ids; + if (!Array.isArray(ids)) throw new Error('ids must be an array'); + + console.log(`[ADMIN DENY MULTI] Denying ${ids.length} items`); + for (const id of ids) { + await deleteItem(+id); + } + + return res.reply({ success: true }); + } catch (err) { + console.error('[ADMIN DENY MULTI ERROR]', err); + return res.reply({ success: false, msg: err.message }, 500); + } + }); + return router; }; diff --git a/views/admin/approve.html b/views/admin/approve.html index d771d02..b4d6c2f 100644 --- a/views/admin/approve.html +++ b/views/admin/approve.html @@ -51,34 +51,142 @@
@endif + +
+ +
+ Back to Admin + + + + @include(snippets/footer) \ No newline at end of file