forked from cadence/out-of-your-element
Finish moving from SQL to New Funny ORM
This commit is contained in:
parent
4e1e590c3a
commit
79bd0254f0
19 changed files with 87 additions and 87 deletions
|
@ -4,15 +4,15 @@ const assert = require("assert").strict
|
|||
const Ty = require("../../types")
|
||||
|
||||
const passthrough = require("../../passthrough")
|
||||
const { discord, sync, db } = passthrough
|
||||
const {discord, sync, db, select} = passthrough
|
||||
|
||||
/**
|
||||
* @param {Ty.Event.Outer<Ty.Event.M_Reaction>} 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
|
||||
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.
|
||||
|
||||
// no need to sync the matrix member to the other side. but if I did need to, this is where I'd do it
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
const assert = require("assert").strict
|
||||
const DiscordTypes = require("discord-api-types/v10")
|
||||
const passthrough = require("../../passthrough")
|
||||
const {discord, db} = passthrough
|
||||
const {discord, db, select} = passthrough
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
if (!forceCreate) {
|
||||
/** @type {{id: string, token: string} | null} */
|
||||
const row = db.prepare("SELECT webhook_id as id, webhook_token as token FROM webhook WHERE channel_id = ?").get(channelID)
|
||||
const row = select("webhook", ["webhook_id", "webhook_token"], "WHERE channel_id = ?").get(channelID)
|
||||
if (row) {
|
||||
return {created: false, ...row}
|
||||
return {
|
||||
id: row.webhook_id,
|
||||
token: row.webhook_token,
|
||||
created: false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ const {promisify} = require("util")
|
|||
const Ty = require("../../types")
|
||||
const DiscordTypes = require("discord-api-types/v10")
|
||||
const passthrough = require("../../passthrough")
|
||||
const {sync, discord, db} = passthrough
|
||||
const {sync, discord, db, select} = passthrough
|
||||
|
||||
/** @type {import("./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 */
|
||||
async function sendEvent(event) {
|
||||
// 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 threadID = undefined
|
||||
if (row.thread_parent) {
|
||||
|
|
|
@ -7,7 +7,7 @@ const TurndownService = require("turndown")
|
|||
const assert = require("assert").strict
|
||||
|
||||
const passthrough = require("../../passthrough")
|
||||
const { sync, db, discord } = passthrough
|
||||
const {sync, db, discord, select, from} = passthrough
|
||||
/** @type {import("../../matrix/file")} */
|
||||
const file = sync.require("../../matrix/file")
|
||||
/** @type {import("../converters/utils")} */
|
||||
|
@ -148,7 +148,7 @@ turndownService.addRule("fencedCodeBlock", {
|
|||
* @returns {Promise<{displayname?: string?, avatar_url?: string?}>}
|
||||
*/
|
||||
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
|
||||
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)
|
||||
|
@ -230,7 +230,7 @@ async function eventToMessage(event, guild, di) {
|
|||
if (relType !== "m.replace") return
|
||||
const originalEventId = relatesTo.event_id
|
||||
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
|
||||
|
||||
// Ok, it's an edit.
|
||||
|
@ -261,7 +261,7 @@ async function eventToMessage(event, guild, di) {
|
|||
if (!repliedToEventId) return
|
||||
let repliedToEvent = await di.api.getEvent(event.room_id, repliedToEventId)
|
||||
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) {
|
||||
replyLine = `<:L1:1144820033948762203><:L2:1144820084079087647>https://discord.com/channels/${guild.id}/${row.channel_id}/${row.message_id} `
|
||||
} else {
|
||||
|
@ -269,7 +269,7 @@ async function eventToMessage(event, guild, di) {
|
|||
}
|
||||
const sender = repliedToEvent.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) {
|
||||
replyLine += `<@${authorID}>`
|
||||
} else {
|
||||
|
@ -312,14 +312,14 @@ async function eventToMessage(event, guild, di) {
|
|||
// Handling mentions of Discord users
|
||||
input = input.replace(/("https:\/\/matrix.to\/#\/(@[^"]+)")>/g, (whole, attributeValue, mxid) => {
|
||||
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
|
||||
return `${attributeValue} data-user-id="${userID}">`
|
||||
})
|
||||
|
||||
// Handling mentions of Discord rooms
|
||||
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
|
||||
return `${attributeValue} data-channel-id="${channelID}">`
|
||||
})
|
||||
|
|
|
@ -2,7 +2,7 @@ const {test} = require("supertape")
|
|||
const {eventToMessage} = require("./event-to-message")
|
||||
const data = require("../../test/data")
|
||||
const {MatrixServerError} = require("../../matrix/mreq")
|
||||
const {db} = require("../../passthrough")
|
||||
const {db, select} = require("../../passthrough")
|
||||
|
||||
/**
|
||||
* @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"}
|
||||
])
|
||||
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")
|
||||
})
|
||||
|
||||
|
@ -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"}
|
||||
])
|
||||
t.equal(called, 1, "getStateEvent should be called once")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue