feat/add-unlink-space-button #63

Open
Elliu wants to merge 10 commits from Elliu/out-of-your-element:feat/add-unlink-space-button into main
Showing only changes of commit 5aff6f9048 - Show all commits

View file

@ -12,6 +12,8 @@ const auth = sync.require("../auth")
const mreq = sync.require("../../matrix/mreq") const mreq = sync.require("../../matrix/mreq")
const {reg} = require("../../matrix/read-registration") const {reg} = require("../../matrix/read-registration")
const me = `@${reg.sender_localpart}:${reg.ooye.server_name}`
/** /**
* @param {H3Event} event * @param {H3Event} event
* @returns {import("../../matrix/api")} * @returns {import("../../matrix/api")}
@ -106,7 +108,10 @@ const schema = {
unlink: z.object({ unlink: z.object({
guild_id: z.string(), guild_id: z.string(),
channel_id: z.string() channel_id: z.string()
}) }),
unlinkSpace: z.object({
guild_id: z.string(),
}),
} }
as.router.post("/api/link-space", defineEventHandler(async event => { 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 // Check bridge has PL 100
const me = `@${reg.sender_localpart}:${reg.ooye.server_name}`
/** @type {Ty.Event.M_Power_Levels?} */ /** @type {Ty.Event.M_Power_Levels?} */
let powerLevelsStateContent = null let powerLevelsStateContent = null
try { try {
@ -255,3 +259,38 @@ as.router.post("/api/unlink", defineEventHandler(async event => {
setResponseHeader(event, "HX-Refresh", "true") setResponseHeader(event, "HX-Refresh", "true")
return null // 204 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) {

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

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:

  • leave the Discord guild as part of this operation
  • change the view that appears in that case (guild_access_denied.pug line 31) to present the easy-mode and self-service buttons directly here, and have the user go through those if they want to do setup for the guild again
  • set guild_active autocreate = 0, forcing it into self-service mode, so it knows it's ready to bridge but nothing gets autmatically re-created (I don't like this one)

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.

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: - leave the Discord guild as part of this operation - change the view that appears in that case (guild_access_denied.pug line 31) to present the easy-mode and self-service buttons directly here, and have the user go through those if they want to do setup for the guild again - set guild_active autocreate = 0, forcing it into self-service mode, so it knows it's ready to bridge but nothing gets autmatically re-created (I don't like this one) 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.

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 invite is 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

Ok, I just pushed doing a full clean of `guild_space`, `guild_active`, `invite`, and leaving the discord server. The clean of `invite` is 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
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)

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

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
}))