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 passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough
const {discord, sync, db, select} = passthrough
/** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api")
/** @type {import("./register-user")} */
@ -17,7 +17,7 @@ const createRoom = sync.require("../actions/create-room")
async function addReaction(data) {
const user = data.member?.user
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.
assert.equal(typeof parentID, "string")
const roomID = await createRoom.ensureRoom(data.channel_id)

View file

@ -3,7 +3,7 @@
const assert = require("assert")
const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough
const {discord, sync, db, select} = passthrough
/** @type {import("../converters/thread-to-announcement")} */
const threadToAnnouncement = sync.require("../converters/thread-to-announcement")
/** @type {import("../../matrix/api")} */
@ -15,8 +15,7 @@ const api = sync.require("../../matrix/api")
* @param {import("discord-api-types/v10").APIThreadChannel} thread
*/
async function announceThread(parentRoomID, threadRoomID, thread) {
/** @type {string?} */
const creatorMxid = db.prepare("SELECT mxid FROM sim WHERE discord_id = ?").pluck().get(thread.owner_id)
const creatorMxid = select("sim", "mxid", "WHERE discord_id = ?").pluck().get(thread.owner_id)
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 passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough
const {discord, sync, db, select} = passthrough
/** @type {import("../../matrix/file")} */
const file = sync.require("../../matrix/file")
/** @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}} guild
* @param {string?} customName
* @param {string | null | undefined} customName
*/
function convertNameAndTopic(channel, guild, customName) {
let channelPrefix =
@ -71,7 +71,7 @@ async function channelToKState(channel, guild) {
const spaceID = await createSpace.ensureSpace(guild.id)
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 customAvatar = row?.custom_avatar
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
}
/** @type {{room_id: string, thread_parent: string?}} */
const existing = db.prepare("SELECT room_id, thread_parent from channel_room WHERE channel_id = ?").get(channelID)
const existing = select("channel_room", ["room_id", "thread_parent"], "WHERE channel_id = ?").get(channelID)
if (!existing) {
const creation = (async () => {
@ -309,9 +308,9 @@ async function _unbridgeRoom(channelID) {
}
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)
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)
// 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 passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough
const {discord, sync, db, select} = passthrough
/** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api")
/** @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
}
/** @type {string?} */
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)
if (!spaceID) {
const creation = (async () => {
@ -118,7 +117,7 @@ async function _syncSpace(guildID, shouldActuallySync) {
const newAvatarState = spaceDiff["m.room.avatar/"]
if (guild.icon && newAvatarState?.url) {
// 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}) => {
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)
assert.ok(guild)
/** @type {string?} */
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)
const guildKState = await guildToKState(guild)
@ -176,7 +174,7 @@ async function syncSpaceFully(guildID) {
}).map(({state_key}) => state_key)
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 (discord.channels.has(channelID)) {
await createRoom.syncRoom(channelID)

View file

@ -1,7 +1,7 @@
// @ts-check
const passthrough = require("../../passthrough")
const { sync, db } = passthrough
const {sync, db, select} = passthrough
/** @type {import("../../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
*/
async function deleteMessage(data) {
/** @type {string?} */
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
/** @type {string[]} */
const eventsToRedact = db.prepare("SELECT event_id FROM event_message WHERE message_id = ?").pluck().all(data.id)
const eventsToRedact = select("event_message", "event_id", "WHERE message_id = ?").pluck().all(data.id)
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
await api.redactEvent(roomID, eventID)

View file

@ -4,7 +4,7 @@ const assert = require("assert")
const reg = require("../../matrix/read-registration")
const passthrough = require("../../passthrough")
const { discord, sync, db } = passthrough
const {discord, sync, db, select} = passthrough
/** @type {import("../../matrix/api")} */
const api = sync.require("../../matrix/api")
/** @type {import("../../matrix/file")} */
@ -47,7 +47,7 @@ async function createSim(user) {
*/
async function ensureSim(user) {
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) {
mxid = existing
} else {
@ -70,7 +70,7 @@ async function ensureSimJoined(user, roomID) {
const mxid = await ensureSim(user)
// 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) {
try {
await api.inviteToRoom(roomID, mxid)
@ -137,7 +137,7 @@ async function syncUser(user, member, guildID, roomID) {
const mxid = await ensureSimJoined(user, roomID)
const content = await memberToStateContent(user, member, guildID)
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
if (existingHash !== profileEventContentHash) {
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) {
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")
/** @ts-ignore @type {import("discord-api-types/v10").APIGuildChannel} */
const channel = discord.channels.get(channelID)
const guildID = channel.guild_id
assert.ok(typeof guildID === "string")
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")
/** @ts-ignore @type {Required<import("discord-api-types/v10").APIGuildMember>} */