1
0
Fork 0

Finish moving from SQL to New Funny ORM

This commit is contained in:
Cadence Ember 2023-09-18 22:51:59 +12:00
parent 4e1e590c3a
commit 79bd0254f0
19 changed files with 87 additions and 87 deletions

View file

@ -3,7 +3,7 @@
const assert = require("assert").strict const assert = require("assert").strict
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough const {discord, sync, db, select} = passthrough
/** @type {import("../../matrix/api")} */ /** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api") const api = sync.require("../../matrix/api")
/** @type {import("./register-user")} */ /** @type {import("./register-user")} */
@ -17,7 +17,7 @@ const createRoom = sync.require("../actions/create-room")
async function addReaction(data) { async function addReaction(data) {
const user = data.member?.user const user = data.member?.user
assert.ok(user && user.username) assert.ok(user && user.username)
const parentID = db.prepare("SELECT event_id FROM event_message WHERE message_id = ? AND part = 0").pluck().get(data.message_id) // 0 = primary const parentID = select("event_message", "event_id", "WHERE message = ? AND part = 0").pluck().get(data.message_id) // 0 = primary
if (!parentID) return // Nothing can be done if the parent message was never bridged. if (!parentID) return // Nothing can be done if the parent message was never bridged.
assert.equal(typeof parentID, "string") assert.equal(typeof parentID, "string")
const roomID = await createRoom.ensureRoom(data.channel_id) const roomID = await createRoom.ensureRoom(data.channel_id)

View file

@ -3,7 +3,7 @@
const assert = require("assert") const assert = require("assert")
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough const {discord, sync, db, select} = passthrough
/** @type {import("../converters/thread-to-announcement")} */ /** @type {import("../converters/thread-to-announcement")} */
const threadToAnnouncement = sync.require("../converters/thread-to-announcement") const threadToAnnouncement = sync.require("../converters/thread-to-announcement")
/** @type {import("../../matrix/api")} */ /** @type {import("../../matrix/api")} */
@ -15,8 +15,7 @@ const api = sync.require("../../matrix/api")
* @param {import("discord-api-types/v10").APIThreadChannel} thread * @param {import("discord-api-types/v10").APIThreadChannel} thread
*/ */
async function announceThread(parentRoomID, threadRoomID, thread) { async function announceThread(parentRoomID, threadRoomID, thread) {
/** @type {string?} */ const creatorMxid = select("sim", "mxid", "WHERE discord_id = ?").pluck().get(thread.owner_id)
const creatorMxid = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(thread.owner_id)
const content = await threadToAnnouncement.threadToAnnouncement(parentRoomID, threadRoomID, creatorMxid, thread, {api}) const content = await threadToAnnouncement.threadToAnnouncement(parentRoomID, threadRoomID, creatorMxid, thread, {api})

View file

@ -5,7 +5,7 @@ const DiscordTypes = require("discord-api-types/v10")
const reg = require("../../matrix/read-registration") const reg = require("../../matrix/read-registration")
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough const {discord, sync, db, select} = passthrough
/** @type {import("../../matrix/file")} */ /** @type {import("../../matrix/file")} */
const file = sync.require("../../matrix/file") const file = sync.require("../../matrix/file")
/** @type {import("../../matrix/api")} */ /** @type {import("../../matrix/api")} */
@ -41,7 +41,7 @@ function applyKStateDiffToRoom(roomID, kstate) {
/** /**
* @param {{id: string, name: string, topic?: string?, type: number}} channel * @param {{id: string, name: string, topic?: string?, type: number}} channel
* @param {{id: string}} guild * @param {{id: string}} guild
* @param {string?} customName * @param {string | null | undefined} customName
*/ */
function convertNameAndTopic(channel, guild, customName) { function convertNameAndTopic(channel, guild, customName) {
let channelPrefix = let channelPrefix =
@ -71,7 +71,7 @@ async function channelToKState(channel, guild) {
const spaceID = await createSpace.ensureSpace(guild.id) const spaceID = await createSpace.ensureSpace(guild.id)
assert.ok(typeof spaceID === "string") assert.ok(typeof spaceID === "string")
const row = db.prepare("SELECT nick, custom_avatar FROM channel_room WHERE channel_id = ?").get(channel.id) const row = select("channel_room", ["nick", "custom_avatar"], "WHERE channel_id = ?").get(channel.id)
const customName = row?.nick const customName = row?.nick
const customAvatar = row?.custom_avatar const customAvatar = row?.custom_avatar
const [convertedName, convertedTopic] = convertNameAndTopic(channel, guild, customName) const [convertedName, convertedTopic] = convertNameAndTopic(channel, guild, customName)
@ -248,8 +248,7 @@ 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
} }
/** @type {{room_id: string, thread_parent: string?}} */ const existing = select("channel_room", ["room_id", "thread_parent"], "WHERE channel_id = ?").get(channelID)
const existing = db.prepare("SELECT room_id, thread_parent from channel_room WHERE channel_id = ?").get(channelID)
if (!existing) { if (!existing) {
const creation = (async () => { const creation = (async () => {
@ -309,9 +308,9 @@ async function _unbridgeRoom(channelID) {
} }
async function unbridgeDeletedChannel(channelID, guildID) { async function unbridgeDeletedChannel(channelID, guildID) {
const roomID = db.prepare("SELECT room_id from channel_room WHERE channel_id = ?").pluck().get(channelID) const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(channelID)
assert.ok(roomID) assert.ok(roomID)
const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(guildID) const spaceID = select("guild_space", "space_id", "WHERE guild_id = ?").pluck().get(guildID)
assert.ok(spaceID) assert.ok(spaceID)
// remove room from being a space member // remove room from being a space member

View file

@ -5,7 +5,7 @@ const DiscordTypes = require("discord-api-types/v10")
const reg = require("../../matrix/read-registration") const reg = require("../../matrix/read-registration")
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough const {discord, sync, db, select} = passthrough
/** @type {import("../../matrix/api")} */ /** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api") const api = sync.require("../../matrix/api")
/** @type {import("../../matrix/file")} */ /** @type {import("../../matrix/file")} */
@ -86,8 +86,7 @@ async function _syncSpace(guildID, shouldActuallySync) {
await inflightSpaceCreate.get(guildID) // just waiting, and then doing a new db query afterwards, is the simplest way of doing it await inflightSpaceCreate.get(guildID) // just waiting, and then doing a new db query afterwards, is the simplest way of doing it
} }
/** @type {string?} */ const spaceID = select("guild_space", "space_id", "WHERE guild_id = ?").pluck().get(guildID)
const spaceID = db.prepare("SELECT space_id from guild_space WHERE guild_id = ?").pluck().get(guildID)
if (!spaceID) { if (!spaceID) {
const creation = (async () => { const creation = (async () => {
@ -118,7 +117,7 @@ async function _syncSpace(guildID, shouldActuallySync) {
const newAvatarState = spaceDiff["m.room.avatar/"] const newAvatarState = spaceDiff["m.room.avatar/"]
if (guild.icon && newAvatarState?.url) { if (guild.icon && newAvatarState?.url) {
// don't try to update rooms with custom avatars though // don't try to update rooms with custom avatars though
const roomsWithCustomAvatars = db.prepare("SELECT room_id FROM channel_room WHERE custom_avatar IS NOT NULL").pluck().all() const roomsWithCustomAvatars = select("channel_room", "room_id", "WHERE custom_avatar IS NOT NULL").pluck().all()
const childRooms = ks.kstateToState(spaceKState).filter(({type, state_key, content}) => { const childRooms = ks.kstateToState(spaceKState).filter(({type, state_key, content}) => {
return type === "m.space.child" && "via" in content && !roomsWithCustomAvatars.includes(state_key) return type === "m.space.child" && "via" in content && !roomsWithCustomAvatars.includes(state_key)
@ -154,8 +153,7 @@ async function syncSpaceFully(guildID) {
const guild = discord.guilds.get(guildID) const guild = discord.guilds.get(guildID)
assert.ok(guild) assert.ok(guild)
/** @type {string?} */ const spaceID = select("guild_space", "space_id", "WHERE guild_id = ?").pluck().get(guildID)
const spaceID = db.prepare("SELECT space_id from guild_space WHERE guild_id = ?").pluck().get(guildID)
const guildKState = await guildToKState(guild) const guildKState = await guildToKState(guild)
@ -176,7 +174,7 @@ async function syncSpaceFully(guildID) {
}).map(({state_key}) => state_key) }).map(({state_key}) => state_key)
for (const roomID of childRooms) { for (const roomID of childRooms) {
const channelID = db.prepare("SELECT channel_id FROM channel_room WHERE room_id = ?").pluck().get(roomID) const channelID = select("channel_room", "channel_id", "WHERE room_id = ?").pluck().get(roomID)
if (!channelID) continue if (!channelID) continue
if (discord.channels.has(channelID)) { if (discord.channels.has(channelID)) {
await createRoom.syncRoom(channelID) await createRoom.syncRoom(channelID)

View file

@ -1,7 +1,7 @@
// @ts-check // @ts-check
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { sync, db } = passthrough const {sync, db, select} = passthrough
/** @type {import("../../matrix/api")} */ /** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api") const api = sync.require("../../matrix/api")
@ -9,13 +9,10 @@ const api = sync.require("../../matrix/api")
* @param {import("discord-api-types/v10").GatewayMessageDeleteDispatchData} data * @param {import("discord-api-types/v10").GatewayMessageDeleteDispatchData} data
*/ */
async function deleteMessage(data) { async function deleteMessage(data) {
/** @type {string?} */ const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(data.channel_id)
const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(data.channel_id)
if (!roomID) return if (!roomID) return
/** @type {string[]} */ const eventsToRedact = select("event_message", "event_id", "WHERE message_id = ?").pluck().all(data.id)
const eventsToRedact = db.prepare("SELECT event_id FROM event_message WHERE message_id = ?").pluck().all(data.id)
for (const eventID of eventsToRedact) { for (const eventID of eventsToRedact) {
// Unfortunately, we can't specify a sender to do the redaction as, unless we find out that info via the audit logs // Unfortunately, we can't specify a sender to do the redaction as, unless we find out that info via the audit logs
await api.redactEvent(roomID, eventID) await api.redactEvent(roomID, eventID)

View file

@ -4,7 +4,7 @@ const assert = require("assert")
const reg = require("../../matrix/read-registration") const reg = require("../../matrix/read-registration")
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough const {discord, sync, db, select} = passthrough
/** @type {import("../../matrix/api")} */ /** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api") const api = sync.require("../../matrix/api")
/** @type {import("../../matrix/file")} */ /** @type {import("../../matrix/file")} */
@ -47,7 +47,7 @@ async function createSim(user) {
*/ */
async function ensureSim(user) { async function ensureSim(user) {
let mxid = null let mxid = null
const existing = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(user.id) const existing = select("sim", "mxid", "WHERE discord_id = ?").pluck().get(user.id)
if (existing) { if (existing) {
mxid = existing mxid = existing
} else { } else {
@ -70,7 +70,7 @@ async function ensureSimJoined(user, roomID) {
const mxid = await ensureSim(user) const mxid = await ensureSim(user)
// Ensure joined // Ensure joined
const existing = db.prepare("SELECT * FROM sim_member WHERE room_id = ? and mxid = ?").get(roomID, mxid) const existing = select("sim_member", "mxid", "WHERE room_id = ? AND mxid = ?").pluck().get(roomID, mxid)
if (!existing) { if (!existing) {
try { try {
await api.inviteToRoom(roomID, mxid) await api.inviteToRoom(roomID, mxid)
@ -137,7 +137,7 @@ async function syncUser(user, member, guildID, roomID) {
const mxid = await ensureSimJoined(user, roomID) const mxid = await ensureSimJoined(user, roomID)
const content = await memberToStateContent(user, member, guildID) const content = await memberToStateContent(user, member, guildID)
const profileEventContentHash = calculateProfileEventContentHash(content) const profileEventContentHash = calculateProfileEventContentHash(content)
const existingHash = db.prepare("SELECT profile_event_content_hash FROM sim_member WHERE room_id = ? AND mxid = ?").pluck().get(roomID, mxid) const existingHash = select("sim_member", "profile_event_content_hash", "WHERE room_id = ? AND mxid = ?").pluck().get(roomID, mxid)
// only do the actual sync if the hash has changed since we last looked // only do the actual sync if the hash has changed since we last looked
if (existingHash !== profileEventContentHash) { if (existingHash !== profileEventContentHash) {
await api.sendState(roomID, "m.room.member", mxid, content, mxid) await api.sendState(roomID, "m.room.member", mxid, content, mxid)
@ -147,17 +147,18 @@ async function syncUser(user, member, guildID, roomID) {
} }
async function syncAllUsersInRoom(roomID) { async function syncAllUsersInRoom(roomID) {
const mxids = db.prepare("SELECT mxid FROM sim_member WHERE room_id = ?").pluck().all(roomID) const mxids = select("sim_member", "mxid", "WHERE room_id = ?").pluck().all(roomID)
const channelID = db.prepare("SELECT channel_id FROM channel_room WHERE room_id = ?").pluck().get(roomID) const channelID = select("channel_room", "channel_id", "WHERE room_id = ?").pluck().get(roomID)
assert.ok(typeof channelID === "string") assert.ok(typeof channelID === "string")
/** @ts-ignore @type {import("discord-api-types/v10").APIGuildChannel} */ /** @ts-ignore @type {import("discord-api-types/v10").APIGuildChannel} */
const channel = discord.channels.get(channelID) const channel = discord.channels.get(channelID)
const guildID = channel.guild_id const guildID = channel.guild_id
assert.ok(typeof guildID === "string") assert.ok(typeof guildID === "string")
for (const mxid of mxids) { for (const mxid of mxids) {
const userID = db.prepare("SELECT discord_id FROM sim WHERE mxid = ?").pluck().get(mxid) const userID = select("sim", "discord_id", "WHERE mxid = ?").pluck().get(mxid)
assert.ok(typeof userID === "string") assert.ok(typeof userID === "string")
/** @ts-ignore @type {Required<import("discord-api-types/v10").APIGuildMember>} */ /** @ts-ignore @type {Required<import("discord-api-types/v10").APIGuildMember>} */

View file

@ -3,7 +3,7 @@
const assert = require("assert") const assert = require("assert")
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough const {discord, sync, db, select} = passthrough
/** @type {import("./message-to-event")} */ /** @type {import("./message-to-event")} */
const messageToEvent = sync.require("../converters/message-to-event") const messageToEvent = sync.require("../converters/message-to-event")
/** @type {import("../actions/register-user")} */ /** @type {import("../actions/register-user")} */
@ -21,17 +21,15 @@ const createRoom = sync.require("../actions/create-room")
async function editToChanges(message, guild, api) { async function editToChanges(message, guild, api) {
// Figure out what events we will be replacing // Figure out what events we will be replacing
const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(message.channel_id) const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(message.channel_id)
/** @type {string?} */ let senderMxid = select("sim", "mxid", "WHERE discord_id = ?").pluck().get(message.author.id) || null
let senderMxid = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(message.author.id) || null
if (senderMxid) { if (senderMxid) {
const senderIsInRoom = db.prepare("SELECT * FROM sim_member WHERE room_id = ? and mxid = ?").get(roomID, senderMxid) const senderIsInRoom = select("sim_member", "mxid", "WHERE room_id = ? AND mxid = ?").get(roomID, senderMxid)
if (!senderIsInRoom) { if (!senderIsInRoom) {
senderMxid = null // just send as ooye bot senderMxid = null // just send as ooye bot
} }
} }
/** @type {{event_id: string, event_type: string, event_subtype: string?, part: number}[]} */ const oldEventRows = select("event_message", ["event_id", "event_type", "event_subtype", "part"], "WHERE message_id = ?").all(message.id)
const oldEventRows = db.prepare("SELECT event_id, event_type, event_subtype, part FROM event_message WHERE message_id = ?").all(message.id)
// Figure out what we will be replacing them with // Figure out what we will be replacing them with

View file

@ -6,7 +6,7 @@ const assert = require("assert").strict
const {PNG} = require("pngjs") const {PNG} = require("pngjs")
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { sync, db, discord } = passthrough const {sync, db, discord, select} = passthrough
/** @type {import("../../matrix/file")} */ /** @type {import("../../matrix/file")} */
const file = sync.require("../../matrix/file") const file = sync.require("../../matrix/file")
//** @type {import("../../matrix/mreq")} */ //** @type {import("../../matrix/mreq")} */
@ -38,7 +38,7 @@ const Rlottie = (async () => {
* @returns {Promise<{mxc: string, info: typeof INFO}>} * @returns {Promise<{mxc: string, info: typeof INFO}>}
*/ */
async function convert(stickerItem) { async function convert(stickerItem) {
const existingMxc = db.prepare("SELECT mxc FROM lottie WHERE id = ?").pluck().get(stickerItem.id) const existingMxc = select("lottie", "mxc", "WHERE id = ?").pluck().get(stickerItem.id)
if (existingMxc) return {mxc: existingMxc, info: INFO} if (existingMxc) return {mxc: existingMxc, info: INFO}
const r = await Rlottie const r = await Rlottie
const res = await fetch(file.DISCORD_IMAGES_BASE + file.sticker(stickerItem)) const res = await fetch(file.DISCORD_IMAGES_BASE + file.sticker(stickerItem))

View file

@ -1,9 +1,9 @@
// @ts-check // @ts-check
const assert = require("assert") const assert = require("assert").strict
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough const {discord, sync, db, select} = passthrough
/** @type {import("../../matrix/read-registration")} */ /** @type {import("../../matrix/read-registration")} */
const reg = sync.require("../../matrix/read-registration.js") const reg = sync.require("../../matrix/read-registration.js")
@ -17,8 +17,7 @@ const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
* @param {{api: import("../../matrix/api")}} di simple-as-nails dependency injection for the matrix API * @param {{api: import("../../matrix/api")}} di simple-as-nails dependency injection for the matrix API
*/ */
async function threadToAnnouncement(parentRoomID, threadRoomID, creatorMxid, thread, di) { async function threadToAnnouncement(parentRoomID, threadRoomID, creatorMxid, thread, di) {
/** @type {string?} */ const branchedFromEventID = select("event_message", "event_id", "WHERE message_id = ?").pluck().get(thread.id)
const branchedFromEventID = db.prepare("SELECT event_id FROM event_message WHERE message_id = ?").pluck().get(thread.id)
/** @type {{"m.mentions"?: any, "m.in_reply_to"?: any}} */ /** @type {{"m.mentions"?: any, "m.in_reply_to"?: any}} */
const context = {} const context = {}
if (branchedFromEventID) { if (branchedFromEventID) {

View file

@ -3,7 +3,7 @@
const assert = require("assert") const assert = require("assert")
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { sync, db } = passthrough const {select} = passthrough
/** /**
* Downcased and stripped username. Can only include a basic set of characters. * Downcased and stripped username. Can only include a basic set of characters.
@ -53,7 +53,7 @@ function userToSimName(user) {
assert.notEqual(user.discriminator, "0000", "cannot create user for a webhook") assert.notEqual(user.discriminator, "0000", "cannot create user for a webhook")
// 1. Is sim user already registered? // 1. Is sim user already registered?
const existing = db.prepare("SELECT sim_name FROM sim WHERE discord_id = ?").pluck().get(user.id) const existing = select("sim", "sim_name", "WHERE discord_id = ?").pluck().get(user.id)
if (existing) return existing if (existing) return existing
// 2. Register based on username (could be new or old format) // 2. Register based on username (could be new or old format)
@ -64,8 +64,7 @@ function userToSimName(user) {
} }
// Check for conflicts with already registered sims // Check for conflicts with already registered sims
/** @type {string[]} */ const matches = select("sim", "sim_name", "WHERE sim_name LIKE ? ESCAPE '@'").pluck().all(downcased + "%")
const matches = db.prepare("SELECT sim_name FROM sim WHERE sim_name LIKE ? ESCAPE '@'").pluck().all(downcased + "%")
// Keep generating until we get a suggestion that doesn't conflict // Keep generating until we get a suggestion that doesn't conflict
for (const suggestion of generateLocalpartAlternatives(preferences)) { for (const suggestion of generateLocalpartAlternatives(preferences)) {
if (!matches.includes(suggestion)) return suggestion if (!matches.includes(suggestion)) return suggestion

View file

@ -1,6 +1,6 @@
const assert = require("assert").strict const assert = require("assert").strict
const util = require("util") const util = require("util")
const {sync, db} = require("../passthrough") const {sync, db, select, from} = require("../passthrough")
/** @type {import("./actions/send-message")}) */ /** @type {import("./actions/send-message")}) */
const sendMessage = sync.require("./actions/send-message") const sendMessage = sync.require("./actions/send-message")
@ -42,7 +42,7 @@ module.exports = {
const channelID = gatewayMessage.d.channel_id const channelID = gatewayMessage.d.channel_id
if (!channelID) return if (!channelID) return
const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(channelID) const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(channelID)
if (!roomID) return if (!roomID) return
let stackLines = e.stack.split("\n") let stackLines = e.stack.split("\n")
@ -80,8 +80,8 @@ module.exports = {
*/ */
async checkMissedMessages(client, guild) { async checkMissedMessages(client, guild) {
if (guild.unavailable) return if (guild.unavailable) return
const bridgedChannels = db.prepare("SELECT channel_id FROM channel_room").pluck().all() const bridgedChannels = select("channel_room", "channel_id").pluck().all()
const prepared = db.prepare("SELECT 1 FROM event_message WHERE message_id = ?").pluck() const prepared = select("event_message", "1", "WHERE message_id = ?").pluck()
for (const channel of guild.channels.concat(guild.threads)) { for (const channel of guild.channels.concat(guild.threads)) {
if (!bridgedChannels.includes(channel.id)) continue if (!bridgedChannels.includes(channel.id)) continue
if (!channel.last_message_id) continue if (!channel.last_message_id) continue
@ -125,7 +125,7 @@ module.exports = {
* @param {import("discord-api-types/v10").APIThreadChannel} thread * @param {import("discord-api-types/v10").APIThreadChannel} thread
*/ */
async onThreadCreate(client, thread) { async onThreadCreate(client, thread) {
const parentRoomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(thread.parent_id) const parentRoomID = select("channel_room", "room_id", "WHERE channel_room = ?").pluck().get(thread.parent_id)
if (!parentRoomID) return // Not interested in a thread if we aren't interested in its wider channel if (!parentRoomID) return // Not interested in a thread if we aren't interested in its wider channel
const threadRoomID = await createRoom.syncRoom(thread.id) // Create room (will share the same inflight as the initial message to the thread) const threadRoomID = await createRoom.syncRoom(thread.id) // Create room (will share the same inflight as the initial message to the thread)
await announceThread.announceThread(parentRoomID, threadRoomID, thread) await announceThread.announceThread(parentRoomID, threadRoomID, thread)
@ -136,7 +136,7 @@ module.exports = {
* @param {import("discord-api-types/v10").GatewayGuildUpdateDispatchData} guild * @param {import("discord-api-types/v10").GatewayGuildUpdateDispatchData} guild
*/ */
async onGuildUpdate(client, guild) { async onGuildUpdate(client, guild) {
const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(guild.id) const spaceID = select("guild_space", "space_id", "WHERE guild_id = ?").pluck().get(guild.id)
if (!spaceID) return if (!spaceID) return
await createSpace.syncSpace(guild.id) await createSpace.syncSpace(guild.id)
}, },
@ -147,7 +147,7 @@ module.exports = {
* @param {boolean} isThread * @param {boolean} isThread
*/ */
async onChannelOrThreadUpdate(client, channelOrThread, isThread) { async onChannelOrThreadUpdate(client, channelOrThread, isThread) {
const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").get(channelOrThread.id) const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(channelOrThread.id)
if (!roomID) return // No target room to update the data on if (!roomID) return // No target room to update the data on
await createRoom.syncRoom(channelOrThread.id) await createRoom.syncRoom(channelOrThread.id)
}, },
@ -158,7 +158,7 @@ module.exports = {
*/ */
async onMessageCreate(client, message) { async onMessageCreate(client, message) {
if (message.webhook_id) { if (message.webhook_id) {
const row = db.prepare("SELECT webhook_id FROM webhook WHERE webhook_id = ?").pluck().get(message.webhook_id) const row = select("webhook", "1", "WHERE webhook_id = ?").pluck().get(message.webhook_id)
if (row) { if (row) {
// The message was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it. // The message was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it.
return return
@ -179,7 +179,7 @@ module.exports = {
*/ */
async onMessageUpdate(client, data) { async onMessageUpdate(client, data) {
if (data.webhook_id) { if (data.webhook_id) {
const row = db.prepare("SELECT webhook_id FROM webhook WHERE webhook_id = ?").pluck().get(data.webhook_id) const row = select("webhook", "1", "WHERE webhook_id = ?").pluck().get(message.webhook_id)
if (row) { if (row) {
// The update was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it. // The update was sent by the bridge's own webhook on discord. We don't want to reflect this back, so just drop it.
return return
@ -222,9 +222,9 @@ module.exports = {
* @param {import("discord-api-types/v10").GatewayTypingStartDispatchData} data * @param {import("discord-api-types/v10").GatewayTypingStartDispatchData} data
*/ */
async onTypingStart(client, data) { async onTypingStart(client, data) {
const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(data.channel_id) const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(data.channel_id)
if (!roomID) return if (!roomID) return
const mxid = db.prepare("SELECT mxid FROM sim INNER JOIN sim_member USING (mxid) WHERE discord_id = ? AND room_id = ?").pluck().get(data.user_id, roomID) const mxid = from("sim").join("sim_member", "mxid").and("WHERE discord_id = ? AND room_id = ?").pluck("mxid").get(data.user_id, roomID)
if (!mxid) return if (!mxid) return
// Each Discord user triggers the notification every 8 seconds as long as they remain typing. // Each Discord user triggers the notification every 8 seconds as long as they remain typing.
// Discord does not send typing stopped events, so typing only stops if the timeout is reached or if the user sends their message. // Discord does not send typing stopped events, so typing only stops if the timeout is reached or if the user sends their message.

7
db/orm-utils.d.ts vendored
View file

@ -27,6 +27,11 @@ export type Models = {
space_id: string space_id: string
} }
lottie: {
id: string
mxc: string
}
member_cache: { member_cache: {
room_id: string room_id: string
mxid: string mxid: string
@ -49,7 +54,7 @@ export type Models = {
sim_member: { sim_member: {
mxid: string mxid: string
room_id: string room_id: string
profile_event_content_hash: string profile_event_content_hash: any
} }
webhook: { webhook: {

View file

@ -5,7 +5,7 @@ const util = require("util")
const DiscordTypes = require("discord-api-types/v10") const DiscordTypes = require("discord-api-types/v10")
const reg = require("../matrix/read-registration") const reg = require("../matrix/read-registration")
const {discord, sync, db} = require("../passthrough") const {discord, sync, db, select} = require("../passthrough")
/** @type {import("../matrix/api")}) */ /** @type {import("../matrix/api")}) */
const api = sync.require("../matrix/api") const api = sync.require("../matrix/api")
/** @type {import("../matrix/file")} */ /** @type {import("../matrix/file")} */
@ -80,7 +80,7 @@ const commands = [{
execute: replyctx( execute: replyctx(
async (message, channel, guild, ctx) => { async (message, channel, guild, ctx) => {
// Guard // Guard
const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(channel.id) const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(channel.id)
if (!roomID) return discord.snow.channel.createMessage(channel.id, { if (!roomID) return discord.snow.channel.createMessage(channel.id, {
...ctx, ...ctx,
content: "This channel isn't bridged to the other side." content: "This channel isn't bridged to the other side."
@ -124,8 +124,8 @@ const commands = [{
execute: replyctx( execute: replyctx(
async (message, channel, guild, ctx) => { async (message, channel, guild, ctx) => {
// Check guild is bridged // Check guild is bridged
const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(guild.id) const spaceID = select("guild_space", "space_id", "WHERE guild_id = ?").pluck().get(guild.id)
const roomID = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(channel.id) const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(channel.id)
if (!spaceID || !roomID) return discord.snow.channel.createMessage(channel.id, { if (!spaceID || !roomID) return discord.snow.channel.createMessage(channel.id, {
...ctx, ...ctx,
content: "This server isn't bridged to Matrix, so you can't invite Matrix users." content: "This server isn't bridged to Matrix, so you can't invite Matrix users."

View file

@ -4,15 +4,15 @@ const assert = require("assert").strict
const Ty = require("../../types") const Ty = require("../../types")
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough const {discord, sync, db, select} = passthrough
/** /**
* @param {Ty.Event.Outer<Ty.Event.M_Reaction>} event * @param {Ty.Event.Outer<Ty.Event.M_Reaction>} event
*/ */
async function addReaction(event) { async function addReaction(event) {
const channelID = db.prepare("SELECT channel_id FROM channel_room WHERE room_id = ?").pluck().get(event.room_id) const channelID = select("channel_room", "channel_id", "WHERE room_id = ?").pluck().get(event.room_id)
if (!channelID) return // We just assume the bridge has already been created if (!channelID) return // We just assume the bridge has already been created
const messageID = db.prepare("SELECT message_id FROM event_message WHERE event_id = ? AND part = 0").pluck().get(event.content["m.relates_to"].event_id) // 0 = primary const messageID = select("event_message", "message_id", "WHERE event_id = ? AND part = 0").pluck().get(event.content["m.relates_to"].event_id) // 0 = primary
if (!messageID) return // Nothing can be done if the parent message was never bridged. if (!messageID) return // Nothing can be done if the parent message was never bridged.
// no need to sync the matrix member to the other side. but if I did need to, this is where I'd do it // no need to sync the matrix member to the other side. but if I did need to, this is where I'd do it

View file

@ -3,7 +3,7 @@
const assert = require("assert").strict const assert = require("assert").strict
const DiscordTypes = require("discord-api-types/v10") const DiscordTypes = require("discord-api-types/v10")
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const {discord, db} = passthrough const {discord, db, select} = passthrough
/** /**
* Look in the database to find webhook credentials for a channel. * Look in the database to find webhook credentials for a channel.
@ -14,10 +14,13 @@ const {discord, db} = passthrough
*/ */
async function ensureWebhook(channelID, forceCreate = false) { async function ensureWebhook(channelID, forceCreate = false) {
if (!forceCreate) { if (!forceCreate) {
/** @type {{id: string, token: string} | null} */ const row = select("webhook", ["webhook_id", "webhook_token"], "WHERE channel_id = ?").get(channelID)
const row = db.prepare("SELECT webhook_id as id, webhook_token as token FROM webhook WHERE channel_id = ?").get(channelID)
if (row) { if (row) {
return {created: false, ...row} return {
id: row.webhook_id,
token: row.webhook_token,
created: false
}
} }
} }

View file

@ -7,7 +7,7 @@ const {promisify} = require("util")
const Ty = require("../../types") const Ty = require("../../types")
const DiscordTypes = require("discord-api-types/v10") const DiscordTypes = require("discord-api-types/v10")
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const {sync, discord, db} = passthrough const {sync, discord, db, select} = passthrough
/** @type {import("./channel-webhook")} */ /** @type {import("./channel-webhook")} */
const channelWebhook = sync.require("./channel-webhook") const channelWebhook = sync.require("./channel-webhook")
@ -53,7 +53,8 @@ async function resolvePendingFiles(message) {
/** @param {Ty.Event.Outer_M_Room_Message | Ty.Event.Outer_M_Room_Message_File | Ty.Event.Outer_M_Sticker} event */ /** @param {Ty.Event.Outer_M_Room_Message | Ty.Event.Outer_M_Room_Message_File | Ty.Event.Outer_M_Sticker} event */
async function sendEvent(event) { async function sendEvent(event) {
// TODO: we just assume the bridge has already been created, is that really ok? // TODO: we just assume the bridge has already been created, is that really ok?
const row = db.prepare("SELECT channel_id, thread_parent FROM channel_room WHERE room_id = ?").get(event.room_id) const row = select("channel_room", ["channel_id", "thread_parent"], "WHERE room_id = ?").get(event.room_id)
assert(row)
let channelID = row.channel_id let channelID = row.channel_id
let threadID = undefined let threadID = undefined
if (row.thread_parent) { if (row.thread_parent) {

View file

@ -7,7 +7,7 @@ const TurndownService = require("turndown")
const assert = require("assert").strict const assert = require("assert").strict
const passthrough = require("../../passthrough") const passthrough = require("../../passthrough")
const { sync, db, discord } = passthrough const {sync, db, discord, select, from} = passthrough
/** @type {import("../../matrix/file")} */ /** @type {import("../../matrix/file")} */
const file = sync.require("../../matrix/file") const file = sync.require("../../matrix/file")
/** @type {import("../converters/utils")} */ /** @type {import("../converters/utils")} */
@ -148,7 +148,7 @@ turndownService.addRule("fencedCodeBlock", {
* @returns {Promise<{displayname?: string?, avatar_url?: string?}>} * @returns {Promise<{displayname?: string?, avatar_url?: string?}>}
*/ */
async function getMemberFromCacheOrHomeserver(roomID, mxid, api) { async function getMemberFromCacheOrHomeserver(roomID, mxid, api) {
const row = db.prepare("SELECT displayname, avatar_url FROM member_cache WHERE room_id = ? AND mxid = ?").get(roomID, mxid) const row = select("member_cache", ["displayname", "avatar_url"], "WHERE room_id = ? AND mxid = ?").get(roomID, mxid)
if (row) return row if (row) return row
return api.getStateEvent(roomID, "m.room.member", mxid).then(event => { return api.getStateEvent(roomID, "m.room.member", mxid).then(event => {
db.prepare("REPLACE INTO member_cache (room_id, mxid, displayname, avatar_url) VALUES (?, ?, ?, ?)").run(roomID, mxid, event?.displayname || null, event?.avatar_url || null) db.prepare("REPLACE INTO member_cache (room_id, mxid, displayname, avatar_url) VALUES (?, ?, ?, ?)").run(roomID, mxid, event?.displayname || null, event?.avatar_url || null)
@ -230,7 +230,7 @@ async function eventToMessage(event, guild, di) {
if (relType !== "m.replace") return if (relType !== "m.replace") return
const originalEventId = relatesTo.event_id const originalEventId = relatesTo.event_id
if (!originalEventId) return if (!originalEventId) return
messageIDsToEdit = db.prepare("SELECT message_id FROM event_message WHERE event_id = ? ORDER BY part").pluck().all(originalEventId) messageIDsToEdit = select("event_message", "message_id", "WHERE event_id = ? ORDER BY part").pluck().all(originalEventId)
if (!messageIDsToEdit.length) return if (!messageIDsToEdit.length) return
// Ok, it's an edit. // Ok, it's an edit.
@ -261,7 +261,7 @@ async function eventToMessage(event, guild, di) {
if (!repliedToEventId) return if (!repliedToEventId) return
let repliedToEvent = await di.api.getEvent(event.room_id, repliedToEventId) let repliedToEvent = await di.api.getEvent(event.room_id, repliedToEventId)
if (!repliedToEvent) return if (!repliedToEvent) return
const row = db.prepare("SELECT channel_id, message_id FROM event_message INNER JOIN message_channel USING (message_id) WHERE event_id = ? ORDER BY part").get(repliedToEventId) const row = from("event_message").join("message_channel", "message_id").select("channel_id", "message_id").and("WHERE event_id = ? ORDER BY part").get(repliedToEventId)
if (row) { if (row) {
replyLine = `<:L1:1144820033948762203><:L2:1144820084079087647>https://discord.com/channels/${guild.id}/${row.channel_id}/${row.message_id} ` replyLine = `<:L1:1144820033948762203><:L2:1144820084079087647>https://discord.com/channels/${guild.id}/${row.channel_id}/${row.message_id} `
} else { } else {
@ -269,7 +269,7 @@ async function eventToMessage(event, guild, di) {
} }
const sender = repliedToEvent.sender const sender = repliedToEvent.sender
const senderName = sender.match(/@([^:]*)/)?.[1] || sender const senderName = sender.match(/@([^:]*)/)?.[1] || sender
const authorID = db.prepare("SELECT discord_id FROM sim WHERE mxid = ?").pluck().get(repliedToEvent.sender) const authorID = select("sim", "discord_id", "WHERE mxid = ?").pluck().get(repliedToEvent.sender)
if (authorID) { if (authorID) {
replyLine += `<@${authorID}>` replyLine += `<@${authorID}>`
} else { } else {
@ -312,14 +312,14 @@ async function eventToMessage(event, guild, di) {
// Handling mentions of Discord users // Handling mentions of Discord users
input = input.replace(/("https:\/\/matrix.to\/#\/(@[^"]+)")>/g, (whole, attributeValue, mxid) => { input = input.replace(/("https:\/\/matrix.to\/#\/(@[^"]+)")>/g, (whole, attributeValue, mxid) => {
if (!utils.eventSenderIsFromDiscord(mxid)) return whole if (!utils.eventSenderIsFromDiscord(mxid)) return whole
const userID = db.prepare("SELECT discord_id FROM sim WHERE mxid = ?").pluck().get(mxid) const userID = select("sim", "discord_id", "WHERE mxid = ?").pluck().get(mxid)
if (!userID) return whole if (!userID) return whole
return `${attributeValue} data-user-id="${userID}">` return `${attributeValue} data-user-id="${userID}">`
}) })
// Handling mentions of Discord rooms // Handling mentions of Discord rooms
input = input.replace(/("https:\/\/matrix.to\/#\/(![^"]+)")>/g, (whole, attributeValue, roomID) => { input = input.replace(/("https:\/\/matrix.to\/#\/(![^"]+)")>/g, (whole, attributeValue, roomID) => {
const channelID = db.prepare("SELECT channel_id FROM channel_room WHERE room_id = ?").pluck().get(roomID) const channelID = select("channel_room", "channel_id", "WHERE room_id = ?").pluck().get(roomID)
if (!channelID) return whole if (!channelID) return whole
return `${attributeValue} data-channel-id="${channelID}">` return `${attributeValue} data-channel-id="${channelID}">`
}) })

View file

@ -2,7 +2,7 @@ const {test} = require("supertape")
const {eventToMessage} = require("./event-to-message") const {eventToMessage} = require("./event-to-message")
const data = require("../../test/data") const data = require("../../test/data")
const {MatrixServerError} = require("../../matrix/mreq") const {MatrixServerError} = require("../../matrix/mreq")
const {db} = require("../../passthrough") const {db, select} = require("../../passthrough")
/** /**
* @param {string} roomID * @param {string} roomID
@ -1414,7 +1414,8 @@ test("event2message: caches the member if the member is not known", async t => {
}] }]
} }
) )
t.deepEqual(db.prepare("SELECT avatar_url, displayname, mxid FROM member_cache WHERE room_id = '!should_be_newly_cached:cadence.moe'").all(), [
t.deepEqual(select("member_cache", ["avatar_url", "displayname", "mxid"], "WHERE room_id = '!should_be_newly_cached:cadence.moe'").all(), [
{avatar_url: "mxc://cadence.moe/this_is_the_avatar", displayname: null, mxid: "@should_be_newly_cached:cadence.moe"} {avatar_url: "mxc://cadence.moe/this_is_the_avatar", displayname: null, mxid: "@should_be_newly_cached:cadence.moe"}
]) ])
t.equal(called, 1, "getStateEvent should be called once") t.equal(called, 1, "getStateEvent should be called once")
@ -1457,7 +1458,7 @@ test("event2message: skips caching the member if the member does not exist, some
}] }]
} }
) )
t.deepEqual(db.prepare("SELECT avatar_url, displayname, mxid FROM member_cache WHERE room_id = '!not_real:cadence.moe'").all(), []) t.deepEqual(select("member_cache", ["avatar_url", "displayname", "mxid"], "WHERE room_id = '!not_real:cadence.moe'").all(), [])
t.equal(called, 1, "getStateEvent should be called once") t.equal(called, 1, "getStateEvent should be called once")
}) })
@ -1500,7 +1501,7 @@ test("event2message: overly long usernames are shifted into the message content"
}] }]
} }
) )
t.deepEqual(db.prepare("SELECT avatar_url, displayname, mxid FROM member_cache WHERE room_id = '!should_be_newly_cached_2:cadence.moe'").all(), [ t.deepEqual(select("member_cache", ["avatar_url", "displayname", "mxid"], "WHERE room_id = '!should_be_newly_cached_2:cadence.moe'").all(), [
{avatar_url: null, displayname: "I am BLACK I am WHITE I am SHORT I am LONG I am EVERYTHING YOU THINK IS IMPORTANT and I DON'T MATTER", mxid: "@should_be_newly_cached_2:cadence.moe"} {avatar_url: null, displayname: "I am BLACK I am WHITE I am SHORT I am LONG I am EVERYTHING YOU THINK IS IMPORTANT and I DON'T MATTER", mxid: "@should_be_newly_cached_2:cadence.moe"}
]) ])
t.equal(called, 1, "getStateEvent should be called once") t.equal(called, 1, "getStateEvent should be called once")

View file

@ -3,7 +3,7 @@
const fetch = require("node-fetch").default const fetch = require("node-fetch").default
const passthrough = require("../passthrough") const passthrough = require("../passthrough")
const { sync, db } = passthrough const {sync, db, select} = passthrough
/** @type {import("./mreq")} */ /** @type {import("./mreq")} */
const mreq = sync.require("./mreq") const mreq = sync.require("./mreq")
@ -33,7 +33,7 @@ async function uploadDiscordFileToMxc(path) {
} }
// Has this file already been uploaded in the past? Grab the existing copy from the database. // Has this file already been uploaded in the past? Grab the existing copy from the database.
const existingFromDb = db.prepare("SELECT mxc_url FROM file WHERE discord_url = ?").pluck().get(url) const existingFromDb = select("file", "mxc_url", "WHERE discord_url = ?").pluck().get(url)
if (typeof existingFromDb === "string") { if (typeof existingFromDb === "string") {
return existingFromDb return existingFromDb
} }