perf: cache staff roles

This commit is contained in:
Isaac 2023-01-12 17:20:21 +00:00
parent db94ab770d
commit d7e1b05586
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
3 changed files with 38 additions and 10 deletions

View File

@ -11,6 +11,29 @@ module.exports.getCommonGuilds = async (client, userId) => await client.guilds.c
return !!member;
});
/**
* @param {import("discord.js").Guild} guild
* @returns {Promise<string[]>}
*/
const updateStaffRoles = async guild => {
const { categories } = await guild.client.prisma.guild.findUnique({
select: { categories: { select: { staffRoles: true } } },
where: { id: guild.id },
});
const staffRoles = [
...new Set(
categories.reduce((acc, c) => {
acc.push(...c.staffRoles);
return acc;
}, []),
),
];
await guild.client.keyv.set(`cache/guild-staff:${guild.id}`, staffRoles);
return staffRoles;
};
module.exports.updateStaffRoles = updateStaffRoles;
/**
*
* @param {import("discord.js").Guild} guild
@ -20,12 +43,9 @@ module.exports.getCommonGuilds = async (client, userId) => await client.guilds.c
module.exports.isStaff = async (guild, userId) => {
/** @type {import("client")} */
const client = guild.client;
if (guild.client.supers.includes(userId)) return true;
const guildMember = await guild.members.fetch(userId);
if (guildMember?.permissions.has(PermissionsBitField.Flags.ManageGuild)) return true;
const { categories } = await client.prisma.guild.findUnique({
select: { categories: true },
where: { id: guild.id },
});
return categories.some(cat => cat.roles.some(r => guildMember.roles.cache.has(r)));
if (client.supers.includes(userId)) return true;
const guildMember = guild.members.cache.get(userId) || await guild.members.fetch(userId);
if (guildMember.permissions.has(PermissionsBitField.Flags.ManageGuild)) return true;
const staffRoles = await client.keyv.get(`cache/guild-staff:${guild.id}`) || await updateStaffRoles(guild);
return staffRoles.some(r => guildMember.roles.cache.has(r));
};

View File

@ -1,16 +1,19 @@
const { logAdminEvent } = require('../../../../../../../lib/logging');
const { updateStaffRoles } = require('../../../../../../../lib/users');
const { randomUUID } = require('crypto');
module.exports.delete = fastify => ({
handler: async (req, res) => {
/** @type {import('client')} */
const client = res.context.config.client;
const guildId = req.params.guild;
const guild = client.guilds.cache.get(req.params.guild);
const categoryId = Number(req.params.category);
const original = categoryId && await client.prisma.category.findUnique({ where: { id: categoryId } });
if (!original || original.guildId !== guildId) return res.status(404).send(new Error('Not Found'));
if (!original || original.guildId !== guild.id) return res.status(404).send(new Error('Not Found'));
const category = await client.prisma.category.delete({ where: { id: categoryId } });
await updateStaffRoles(guild);
logAdminEvent(client, {
action: 'delete',
guildId: req.params.guild,
@ -137,6 +140,8 @@ module.exports.patch = fastify => ({
where: { id: categoryId },
});
await updateStaffRoles(guild);
logAdminEvent(client, {
action: 'update',
diff: {

View File

@ -1,4 +1,5 @@
const { logAdminEvent } = require('../../../../../../lib/logging');
const { updateStaffRoles } = require('../../../../../../lib/users');
const emoji = require('node-emoji');
const { ChannelType: { GuildCategory } } = require('discord.js');
const ms = require('ms');
@ -93,6 +94,8 @@ module.exports.post = fastify => ({
},
});
await updateStaffRoles(guild);
logAdminEvent(client, {
action: 'create',
guildId: guild.id,