Compare commits

..

3 commits

2 changed files with 10 additions and 27 deletions

View file

@ -1,6 +0,0 @@
[*]
indent_style = tab
[*.pug]
indent_style = space
indent_size = 2

View file

@ -41,41 +41,29 @@ 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"})
}
/**
* @param {H3Event} event
* @param {string} guild_id
* @returns {Promise<DiscordTypes.APIGuild & {members: DiscordTypes.APIGuildMember[]}>}
*/
function validateBotIsInGuild(guild) {
if (!guild)
throw createError({status: 400, message: "Bad Request", data: "Discord guild does not exist or bot has not joined it"})
}
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)
if (!guild)
throw createError({status: 400, message: "Bad Request", data: "Discord guild does not exist or bot has not joined it"})
validateBotIsInGuild(guild)
return guild
}
/**
* @param {H3Event} event
* @param {string} channel_id
* @param {string} guild_id
*/
async function doRoomUnlink(event, channel_id, guild_id) {
const createRoom = getCreateRoom(event)
async function doRoomUnlink(createRoom, channel_id, guild_id) {
// Check that the channel (if it exists) is part of this guild
/** @type {any} */
let channel = discord.channels.get(channel_id)
@ -170,7 +158,7 @@ as.router.post("/api/link", defineEventHandler(async event => {
const createSpace = getCreateSpace(event)
const guildID = parsedBody.guild_id
const guild = await validateGuildAccess(event, guildID)
guild = await validateGuildAccess(event, guildID)
const spaceID = await createSpace.ensureSpace(guild)
// Check channel exists
@ -234,9 +222,10 @@ 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(event, channel_id, guild_id)
await doRoomUnlink(createRoom, channel_id, guild_id)
setResponseHeader(event, "HX-Refresh", "true")
return null // 204