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,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);