This commit is contained in:
Isaac
2022-07-15 23:19:42 +01:00
parent 145514a86b
commit 97623f3203
15 changed files with 193 additions and 57 deletions

View File

@@ -1,6 +1,6 @@
module.exports.get = fastify => ({
handler: async (req, res) => {
/** @type {import('../../../../../../client')} */
/** @type {import('client')} */
const client = res.context.config.client;
const categories = await client.prisma.guild.findUnique({ where: { id: req.params.guild } }).categories();
@@ -12,7 +12,7 @@ module.exports.get = fastify => ({
module.exports.post = fastify => ({
handler: async (req, res) => {
/** @type {import('../../../../../../client')} */
/** @type {import('client')} */
const client = res.context.config.client;
const user = await client.users.fetch(req.user.payload.id);

View File

@@ -0,0 +1,13 @@
module.exports.get = fastify => ({
handler: async (req, res) => {
/** @type {import('client')} */
const client = res.context.config.client;
const id = req.params.guild;
const guild = client.guilds.cache.get(id) ?? {};
const { query } = req.query;
if (!query) return {};
const data = query.split(/\./g).reduce((acc, part) => acc && acc[part], guild);
return data;
},
onRequest: [fastify.authenticate, fastify.isAdmin],
});

View File

@@ -1,40 +1,53 @@
module.exports.delete = fastify => ({
handler: async (req, res) => {
/** @type {import('../../../../../client')} */
const client = res.context.config.client;
await client.prisma.guild.delete({ where: { id: req.params.guild } });
const settings = await client.prisma.guild.create({ data: { id: req.params.guild } });
res.send(settings);
},
onRequest: [fastify.authenticate, fastify.isAdmin],
});
/* eslint-disable no-underscore-dangle */
const ms = require('ms');
module.exports.get = fastify => ({
handler: async (req, res) => {
/** @type {import('../../../../../client')} */
/** @type {import("client")} */
const client = res.context.config.client;
const id = req.params.guild;
const cacheKey = `cache/stats/guild:${id}`;
let cached = await client.keyv.get(cacheKey);
const settings = await client.prisma.guild.findUnique({ where: { id: req.params.guild } }) ??
await client.prisma.guild.create({ data: { id: req.params.guild } });
if (!cached) {
const guild = client.guilds.cache.get(id);
const settings = await client.prisma.guild.findUnique({ where: { id } }) ??
await client.prisma.guild.create({ data: { id } });
const categories = await client.prisma.category.findMany({
select: {
_count: { select: { tickets: true } },
id: true,
name: true,
},
where: { guildId: id },
});
const tickets = await client.prisma.ticket.findMany({
select: {
createdAt: true,
firstResponseAt: true,
},
where: { guildId: id },
});
cached = {
createdAt: settings.createdAt,
id: guild.id,
logo: guild.iconURL(),
name: guild.name,
stats: {
avgResponseTime: ms(tickets.reduce((total, ticket) => total + (ticket.firstResponseAt - ticket.createdAt), 0) ?? 1 / tickets.length),
categories: categories.map(c => ({
id: c.id,
name: c.name,
tickets: c._count.tickets,
})),
tags: await client.prisma.tag.count({ where: { guildId: id } }),
tickets: tickets.length,
},
};
await client.keyv.set(cacheKey, cached, ms('5m'));
}
res.send(settings);
},
onRequest: [fastify.authenticate, fastify.isAdmin],
});
module.exports.patch = fastify => ({
handler: async (req, res) => {
/** @type {import('../../../../../client')} */
const client = res.context.config.client;
const settings = await client.prisma.guild.update({
data: req.body,
where: { id: req.params.guild },
});
res.send(settings);
return cached;
},
onRequest: [fastify.authenticate, fastify.isAdmin],
});

View File

@@ -0,0 +1,40 @@
module.exports.delete = fastify => ({
handler: async (req, res) => {
/** @type {import('client')} */
const client = res.context.config.client;
const id = req.params.guild;
await client.prisma.guild.delete({ where: { id } });
const settings = await client.prisma.guild.create({ data: { id } });
return settings;
},
onRequest: [fastify.authenticate, fastify.isAdmin],
});
module.exports.get = fastify => ({
handler: async (req, res) => {
/** @type {import('client')} */
const client = res.context.config.client;
const id = req.params.guild;
const settings = await client.prisma.guild.findUnique({ where: { id } }) ??
await client.prisma.guild.create({ data: { id } });
return settings;
},
onRequest: [fastify.authenticate, fastify.isAdmin],
});
module.exports.patch = fastify => ({
handler: async (req, res) => {
/** @type {import('client')} */
const client = res.context.config.client;
const id = req.params.guild;
const settings = await client.prisma.guild.update({
data: req.body,
where: { id },
});
return settings;
},
onRequest: [fastify.authenticate, fastify.isAdmin],
});

View File

@@ -4,6 +4,7 @@ module.exports.get = fastify => ({
const guilds = client.guilds.cache
.filter(async guild => {
const member = await guild.members.fetch(req.user.payload.id);
if (!member) return false;
return member.permissions.has('MANAGE_GUILD');
})
.map(guild => ({

39
src/routes/api/client.js Normal file
View File

@@ -0,0 +1,39 @@
const ms = require('ms');
module.exports.get = () => ({
handler: async (req, res) => {
/** @type {import("client")} */
const client = res.context.config.client;
const cacheKey = 'cache/stats/client';
let cached = await client.keyv.get(cacheKey);
if (!cached) {
const tickets = await client.prisma.ticket.findMany({
select: {
createdAt: true,
firstResponseAt: true,
},
});
const users = await client.prisma.user.findMany({ select: { messageCount: true } });
cached = {
avatar: client.user.avatarURL(),
discriminator: client.user.discriminator,
id: client.user.id,
stats: {
activatedUsers: users.length,
archivedMessages: users.reduce((total, user) => total + user.messageCount, 0), // don't count archivedMessage table rows, they get deleted
avgResponseTime: ms(tickets.reduce((total, ticket) => total + (ticket.firstResponseAt - ticket.createdAt), 0) ?? 1 / tickets.length),
categories: await client.prisma.category.count(),
guilds: client.guilds.cache.size,
members: client.guilds.cache.reduce((t, g) => t + g.memberCount, 0),
tags: await client.prisma.tag.count(),
tickets: tickets.length,
},
username: client.user.username,
};
await client.keyv.set(cacheKey, cached, ms('5m'));
}
return cached;
},
});