From 0acf59bf489f417ec572cab125549c111a5e0e5f Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 7 Sep 2023 23:20:48 +1200 Subject: [PATCH] forcing space sync will unbridge deleted channels --- d2m/actions/create-room.js | 10 +++++++--- d2m/actions/create-space.js | 6 +++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/d2m/actions/create-room.js b/d2m/actions/create-room.js index e8284dfd..d8bab512 100644 --- a/d2m/actions/create-room.js +++ b/d2m/actions/create-room.js @@ -303,9 +303,13 @@ async function _unbridgeRoom(channelID) { /** @ts-ignore @type {DiscordTypes.APIGuildChannel} */ const channel = discord.channels.get(channelID) assert.ok(channel) + return unbridgeDeletedChannel(channel.id, channel.guild_id) +} + +async function unbridgeDeletedChannel(channelID, guildID) { const roomID = db.prepare("SELECT room_id from channel_room WHERE channel_id = ?").pluck().get(channelID) assert.ok(roomID) - const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(channel.guild_id) + const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(guildID) assert.ok(spaceID) // remove room from being a space member @@ -313,7 +317,7 @@ async function _unbridgeRoom(channelID) { await api.sendState(spaceID, "m.space.child", roomID, {}) // remove declaration that the room is bridged - await api.sendState(roomID, "uk.half-shot.bridge", `moe.cadence.ooye://discord/${channel.guild_id}/${channel.id}`, {}) + await api.sendState(roomID, "uk.half-shot.bridge", `moe.cadence.ooye://discord/${guildID}/${channelID}`, {}) // send a notification in the room await api.sendEvent(roomID, "m.room.message", { @@ -329,7 +333,6 @@ async function _unbridgeRoom(channelID) { assert.equal(changes, 1) } - /** * Async because it gets all space state from the homeserver, then if necessary sends one state event back. * @param {DiscordTypes.APIGuildTextChannel} channel @@ -377,3 +380,4 @@ module.exports.applyKStateDiffToRoom = applyKStateDiffToRoom module.exports.postApplyPowerLevels = postApplyPowerLevels module.exports._convertNameAndTopic = convertNameAndTopic module.exports._unbridgeRoom = _unbridgeRoom +module.exports.unbridgeDeletedChannel = unbridgeDeletedChannel diff --git a/d2m/actions/create-space.js b/d2m/actions/create-space.js index 0bdf1d13..34cf88ac 100644 --- a/d2m/actions/create-space.js +++ b/d2m/actions/create-space.js @@ -145,7 +145,11 @@ async function syncSpaceFully(guildID) { for (const roomID of childRooms) { const channelID = db.prepare("SELECT channel_id FROM channel_room WHERE room_id = ?").pluck().get(roomID) if (!channelID) continue - await createRoom.syncRoom(channelID) + if (discord.channels.has(channelID)) { + await createRoom.syncRoom(channelID) + } else { + await createRoom.unbridgeDeletedChannel(channelID, guildID) + } } return spaceID