Compare commits
No commits in common. "8915e8d96c9fb401d1972996cec318f9f68187d0" and "dbbb8281e6304c73ebd1b6be0e7a35bd86794f6b" have entirely different histories.
8915e8d96c
...
dbbb8281e6
4 changed files with 18 additions and 38 deletions
|
@ -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 0.
|
- 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.
|
- Event dispatcher will only ensureRoom if the guild_active state is 1
|
||||||
|
|
|
@ -279,17 +279,6 @@ 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
|
||||||
|
@ -308,7 +297,6 @@ function existsOrAutocreatable(channelID, guildID) {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
|
@ -323,17 +311,9 @@ 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 = existsOrAutocreatable(channelID, guild.id)
|
const existing = select("channel_room", ["room_id", "thread_parent"], {channel_id: channelID}).get()
|
||||||
|
|
||||||
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)
|
||||||
|
@ -373,12 +353,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. Please check that a channel_room entry exists or guild autocreate = 1 before calling this. */
|
/** Ensures the room exists. If it doesn't, creates the room with an accurate initial state. */
|
||||||
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. Please check that a channel_room entry exists or guild autocreate = 1 before calling this. */
|
/** Actually syncs. Gets all room state from the homeserver in order to diff, and uploads the icon to mxc if it has changed. */
|
||||||
function syncRoom(channelID) {
|
function syncRoom(channelID) {
|
||||||
return _syncRoom(channelID, true)
|
return _syncRoom(channelID, true)
|
||||||
}
|
}
|
||||||
|
|
2
src/db/orm-defs.d.ts
vendored
2
src/db/orm-defs.d.ts
vendored
|
@ -35,7 +35,7 @@ export type Models = {
|
||||||
|
|
||||||
guild_active: {
|
guild_active: {
|
||||||
guild_id: string
|
guild_id: string
|
||||||
autocreate: 0 | 1
|
autocreate: number
|
||||||
}
|
}
|
||||||
|
|
||||||
lottie: {
|
lottie: {
|
||||||
|
|
|
@ -4,10 +4,6 @@ const DiscordTypes = require("discord-api-types/v10")
|
||||||
const assert = require("assert/strict")
|
const assert = require("assert/strict")
|
||||||
const {discord, sync, db, select, from} = require("../../passthrough")
|
const {discord, sync, db, select, from} = require("../../passthrough")
|
||||||
|
|
||||||
/** @type {import("../../d2m/actions/create-room")} */
|
|
||||||
const createRoom = sync.require("../../d2m/actions/create-room")
|
|
||||||
/** @type {import("../../d2m/actions/create-space")} */
|
|
||||||
const createSpace = sync.require("../../d2m/actions/create-space")
|
|
||||||
/** @type {import("../../matrix/api")} */
|
/** @type {import("../../matrix/api")} */
|
||||||
const api = sync.require("../../matrix/api")
|
const api = sync.require("../../matrix/api")
|
||||||
|
|
||||||
|
@ -16,6 +12,17 @@ const api = sync.require("../../matrix/api")
|
||||||
* @returns {Promise<DiscordTypes.APIInteractionResponse>}
|
* @returns {Promise<DiscordTypes.APIInteractionResponse>}
|
||||||
*/
|
*/
|
||||||
async function _interact({data, channel, guild_id}) {
|
async function _interact({data, channel, guild_id}) {
|
||||||
|
// Check guild is bridged
|
||||||
|
const spaceID = select("guild_space", "space_id", {guild_id}).pluck().get()
|
||||||
|
const roomID = select("channel_room", "room_id", {channel_id: channel.id}).pluck().get()
|
||||||
|
if (!spaceID || !roomID) return {
|
||||||
|
type: DiscordTypes.InteractionResponseType.ChannelMessageWithSource,
|
||||||
|
data: {
|
||||||
|
content: "This server isn't bridged to Matrix, so you can't invite Matrix users.",
|
||||||
|
flags: DiscordTypes.MessageFlags.Ephemeral
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Get named MXID
|
// Get named MXID
|
||||||
/** @type {DiscordTypes.APIApplicationCommandInteractionDataStringOption[] | undefined} */ // @ts-ignore
|
/** @type {DiscordTypes.APIApplicationCommandInteractionDataStringOption[] | undefined} */ // @ts-ignore
|
||||||
const options = data.options
|
const options = data.options
|
||||||
|
@ -29,13 +36,6 @@ async function _interact({data, channel, guild_id}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure guild and room are bridged
|
|
||||||
db.prepare("INSERT OR IGNORE INTO guild_active (guild_id, autocreate) VALUES (?, 1)").run(guild_id)
|
|
||||||
const roomID = await createRoom.ensureRoom(channel.id)
|
|
||||||
assert(roomID)
|
|
||||||
const spaceID = select("guild_space", "space_id", {guild_id}).pluck().get()
|
|
||||||
assert(spaceID)
|
|
||||||
|
|
||||||
// Check for existing invite to the space
|
// Check for existing invite to the space
|
||||||
let spaceMember
|
let spaceMember
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in a new issue