kutt/server/queries/link.js

192 lines
4.3 KiB
JavaScript
Raw Normal View History

2024-08-11 18:41:03 +03:30
const bcrypt = require("bcryptjs");
2020-01-11 17:40:25 +03:30
2024-08-11 18:41:03 +03:30
// FIXME: circular dependency
const CustomError = require("../utils").CustomError;
const redis = require("../redis");
const knex = require("../knex");
2020-01-11 17:40:25 +03:30
2020-01-30 18:51:52 +03:30
const selectable = [
"links.id",
"links.address",
"links.banned",
"links.created_at",
"links.domain_id",
"links.updated_at",
"links.password",
"links.description",
2020-08-04 20:32:23 +04:30
"links.expire_in",
2020-01-30 18:51:52 +03:30
"links.target",
"links.visit_count",
"links.user_id",
"links.uuid",
"domains.address as domain"
];
2024-08-11 18:41:03 +03:30
function normalizeMatch(match) {
2020-01-30 18:51:52 +03:30
const newMatch = { ...match };
if (newMatch.address) {
newMatch["links.address"] = newMatch.address;
delete newMatch.address;
}
if (newMatch.user_id) {
newMatch["links.user_id"] = newMatch.user_id;
delete newMatch.user_id;
}
if (newMatch.uuid) {
newMatch["links.uuid"] = newMatch.uuid;
delete newMatch.uuid;
}
return newMatch;
};
2024-08-11 18:41:03 +03:30
async function total(match, params) {
const query = knex("links");
2020-01-30 18:51:52 +03:30
Object.entries(match).forEach(([key, value]) => {
query.andWhere(key, ...(Array.isArray(value) ? value : [value]));
});
2024-08-11 18:41:03 +03:30
if (params?.search) {
query.andWhereRaw(
"links.description || ' ' || links.address || ' ' || target ILIKE '%' || ? || '%'",
[params.search]
);
2020-01-11 17:40:25 +03:30
}
2024-08-11 18:41:03 +03:30
2020-01-30 18:51:52 +03:30
const [{ count }] = await query.count("id");
2024-08-11 18:41:03 +03:30
2020-01-11 17:40:25 +03:30
return typeof count === "number" ? count : parseInt(count);
}
2024-08-11 18:41:03 +03:30
async function get(match, params) {
const query = knex("links")
2020-01-30 18:51:52 +03:30
.select(...selectable)
.where(normalizeMatch(match))
.offset(params.skip)
.limit(params.limit)
2020-01-11 17:40:25 +03:30
.orderBy("created_at", "desc");
2024-08-11 18:41:03 +03:30
if (params?.search) {
query.andWhereRaw(
2020-11-11 13:13:06 +03:30
"concat_ws(' ', description, links.address, target, domains.address) ILIKE '%' || ? || '%'",
[params.search]
);
2020-01-11 17:40:25 +03:30
}
2024-08-11 18:41:03 +03:30
2020-01-11 17:40:25 +03:30
query.leftJoin("domains", "links.domain_id", "domains.id");
2024-08-11 18:41:03 +03:30
const links = await query;
2020-01-11 17:40:25 +03:30
return links;
2024-08-11 18:41:03 +03:30
}
2020-01-11 17:40:25 +03:30
2024-08-11 18:41:03 +03:30
async function find(match) {
2020-01-30 18:51:52 +03:30
if (match.address && match.domain_id) {
const key = redis.key.link(match.address, match.domain_id);
2024-08-11 18:41:03 +03:30
const cachedLink = await redis.client.get(key);
2020-01-30 18:51:52 +03:30
if (cachedLink) return JSON.parse(cachedLink);
}
2024-08-11 18:41:03 +03:30
const link = await knex("links")
2020-01-30 18:51:52 +03:30
.select(...selectable)
.where(normalizeMatch(match))
.leftJoin("domains", "links.domain_id", "domains.id")
2020-01-11 17:40:25 +03:30
.first();
2024-08-11 18:41:03 +03:30
2020-01-11 17:40:25 +03:30
if (link) {
2020-01-30 18:51:52 +03:30
const key = redis.key.link(link.address, link.domain_id);
2024-08-11 18:41:03 +03:30
redis.client.set(key, JSON.stringify(link), "EX", 60 * 60 * 2);
2020-01-11 17:40:25 +03:30
}
2024-08-11 18:41:03 +03:30
2020-01-11 17:40:25 +03:30
return link;
}
2024-08-11 18:41:03 +03:30
async function create(params) {
let encryptedPassword = null;
2020-01-30 18:51:52 +03:30
if (params.password) {
2020-01-11 17:40:25 +03:30
const salt = await bcrypt.genSalt(12);
2020-01-30 18:51:52 +03:30
encryptedPassword = await bcrypt.hash(params.password, salt);
2020-01-11 17:40:25 +03:30
}
2024-08-11 18:41:03 +03:30
const [link] = await knex(
2020-01-30 18:51:52 +03:30
"links"
).insert(
2020-01-11 17:40:25 +03:30
{
password: encryptedPassword,
2020-01-30 18:51:52 +03:30
domain_id: params.domain_id || null,
user_id: params.user_id || null,
address: params.address,
description: params.description || null,
2020-08-04 20:32:23 +04:30
expire_in: params.expire_in || null,
2020-01-30 18:51:52 +03:30
target: params.target
2020-01-11 17:40:25 +03:30
},
"*"
);
2024-08-11 18:41:03 +03:30
2020-01-30 18:51:52 +03:30
return link;
2024-08-11 18:41:03 +03:30
}
2020-01-30 18:51:52 +03:30
2024-08-11 18:41:03 +03:30
async function remove(match) {
const link = await knex("links").where(match).first();
2020-01-30 18:51:52 +03:30
if (!link) {
throw new CustomError("Link was not found.");
}
2024-08-11 18:41:03 +03:30
const deletedLink = await knex("links").where("id", link.id).delete();
2020-01-30 18:51:52 +03:30
redis.remove.link(link);
2024-08-11 18:41:03 +03:30
2020-01-30 18:51:52 +03:30
return !!deletedLink;
2024-08-11 18:41:03 +03:30
}
2020-08-04 20:32:23 +04:30
2024-08-11 18:41:03 +03:30
async function batchRemove(match) {
const deleteQuery = knex("links");
const findQuery = knex("links");
2020-08-04 20:32:23 +04:30
Object.entries(match).forEach(([key, value]) => {
findQuery.andWhere(key, ...(Array.isArray(value) ? value : [value]));
deleteQuery.andWhere(key, ...(Array.isArray(value) ? value : [value]));
});
2024-08-11 18:41:03 +03:30
2020-08-04 20:32:23 +04:30
const links = await findQuery;
2024-08-11 18:41:03 +03:30
2020-08-04 20:32:23 +04:30
links.forEach(redis.remove.link);
2024-08-11 18:41:03 +03:30
2020-08-04 20:32:23 +04:30
await deleteQuery.delete();
2024-08-11 18:41:03 +03:30
}
2020-08-04 20:32:23 +04:30
2024-08-11 18:41:03 +03:30
async function update(match, update) {
2021-12-03 12:56:07 +11:00
if (update.password) {
const salt = await bcrypt.genSalt(12);
update.password = await bcrypt.hash(update.password, salt);
}
2024-08-11 18:41:03 +03:30
const links = await knex("links")
2020-01-30 18:51:52 +03:30
.where(match)
.update({ ...update, updated_at: new Date().toISOString() }, "*");
2024-08-11 18:41:03 +03:30
2020-01-30 18:51:52 +03:30
links.forEach(redis.remove.link);
2024-08-11 18:41:03 +03:30
2020-01-30 18:51:52 +03:30
return links;
2024-08-11 18:41:03 +03:30
}
2020-01-30 18:51:52 +03:30
2024-08-11 18:41:03 +03:30
function incrementVisit(match) {
return knex("links").where(match).increment("visit_count", 1);
}
module.exports = {
normalizeMatch,
batchRemove,
create,
find,
get,
incrementVisit,
remove,
total,
update,
}