category subspaces #113

Open
mschae wants to merge 9 commits from mschae/out-of-your-element:category-subspaces into main
6 changed files with 44 additions and 22 deletions
Showing only changes of commit d1a6251dcc - Show all commits

correctly fix state when unbridging category (untested)

mschae23 2026-11-22 15:58:27 +01:00
Signed by untrusted user: mschae
GPG key ID: A87721D300DE88AB

View file

@ -505,12 +505,16 @@ function syncRoom(channelID) {
* @param {{id: string, topic?: string?}} channel channel-ish (just needs an id, topic is optional) * @param {{id: string, topic?: string?}} channel channel-ish (just needs an id, topic is optional)
* @param {string} guildID * @param {string} guildID
* @param {string} messageBeforeLeave * @param {string} messageBeforeLeave
* @param {boolean} all whether all rooms are being unbridged (if not, an effort should be made to fix other rooms
* depending on this one, like a category is being unbridged)
*/ */
async function unbridgeChannel(channel, guildID, messageBeforeLeave = "This room was removed from the bridge.") { async function unbridgeChannel(channel, guildID, all, messageBeforeLeave = "This room was removed from the bridge.") {
const roomID = select("channel_room", "room_id", {channel_id: channel.id}).pluck().get() const channelRoomRow = select("channel_room", ["room_id", "parent_space_id"], {channel_id: channel.id}).get()
assert.ok(roomID) assert.ok(channelRoomRow)
const roomID = channelRoomRow.room_id
const row = from("guild_space").join("guild_active", "guild_id").select("space_id", "autocreate").where({guild_id: guildID}).get() const row = from("guild_space").join("guild_active", "guild_id").select("space_id", "autocreate").where({guild_id: guildID}).get()
assert.ok(row) assert.ok(row)
const parentSpaceID = channelRoomRow.parent_space_id ?? row.space_id
let botInRoom = true let botInRoom = true
@ -537,6 +541,27 @@ async function unbridgeChannel(channel, guildID, messageBeforeLeave = "This room
db.prepare("DELETE FROM webhook WHERE channel_id = ?").run(channel.id) db.prepare("DELETE FROM webhook WHERE channel_id = ?").run(channel.id)
} }
// if it is an easy mode room, clean up the room from the managed space and make it clear it's not being bridged
// (don't do this for self-service rooms, because they might continue to be used on Matrix or linked somewhere else later)
if (row.autocreate === 1) {
// remove room from being a space member
await api.sendState(roomID, "m.space.parent", parentSpaceID, {})
await api.sendState(parentSpaceID, "m.space.child", roomID, {})
if (!all && roomID !== row.space_id) {
const childrenRoomIDs = select("channel_room", "room_id", {parent_space_id: roomID}).pluck().all()
for (const childrenRoomID of childrenRoomIDs) {
const parentState = await api.getStateEvent(childrenRoomID, "m.space.parent", roomID) ?? {via: [reg.ooye.server_name]}
const childState = await api.getStateEvent(roomID, "m.space.child", childrenRoomID) ?? {via: [reg.ooye.server_name]}
delete childState["order"]
await api.sendState(childrenRoomID, "m.space.parent", roomID, {})
await api.sendState(childrenRoomID, "m.space.parent", roomID, parentState)
await api.sendState(parentSpaceID, "m.space.child", childrenRoomID, childState)
}
}
}
// delete room from database // delete room from database
db.prepare("DELETE FROM member_cache WHERE room_id = ?").run(roomID) db.prepare("DELETE FROM member_cache WHERE room_id = ?").run(roomID)
db.prepare("DELETE FROM channel_room WHERE room_id = ? AND channel_id = ?").run(roomID, channel.id) // cascades to most other tables, like messages and historical rooms db.prepare("DELETE FROM channel_room WHERE room_id = ? AND channel_id = ?").run(roomID, channel.id) // cascades to most other tables, like messages and historical rooms
@ -568,14 +593,6 @@ async function unbridgeChannel(channel, guildID, messageBeforeLeave = "This room
body: `⚠️ ${messageBeforeLeave}` body: `⚠️ ${messageBeforeLeave}`
}) })
// if it is an easy mode room, clean up the room from the managed space and make it clear it's not being bridged
// (don't do this for self-service rooms, because they might continue to be used on Matrix or linked somewhere else later)
if (row.autocreate === 1) {
// remove room from being a space member
await api.sendState(roomID, "m.space.parent", row.space_id, {})
await api.sendState(row.space_id, "m.space.child", roomID, {})
}
// if it is a self-service room, remove sim members // if it is a self-service room, remove sim members
// (the room can be used with less clutter and the member list makes sense if it's bridged somewhere else) // (the room can be used with less clutter and the member list makes sense if it's bridged somewhere else)
if (row.autocreate === 0) { if (row.autocreate === 0) {

View file

@ -208,7 +208,7 @@ async function syncSpaceFully(guildID) {
if (discord.channels.has(channelID)) { if (discord.channels.has(channelID)) {
await createRoom.syncRoom(channelID) await createRoom.syncRoom(channelID)
} else { } else {
await createRoom.unbridgeChannel({id: channelID}, guildID) await createRoom.unbridgeChannel({id: channelID}, guildID, false)
} }
} }

View file

@ -288,7 +288,7 @@ module.exports = {
if (!guildID) return // channel must have been a DM channel or something if (!guildID) return // channel must have been a DM channel or something
const roomID = select("channel_room", "room_id", {channel_id: channel.id}).pluck().get() const roomID = select("channel_room", "room_id", {channel_id: channel.id}).pluck().get()
if (!roomID) return // channel wasn't being bridged in the first place if (!roomID) return // channel wasn't being bridged in the first place
await createRoom.unbridgeChannel(channel, guildID) await createRoom.unbridgeChannel(channel, guildID, false)
}, },
/** /**

View file

@ -483,7 +483,7 @@ async event => {
if (!channelID) return if (!channelID) return
const channel = discord.channels.get(channelID) const channel = discord.channels.get(channelID)
if (!channel) return if (!channel) return
await createRoom.unbridgeChannel(channel, channel["guild_id"], "Encrypted rooms are not supported. This room was removed from the bridge.") await createRoom.unbridgeChannel(channel, channel["guild_id"], false, "Encrypted rooms are not supported. This room was removed from the bridge.")
})) }))
sync.addTemporaryListener(as, "ephemeral_type:m.typing", guard("m.typing", sync.addTemporaryListener(as, "ephemeral_type:m.typing", guard("m.typing",

View file

@ -74,8 +74,9 @@ const schema = {
* @param {H3Event} event * @param {H3Event} event
* @param {string} channel_id * @param {string} channel_id
* @param {string} guild_id * @param {string} guild_id
* @param {boolean} all
*/ */
async function validateAndUnbridgeChannel(event, channel_id, guild_id) { async function validateAndUnbridgeChannel(event, channel_id, guild_id, all) {
const createRoom = getCreateRoom(event) const createRoom = getCreateRoom(event)
// Check channel is currently bridged // Check channel is currently bridged
@ -94,7 +95,7 @@ async function validateAndUnbridgeChannel(event, channel_id, guild_id) {
} }
// Do it // Do it
await createRoom.unbridgeChannel(channel, guild_id) await createRoom.unbridgeChannel(channel, guild_id, all)
} }
as.router.post("/api/link-space", defineEventHandler(async event => { as.router.post("/api/link-space", defineEventHandler(async event => {
@ -274,7 +275,7 @@ as.router.post("/api/unlink", defineEventHandler(async event => {
const guild = discord.guilds.get(guild_id) 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"}) if (!guild) throw createError({status: 400, message: "Bad Request", data: "Discord guild does not exist or bot has not joined it"})
await validateAndUnbridgeChannel(event, channel_id, guild_id) await validateAndUnbridgeChannel(event, channel_id, guild_id, false)
setResponseHeader(event, "HX-Refresh", "true") setResponseHeader(event, "HX-Refresh", "true")
return null // 204 return null // 204
@ -305,7 +306,7 @@ as.router.post("/api/unlink-space", defineEventHandler(async event => {
// Unlink all rooms // Unlink all rooms
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 validateAndUnbridgeChannel(event, channel.channel_id, guild_id) await validateAndUnbridgeChannel(event, channel.channel_id, guild_id, true)
} }
// Verify all rooms were unlinked // Verify all rooms were unlinked

View file

@ -666,9 +666,10 @@ test("web unlink room: successfully calls unbridgeChannel when the channel does
guild_id: "665289423482519565" guild_id: "665289423482519565"
}, },
createRoom: { createRoom: {
async unbridgeChannel(channel) { async unbridgeChannel(channel, guildID, all) {
called++ called++
t.equal(channel.id, "665310973967597573") t.equal(channel.id, "665310973967597573")
t.equal(all, false)
} }
} }
}) })
@ -686,9 +687,10 @@ test("web unlink room: successfully calls unbridgeChannel when the channel does
guild_id: "112760669178241024" guild_id: "112760669178241024"
}, },
createRoom: { createRoom: {
async unbridgeChannel(channel) { async unbridgeChannel(channel, guildID, all) {
called++ called++
t.equal(channel.id, "489237891895768942") t.equal(channel.id, "489237891895768942")
t.equal(all, false)
} }
} }
}) })
@ -766,10 +768,11 @@ test("web unlink space: correctly abort unlinking if some linked channels remain
guild_id: "665289423482519565", guild_id: "665289423482519565",
}, },
createRoom: { createRoom: {
async unbridgeChannel(channel, guildID) { async unbridgeChannel(channel, guildID, all) {
unbridgedChannel = true unbridgedChannel = true
t.ok(["1438284564815548418", "665310973967597573"].includes(channel.id)) t.ok(["1438284564815548418", "665310973967597573"].includes(channel.id))
t.equal(guildID, "665289423482519565") t.equal(guildID, "665289423482519565")
t.equal(all, true)
// Do not actually delete the link from DB, should trigger error later in check // Do not actually delete the link from DB, should trigger error later in check
} }
}, },
@ -811,10 +814,11 @@ test("web unlink space: successfully calls unbridgeChannel on linked channels in
guild_id: "665289423482519565", guild_id: "665289423482519565",
}, },
createRoom: { createRoom: {
async unbridgeChannel(channel, guildID) { async unbridgeChannel(channel, guildID, all) {
unbridgedChannel = true unbridgedChannel = true
t.ok(["1438284564815548418", "665310973967597573"].includes(channel.id)) t.ok(["1438284564815548418", "665310973967597573"].includes(channel.id))
t.equal(guildID, "665289423482519565") t.equal(guildID, "665289423482519565")
t.equal(all, true)
// In order to not simulate channel deletion and not trigger the post unlink channels, pre-unlink space check // In order to not simulate channel deletion and not trigger the post unlink channels, pre-unlink space check
db.prepare("DELETE FROM channel_room WHERE channel_id = ?").run(channel.id) db.prepare("DELETE FROM channel_room WHERE channel_id = ?").run(channel.id)