diff --git a/src/i18n/en-GB.yml b/src/i18n/en-GB.yml index dc5993a..5df10cb 100644 --- a/src/i18n/en-GB.yml +++ b/src/i18n/en-GB.yml @@ -13,6 +13,7 @@ log: panel: 'a panel' question: 'a question' settings: 'the settings' + tag: 'a tag' title: joined: '{targetType} {verb}' target: @@ -20,6 +21,7 @@ log: panel: 'Panel' question: 'Question' settings: 'Settings' + tag: 'Tag' verb: create: 'created' delete: 'deleted' diff --git a/src/routes/api/admin/guilds/[guild]/tags/[tag].js b/src/routes/api/admin/guilds/[guild]/tags/[tag].js new file mode 100644 index 0000000..6edea35 --- /dev/null +++ b/src/routes/api/admin/guilds/[guild]/tags/[tag].js @@ -0,0 +1,42 @@ +const { logAdminEvent } = require('../../../../../../lib/logging'); + +module.exports.delete = fastify => ({ + handler: async (req, res) => { + /** @type {import('client')} */ + const client = res.context.config.client; + const guildId = req.params.guild; + const tagId = req.params.tag; + const original = tagId && await client.prisma.tag.findUnique({ where: { id: tagId } }); + if (original.guildId !== guildId) return res.status(404).send(new Error('Not Found')); + const tag = await client.prisma.tag.delete({ where: { id: tagId } }); + + logAdminEvent(client, { + action: 'delete', + guildId: req.params.guild, + target: { + id: tag.id, + name: tag.name, + type: 'tag', + }, + userId: req.user.payload.id, + }); + + return tag; + }, + onRequest: [fastify.authenticate, fastify.isAdmin], +}); + +module.exports.get = fastify => ({ + handler: async (req, res) => { + /** @type {import('client')} */ + const client = res.context.config.client; + const guildId = req.params.guild; + const tagId = Number(req.params.tag); + const tag = await client.prisma.tag.findUnique({ where: { id: tagId } }); + + if (!tag || tag.guildId !== guildId) return res.status(404).send(new Error('Not Found')); + + return tag; + }, + onRequest: [fastify.authenticate, fastify.isAdmin], +}); \ No newline at end of file diff --git a/src/routes/api/admin/guilds/[guild]/tags/index.js b/src/routes/api/admin/guilds/[guild]/tags/index.js new file mode 100644 index 0000000..73f31d4 --- /dev/null +++ b/src/routes/api/admin/guilds/[guild]/tags/index.js @@ -0,0 +1,46 @@ +const { logAdminEvent } = require('../../../../../../lib/logging'); + +module.exports.get = fastify => ({ + handler: async (req, res) => { + /** @type {import('client')} */ + const client = res.context.config.client; + + const { tags } = await client.prisma.guild.findUnique({ + select: { tags: true }, + where: { id: req.params.guild }, + }); + + return tags; + }, + onRequest: [fastify.authenticate, fastify.isAdmin], +}); + + +module.exports.post = fastify => ({ + handler: async (req, res) => { + /** @type {import('client')} */ + const client = res.context.config.client; + const guild = client.guilds.cache.get(req.params.guild); + const data = req.body; + const tag = await client.prisma.tag.create({ + data: { + guild: { connect: { id: guild.id } }, + ...data, + }, + }); + + logAdminEvent(client, { + action: 'create', + guildId: guild.id, + target: { + id: tag.id, + name: tag.name, + type: 'tag', + }, + userId: req.user.payload.id, + }); + + return tag; + }, + onRequest: [fastify.authenticate, fastify.isAdmin], +}); \ No newline at end of file diff --git a/src/routes/auth/logout.js b/src/routes/auth/logout.js new file mode 100644 index 0000000..9a0a6c9 --- /dev/null +++ b/src/routes/auth/logout.js @@ -0,0 +1,7 @@ +module.exports.get = () => ({ + handler: async function (req, res) { // must NOT use arrow function syntax + res + .clearCookie('token', '/') + .send('Logged out.'); + }, +}); \ No newline at end of file diff --git a/user/example.config.yml b/user/example.config.yml index d443b6e..3519d93 100644 --- a/user/example.config.yml +++ b/user/example.config.yml @@ -19,4 +19,6 @@ logs: directory: ./logs enabled: true keepFor: 30 # days - level: info \ No newline at end of file + level: info +overrides: + disableArchives: false \ No newline at end of file