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

@ -17,9 +17,11 @@ export default async bot => {
continue;
const f0ck = await db`
select dest, username, userchannel, usernetwork
select dest, mime, username, userchannel, usernetwork
from "items"
where id = ${id}
where
id = ${id} and
active = 'true'
limit 1
`;
const level = getLevel(e.user).level;
@ -44,18 +46,66 @@ export default async bot => {
continue;
}
await fs.unlink(`./public/b/${f0ck[0].dest}`).catch(_=>{});
await fs.unlink(`./public/t/${id}`).catch(_=>{});
await db`update "items" set active = 'false' where id = ${id}`;
await db`
delete from "items"
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(_=>{});
}
deleted.push(id);
}
e.reply(`deleted ${deleted.length}/${e.args.length} f0cks (${deleted.join(",")})`);
}
}, {
name: "recover",
call: /^\!(recover) .*/i,
active: true,
level: 100,
f: async e => {
let recovered = [];
for(let id of e.args) {
id = +id;
if(id <= 1)
continue;
const f0ck = await db`
select dest, mime
from "items"
where
id = ${id} and
active = 'false'
limit 1
`;
if(f0ck.length === 0) {
e.reply(`f0ck ${id}: f0ck not found`);
continue;
}
await fs.copyFile(`./deleted/b/${f0ck[0].dest}`, `./public/b/${f0ck[0].dest}`).catch(_=>{});
await fs.copyFile(`./deleted/t/${id}.webp`, `./public/t/${id}.webp`).catch(_=>{});
await fs.unlink(`./deleted/b/${f0ck[0].dest}`).catch(_=>{});
await fs.unlink(`./deleted/t/${id}.webp`).catch(_=>{});
if(f0ck[0].mime.startsWith('audio')) {
await fs.copyFile(`./deleted/ca/${id}.webp`, `./public/ca/${id}.webp`).catch(_=>{});
await fs.unlink(`./deleted/ca/${id}.webp`).catch(_=>{});
}
await db`update "items" set active = 'true' where id = ${id}`;
recovered.push(id);
}
e.reply(`recovered ${recovered.length}/${e.args.length} f0cks (${recovered.join(",")})`);
}
}]
};

View File

@ -137,7 +137,7 @@ export default async bot => {
userchannel: e.channel,
usernetwork: e.network,
stamp: ~~(new Date() / 1000),
active: 1
active: 'true'
}, 'src', 'dest', 'mime', 'size', 'checksum', 'username', 'userchannel', 'usernetwork', 'stamp', 'active')
}
`;