soft delete v1

This commit is contained in:
2022-05-22 04:11:15 +00:00
parent d2091494a1
commit c6fbe956a0
6 changed files with 108 additions and 25 deletions

View File

@ -1,3 +1,4 @@
import { promises as fs } from "fs";
import db from '../../sql.mjs';
import lib from '../../lib.mjs';
import search from '../../routeinc/search.mjs';
@ -16,8 +17,10 @@ export default router => {
const rows = await db`
select *
from "items"
where mime ilike ${mime}
and username ilike ${user}
where
mime ilike ${mime} and
username ilike ${user} and
active = 'true'
order by random()
limit 1
`;
@ -46,7 +49,8 @@ export default router => {
from "items"
left join "tags_assign" on "tags_assign".item_id = "items".id and ("tags_assign".tag_id = 1 or "tags_assign".tag_id = 2)
where
${db.unsafe(modequery)}
${db.unsafe(modequery)} and
active = 'true'
${
opt.older
? db`and id <= ${opt.older}`
@ -76,20 +80,20 @@ export default router => {
const item = await db`
select *
from "items"
where id = ${+id}
where id = ${+id} and active = 'true'
limit 1
`;
const next = await db`
select id
from "items"
where id > ${+id}
where id > ${+id} and active = 'true'
order by id
limit 1
`;
const prev = await db`
select id
from "items"
where id < ${+id}
where id < ${+id} and active = 'true'
order by id desc
limit 1
`;
@ -122,7 +126,7 @@ export default router => {
const rows = db`
select id, mime, size, src, stamp, userchannel, username, usernetwork
from "items"
where username = ${user}
where username = ${user} and active = 'true'
order by stamp desc
limit ${+eps}
`;
@ -222,19 +226,42 @@ export default router => {
msg: 'no postid'
});
}
const postid = +req.post.postid;
const id = +req.post.postid;
if(postid <= 1) {
if(id <= 1) {
return res.json({
success: false
});
}
await db`
delete from "items"
where id = ${+postid}
const f0ck = await db`
select dest, mime
from "items"
where
id = ${id} and
active = 'true'
limit 1
`;
if(f0ck.length === 0) {
return res.json({
success: false,
msg: `f0ck ${id}: f0ck not found`
});
}
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(_=>{});
}
res.json({
success: true
});

View File

@ -16,7 +16,7 @@ export default router => {
const itemid = (await db`
select id
from "items"
where id = ${+avatar}
where id = ${+avatar} and active = 'true'
`)?.[0]?.id;
if(!itemid) {

View File

@ -40,7 +40,9 @@ export default (router, tpl) => {
ret = await db`
select *
from "items"
where src ilike ${'%' + tag.substring(4) + '%'}
where
src ilike ${'%' + tag.substring(4) + '%'} and
active = 1
group by "items".id
order by "items".id desc
offset ${offset}
@ -53,7 +55,7 @@ export default (router, tpl) => {
from "tags"
left join "tags_assign" on "tags_assign".tag_id = "tags".id
left join "items" on "items".id = "tags_assign".item_id
where "tags".tag ilike ${'%' + tag + '%'}
where "tags".tag ilike ${'%' + tag + '%'} and "items".active = 1
group by "items".id, "tags".tag
offset ${offset}
limit ${_eps}