perf: threads everywhere! (for encryption & decryption)

This commit is contained in:
Isaac 2025-02-12 04:26:15 +00:00
parent 5a908e77a7
commit d99cb202d5
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
16 changed files with 529 additions and 424 deletions

View File

@ -1,11 +1,10 @@
/* eslint-disable no-underscore-dangle */ /* eslint-disable no-underscore-dangle */
const { Autocompleter } = require('@eartharoid/dbf'); const { Autocompleter } = require('@eartharoid/dbf');
const emoji = require('node-emoji'); const emoji = require('node-emoji');
const Cryptr = require('cryptr');
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
const Keyv = require('keyv'); const Keyv = require('keyv');
const ms = require('ms'); const ms = require('ms');
const { isStaff } = require('../lib/users'); const { isStaff } = require('../lib/users');
const { reusable } = require('../lib/threads');
module.exports = class TicketCompleter extends Autocompleter { module.exports = class TicketCompleter extends Autocompleter {
constructor(client, options) { constructor(client, options) {
@ -30,6 +29,7 @@ module.exports = class TicketCompleter extends Autocompleter {
let tickets = await this.cache.get(cacheKey); let tickets = await this.cache.get(cacheKey);
if (!tickets) { if (!tickets) {
const cmd = client.commands.commands.slash.get('transcript');
const { locale } = await client.prisma.guild.findUnique({ const { locale } = await client.prisma.guild.findUnique({
select: { locale: true }, select: { locale: true },
where: { id: guildId }, where: { id: guildId },
@ -42,15 +42,25 @@ module.exports = class TicketCompleter extends Autocompleter {
open, open,
}, },
}); });
tickets = tickets
.filter(ticket => client.commands.commands.slash.get('transcript').shouldAllowAccess(interaction, ticket)) const worker = await reusable('crypto');
.map(ticket => { try {
tickets = await Promise.all(
tickets
.filter(ticket => cmd.shouldAllowAccess(interaction, ticket))
.map(async ticket => {
const getTopic = async () => (await worker.decrypt(ticket.topic)).replace(/\n/g, ' ').substring(0, 50);
const date = new Date(ticket.createdAt).toLocaleString([locale, 'en-GB'], { dateStyle: 'short' }); const date = new Date(ticket.createdAt).toLocaleString([locale, 'en-GB'], { dateStyle: 'short' });
const topic = ticket.topic ? '- ' + decrypt(ticket.topic).replace(/\n/g, ' ').substring(0, 50) : ''; const topic = ticket.topic ? '- ' + (await getTopic()) : '';
const category = emoji.hasEmoji(ticket.category.emoji) ? emoji.get(ticket.category.emoji) + ' ' + ticket.category.name : ticket.category.name; const category = emoji.hasEmoji(ticket.category.emoji) ? emoji.get(ticket.category.emoji) + ' ' + ticket.category.name : ticket.category.name;
ticket._name = `${category} #${ticket.number} (${date}) ${topic}`; ticket._name = `${category} #${ticket.number} (${date}) ${topic}`;
return ticket; return ticket;
}); }),
);
} finally {
await worker.terminate();
}
this.cache.set(cacheKey, tickets, ms('1m')); this.cache.set(cacheKey, tickets, ms('1m'));
} }

View File

@ -7,9 +7,8 @@ const {
TextInputBuilder, TextInputBuilder,
TextInputStyle, TextInputStyle,
} = require('discord.js'); } = require('discord.js');
const { reusable } = require('../lib/threads');
const emoji = require('node-emoji'); const emoji = require('node-emoji');
const Cryptr = require('cryptr');
const cryptr = new Cryptr(process.env.ENCRYPTION_KEY);
module.exports = class EditButton extends Button { module.exports = class EditButton extends Button {
constructor(client, options) { constructor(client, options) {
@ -35,6 +34,9 @@ module.exports = class EditButton extends Button {
const getMessage = client.i18n.getLocale(ticket.guild.locale); const getMessage = client.i18n.getLocale(ticket.guild.locale);
const worker = await reusable('crypto');
try {
if (ticket.questionAnswers.length === 0) { if (ticket.questionAnswers.length === 0) {
const field = new TextInputBuilder() const field = new TextInputBuilder()
.setCustomId('topic') .setCustomId('topic')
@ -44,7 +46,7 @@ module.exports = class EditButton extends Button {
.setMinLength(5) .setMinLength(5)
.setPlaceholder(getMessage('modals.topic.placeholder')) .setPlaceholder(getMessage('modals.topic.placeholder'))
.setRequired(true); .setRequired(true);
if (ticket.topic) field.setValue(cryptr.decrypt(ticket.topic)); if (ticket.topic) field.setValue(await worker.decrypt(ticket.topic));
await interaction.showModal( await interaction.showModal(
new ModalBuilder() new ModalBuilder()
.setCustomId(JSON.stringify({ .setCustomId(JSON.stringify({
@ -66,9 +68,10 @@ module.exports = class EditButton extends Button {
})) }))
.setTitle(ticket.category.name) .setTitle(ticket.category.name)
.setComponents( .setComponents(
await Promise.all(
ticket.questionAnswers ticket.questionAnswers
.filter(a => a.question.type === 'TEXT') // TODO: remove this when modals support select menus .filter(a => a.question.type === 'TEXT') // TODO: remove this when modals support select menus
.map(a => { .map(async a => {
if (a.question.type === 'TEXT') { if (a.question.type === 'TEXT') {
const field = new TextInputBuilder() const field = new TextInputBuilder()
.setCustomId(String(a.id)) .setCustomId(String(a.id))
@ -78,7 +81,7 @@ module.exports = class EditButton extends Button {
.setMinLength(a.question.minLength) .setMinLength(a.question.minLength)
.setPlaceholder(a.question.placeholder) .setPlaceholder(a.question.placeholder)
.setRequired(a.question.required); .setRequired(a.question.required);
if (a.value) field.setValue(cryptr.decrypt(a.value)); if (a.value) field.setValue(await worker.decrypt(a.value));
else if (a.question.value) field.setValue(a.question.value); else if (a.question.value) field.setValue(a.question.value);
return new ActionRowBuilder().setComponents(field); return new ActionRowBuilder().setComponents(field);
} else if (a.question.type === 'MENU') { } else if (a.question.type === 'MENU') {
@ -95,7 +98,11 @@ module.exports = class EditButton extends Button {
.setValue(String(i)) .setValue(String(i))
.setLabel(o.label); .setLabel(o.label);
if (o.description) builder.setDescription(o.description); if (o.description) builder.setDescription(o.description);
if (o.emoji) builder.setEmoji(emoji.hasEmoji(o.emoji) ? emoji.get(o.emoji) : { id: o.emoji }); if (o.emoji) {
builder.setEmoji(emoji.hasEmoji(o.emoji)
? emoji.get(o.emoji)
: { id: o.emoji });
}
return builder; return builder;
}), }),
), ),
@ -103,7 +110,11 @@ module.exports = class EditButton extends Button {
} }
}), }),
), ),
),
); );
} }
} finally {
await worker.terminate();
}
} }
}; };

View File

@ -5,8 +5,7 @@ const {
} = require('discord.js'); } = require('discord.js');
const { isStaff } = require('../../lib/users'); const { isStaff } = require('../../lib/users');
const ExtendedEmbedBuilder = require('../../lib/embed'); const ExtendedEmbedBuilder = require('../../lib/embed');
const Cryptr = require('cryptr'); const { reusable } = require('../../lib/threads');
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
module.exports = class TicketsSlashCommand extends SlashCommand { module.exports = class TicketsSlashCommand extends SlashCommand {
constructor(client, options) { constructor(client, options) {
@ -116,13 +115,18 @@ module.exports = class TicketsSlashCommand extends SlashCommand {
}, },
}); });
const worker = await reusable('crypto');
try {
if (open.length >= 1) { if (open.length >= 1) {
fields.push({ fields.push({
name: getMessage('commands.slash.tickets.response.fields.open.name'), name: getMessage('commands.slash.tickets.response.fields.open.name'),
value: open.map(ticket => { value: (await Promise.all(
const topic = ticket.topic ? `- \`${decrypt(ticket.topic).replace(/\n/g, ' ').slice(0, 30)}\`` : ''; open.map(async ticket => {
const getTopic = async () => (await worker.decrypt(ticket.topic)).replace(/\n/g, ' ').substring(0, 30);
const topic = ticket.topic ? `- \`${await getTopic()}\`` : '';
return `> <#${ticket.id}> ${topic}`; return `> <#${ticket.id}> ${topic}`;
}).join('\n'), }),
)).join('\n'),
}); });
} }
@ -138,12 +142,18 @@ module.exports = class TicketsSlashCommand extends SlashCommand {
} else { } else {
fields.push({ fields.push({
name: getMessage('commands.slash.tickets.response.fields.closed.name'), name: getMessage('commands.slash.tickets.response.fields.closed.name'),
value: closed.map(ticket => { value: (await Promise.all(
const topic = ticket.topic ? `- \`${decrypt(ticket.topic).replace(/\n/g, ' ').slice(0, 30)}\`` : ''; closed.map(async ticket => {
const getTopic = async () => (await worker.decrypt(ticket.topic)).replace(/\n/g, ' ').substring(0, 30);
const topic = ticket.topic ? `- \`${await getTopic()}\`` : '';
return `> ${ticket.category.name} #${ticket.number} (\`${ticket.id}\`) ${topic}`; return `> ${ticket.category.name} #${ticket.number} (\`${ticket.id}\`) ${topic}`;
}).join('\n'), }),
)).join('\n'),
}); });
} }
} finally {
await worker.terminate();
}
// TODO: add portal URL to view all (this list is limited to the last 10) // TODO: add portal URL to view all (this list is limited to the last 10)
const embed = new ExtendedEmbedBuilder({ const embed = new ExtendedEmbedBuilder({

View File

@ -5,9 +5,8 @@ const {
TextInputBuilder, TextInputBuilder,
TextInputStyle, TextInputStyle,
} = require('discord.js'); } = require('discord.js');
const Cryptr = require('cryptr');
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
const ExtendedEmbedBuilder = require('../../lib/embed'); const ExtendedEmbedBuilder = require('../../lib/embed');
const { quick } = require('../../lib/threads');
module.exports = class TopicSlashCommand extends SlashCommand { module.exports = class TopicSlashCommand extends SlashCommand {
constructor(client, options) { constructor(client, options) {
@ -66,7 +65,8 @@ module.exports = class TopicSlashCommand extends SlashCommand {
.setPlaceholder(getMessage('modals.topic.placeholder')) .setPlaceholder(getMessage('modals.topic.placeholder'))
.setRequired(true); .setRequired(true);
if (ticket.topic) field.setValue(decrypt(ticket.topic)); // why can't discord.js accept null or undefined :( // why can't discord.js accept null or undefined :(
if (ticket.topic) field.setValue(await quick('crypto', w => w.decrypt(ticket.topic)));
await interaction.showModal( await interaction.showModal(
new ModalBuilder() new ModalBuilder()

View File

@ -7,9 +7,8 @@ const fs = require('fs');
const { join } = require('path'); const { join } = require('path');
const Mustache = require('mustache'); const Mustache = require('mustache');
const { AttachmentBuilder } = require('discord.js'); const { AttachmentBuilder } = require('discord.js');
const Cryptr = require('cryptr');
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
const ExtendedEmbedBuilder = require('../../lib/embed'); const ExtendedEmbedBuilder = require('../../lib/embed');
const { quick } = require('../../lib/threads');
module.exports = class TranscriptSlashCommand extends SlashCommand { module.exports = class TranscriptSlashCommand extends SlashCommand {
constructor(client, options) { constructor(client, options) {
@ -61,31 +60,9 @@ module.exports = class TranscriptSlashCommand extends SlashCommand {
/** @type {import("client")} */ /** @type {import("client")} */
const client = this.client; const client = this.client;
ticket.claimedBy = ticket.archivedUsers.find(u => u.userId === ticket.claimedById); // TODO: use a pool of multiple threads
ticket.closedBy = ticket.archivedUsers.find(u => u.userId === ticket.closedById); // this is still slow for lots of messages
ticket.createdBy = ticket.archivedUsers.find(u => u.userId === ticket.createdById); ticket = await quick('transcript', w => w(ticket));
if (ticket.closedReason) ticket.closedReason = decrypt(ticket.closedReason);
if (ticket.feedback?.comment) ticket.feedback.comment = decrypt(ticket.feedback.comment);
if (ticket.topic) ticket.topic = decrypt(ticket.topic).replace(/\n/g, '\n\t');
ticket.archivedUsers.forEach((user, i) => {
if (user.displayName) user.displayName = decrypt(user.displayName);
user.username = decrypt(user.username);
ticket.archivedUsers[i] = user;
});
ticket.archivedMessages.forEach((message, i) => {
message.author = ticket.archivedUsers.find(u => u.userId === message.authorId);
message.content = JSON.parse(decrypt(message.content));
message.text = message.content.content?.replace(/\n/g, '\n\t') ?? '';
message.content.attachments?.forEach(a => (message.text += '\n\t' + a.url));
message.content.embeds?.forEach(() => (message.text += '\n\t[embedded content]'));
message.number = 'M' + String(i + 1).padStart(ticket.archivedMessages.length.toString().length, '0');
ticket.archivedMessages[i] = message;
});
ticket.pinnedMessageIds = ticket.pinnedMessageIds.map(id => ticket.archivedMessages.find(message => message.id === id)?.number);
const channelName = ticket.category.channelName const channelName = ticket.category.channelName
.replace(/{+\s?(user)?name\s?}+/gi, ticket.createdBy?.username) .replace(/{+\s?(user)?name\s?}+/gi, ticket.createdBy?.username)

View File

@ -3,8 +3,8 @@ const {
ApplicationCommandOptionType, ApplicationCommandOptionType,
EmbedBuilder, EmbedBuilder,
} = require('discord.js'); } = require('discord.js');
const Cryptr = require('cryptr'); const { quick } = require('../../lib/threads');
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
module.exports = class TransferSlashCommand extends SlashCommand { module.exports = class TransferSlashCommand extends SlashCommand {
constructor(client, options) { constructor(client, options) {
@ -71,7 +71,7 @@ module.exports = class TransferSlashCommand extends SlashCommand {
}), }),
interaction.channel.edit({ interaction.channel.edit({
name: channelName, name: channelName,
topic: `${member.toString()}${ticket.topic?.length > 0 ? ` | ${decrypt(ticket.topic)}` : ''}`, topic: `${member.toString()}${ticket.topic && ` | ${await quick('crypto', w => w.decrypt(ticket.topic))}`}`,
}), }),
interaction.channel.permissionOverwrites.edit( interaction.channel.permissionOverwrites.edit(
member, member,

View File

@ -39,7 +39,7 @@ async function sendToHouston(client) {
activated_users: users._count, activated_users: users._count,
arch: process.arch, arch: process.arch,
database: process.env.DB_PROVIDER, database: process.env.DB_PROVIDER,
guilds: await relativePool(0.25, 'stats', pool => Promise.all( guilds: await relativePool(.25, 'stats', pool => Promise.all(
guilds guilds
.filter(guild => client.guilds.cache.has(guild.id)) .filter(guild => client.guilds.cache.has(guild.id))
.map(guild => { .map(guild => {

View File

@ -8,13 +8,13 @@ const { cpus } = require('node:os');
/** /**
* Use a thread pool of a fixed size * Use a thread pool of a fixed size
* @param {number} size number of threads
* @param {string} name name of file in workers directory * @param {string} name name of file in workers directory
* @param {function} fun async function * @param {function} fun async function
* @param {import('threads/dist/master/pool').PoolOptions} options
* @returns {Promise<any>} * @returns {Promise<any>}
*/ */
async function pool(size, name, fun) { async function pool(name, fun, options) {
const pool = Pool(() => spawn(new Worker(`./workers/${name}.js`)), { size }); const pool = Pool(() => spawn(new Worker(`./workers/${name}.js`)), options);
try { try {
return await fun(pool); return await fun(pool);
} finally { } finally {
@ -40,19 +40,35 @@ async function quick(name, fun) {
/** /**
* Use a thread pool of a variable size * Use a thread pool of a variable size
* @param {number} size fraction of available CPU cores to use (ceil'd) * @param {number} fraction fraction of available CPU cores to use (ceil'd)
* @param {string} name name of file in workers directory * @param {string} name name of file in workers directory
* @param {function} fun async function * @param {function} fun async function
* @param {import('threads/dist/master/pool').PoolOptions} options
* @returns {Promise<any>} * @returns {Promise<any>}
*/ */
function relativePool(fraction, ...args) { function relativePool(fraction, name, fun, options) {
// ! ceiL: at least 1 // ! ceiL: at least 1
const poolSize = Math.ceil(fraction * cpus().length); const size = Math.ceil(fraction * cpus().length);
return pool(poolSize, ...args); return pool(name, fun, {
...options,
size,
});
} }
/**
* Spawn one thread
* @param {string} name name of file in workers directory
* @returns {Promise<{terminate: function}>}
*/
async function reusable(name) {
const thread = await spawn(new Worker(`./workers/${name}.js`));
thread.terminate = () => Thread.terminate(thread);
return thread;
};
module.exports = { module.exports = {
pool, pool,
quick, quick,
relativePool, relativePool,
reusable,
}; };

View File

@ -1,5 +1,5 @@
const Cryptr = require('cryptr'); const { reusable } = require('../threads');
const { encrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
/** /**
* Returns highest (roles.highest) hoisted role, or everyone * Returns highest (roles.highest) hoisted role, or everyone
@ -71,16 +71,18 @@ module.exports = class TicketArchiver {
}); });
} }
const worker = await reusable('crypto');
try {
for (const member of members) { for (const member of members) {
const data = { const data = {
avatar: member.avatar || member.user.avatar, // TODO: save avatar in user/avatars/ avatar: member.avatar || member.user.avatar, // TODO: save avatar in user/avatars/
bot: member.user.bot, bot: member.user.bot,
discriminator: member.user.discriminator, discriminator: member.user.discriminator,
displayName: member.displayName ? encrypt(member.displayName) : null, displayName: member.displayName ? await worker.encrypt(member.displayName) : null,
roleId: !!member && hoistedRole(member).id, roleId: !!member && hoistedRole(member).id,
ticketId, ticketId,
userId: member.user.id, userId: member.user.id,
username: encrypt(member.user.username), username: await worker.encrypt(member.user.username),
}; };
await this.client.prisma.archivedUser.upsert({ await this.client.prisma.archivedUser.upsert({
create: data, create: data,
@ -106,7 +108,7 @@ module.exports = class TicketArchiver {
}, },
}, },
}, },
content: encrypt( content: await worker.encrypt(
JSON.stringify({ JSON.stringify({
attachments: [...message.attachments.values()], attachments: [...message.attachments.values()],
components: [...message.components.values()], components: [...message.components.values()],
@ -151,5 +153,8 @@ module.exports = class TicketArchiver {
}, },
where: { id: ticketId }, where: { id: ticketId },
}); });
} finally {
await worker.terminate();
}
} }
}; };

View File

@ -19,13 +19,13 @@ const { logTicketEvent } = require('../logging');
const { isStaff } = require('../users'); const { isStaff } = require('../users');
const { Collection } = require('discord.js'); const { Collection } = require('discord.js');
const spacetime = require('spacetime'); const spacetime = require('spacetime');
const Cryptr = require('cryptr');
const {
decrypt,
encrypt,
} = new Cryptr(process.env.ENCRYPTION_KEY);
const { getSUID } = require('../logging'); const { getSUID } = require('../logging');
const { getAverageTimes } = require('../stats'); const { getAverageTimes } = require('../stats');
const {
quick,
reusable,
} = require('../threads');
/** /**
* @typedef {import('@prisma/client').Category & * @typedef {import('@prisma/client').Category &
@ -148,7 +148,9 @@ module.exports = class TicketManager {
/** /**
* @param {object} data * @param {object} data
* @param {string} data.categoryId * @param {string} data.categoryId
* @param {import("discord.js").ChatInputCommandInteraction|import("discord.js").ButtonInteraction|import("discord.js").SelectMenuInteraction} data.interaction * @param {import("discord.js").ChatInputCommandInteraction
* | import("discord.js").ButtonInteraction
* | import("discord.js").SelectMenuInteraction} data.interaction
* @param {string?} [data.topic] * @param {string?} [data.topic]
*/ */
async create({ async create({
@ -353,7 +355,9 @@ module.exports = class TicketManager {
/** /**
* @param {object} data * @param {object} data
* @param {string} data.category * @param {string} data.category
* @param {import("discord.js").ButtonInteraction|import("discord.js").SelectMenuInteraction|import("discord.js").ModalSubmitInteraction} data.interaction * @param {import("discord.js").ButtonInteraction
* | import("discord.js").SelectMenuInteraction
* | import("discord.js").ModalSubmitInteraction} data.interaction
* @param {string?} [data.topic] * @param {string?} [data.topic]
*/ */
async postQuestions({ async postQuestions({
@ -367,11 +371,22 @@ module.exports = class TicketManager {
let answers; let answers;
if (interaction.isModalSubmit()) { if (interaction.isModalSubmit()) {
if (action === 'questions') { if (action === 'questions') {
answers = category.questions.filter(q => q.type === 'TEXT').map(q => ({ const worker = await reusable('crypto');
try {
answers = await Promise.all(
category.questions
.filter(q => q.type === 'TEXT')
.map(async q => ({
questionId: q.id, questionId: q.id,
userId: interaction.user.id, userId: interaction.user.id,
value: interaction.fields.getTextInputValue(q.id) ? encrypt(interaction.fields.getTextInputValue(q.id)) : '', value: interaction.fields.getTextInputValue(q.id)
})); ? await worker.encrypt(interaction.fields.getTextInputValue(q.id))
: '', // TODO: maybe this should be null?
})),
);
} finally {
await worker.terminate();
}
if (category.customTopic) topic = interaction.fields.getTextInputValue(category.customTopic); if (category.customTopic) topic = interaction.fields.getTextInputValue(category.customTopic);
} else if (action === 'topic') { } else if (action === 'topic') {
topic = interaction.fields.getTextInputValue('topic'); topic = interaction.fields.getTextInputValue('topic');
@ -612,7 +627,7 @@ module.exports = class TicketManager {
embed.addFields({ embed.addFields({
inline: false, inline: false,
name: getMessage('ticket.references_ticket.fields.topic'), name: getMessage('ticket.references_ticket.fields.topic'),
value: decrypt(ticket.topic), value: await quick('crypto', worker => worker.decrypt(ticket.topic)),
}); });
} }
await channel.send({ embeds: [embed] }); await channel.send({ embeds: [embed] });
@ -631,7 +646,7 @@ module.exports = class TicketManager {
id: channel.id, id: channel.id,
number, number,
openingMessageId: sent.id, openingMessageId: sent.id,
topic: topic ? encrypt(topic) : null, topic: topic ? await quick('crypto', worker => worker.encrypt(topic)) : null,
}; };
if (referencesTicketId) data.referencesTicket = { connect: { id: referencesTicketId } }; if (referencesTicketId) data.referencesTicket = { connect: { id: referencesTicketId } };
if (answers) data.questionAnswers = { createMany: { data: answers } }; if (answers) data.questionAnswers = { createMany: { data: answers } };
@ -1073,7 +1088,9 @@ module.exports = class TicketManager {
} }
/** /**
* @param {import("discord.js").ChatInputCommandInteraction|import("discord.js").ButtonInteraction|import("discord.js").ModalSubmitInteraction} interaction * @param {import("discord.js").ChatInputCommandInteraction
* | import("discord.js").ButtonInteraction
* | import("discord.js").ModalSubmitInteraction} interaction
* @param {string} reason * @param {string} reason
*/ */
async requestClose(interaction, reason) { async requestClose(interaction, reason) {
@ -1143,7 +1160,9 @@ module.exports = class TicketManager {
} }
/** /**
* @param {import("discord.js").ChatInputCommandInteraction|import("discord.js").ButtonInteraction|import("discord.js").ModalSubmitInteraction} interaction * @param {import("discord.js").ChatInputCommandInteraction
* | import("discord.js").ButtonInteraction
* | import("discord.js").ModalSubmitInteraction} interaction
*/ */
async acceptClose(interaction) { async acceptClose(interaction) {
const ticket = await this.getTicket(interaction.channel.id); const ticket = await this.getTicket(interaction.channel.id);
@ -1191,7 +1210,7 @@ module.exports = class TicketManager {
where: { id: closedBy }, where: { id: closedBy },
}, },
} || undefined, // Prisma wants undefined not null because it is a relation } || undefined, // Prisma wants undefined not null because it is a relation
closedReason: reason && encrypt(reason), closedReason: reason && await quick('crypto', worker => worker.encrypt(reason)),
messageCount: archivedMessages, messageCount: archivedMessages,
open: false, open: false,
}; };
@ -1248,7 +1267,7 @@ module.exports = class TicketManager {
embed.addFields({ embed.addFields({
inline: true, inline: true,
name: getMessage('dm.closed.fields.topic'), name: getMessage('dm.closed.fields.topic'),
value: decrypt(ticket.topic), value: await quick('crypto', worker => worker.decrypt(ticket.topic)),
}); });
} }

11
src/lib/workers/crypto.js Normal file
View File

@ -0,0 +1,11 @@
const { expose } = require('threads/worker');
const Cryptr = require('cryptr');
const {
encrypt,
decrypt,
} = new Cryptr(process.env.ENCRYPTION_KEY);
expose({
decrypt,
encrypt,
});

View File

@ -0,0 +1,36 @@
const { expose } = require('threads/worker');
const Cryptr = require('cryptr');
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
function getTranscript(ticket) {
ticket.claimedBy = ticket.archivedUsers.find(u => u.userId === ticket.claimedById);
ticket.closedBy = ticket.archivedUsers.find(u => u.userId === ticket.closedById);
ticket.createdBy = ticket.archivedUsers.find(u => u.userId === ticket.createdById);
if (ticket.closedReason) ticket.closedReason = decrypt(ticket.closedReason);
if (ticket.feedback?.comment) ticket.feedback.comment = decrypt(ticket.feedback.comment);
if (ticket.topic) ticket.topic = decrypt(ticket.topic).replace(/\n/g, '\n\t');
ticket.archivedUsers.forEach((user, i) => {
if (user.displayName) user.displayName = decrypt(user.displayName);
user.username = decrypt(user.username);
ticket.archivedUsers[i] = user;
});
ticket.archivedMessages.forEach((message, i) => {
message.author = ticket.archivedUsers.find(u => u.userId === message.authorId);
message.content = JSON.parse(decrypt(message.content));
message.text = message.content.content?.replace(/\n/g, '\n\t') ?? '';
message.content.attachments?.forEach(a => (message.text += '\n\t' + a.url));
message.content.embeds?.forEach(() => (message.text += '\n\t[embedded content]'));
message.number = 'M' + String(i + 1).padStart(ticket.archivedMessages.length.toString().length, '0');
ticket.archivedMessages[i] = message;
});
ticket.pinnedMessageIds = ticket.pinnedMessageIds.map(id => ticket.archivedMessages.find(message => message.id === id)?.number);
return ticket;
}
expose(getTranscript);

View File

@ -1,8 +1,7 @@
const { Listener } = require('@eartharoid/dbf'); const { Listener } = require('@eartharoid/dbf');
const { AuditLogEvent } = require('discord.js'); const { AuditLogEvent } = require('discord.js');
const { logMessageEvent } = require('../../lib/logging'); const { logMessageEvent } = require('../../lib/logging');
const Cryptr = require('cryptr'); const { quick } = require('../../lib/threads');
const { decrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
module.exports = class extends Listener { module.exports = class extends Listener {
constructor(client, options) { constructor(client, options) {
@ -38,8 +37,11 @@ module.exports = class extends Listener {
if (ticket.guild.archive) { if (ticket.guild.archive) {
try { try {
const archived = await client.prisma.archivedMessage.findUnique({ where: { id: message.id } }); const archived = await client.prisma.archivedMessage.findUnique({ where: { id: message.id } });
if (archived) { if (archived?.content) {
if (!content) content = JSON.parse(decrypt(archived.content)).content; // won't be cleaned if (!content) {
const string = await quick('crypto', worker => worker.decrypt(archived.content));
content = JSON.parse(string).content; // won't be cleaned
}
await client.prisma.archivedMessage.update({ await client.prisma.archivedMessage.update({
data: { deleted: true }, data: { deleted: true },
where: { id: message.id }, where: { id: message.id },

View File

@ -1,7 +1,6 @@
const { Modal } = require('@eartharoid/dbf'); const { Modal } = require('@eartharoid/dbf');
const ExtendedEmbedBuilder = require('../lib/embed'); const ExtendedEmbedBuilder = require('../lib/embed');
const Cryptr = require('cryptr'); const { quick } = require('../lib/threads');
const { encrypt } = new Cryptr(process.env.ENCRYPTION_KEY);
module.exports = class FeedbackModal extends Modal { module.exports = class FeedbackModal extends Modal {
constructor(client, options) { constructor(client, options) {
@ -26,7 +25,7 @@ module.exports = class FeedbackModal extends Modal {
rating = Math.min(Math.max(rating, 1), 5); // clamp between 1 and 5 (0 and null become 1, 6 becomes 5) rating = Math.min(Math.max(rating, 1), 5); // clamp between 1 and 5 (0 and null become 1, 6 becomes 5)
const data = { const data = {
comment: comment?.length > 0 ? encrypt(comment) : null, comment: comment?.length > 0 ? await quick('crypto', worker => worker.encrypt(comment)) : null,
guild: { connect: { id: interaction.guild.id } }, guild: { connect: { id: interaction.guild.id } },
rating, rating,
user: { connect: { id: interaction.user.id } }, user: { connect: { id: interaction.user.id } },

View File

@ -2,11 +2,8 @@ const { Modal } = require('@eartharoid/dbf');
const { EmbedBuilder } = require('discord.js'); const { EmbedBuilder } = require('discord.js');
const ExtendedEmbedBuilder = require('../lib/embed'); const ExtendedEmbedBuilder = require('../lib/embed');
const { logTicketEvent } = require('../lib/logging'); const { logTicketEvent } = require('../lib/logging');
const Cryptr = require('cryptr'); const { reusable } = require('../lib/threads');
const {
encrypt,
decrypt,
} = new Cryptr(process.env.ENCRYPTION_KEY);
module.exports = class QuestionsModal extends Modal { module.exports = class QuestionsModal extends Modal {
constructor(client, options) { constructor(client, options) {
@ -26,6 +23,8 @@ module.exports = class QuestionsModal extends Modal {
const client = this.client; const client = this.client;
if (id.edit) { if (id.edit) {
const worker = await reusable('crypto');
try {
await interaction.deferReply({ ephemeral: true }); await interaction.deferReply({ ephemeral: true });
const { category } = await client.prisma.ticket.findUnique({ const { category } = await client.prisma.ticket.findUnique({
@ -60,12 +59,15 @@ module.exports = class QuestionsModal extends Modal {
const ticket = await client.prisma.ticket.update({ const ticket = await client.prisma.ticket.update({
data: { data: {
questionAnswers: { questionAnswers: {
update: interaction.fields.fields.map(f => ({ update: await Promise.all(
data: { value: f.value ? encrypt(f.value) : '' }, interaction.fields.fields
.map(async f => ({
data: { value: f.value ? await worker.encrypt(f.value) : '' },
where: { id: Number(f.customId) }, where: { id: Number(f.customId) },
})), })),
),
}, },
topic: topic ? encrypt(topic) : null, topic: topic ? await worker.encrypt(topic) : null,
}, },
select, select,
where: { id: interaction.channel.id }, where: { id: interaction.channel.id },
@ -79,11 +81,13 @@ module.exports = class QuestionsModal extends Modal {
const embeds = [...opening.embeds]; const embeds = [...opening.embeds];
embeds[1] = new EmbedBuilder(embeds[1].data) embeds[1] = new EmbedBuilder(embeds[1].data)
.setFields( .setFields(
await Promise.all(
ticket.questionAnswers ticket.questionAnswers
.map(a => ({ .map(async a => ({
name: a.question.label, name: a.question.label,
value: a.value ? decrypt(a.value) : getMessage('ticket.answers.no_value'), value: a.value ? await worker.decrypt(a.value) : getMessage('ticket.answers.no_value'),
})), })),
),
); );
await opening.edit({ embeds }); await opening.edit({ embeds });
} }
@ -101,19 +105,19 @@ module.exports = class QuestionsModal extends Modal {
}); });
/** @param {ticket} ticket */ /** @param {ticket} ticket */
const makeDiff = ticket => { const makeDiff = async ticket => {
const diff = {}; const diff = {};
ticket.questionAnswers.forEach(a => { for (const a of ticket.questionAnswers) {
diff[a.question.label] = a.value ? decrypt(a.value) : getMessage('ticket.answers.no_value'); diff[a.question.label] = a.value ? await worker.decrypt(a.value) : getMessage('ticket.answers.no_value');
}); }
return diff; return diff;
}; };
logTicketEvent(this.client, { logTicketEvent(this.client, {
action: 'update', action: 'update',
diff: { diff: {
original: makeDiff(original), original: await makeDiff(original),
updated: makeDiff(ticket), updated: await makeDiff(ticket),
}, },
target: { target: {
id: ticket.id, id: ticket.id,
@ -121,6 +125,9 @@ module.exports = class QuestionsModal extends Modal {
}, },
userId: interaction.user.id, userId: interaction.user.id,
}); });
} finally {
await worker.terminate();
}
} else { } else {
await this.client.tickets.postQuestions({ await this.client.tickets.postQuestions({
...id, ...id,

View File

@ -2,11 +2,7 @@ const { Modal } = require('@eartharoid/dbf');
const { EmbedBuilder } = require('discord.js'); const { EmbedBuilder } = require('discord.js');
const ExtendedEmbedBuilder = require('../lib/embed'); const ExtendedEmbedBuilder = require('../lib/embed');
const { logTicketEvent } = require('../lib/logging'); const { logTicketEvent } = require('../lib/logging');
const Cryptr = require('cryptr'); const { reusable } = require('../lib/threads');
const {
encrypt,
decrypt,
} = new Cryptr(process.env.ENCRYPTION_KEY);
module.exports = class TopicModal extends Modal { module.exports = class TopicModal extends Modal {
constructor(client, options) { constructor(client, options) {
@ -21,6 +17,8 @@ module.exports = class TopicModal extends Modal {
const client = this.client; const client = this.client;
if (id.edit) { if (id.edit) {
const worker = await reusable('crypto');
try {
await interaction.deferReply({ ephemeral: true }); await interaction.deferReply({ ephemeral: true });
const topic = interaction.fields.getTextInputValue('topic'); const topic = interaction.fields.getTextInputValue('topic');
const select = { const select = {
@ -41,7 +39,7 @@ module.exports = class TopicModal extends Modal {
where: { id: interaction.channel.id }, where: { id: interaction.channel.id },
}); });
const ticket = await client.prisma.ticket.update({ const ticket = await client.prisma.ticket.update({
data: { topic: topic ? encrypt(topic) : null }, data: { topic: topic ? await worker.encrypt(topic) : null },
select, select,
where: { id: interaction.channel.id }, where: { id: interaction.channel.id },
}); });
@ -73,17 +71,17 @@ module.exports = class TopicModal extends Modal {
}); });
/** @param {ticket} ticket */ /** @param {ticket} ticket */
const makeDiff = ticket => { const makeDiff = async ticket => {
const diff = {}; const diff = {};
diff[getMessage('ticket.opening_message.fields.topic')] = ticket.topic ? decrypt(ticket.topic) : ' '; diff[getMessage('ticket.opening_message.fields.topic')] = ticket.topic ? await worker.decrypt(ticket.topic) : ' ';
return diff; return diff;
}; };
logTicketEvent(this.client, { logTicketEvent(this.client, {
action: 'update', action: 'update',
diff: { diff: {
original: makeDiff(original), original: await makeDiff(original),
updated: makeDiff(ticket), updated: await makeDiff(ticket),
}, },
target: { target: {
id: ticket.id, id: ticket.id,
@ -91,6 +89,10 @@ module.exports = class TopicModal extends Modal {
}, },
userId: interaction.user.id, userId: interaction.user.id,
}); });
} finally {
await worker.terminate();
}
} else { } else {
await this.client.tickets.postQuestions({ await this.client.tickets.postQuestions({
...id, ...id,