From 4f040e40d6f1d621f60e35305eaa056ba917001c Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Mon, 2 Dec 2024 16:33:18 +1300 Subject: [PATCH] Autocreate space if autocreating the room --- docs/self-service-room-creation-rules.md | 3 --- src/d2m/actions/create-room.js | 9 +++++---- src/d2m/actions/create-space.js | 2 +- src/d2m/converters/message-to-event.test.js | 2 +- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/docs/self-service-room-creation-rules.md b/docs/self-service-room-creation-rules.md index c47ca14..70892fc 100644 --- a/docs/self-service-room-creation-rules.md +++ b/docs/self-service-room-creation-rules.md @@ -61,8 +61,6 @@ So there will be 3 states of whether a guild is self-service or not. At first, i Pressing buttons on web or using the /invite command on a guild will insert a row into guild_active, allowing it to be bridged. -One more thing. Before v3, when a Matrix room was autocreated it would autocreate the space as well, if it needed to. But now, since nothing will be created until the user takes an action, the guild will always be created directly in response to a request. So room creation can now trust that the guild exists already. - 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. @@ -70,5 +68,4 @@ So here's all the technical changes needed to support self-service in v3: - 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 0. - Event dispatcher will only ensureRoom if the guild_active state is 1. -- createRoom can trust that the space exists because we check that in a calling function. - createRoom will only create other dependencies if the guild is autocreate. diff --git a/src/d2m/actions/create-room.js b/src/d2m/actions/create-room.js index 2d8d1af..ece8ef7 100644 --- a/src/d2m/actions/create-room.js +++ b/src/d2m/actions/create-room.js @@ -15,6 +15,8 @@ const api = sync.require("../../matrix/api") const ks = sync.require("../../matrix/kstate") /** @type {import("../../discord/utils")} */ const utils = sync.require("../../discord/utils") +/** @type {import("./create-space")} */ +const createSpace = sync.require("./create-space") /** * There are 3 levels of room privacy: @@ -93,11 +95,9 @@ function convertNameAndTopic(channel, guild, customName) { async function channelToKState(channel, guild, di) { // @ts-ignore const parentChannel = discord.channels.get(channel.parent_id) - const guildRow = select("guild_space", ["space_id", "privacy_level"], {guild_id: guild.id}).get() - assert(guildRow) /** Used for membership/permission checks. */ - let guildSpaceID = guildRow.space_id + const guildSpaceID = await createSpace.ensureSpace(guild) /** Used as the literal parent on Matrix, for categorisation. Will be the same as `guildSpaceID` unless it's a forum channel's thread, in which case a different space is used to group those threads. */ let parentSpaceID = guildSpaceID if (parentChannel?.type === DiscordTypes.ChannelType.GuildForum) { @@ -117,7 +117,8 @@ async function channelToKState(channel, guild, di) { avatarEventContent.url = {$url: file.guildIcon(guild)} } - const privacyLevel = guildRow.privacy_level + const privacyLevel = select("guild_space", "privacy_level", {guild_id: guild.id}).pluck().get() + assert(privacyLevel != null) // already ensured the space exists let history_visibility = PRIVACY_ENUMS.ROOM_HISTORY_VISIBILITY[privacyLevel] if (channel["thread_metadata"]) history_visibility = "world_readable" diff --git a/src/d2m/actions/create-space.js b/src/d2m/actions/create-space.js index 0c3d5e3..04d1413 100644 --- a/src/d2m/actions/create-space.js +++ b/src/d2m/actions/create-space.js @@ -93,7 +93,7 @@ async function _syncSpace(guild, shouldActuallySync) { if (!row) { const autocreate = select("guild_active", "autocreate", {guild_id: guild.id}).pluck().get() - assert.equal(autocreate, 1, `refusing to implicitly create guild ${guild.id}. set the guild_active data first before calling ensureSpace/syncSpace.`) + assert.equal(autocreate, 1, `refusing to implicitly create a space for guild ${guild.id}. set the guild_active data first before calling ensureSpace/syncSpace.`) const creation = (async () => { const guildKState = await guildToKState(guild, createRoom.DEFAULT_PRIVACY_LEVEL) // New spaces will have to use the default privacy level; we obviously can't look up the existing entry diff --git a/src/d2m/converters/message-to-event.test.js b/src/d2m/converters/message-to-event.test.js index 50f0908..6f77744 100644 --- a/src/d2m/converters/message-to-event.test.js +++ b/src/d2m/converters/message-to-event.test.js @@ -575,7 +575,7 @@ test("message2event: voice message", async t => { external_url: "https://bridge.example.org/download/discordcdn/1099031887500034088/1112476845502365786/voice-message.ogg", filename: "voice-message.ogg", info: { - duration: 3960.0000381469727, + duration: 3960, mimetype: "audio/ogg", size: 10584, },