Make ensureRoom/syncRoom check if autocreatable

This commit is contained in:
Cadence Ember 2024-09-24 17:21:35 +12:00
parent dbbb8281e6
commit af68657ec4
3 changed files with 27 additions and 7 deletions

View file

@ -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: 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 /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 "easy mode" web button, REPLACE INTO state 1 and ensureSpace.
- When bot is added through "self-service" web button, REPLACE INTO state 2. - 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 - Event dispatcher will only ensureRoom if the guild_active state is 1.

View file

@ -279,6 +279,17 @@ function channelToGuild(channel) {
return guild 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: Ensure flow:
1. Get IDs 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 {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) * @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<string>} room ID * @returns {Promise<string>} 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 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) { 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 creation = (async () => {
const {spaceID, privacyLevel, channelKState} = await channelToKState(channel, guild, {api}) const {spaceID, privacyLevel, channelKState} = await channelToKState(channel, guild, {api})
const roomID = await createRoom(channel, guild, spaceID, channelKState, privacyLevel) const roomID = await createRoom(channel, guild, spaceID, channelKState, privacyLevel)
@ -353,12 +373,12 @@ async function _syncRoom(channelID, shouldActuallySync) {
return roomID 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) { function ensureRoom(channelID) {
return _syncRoom(channelID, false) 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) { function syncRoom(channelID) {
return _syncRoom(channelID, true) return _syncRoom(channelID, true)
} }

View file

@ -35,7 +35,7 @@ export type Models = {
guild_active: { guild_active: {
guild_id: string guild_id: string
autocreate: number autocreate: 0 | 1
} }
lottie: { lottie: {