diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..089c28f8 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,6 @@ +[*] +indent_style = tab + +[*.pug] +indent_style = space +indent_size = 2 diff --git a/src/web/routes/link.js b/src/web/routes/link.js index bde1499d..930a6601 100644 --- a/src/web/routes/link.js +++ b/src/web/routes/link.js @@ -41,29 +41,41 @@ function getCreateSpace(event) { return event.context.createSpace || sync.require("../../d2m/actions/create-space") } +/** + * @param {H3Event} event + * @param {string} guild_id + */ async function validateUserHaveRightsOnGuild(event, guild_id) { const managed = await auth.getManagedGuilds(event) if (!managed.has(guild_id)) throw createError({status: 403, message: "Forbidden", data: "Can't edit a guild you don't have Manage Server permissions in"}) } -function validateBotIsInGuild(guild) { - if (!guild) - throw createError({status: 400, message: "Bad Request", data: "Discord guild does not exist or bot has not joined it"}) -} - +/** + * @param {H3Event} event + * @param {string} guild_id + * @returns {Promise} + */ async function validateGuildAccess(event, guild_id) { // Check guild ID or nonce await validateUserHaveRightsOnGuild(event, guild_id) // Check guild exists const guild = discord.guilds.get(guild_id) - validateBotIsInGuild(guild) + if (!guild) + throw createError({status: 400, message: "Bad Request", data: "Discord guild does not exist or bot has not joined it"}) return guild } -async function doRoomUnlink(createRoom, channel_id, guild_id) { +/** + * @param {H3Event} event + * @param {string} channel_id + * @param {string} guild_id + */ +async function doRoomUnlink(event, channel_id, guild_id) { + const createRoom = getCreateRoom(event) + // Check that the channel (if it exists) is part of this guild /** @type {any} */ let channel = discord.channels.get(channel_id) @@ -158,7 +170,7 @@ as.router.post("/api/link", defineEventHandler(async event => { const createSpace = getCreateSpace(event) const guildID = parsedBody.guild_id - guild = await validateGuildAccess(event, guildID) + const guild = await validateGuildAccess(event, guildID) const spaceID = await createSpace.ensureSpace(guild) // Check channel exists @@ -222,10 +234,9 @@ as.router.post("/api/link", defineEventHandler(async event => { as.router.post("/api/unlink", defineEventHandler(async event => { const {channel_id, guild_id} = await readValidatedBody(event, schema.unlink.parse) - const createRoom = getCreateRoom(event) await validateGuildAccess(event, guild_id) - await doRoomUnlink(createRoom, channel_id, guild_id) + await doRoomUnlink(event, channel_id, guild_id) setResponseHeader(event, "HX-Refresh", "true") return null // 204