From af68657ec42a040a98ad01e19135f6b9b58a3024 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Tue, 24 Sep 2024 17:21:35 +1200 Subject: [PATCH] Make ensureRoom/syncRoom check if autocreatable --- docs/self-service-room-creation-rules.md | 6 +++--- src/d2m/actions/create-room.js | 26 +++++++++++++++++++++--- src/db/orm-defs.d.ts | 2 +- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/docs/self-service-room-creation-rules.md b/docs/self-service-room-creation-rules.md index 6292fe72..1638f094 100644 --- a/docs/self-service-room-creation-rules.md +++ b/docs/self-service-room-creation-rules.md @@ -63,8 +63,8 @@ Pressing buttons on web or using the /invite command on a guild will insert a ro So here's all the technical changes needed to support self-service in v3: -- New guild_active table showing whether, and how, a guild is bridged +- New guild_active table showing whether, and how, a guild is bridged. - When /invite command is used, INSERT OR IGNORE INTO state 1 and ensureRoom + ensureSpace. - When bot is added through "easy mode" web button, REPLACE INTO state 1 and ensureSpace. -- When bot is added through "self-service" web button, REPLACE INTO state 2. -- Event dispatcher will only ensureRoom if the guild_active state is 1 +- When bot is added through "self-service" web button, REPLACE INTO state 0. +- Event dispatcher will only ensureRoom if the guild_active state is 1. diff --git a/src/d2m/actions/create-room.js b/src/d2m/actions/create-room.js index 509d0ff7..9be489b1 100644 --- a/src/d2m/actions/create-room.js +++ b/src/d2m/actions/create-room.js @@ -279,6 +279,17 @@ function channelToGuild(channel) { return guild } +/** + * @param {string} channelID + * @param {string} guildID + */ +function existsOrAutocreatable(channelID, guildID) { + const existing = select("channel_room", ["room_id", "thread_parent"], {channel_id: channelID}).get() + if (existing) return existing + const autocreate = select("guild_active", "autocreate", {guild_id: guildID}).pluck().get() + return autocreate +} + /* Ensure flow: 1. Get IDs @@ -297,6 +308,7 @@ function channelToGuild(channel) { */ /** + * Create room and/or sync room data. Please check that a channel_room entry exists or autocreate = 1 before calling this. * @param {string} channelID * @param {boolean} shouldActuallySync false if just need to ensure room exists (which is a quick database check), true if also want to sync room data when it does exist (slow) * @returns {Promise} room ID @@ -311,9 +323,17 @@ async function _syncRoom(channelID, shouldActuallySync) { await inflightRoomCreate.get(channelID) // just waiting, and then doing a new db query afterwards, is the simplest way of doing it } - const existing = select("channel_room", ["room_id", "thread_parent"], {channel_id: channelID}).get() + const existing = existsOrAutocreatable(channelID, guild.id) + + if (existing === 0) { + throw new Error("refusing to create a new matrix room when autocreate is deactivated") + } if (!existing) { + throw new Error("refusing to craete a new matrix room when there is no guild_active entry") + } + + if (existing === 1) { const creation = (async () => { const {spaceID, privacyLevel, channelKState} = await channelToKState(channel, guild, {api}) const roomID = await createRoom(channel, guild, spaceID, channelKState, privacyLevel) @@ -353,12 +373,12 @@ async function _syncRoom(channelID, shouldActuallySync) { return roomID } -/** Ensures the room exists. If it doesn't, creates the room with an accurate initial state. */ +/** Ensures the room exists. If it doesn't, creates the room with an accurate initial state. Please check that a channel_room entry exists or guild autocreate = 1 before calling this. */ function ensureRoom(channelID) { return _syncRoom(channelID, false) } -/** Actually syncs. Gets all room state from the homeserver in order to diff, and uploads the icon to mxc if it has changed. */ +/** Actually syncs. Gets all room state from the homeserver in order to diff, and uploads the icon to mxc if it has changed. Please check that a channel_room entry exists or guild autocreate = 1 before calling this. */ function syncRoom(channelID) { return _syncRoom(channelID, true) } diff --git a/src/db/orm-defs.d.ts b/src/db/orm-defs.d.ts index 02003aff..b1e6b795 100644 --- a/src/db/orm-defs.d.ts +++ b/src/db/orm-defs.d.ts @@ -35,7 +35,7 @@ export type Models = { guild_active: { guild_id: string - autocreate: number + autocreate: 0 | 1 } lottie: {