feat/add-unlink-space-button #63

Manually merged
cadence merged 12 commits from Elliu/out-of-your-element:feat/add-unlink-space-button into main 2026-02-13 06:32:34 +00:00
Showing only changes of commit f4c1ea7c7f - Show all commits

View file

@ -266,31 +266,38 @@ as.router.post("/api/unlink-space", defineEventHandler(async event => {
await validateGuildAccess(event, guild_id) await validateGuildAccess(event, guild_id)
const spaceID = select("guild_space", "space_id", {guild_id: guild_id}).pluck().get() const spaceID = select("guild_space", "space_id", {guild_id: guild_id}).pluck().get()
if (!spaceID) if (!spaceID) {
throw createError({status: 400, message: "Bad Request", data: "Matrix space does not exist or bot has not linked it"}) throw createError({status: 400, message: "Bad Request", data: "Matrix space does not exist or bot has not linked it"})
}
const linkedChannels = select("channel_room", ["channel_id", "room_id", "name", "nick"], {guild_id: guild_id}).all() const linkedChannels = select("channel_room", ["channel_id", "room_id", "name", "nick"], {guild_id: guild_id}).all()
for (const channel of linkedChannels) { for (const channel of linkedChannels) {
await doRoomUnlink(event, channel.channel_id, guild_id) await doRoomUnlink(event, channel.channel_id, guild_id)
// FIXME: probably fix the underlying issue instead:
// If not waiting for ~1s, then the room is half unbridged:
// the resources in the room is not properly cleaned up, meaning that the sim users
// and the bridge user are not power demoted nor leave the room
// The entry from the channel_room table is not deleted
// After that, writing in the discord channel does nothing,
// and writing in the matrix channel spawns an error for not finding guild_id
await new Promise(r => setTimeout(r, 5000));
} }
const remainingLinkedChannels = select("channel_room", ["channel_id", "room_id", "name", "nick"], {guild_id: guild_id}).all() const remainingLinkedChannels = select("channel_room", ["channel_id", "room_id", "name", "nick"], {guild_id: guild_id}).all()
if (remainingLinkedChannels.length !== 0) if (remainingLinkedChannels.length !== 0) {
throw createError({status: 500, message: "Internal Server Error", data: "Some linked room still exists after trying to unlink all of them. Aborting the space unlinking..."}) throw createError({status: 500, message: "Internal Server Error", data: "Some linked room still exists after trying to unlink all of them. Aborting the space unlinking..."})
}
await api.setUserPower(spaceID, me, 0) await api.setUserPower(spaceID, me, 0)
await api.leaveRoom(spaceID) await api.leaveRoom(spaceID)
db.prepare("DELETE FROM guild_space WHERE guild_id=? AND space_id=?").run(guild_id, spaceID) db.prepare("DELETE FROM guild_space WHERE guild_id=? AND space_id=?").run(guild_id, spaceID)
db.prepare("DELETE FROM guild_active WHERE guild_id=?").run(guild_id)
await discord.snow.user.leaveGuild(guild_id)
db.prepare("DELETE FROM invite WHERE room_id=?").run(spaceID)
// NOTE: not deleting from guild_active as this can lead to inconsistent state: setResponseHeader(event, "HX-Redirect", "/")
// if we only delete from DB, the guild is still displayed on the top-right dropdown, return null
// but when selected we get the "Please add the bot to your server using the buttons on the home page." page
//
// So either keep as-is, or delete from guild_active, but also leave the discord guild? Not sure if we want that or not
// db.prepare("DELETE FROM guild_active WHERE guild_id=?").run(guild_id)
setResponseHeader(event, "HX-Refresh", "true")
return null // 204
})) }))