change psql-lib from knex to postgres.js

This commit is contained in:
Flummi
2022-03-31 13:34:51 +02:00
parent f750a9a60f
commit c498a33cbe
20 changed files with 689 additions and 973 deletions

View File

@ -1,7 +1,5 @@
import { getLevel } from "../admin.mjs";
import fetch from "flumm-fetch-cookies";
import sql from "../sql.mjs";
import vm from "vm";
let maxoutput = 750;
@ -9,7 +7,6 @@ let context = vm.createContext({
e: null,
bot: null,
admins: null,
sql: sql,
fetch: fetch,
a: null,

View File

@ -1,5 +1,5 @@
import { promises as fs } from "fs";
import sql from "../sql.mjs";
import db from "../sql.mjs";
export default async bot => {
@ -14,14 +14,22 @@ export default async bot => {
if(id <= 0)
return false;
const f0ck = await sql("items").select("dest").where("id", id).limit(1);
const f0ck = await db`
select dest
from "items"
where id = ${+id}
limit 1
`;
if(f0ck.length === 0)
return false;
await fs.unlink(`./public/b/${f0ck[0].dest}`).catch(_=>{});
await fs.unlink(`./public/t/${id}`).catch(_=>{});
await sql("items").where("id", id).del().limit(1);
await db`
delete from "items"
where id = ${+id}
`;
return id;
}))).filter(d => d);

View File

