feat/add-unlink-space-button #63
1 changed files with 41 additions and 2 deletions
|
|
@ -12,6 +12,8 @@ const auth = sync.require("../auth")
|
|||
const mreq = sync.require("../../matrix/mreq")
|
||||
const {reg} = require("../../matrix/read-registration")
|
||||
|
||||
const me = `@${reg.sender_localpart}:${reg.ooye.server_name}`
|
||||
|
||||
/**
|
||||
* @param {H3Event} event
|
||||
* @returns {import("../../matrix/api")}
|
||||
|
|
@ -106,7 +108,10 @@ const schema = {
|
|||
unlink: z.object({
|
||||
guild_id: z.string(),
|
||||
channel_id: z.string()
|
||||
})
|
||||
}),
|
||||
unlinkSpace: z.object({
|
||||
guild_id: z.string(),
|
||||
}),
|
||||
}
|
||||
|
||||
as.router.post("/api/link-space", defineEventHandler(async event => {
|
||||
|
|
@ -140,7 +145,6 @@ as.router.post("/api/link-space", defineEventHandler(async event => {
|
|||
}
|
||||
|
||||
// Check bridge has PL 100
|
||||
const me = `@${reg.sender_localpart}:${reg.ooye.server_name}`
|
||||
/** @type {Ty.Event.M_Power_Levels?} */
|
||||
let powerLevelsStateContent = null
|
||||
try {
|
||||
|
|
@ -255,3 +259,38 @@ as.router.post("/api/unlink", defineEventHandler(async event => {
|
|||
setResponseHeader(event, "HX-Refresh", "true")
|
||||
return null // 204
|
||||
}))
|
||||
|
||||
as.router.post("/api/unlink-space", defineEventHandler(async event => {
|
||||
const {guild_id} = await readValidatedBody(event, schema.unlinkSpace.parse)
|
||||
const api = getAPI(event)
|
||||
await validateGuildAccess(event, guild_id)
|
||||
|
||||
const spaceID = select("guild_space", "space_id", {guild_id: guild_id}).pluck().get()
|
||||
if (!spaceID)
|
||||
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()
|
||||
|
||||
for (const channel of linkedChannels) {
|
||||
|
|
||||
await doRoomUnlink(event, channel.channel_id, guild_id)
|
||||
}
|
||||
|
||||
const remainingLinkedChannels = select("channel_room", ["channel_id", "room_id", "name", "nick"], {guild_id: guild_id}).all()
|
||||
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..."})
|
||||
|
||||
await api.setUserPower(spaceID, me, 0)
|
||||
await api.leaveRoom(spaceID)
|
||||
|
Elliu
commented
Also, this line causes issue when running the test. Also, this line causes issue when running the test.
I couldn't add the `snow` member like `createRoom` or `api` in the test to add the function / member definition because it wasn't defined in the type, not really sure what's the best way to fix it
|
||||
|
||||
db.prepare("DELETE FROM guild_space WHERE guild_id=? AND space_id=?").run(guild_id, spaceID)
|
||||
|
||||
// NOTE: not deleting from guild_active as this can lead to inconsistent state:
|
||||
// if we only delete from DB, the guild is still displayed on the top-right dropdown,
|
||||
// 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
|
||||
}))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue
Hi, there is this issue here, I'm not sure what would be the best thing to do with this situation, please let me know if you have any ideas of what should be best
Nice find, thanks for pointing it out. For further reading, the purpose and values of guild_active are explained here: https://gitdab.com/cadence/out-of-your-element/src/branch/main/docs/self-service-room-creation-rules.md
The message "Please add the bot to your server using the buttons on the home page" is deliberately designed to appear in this situation. However, the user experience here could be better.
There's a couple of possible paths forward here:
Because per the explainer doc, it's bad to leave around the a guild_active entry if you don't want the guild to be bridged. If Easy Mode was previously selected, it would cause channels to bridge themselves again just from anybody sending a message in the server. That would be pretty weird if somebody just clicked unbridge and it starts bridging again seemingly by itself! So we can't just do nothing here.
I'll let you pick which path forward you'd like to go with. I don't have any preference between the first two.
I note that you've coded it to always leave the Matrix space after this operation. I don't think it would be out of character for it to leave the Discord server as well.
Ok, I just pushed doing a full clean of
guild_space,guild_active,invite, and leaving the discord server.The clean of
inviteis not strictly necessary, but I you don't clean it, it might lead to buttons in the "select a space" from the self service mode later where the unbridged space is here but if you click it, you get an error because it cannot join it