@ -2,14 +2,17 @@ import fetch from "flumm-fetch-cookies";
import { promises as fs } from "fs";
import { exec } from "child_process";
import cfg from "../config.mjs";
import sql from "../sql.mjs";
import db from "../sql.mjs";
import lib from "../lib.mjs";
const cleanTags = async () => {
const tags = await sql("tags")
.leftJoin("tags_assign", "tags_assign.tag_id", "tags.id")
.whereNull("tags_assign.item_id");
/*const cleanTags = async () => {
const tags = await db`
select *
from "tags"
left join "tags_assign" on "tags_assign".tag_id = "tags".id
where "tags_assign".item_id is null
`;
if(tags.length === 0)
return 0;
@ -23,7 +26,7 @@ const cleanTags = async () => {
});
await deleteTag.del();
return dtags;
};
};*/
export default async bot => {
@ -49,7 +52,10 @@ export default async bot => {
case "limit":
return e.reply(`up to ${lib.formatSize(cfg.main.maxfilesize)} (${lib.formatSize(cfg.main.maxfilesize * cfg.main.adminmultiplier)} for admins)`);
case "thumb":
const rows = await sql("items").select("id");
const rows = await db`
select id
from "items"
`;
const dir = (await fs.readdir("./public/t")).filter(d => d.endsWith(".png")).map(e => +e.split(".")[0]);
const tmp = [];
for(let row of rows)
@ -69,10 +75,10 @@ export default async bot => {
e.reply("hay hay patron, hemen!");
exec("sudo systemctl restart f0ck");
break;
case "cleanTags":
/*case "cleanTags":
const tags = await cleanTags();
e.reply(tags + " tags removed");
break;
break;*/
case "clearTmp":
await Promise.all((await fs.readdir("./tmp")).filter(d => d !== ".empty").map(async d => fs.unlink(`./tmp/${d}`)));
e.reply("cleared lol");

View File

@ -1,5 +1,5 @@
import cfg from "../config.mjs";
import sql from "../sql.mjs";
import db from "../sql.mjs";
import lib from "../lib.mjs";
const regex = /(https\:\/\/f0ck\.me|http\:\/\/fockmoonsb24iczs7odozzy5uktlzbcgp337nabrgffzxv5ihabgpvyd\.onion)(\/(video|image|audio))?\/(\d+|(?:b\/)(\w{8})\.(jpg|webm|gif|mp4|png|mov|mp3|ogg|flac))/gi;
@ -12,14 +12,15 @@ export default async bot => {
active: true,
f: async e => {
const dat = e.message.match(regex)[0].split(/\//).pop();
let query = sql("items").select("id", "mime", "size", "username", "userchannel", "usernetwork", "stamp");
const rows = await db`
select id, mime, size, username, userchannel, usernetwork, stamp
from "items"
${ dat.includes('.')
? db`where dest ilike ${'%' + dat + '%'}`
: db`where id = ${dat}`
}
`;
if(dat.includes("."))
query = query.where("dest", "like", `%${dat}%`);
else
query = query.where("id", dat);
const rows = await query;
if(rows.length === 0)
return e.reply("no f0cks given! lol D:");

View File

@ -1,4 +1,4 @@
import sql from "../sql.mjs";
import db from "../sql.mjs";
import lib from "../lib.mjs";
import cfg from "../config.mjs";
@ -7,10 +7,10 @@ export default async bot => {
return [{
name: "f0ckrand",
call: /^gib f0ck/i,
active: true,
active: false,
f: async e => {
let args = e.args.slice(1);
let rows = sql("items").select("id", "username", "mime", "size");
/*let rows = sql("items").select("id", "username", "mime", "size");
for(let i = 0; i < args.length; i++) {
if(args[i].charAt(0) === "!")
@ -19,7 +19,7 @@ export default async bot => {
rows = rows.where("username", "ilike", args[i]);
}
rows = await rows.orderByRaw("random()").limit(1);
rows = await rows.orderByRaw("random()").limit(1);*/
if(rows.length === 0)
return e.reply("nothing found, f0cker");

View File

@ -1,5 +1,5 @@
import cfg from "../config.mjs";
import sql from "../sql.mjs";
import db from "../sql.mjs";
import lib from "../lib.mjs";
import { getLevel } from "../admin.mjs";
import fetch from "flumm-fetch-cookies";
@ -45,12 +45,18 @@ export default async bot => {
links.forEach(async link => {
// check repost (link)
const q_repost = await sql("items").select("id").where("src", link);
const q_repost = await db`
select id
from "items"
where src = ${link}
`;
if(q_repost.length > 0)
return e.reply(`repost motherf0cker (link): ${cfg.main.url}/${q_repost[0].id}`);
// generate uuid
const uuid = (await sql.select(sql.raw("gen_random_uuid() as uuid")))[0].uuid.substring(0, 8);
const uuid = (await db`
select gen_random_uuid() as uuid
`)[0].uuid.substring(0, 8);
const maxfilesize = (getLevel(e.user).level > 50 ? cfg.main.maxfilesize * cfg.main.adminmultiplier : cfg.main.maxfilesize) / 1024;
@ -108,26 +114,40 @@ export default async bot => {
}
// check repost (checksum)
const q_repostc = await sql("items").select("id").where("checksum", checksum);
const q_repostc = await db`
select id
from "items"
where checksum = ${checksum}
`;
if(q_repostc.length > 0)
return e.reply(`repost motherf0cker (checksum): ${cfg.main.url}/${q_repostc[0].id}`);
await fs.promises.copyFile(`./tmp/${filename}`, `./public/b/${filename}`);
await fs.promises.unlink(`./tmp/${filename}`).catch(_=>{});
await sql("items").insert({
src: e.photo ? "" : link,
dest: filename,
mime: mime,
size: size,
checksum: checksum,
username: e.user.nick || e.user.username,
userchannel: e.channel,
usernetwork: e.network,
stamp: ~~(new Date() / 1000),
active: 1
});
const itemid = (await sql('items').where('dest', filename).limit(1))[0].id;
await db`
insert into items ${
db({
src: e.photo ? "" : link,
dest: filename,
mime: mime,
size: size,
checksum: checksum,
username: e.user.nick || e.user.username,
userchannel: e.channel,
usernetwork: e.network,
stamp: ~~(new Date() / 1000),
active: 1
}, 'src', 'dest', 'mime', 'size', 'checksum', 'username', 'userchannel', 'usernetwork', 'stamp', 'active')
}
`;
const itemid = (await db`
select *
from "items"
where dest = ${filename}
limit 1
`)[0].id;
// generate thumbnail
try {
@ -184,11 +204,15 @@ export default async bot => {
}
if(tag === 'sfw' || tag === 'nsfw') {
await sql("tags_assign").insert({
tag_id: tag === "sfw" ? 1 : 2,
item_id: itemid,
user_id: 7 // user: autotagger (ID: 7)
});
await db`
insert into "tags_assign" ${
db({
tag_id: tag === "sfw" ? 1 : 2,
item_id: itemid,
user_id: 7 // user: autotagger (ID: 7)
}, 'tag_id', 'item_id', 'user_id')
}
`;
}
} catch(err) {
console.error(err);

View File

@ -1,12 +1,8 @@
import sql from "../sql.mjs";
import db from "../sql.mjs";
import cfg from "../config.mjs";
import lib from "../lib.mjs";
import { getLevel } from "../admin.mjs";
const getTags = async itemid => await sql("tags_assign")
.leftJoin("tags", "tags.id", "tags_assign.tag_id")
.where("tags_assign.item_id", itemid)
.select("tags.id", "tags.tag");
export default async bot => {
return [{
name: "tags show",
@ -17,7 +13,7 @@ export default async bot => {
const id = +e.args[1];
if(!id)
return e.reply("lol no");
const tags = (await getTags(id)).map(t => t.tag);
const tags = (await lib.getTags(id)).map(t => t.tag);
if(tags.length === 0)
return e.reply(`item ${cfg.main.url}/${id} has no tags!`);
return e.reply(`item ${cfg.main.url}/${id} is tagged as: ${tags.join(', ')}`);
@ -32,7 +28,7 @@ export default async bot => {
if(!id)
return e.reply("lol no");
const tags = (await getTags(id)).map(t => t.tag);
const tags = (await lib.getTags(id)).map(t => t.tag);
const newtags = (e.message.includes(",")
? e.args.splice(2).join(" ").trim().split(",")
@ -44,26 +40,38 @@ export default async bot => {
await Promise.all(newtags.map(async ntag => {
try {
let tagid;
const tag_exists = await sql("tags").select("id", "tag").where("tag", ntag);
const tag_exists = await db`
select id, tag
from "tags"
where tag = ${ntag}
`;
if(tag_exists.length === 0) { // create new tag
tagid = (await sql("tags").insert({
tag: ntag
}))[0];
tagid = (await db`
insert into "tags" ${
db({
tag: ntag
}, 'tag')
}
`)[0];
}
else {
tagid = tag_exists[0].id;
}
return await sql("tags_assign").insert({
tag_id: tagid,
item_id: id,
prefix: `${e.user.prefix}${e.channel}`
});
return await db`
insert into "tags_assign" ${
db({
tag_id: tagid,
item_id: id,
prefix: `${e.user.prefix}${e.channel}`
}, 'tag_id', 'item_id', 'prefix')
}
`;
} catch(err) {
console.error(err);
}
}));
const ntags = (await getTags(id)).map(t => t.tag);
const ntags = (await lib.getTags(id)).map(t => t.tag);
if(ntags.length === 0)
return e.reply(`item ${cfg.main.url}/${id} has no tags!`);
return e.reply(`item ${cfg.main.url}/${id} is now tagged as: ${ntags.join(', ')}`);
@ -78,7 +86,7 @@ export default async bot => {
if(!id)
return e.reply("lol no");
const tags = await getTags(id);
const tags = await lib.getTags(id);
const removetags = (e.message.includes(",")
? e.args.splice(2).join(" ").trim().split(",")
@ -96,13 +104,19 @@ export default async bot => {
msg: "tag is not assigned"
};
}
let q = sql("tags_assign").where("tag_id", tagid).andWhere("item_id", id).del();
if(getLevel(e.user).level < 50)
q = q.andWhere("prefix", `${e.user.prefix}${e.channel}`);
const q = await db`
delete from "tags_assign"
where tag_id = ${+tagid}
and item_id = ${+id}
${ getLevel(e.user.level < 50)
? db`and prefix = ${e.user.prefix + e.channel}`
: db``
}
`;
return {
success: !!(await q),
success: !!q,
tag: rtag,
tagid: tagid
};
@ -110,7 +124,7 @@ export default async bot => {
e.reply(JSON.stringify(res));
const ntags = (await getTags(id)).map(t => t.tag);
const ntags = (await lib.getTags(id)).map(t => t.tag);
if(ntags.length === 0)
return e.reply(`item ${cfg.main.url}/${id} has no tags!`);
return e.reply(`item ${cfg.main.url}/${id} is now tagged as: ${ntags.join(', ')}`);